Port Homebrew::DevCmd::TapNew

This commit is contained in:
Douglas Eichelberger 2024-03-21 21:47:55 -07:00
parent 0d04f198d2
commit e0519d736a
2 changed files with 151 additions and 144 deletions

View File

@ -1,182 +1,188 @@
# typed: true # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command"
require "fileutils"
require "tap" require "tap"
require "cli/parser" require "cli/parser"
module Homebrew module Homebrew
sig { returns(CLI::Parser) } module DevCmd
def self.tap_new_args class TapNew < AbstractCommand
Homebrew::CLI::Parser.new do include FileUtils
usage_banner "`tap-new` [<options>] <user>`/`<repo>"
description <<~EOS
Generate the template files for a new tap.
EOS
switch "--no-git", cmd_args do
description: "Don't initialize a Git repository for the tap." usage_banner "`tap-new` [<options>] <user>`/`<repo>"
flag "--pull-label=", description <<~EOS
description: "Label name for pull requests ready to be pulled (default: `pr-pull`)." Generate the template files for a new tap.
flag "--branch=", EOS
description: "Initialize Git repository and setup GitHub Actions workflows with the " \
"specified branch name (default: `main`)."
switch "--github-packages",
description: "Upload bottles to GitHub Packages."
named_args :tap, number: 1 switch "--no-git",
end description: "Don't initialize a Git repository for the tap."
end flag "--pull-label=",
description: "Label name for pull requests ready to be pulled (default: `pr-pull`)."
flag "--branch=",
description: "Initialize Git repository and setup GitHub Actions workflows with the " \
"specified branch name (default: `main`)."
switch "--github-packages",
description: "Upload bottles to GitHub Packages."
def self.tap_new named_args :tap, number: 1
args = tap_new_args.parse end
label = args.pull_label || "pr-pull" sig { override.void }
branch = args.branch || "main" def run
label = args.pull_label || "pr-pull"
branch = args.branch || "main"
tap = args.named.to_taps.first tap = args.named.to_taps.fetch(0)
odie "Invalid tap name '#{tap}'" unless tap.path.to_s.match?(HOMEBREW_TAP_PATH_REGEX) odie "Invalid tap name '#{tap}'" unless tap.path.to_s.match?(HOMEBREW_TAP_PATH_REGEX)
titleized_user = tap.user.dup titleized_user = tap.user.dup
titleized_repo = tap.repo.dup titleized_repo = tap.repo.dup
titleized_user[0] = titleized_user[0].upcase titleized_user[0] = titleized_user[0].upcase
titleized_repo[0] = titleized_repo[0].upcase titleized_repo[0] = titleized_repo[0].upcase
root_url = GitHubPackages.root_url(tap.user, "homebrew-#{tap.repo}") if args.github_packages? root_url = GitHubPackages.root_url(tap.user, "homebrew-#{tap.repo}") if args.github_packages?
(tap.path/"Formula").mkpath (tap.path/"Formula").mkpath
readme = <<~MARKDOWN readme = <<~MARKDOWN
# #{titleized_user} #{titleized_repo} # #{titleized_user} #{titleized_repo}
## How do I install these formulae? ## How do I install these formulae?
`brew install #{tap}/<formula>` `brew install #{tap}/<formula>`
Or `brew tap #{tap}` and then `brew install <formula>`. Or `brew tap #{tap}` and then `brew install <formula>`.
## Documentation ## Documentation
`brew help`, `man brew` or check [Homebrew's documentation](https://docs.brew.sh). `brew help`, `man brew` or check [Homebrew's documentation](https://docs.brew.sh).
MARKDOWN MARKDOWN
write_path(tap, "README.md", readme) write_path(tap, "README.md", readme)
actions_main = <<~YAML actions_main = <<~YAML
name: brew test-bot name: brew test-bot
on: on:
push: push:
branches: branches:
- #{branch} - #{branch}
pull_request: pull_request:
jobs: jobs:
test-bot: test-bot:
strategy: strategy:
matrix: matrix:
os: [ubuntu-22.04, macos-13] os: [ubuntu-22.04, macos-13]
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
steps: steps:
- name: Set up Homebrew - name: Set up Homebrew
id: set-up-homebrew id: set-up-homebrew
uses: Homebrew/actions/setup-homebrew@master uses: Homebrew/actions/setup-homebrew@master
- name: Cache Homebrew Bundler RubyGems - name: Cache Homebrew Bundler RubyGems
id: cache id: cache
uses: actions/cache@v3 uses: actions/cache@v3
with: with:
path: ${{ steps.set-up-homebrew.outputs.gems-path }} path: ${{ steps.set-up-homebrew.outputs.gems-path }}
key: ${{ runner.os }}-rubygems-${{ steps.set-up-homebrew.outputs.gems-hash }} key: ${{ runner.os }}-rubygems-${{ steps.set-up-homebrew.outputs.gems-hash }}
restore-keys: ${{ runner.os }}-rubygems- restore-keys: ${{ runner.os }}-rubygems-
- run: brew test-bot --only-cleanup-before - run: brew test-bot --only-cleanup-before
- run: brew test-bot --only-setup - run: brew test-bot --only-setup
- run: brew test-bot --only-tap-syntax - run: brew test-bot --only-tap-syntax
- run: brew test-bot --only-formulae#{" --root-url=#{root_url}" if root_url} - run: brew test-bot --only-formulae#{" --root-url=#{root_url}" if root_url}
if: github.event_name == 'pull_request' if: github.event_name == 'pull_request'
- name: Upload bottles as artifact - name: Upload bottles as artifact
if: always() && github.event_name == 'pull_request' if: always() && github.event_name == 'pull_request'
uses: actions/upload-artifact@v3 uses: actions/upload-artifact@v3
with: with:
name: bottles name: bottles
path: '*.bottle.*' path: '*.bottle.*'
YAML YAML
actions_publish = <<~YAML actions_publish = <<~YAML
name: brew pr-pull name: brew pr-pull
on: on:
pull_request_target: pull_request_target:
types: types:
- labeled - labeled
jobs: jobs:
pr-pull: pr-pull:
if: contains(github.event.pull_request.labels.*.name, '#{label}') if: contains(github.event.pull_request.labels.*.name, '#{label}')
runs-on: ubuntu-22.04 runs-on: ubuntu-22.04
permissions: permissions:
contents: write contents: write
packages: #{args.github_packages? ? "write" : "none"} packages: #{args.github_packages? ? "write" : "none"}
pull-requests: write pull-requests: write
steps: steps:
- name: Set up Homebrew - name: Set up Homebrew
uses: Homebrew/actions/setup-homebrew@master uses: Homebrew/actions/setup-homebrew@master
- name: Set up git - name: Set up git
uses: Homebrew/actions/git-user-config@master uses: Homebrew/actions/git-user-config@master
- name: Pull bottles - name: Pull bottles
env: env:
HOMEBREW_GITHUB_API_TOKEN: ${{ github.token }} HOMEBREW_GITHUB_API_TOKEN: ${{ github.token }}
HOMEBREW_GITHUB_PACKAGES_TOKEN: ${{ github.token }} HOMEBREW_GITHUB_PACKAGES_TOKEN: ${{ github.token }}
HOMEBREW_GITHUB_PACKAGES_USER: ${{ github.actor }} HOMEBREW_GITHUB_PACKAGES_USER: ${{ github.actor }}
PULL_REQUEST: ${{ github.event.pull_request.number }} PULL_REQUEST: ${{ github.event.pull_request.number }}
run: brew pr-pull --debug --tap=$GITHUB_REPOSITORY $PULL_REQUEST run: brew pr-pull --debug --tap=$GITHUB_REPOSITORY $PULL_REQUEST
- name: Push commits - name: Push commits
uses: Homebrew/actions/git-try-push@master uses: Homebrew/actions/git-try-push@master
with: with:
token: ${{ github.token }} token: ${{ github.token }}
branch: #{branch} branch: #{branch}
- name: Delete branch - name: Delete branch
if: github.event.pull_request.head.repo.fork == false if: github.event.pull_request.head.repo.fork == false
env: env:
BRANCH: ${{ github.event.pull_request.head.ref }} BRANCH: ${{ github.event.pull_request.head.ref }}
run: git push --delete origin $BRANCH run: git push --delete origin $BRANCH
YAML YAML
(tap.path/".github/workflows").mkpath (tap.path/".github/workflows").mkpath
write_path(tap, ".github/workflows/tests.yml", actions_main) write_path(tap, ".github/workflows/tests.yml", actions_main)
write_path(tap, ".github/workflows/publish.yml", actions_publish) write_path(tap, ".github/workflows/publish.yml", actions_publish)
unless args.no_git? unless args.no_git?
cd tap.path do cd tap.path do
Utils::Git.set_name_email! Utils::Git.set_name_email!
Utils::Git.setup_gpg! Utils::Git.setup_gpg!
# Would be nice to use --initial-branch here but it's not available in # Would be nice to use --initial-branch here but it's not available in
# older versions of Git that we support. # older versions of Git that we support.
safe_system "git", "-c", "init.defaultBranch=#{branch}", "init" safe_system "git", "-c", "init.defaultBranch=#{branch}", "init"
safe_system "git", "add", "--all" safe_system "git", "add", "--all"
safe_system "git", "commit", "-m", "Create #{tap} tap" safe_system "git", "commit", "-m", "Create #{tap} tap"
safe_system "git", "branch", "-m", branch safe_system "git", "branch", "-m", branch
end
end
ohai "Created #{tap}"
puts <<~EOS
#{tap.path}
When a pull request making changes to a formula (or formulae) becomes green
(all checks passed), then you can publish the built bottles.
To do so, label your PR as `#{label}` and the workflow will be triggered.
EOS
end
private
def write_path(tap, filename, content)
path = tap.path/filename
tap.path.mkpath
odie "#{path} already exists" if path.exist?
path.write content
end end
end end
ohai "Created #{tap}"
puts <<~EOS
#{tap.path}
When a pull request making changes to a formula (or formulae) becomes green
(all checks passed), then you can publish the built bottles.
To do so, label your PR as `#{label}` and the workflow will be triggered.
EOS
end
def self.write_path(tap, filename, content)
path = tap.path/filename
tap.path.mkpath
odie "#{path} already exists" if path.exist?
path.write content
end end
end end

View File

@ -1,8 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
require "cmd/shared_examples/args_parse" require "cmd/shared_examples/args_parse"
require "dev-cmd/tap-new"
RSpec.describe "brew tap-new" do RSpec.describe Homebrew::DevCmd::TapNew do
it_behaves_like "parseable arguments" it_behaves_like "parseable arguments"
it "initializes a new tap with a README file and GitHub Actions CI", :integration_test do it "initializes a new tap with a README file and GitHub Actions CI", :integration_test do