| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 16:47:30 +05:30
										 |  |  | require "tap" | 
					
						
							| 
									
										
										
										
											2019-04-17 18:25:08 +09:00
										 |  |  | require "cli/parser" | 
					
						
							| 
									
										
										
										
											2018-04-01 16:47:30 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 13:41:53 +01:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   extend T::Sig | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 13:41:53 +01:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   sig { returns(CLI::Parser) } | 
					
						
							| 
									
										
										
										
											2018-07-30 18:25:38 +05:30
										 |  |  |   def tap_new_args | 
					
						
							|  |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							| 
									
										
										
										
											2021-01-15 15:04:02 -05:00
										 |  |  |       usage_banner "`tap-new` [<options>] <user>`/`<repo>" | 
					
						
							|  |  |  |       description <<~EOS | 
					
						
							| 
									
										
										
										
											2018-09-28 21:39:52 +05:30
										 |  |  |         Generate the template files for a new tap. | 
					
						
							|  |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-18 13:02:40 +02:00
										 |  |  |       switch "--no-git", | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |              description: "Don't initialize a Git repository for the tap." | 
					
						
							| 
									
										
										
										
											2020-10-10 12:28:34 -04:00
										 |  |  |       flag   "--pull-label=", | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |              description: "Label name for pull requests ready to be pulled (default: `pr-pull`)." | 
					
						
							| 
									
										
										
										
											2020-10-10 12:28:34 -04:00
										 |  |  |       flag   "--branch=", | 
					
						
							| 
									
										
										
										
											2021-03-01 13:40:08 +00:00
										 |  |  |              description: "Initialize Git repository and setup GitHub Actions workflows with the " \ | 
					
						
							|  |  |  |                           "specified branch name (default: `main`)." | 
					
						
							| 
									
										
										
										
											2021-11-19 12:30:28 -08:00
										 |  |  |       switch "--github-packages", | 
					
						
							|  |  |  |              description: "Upload bottles to GitHub Packages." | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |       named_args :tap, number: 1
 | 
					
						
							| 
									
										
										
										
											2018-04-01 16:47:30 +05:30
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-07-30 18:25:38 +05:30
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def tap_new | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |     args = tap_new_args.parse | 
					
						
							| 
									
										
										
										
											2018-04-01 16:47:30 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-18 13:02:40 +02:00
										 |  |  |     label = args.pull_label || "pr-pull" | 
					
						
							| 
									
										
										
										
											2020-10-10 12:28:34 -04:00
										 |  |  |     branch = args.branch || "main" | 
					
						
							| 
									
										
										
										
											2020-09-18 13:02:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  |     tap = args.named.to_taps.first | 
					
						
							| 
									
										
										
										
											2021-04-13 02:43:38 +05:30
										 |  |  |     odie "Invalid tap name '#{tap}'" unless tap.path.to_s.match?(HOMEBREW_TAP_PATH_REGEX) | 
					
						
							| 
									
										
										
										
											2020-06-12 11:42:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 13:41:53 +01:00
										 |  |  |     titleized_user = tap.user.dup | 
					
						
							|  |  |  |     titleized_repo = tap.repo.dup | 
					
						
							|  |  |  |     titleized_user[0] = titleized_user[0].upcase | 
					
						
							|  |  |  |     titleized_repo[0] = titleized_repo[0].upcase | 
					
						
							| 
									
										
										
										
											2021-11-22 10:06:43 -08:00
										 |  |  |     root_url = GitHubPackages.root_url(tap.user, "homebrew-#{tap.repo}") if args.github_packages? | 
					
						
							| 
									
										
										
										
											2021-11-19 12:30:28 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 13:41:53 +01:00
										 |  |  |     (tap.path/"Formula").mkpath | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-11 15:17:40 +02:00
										 |  |  |     readme = <<~MARKDOWN | 
					
						
							| 
									
										
										
										
											2016-10-13 13:41:53 +01:00
										 |  |  |       # #{titleized_user} #{titleized_repo} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       ## How do I install these formulae? | 
					
						
							| 
									
										
										
										
											2021-03-01 13:40:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 13:41:53 +01:00
										 |  |  |       `brew install #{tap}/<formula>` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Or `brew tap #{tap}` and then `brew install <formula>`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       ## Documentation | 
					
						
							| 
									
										
										
										
											2021-03-01 13:40:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-27 15:59:37 +01:00
										 |  |  |       `brew help`, `man brew` or check [Homebrew's documentation](https://docs.brew.sh). | 
					
						
							| 
									
										
										
										
											2018-07-11 15:17:40 +02:00
										 |  |  |     MARKDOWN | 
					
						
							| 
									
										
										
										
											2016-10-13 13:41:53 +01:00
										 |  |  |     write_path(tap, "README.md", readme) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-18 13:02:40 +02:00
										 |  |  |     actions_main = <<~YAML | 
					
						
							| 
									
										
										
										
											2020-02-15 17:27:13 +00:00
										 |  |  |       name: brew test-bot | 
					
						
							|  |  |  |       on: | 
					
						
							|  |  |  |         push: | 
					
						
							| 
									
										
										
										
											2021-03-01 13:40:08 +00:00
										 |  |  |           branches: | 
					
						
							|  |  |  |             - #{branch} | 
					
						
							| 
									
										
										
										
											2020-08-11 19:33:33 +02:00
										 |  |  |         pull_request: | 
					
						
							| 
									
										
										
										
											2018-12-30 21:13:24 +00:00
										 |  |  |       jobs: | 
					
						
							| 
									
										
										
										
											2020-02-15 17:27:13 +00:00
										 |  |  |         test-bot: | 
					
						
							| 
									
										
										
										
											2020-08-05 20:10:09 +01:00
										 |  |  |           strategy: | 
					
						
							|  |  |  |             matrix: | 
					
						
							| 
									
										
										
										
											2021-03-01 13:40:08 +00:00
										 |  |  |               os: [ubuntu-latest, macos-latest] | 
					
						
							| 
									
										
										
										
											2020-09-18 13:02:40 +02:00
										 |  |  |           runs-on: ${{ matrix.os }} | 
					
						
							| 
									
										
										
										
											2020-02-15 17:27:13 +00:00
										 |  |  |           steps: | 
					
						
							| 
									
										
										
										
											2020-08-04 11:57:39 +01:00
										 |  |  |             - name: Set up Homebrew | 
					
						
							| 
									
										
										
										
											2020-08-10 20:45:00 +02:00
										 |  |  |               id: set-up-homebrew | 
					
						
							| 
									
										
										
										
											2020-08-05 20:10:09 +01:00
										 |  |  |               uses: Homebrew/actions/setup-homebrew@master | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             - name: Cache Homebrew Bundler RubyGems | 
					
						
							|  |  |  |               id: cache | 
					
						
							| 
									
										
										
										
											2020-08-13 14:54:22 +01:00
										 |  |  |               uses: actions/cache@v1 | 
					
						
							| 
									
										
										
										
											2020-08-05 20:10:09 +01:00
										 |  |  |               with: | 
					
						
							|  |  |  |                 path: ${{ steps.set-up-homebrew.outputs.gems-path }} | 
					
						
							|  |  |  |                 key: ${{ runner.os }}-rubygems-${{ steps.set-up-homebrew.outputs.gems-hash }} | 
					
						
							|  |  |  |                 restore-keys: ${{ runner.os }}-rubygems- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             - name: Install Homebrew Bundler RubyGems | 
					
						
							|  |  |  |               if: steps.cache.outputs.cache-hit != 'true' | 
					
						
							|  |  |  |               run: brew install-bundler-gems | 
					
						
							| 
									
										
										
										
											2020-08-04 11:57:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 14:54:22 +01:00
										 |  |  |             - run: brew test-bot --only-cleanup-before | 
					
						
							| 
									
										
										
										
											2020-08-04 11:57:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 14:54:22 +01:00
										 |  |  |             - run: brew test-bot --only-setup | 
					
						
							| 
									
										
										
										
											2020-08-04 11:57:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 14:54:22 +01:00
										 |  |  |             - run: brew test-bot --only-tap-syntax | 
					
						
							| 
									
										
										
										
											2020-08-04 11:57:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-19 12:30:28 -08:00
										 |  |  |             - run: brew test-bot --only-formulae#{" --root-url=#{root_url}" if root_url} | 
					
						
							| 
									
										
										
										
											2020-08-04 11:57:39 +01:00
										 |  |  |               if: github.event_name == 'pull_request' | 
					
						
							| 
									
										
										
										
											2020-09-18 13:02:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             - name: Upload bottles as artifact | 
					
						
							|  |  |  |               if: always() && github.event_name == 'pull_request' | 
					
						
							|  |  |  |               uses: actions/upload-artifact@main | 
					
						
							|  |  |  |               with: | 
					
						
							|  |  |  |                 name: bottles | 
					
						
							|  |  |  |                 path: '*.bottle.*' | 
					
						
							|  |  |  |     YAML | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     actions_publish = <<~YAML | 
					
						
							|  |  |  |       name: brew pr-pull | 
					
						
							|  |  |  |       on: | 
					
						
							|  |  |  |         pull_request_target: | 
					
						
							|  |  |  |           types: | 
					
						
							|  |  |  |             - labeled | 
					
						
							|  |  |  |       jobs: | 
					
						
							|  |  |  |         pr-pull: | 
					
						
							| 
									
										
										
										
											2020-10-10 12:28:34 -04:00
										 |  |  |           if: contains(github.event.pull_request.labels.*.name, '#{label}') | 
					
						
							| 
									
										
										
										
											2020-09-18 13:02:40 +02:00
										 |  |  |           runs-on: ubuntu-latest | 
					
						
							|  |  |  |           steps: | 
					
						
							|  |  |  |             - name: Set up Homebrew | 
					
						
							|  |  |  |               uses: Homebrew/actions/setup-homebrew@master | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             - name: Set up git | 
					
						
							|  |  |  |               uses: Homebrew/actions/git-user-config@master | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             - name: Pull bottles | 
					
						
							|  |  |  |               env: | 
					
						
							| 
									
										
										
										
											2021-11-22 10:06:43 -08:00
										 |  |  |                 HOMEBREW_GITHUB_API_TOKEN: ${{ github.token }} | 
					
						
							|  |  |  |                 HOMEBREW_GITHUB_PACKAGES_TOKEN: ${{ github.token }} | 
					
						
							|  |  |  |                 HOMEBREW_GITHUB_PACKAGES_USER: ${{ github.actor }} | 
					
						
							|  |  |  |                 PULL_REQUEST: ${{ github.event.pull_request.number }} | 
					
						
							| 
									
										
										
										
											2020-09-18 13:02:40 +02:00
										 |  |  |               run: brew pr-pull --debug --tap=$GITHUB_REPOSITORY $PULL_REQUEST | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             - name: Push commits | 
					
						
							|  |  |  |               uses: Homebrew/actions/git-try-push@master | 
					
						
							|  |  |  |               with: | 
					
						
							|  |  |  |                 token: ${{ github.token }} | 
					
						
							| 
									
										
										
										
											2020-10-10 12:28:34 -04:00
										 |  |  |                 branch: #{branch} | 
					
						
							| 
									
										
										
										
											2020-09-18 13:02:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             - name: Delete branch | 
					
						
							|  |  |  |               if: github.event.pull_request.head.repo.fork == false | 
					
						
							|  |  |  |               env: | 
					
						
							|  |  |  |                 BRANCH: ${{ github.event.pull_request.head.ref }} | 
					
						
							|  |  |  |               run: git push --delete origin $BRANCH | 
					
						
							| 
									
										
										
										
											2018-07-11 15:17:40 +02:00
										 |  |  |     YAML | 
					
						
							| 
									
										
										
										
											2020-02-15 17:27:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     (tap.path/".github/workflows").mkpath | 
					
						
							| 
									
										
										
										
											2020-09-18 13:02:40 +02:00
										 |  |  |     write_path(tap, ".github/workflows/tests.yml", actions_main) | 
					
						
							|  |  |  |     write_path(tap, ".github/workflows/publish.yml", actions_publish) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     unless args.no_git? | 
					
						
							|  |  |  |       cd tap.path do | 
					
						
							| 
									
										
										
										
											2021-04-02 14:58:31 +05:30
										 |  |  |         Utils::Git.set_name_email! | 
					
						
							|  |  |  |         Utils::Git.setup_gpg! | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 10:47:44 +00:00
										 |  |  |         # Would be nice to use --initial-branch here but it's not available in | 
					
						
							|  |  |  |         # older versions of Git that we support. | 
					
						
							|  |  |  |         safe_system "git", "-c", "init.defaultBranch=#{branch}", "init" | 
					
						
							| 
									
										
										
										
											2020-09-18 13:02:40 +02:00
										 |  |  |         safe_system "git", "add", "--all" | 
					
						
							|  |  |  |         safe_system "git", "commit", "-m", "Create #{tap} tap" | 
					
						
							| 
									
										
										
										
											2020-10-10 12:28:34 -04:00
										 |  |  |         safe_system "git", "branch", "-m", branch | 
					
						
							| 
									
										
										
										
											2020-09-18 13:02:40 +02:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-23 12:41:35 +00:00
										 |  |  |     ohai "Created #{tap}" | 
					
						
							| 
									
										
										
										
											2020-09-18 13:02:40 +02:00
										 |  |  |     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 | 
					
						
							| 
									
										
										
										
											2016-10-13 13:41:53 +01:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2018-10-08 22:49:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def write_path(tap, filename, content) | 
					
						
							|  |  |  |     path = tap.path/filename | 
					
						
							|  |  |  |     tap.path.mkpath | 
					
						
							| 
									
										
										
										
											2020-12-03 00:02:22 -05:00
										 |  |  |     odie "#{path} already exists" if path.exist? | 
					
						
							| 
									
										
										
										
											2018-10-08 22:49:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     path.write content | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2016-10-13 13:41:53 +01:00
										 |  |  | end |