2023-03-13 18:31:26 -07:00
|
|
|
# typed: true
|
2020-09-04 16:58:31 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "cask"
|
2021-08-24 18:15:04 -07:00
|
|
|
require "cask/download"
|
2020-09-04 16:58:31 -07:00
|
|
|
require "cli/parser"
|
|
|
|
require "utils/tar"
|
|
|
|
|
|
|
|
module Homebrew
|
2020-10-20 12:03:48 +02:00
|
|
|
extend T::Sig
|
|
|
|
|
2020-09-04 16:58:31 -07:00
|
|
|
module_function
|
|
|
|
|
2020-10-20 12:03:48 +02:00
|
|
|
sig { returns(CLI::Parser) }
|
2020-09-04 16:58:31 -07:00
|
|
|
def bump_cask_pr_args
|
|
|
|
Homebrew::CLI::Parser.new do
|
2021-01-15 15:04:02 -05:00
|
|
|
description <<~EOS
|
2020-09-04 16:58:31 -07:00
|
|
|
Create a pull request to update <cask> with a new version.
|
|
|
|
|
|
|
|
A best effort to determine the <SHA-256> will be made if the value is not
|
|
|
|
supplied by the user.
|
|
|
|
EOS
|
|
|
|
switch "-n", "--dry-run",
|
|
|
|
description: "Print what would be done rather than doing it."
|
2021-10-20 22:36:07 +11:00
|
|
|
switch "--write-only",
|
2020-09-04 16:58:31 -07:00
|
|
|
description: "Make the expected file modifications without taking any Git actions."
|
|
|
|
switch "--commit",
|
2021-11-19 12:14:47 -08:00
|
|
|
depends_on: "--write-only",
|
2022-06-28 10:09:59 +01:00
|
|
|
description: "When passed with `--write-only`, generate a new commit after writing changes " \
|
2020-09-04 16:58:31 -07:00
|
|
|
"to the cask file."
|
|
|
|
switch "--no-audit",
|
2020-11-18 08:10:21 +01:00
|
|
|
description: "Don't run `brew audit` before opening the PR."
|
2020-09-16 10:56:54 -07:00
|
|
|
switch "--online",
|
2020-11-18 08:10:21 +01:00
|
|
|
description: "Run `brew audit --online` before opening the PR."
|
2020-09-04 16:58:31 -07:00
|
|
|
switch "--no-style",
|
2020-11-18 08:10:21 +01:00
|
|
|
description: "Don't run `brew style --fix` before opening the PR."
|
2020-09-04 16:58:31 -07:00
|
|
|
switch "--no-browse",
|
|
|
|
description: "Print the pull request URL instead of opening in a browser."
|
|
|
|
switch "--no-fork",
|
|
|
|
description: "Don't try to fork the repository."
|
|
|
|
flag "--version=",
|
|
|
|
description: "Specify the new <version> for the cask."
|
|
|
|
flag "--message=",
|
2022-11-19 23:03:23 +00:00
|
|
|
description: "Prepend <message> to the default pull request message."
|
2020-09-04 16:58:31 -07:00
|
|
|
flag "--url=",
|
|
|
|
description: "Specify the <URL> for the new download."
|
|
|
|
flag "--sha256=",
|
|
|
|
description: "Specify the <SHA-256> checksum of the new download."
|
2021-04-18 20:53:01 +02:00
|
|
|
flag "--fork-org=",
|
|
|
|
description: "Use the specified GitHub organization for forking."
|
2020-09-04 16:58:31 -07:00
|
|
|
switch "-f", "--force",
|
|
|
|
description: "Ignore duplicate open PRs."
|
|
|
|
|
|
|
|
conflicts "--dry-run", "--write"
|
2020-09-16 10:56:54 -07:00
|
|
|
conflicts "--no-audit", "--online"
|
2021-01-10 14:26:40 -05:00
|
|
|
|
|
|
|
named_args :cask, number: 1
|
2020-09-04 16:58:31 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def bump_cask_pr
|
|
|
|
args = bump_cask_pr_args.parse
|
|
|
|
|
2021-04-30 12:00:28 +01:00
|
|
|
# This will be run by `brew style` later so run it first to not start
|
|
|
|
# spamming during normal output.
|
|
|
|
Homebrew.install_bundler_gems!
|
|
|
|
|
2020-09-04 16:58:31 -07:00
|
|
|
# As this command is simplifying user-run commands then let's just use a
|
|
|
|
# user path, too.
|
2022-06-15 05:40:43 +01:00
|
|
|
ENV["PATH"] = PATH.new(ORIGINAL_PATHS).to_s
|
2020-09-04 16:58:31 -07:00
|
|
|
|
|
|
|
# Use the user's browser, too.
|
|
|
|
ENV["BROWSER"] = Homebrew::EnvConfig.browser
|
|
|
|
|
|
|
|
cask = args.named.to_casks.first
|
2021-01-08 11:42:37 -08:00
|
|
|
|
|
|
|
odie "This cask is not in a tap!" if cask.tap.blank?
|
|
|
|
odie "This cask's tap is not a Git repository!" unless cask.tap.git?
|
|
|
|
|
2020-09-04 16:58:31 -07:00
|
|
|
new_version = args.version
|
2022-10-12 18:20:53 -04:00
|
|
|
new_version = :latest if ["latest", ":latest"].include? new_version
|
2020-12-08 12:42:02 -08:00
|
|
|
new_version = Cask::DSL::Version.new(new_version) if new_version.present?
|
2020-09-04 16:58:31 -07:00
|
|
|
new_base_url = args.url
|
|
|
|
new_hash = args.sha256
|
2020-12-08 12:42:02 -08:00
|
|
|
new_hash = :no_check if ["no_check", ":no_check"].include? new_hash
|
|
|
|
|
|
|
|
if new_version.nil? && new_base_url.nil? && new_hash.nil?
|
2023-02-10 23:15:40 -05:00
|
|
|
raise UsageError, "No `--version`, `--url` or `--sha256` argument specified!"
|
2020-12-08 12:42:02 -08:00
|
|
|
end
|
2020-09-04 16:58:31 -07:00
|
|
|
|
|
|
|
old_version = cask.version
|
2020-12-04 00:07:02 +01:00
|
|
|
old_hash = cask.sha256
|
2020-09-04 16:58:31 -07:00
|
|
|
|
2023-01-19 17:06:42 -08:00
|
|
|
check_pull_requests(cask, state: "open", args: args)
|
|
|
|
# if we haven't already found open requests, try for an exact match across closed requests
|
|
|
|
check_pull_requests(cask, state: "closed", args: args, version: new_version) if new_version.present?
|
2020-09-04 16:58:31 -07:00
|
|
|
|
|
|
|
old_contents = File.read(cask.sourcefile_path)
|
|
|
|
|
2020-09-10 20:02:26 -07:00
|
|
|
replacement_pairs = []
|
|
|
|
|
2020-12-08 12:42:02 -08:00
|
|
|
if new_version.present?
|
|
|
|
old_version_regex = old_version.latest? ? ":latest" : "[\"']#{Regexp.escape(old_version.to_s)}[\"']"
|
|
|
|
|
|
|
|
replacement_pairs << [
|
|
|
|
/version\s+#{old_version_regex}/m,
|
|
|
|
"version #{new_version.latest? ? ":latest" : "\"#{new_version}\""}",
|
2020-09-10 20:02:26 -07:00
|
|
|
]
|
2022-10-12 18:20:53 -04:00
|
|
|
if new_version.latest? || new_hash == :no_check
|
|
|
|
opoo "Ignoring specified `--sha256=` argument." if new_hash.is_a? String
|
|
|
|
replacement_pairs << [/"#{old_hash}"/, ":no_check"] if old_hash != :no_check
|
|
|
|
elsif old_hash != :no_check
|
|
|
|
if new_hash.nil? || cask.languages.present?
|
|
|
|
if new_hash.present? && cask.languages.present?
|
|
|
|
opoo "Multiple hash replacements required; ignoring specified `--sha256=` argument."
|
|
|
|
end
|
|
|
|
tmp_contents = Utils::Inreplace.inreplace_pairs(cask.sourcefile_path,
|
|
|
|
replacement_pairs.uniq.compact,
|
|
|
|
read_only_run: true,
|
|
|
|
silent: true)
|
|
|
|
|
|
|
|
tmp_cask = Cask::CaskLoader.load(tmp_contents)
|
|
|
|
tmp_config = tmp_cask.config
|
|
|
|
|
|
|
|
[:arm, :intel].each do |arch|
|
|
|
|
Homebrew::SimulateSystem.arch = arch
|
|
|
|
|
|
|
|
languages = cask.languages
|
|
|
|
languages = [nil] if languages.empty?
|
|
|
|
languages.each do |language|
|
|
|
|
new_hash_config = if language.blank?
|
|
|
|
tmp_config
|
|
|
|
else
|
|
|
|
tmp_config.merge(Cask::Config.new(explicit: { languages: [language] }))
|
|
|
|
end
|
|
|
|
|
|
|
|
new_hash_cask = Cask::CaskLoader.load(tmp_contents)
|
|
|
|
new_hash_cask.config = new_hash_config
|
|
|
|
old_hash = new_hash_cask.sha256.to_s
|
|
|
|
|
|
|
|
cask_download = Cask::Download.new(new_hash_cask, quarantine: true)
|
|
|
|
download = cask_download.fetch(verify_download_integrity: false)
|
|
|
|
Utils::Tar.validate_file(download)
|
|
|
|
|
|
|
|
replacement_pairs << [new_hash_cask.sha256.to_s, download.sha256]
|
|
|
|
end
|
2023-03-25 11:56:05 +01:00
|
|
|
ensure
|
2022-10-12 18:20:53 -04:00
|
|
|
Homebrew::SimulateSystem.clear
|
|
|
|
end
|
|
|
|
elsif new_hash.present?
|
|
|
|
opoo "Cask contains multiple hashes; only updating hash for current arch." if cask.on_system_blocks_exist?
|
|
|
|
replacement_pairs << [old_hash.to_s, new_hash]
|
|
|
|
end
|
|
|
|
end
|
2020-09-10 20:02:26 -07:00
|
|
|
end
|
2020-09-04 16:58:31 -07:00
|
|
|
|
|
|
|
if new_base_url.present?
|
|
|
|
m = /^ +url "(.+?)"\n/m.match(old_contents)
|
|
|
|
odie "Could not find old URL in cask!" if m.nil?
|
|
|
|
|
2023-03-13 18:31:26 -07:00
|
|
|
old_base_url = m.captures.fetch(0)
|
2020-09-04 16:58:31 -07:00
|
|
|
|
|
|
|
replacement_pairs << [
|
|
|
|
/#{Regexp.escape(old_base_url)}/,
|
|
|
|
new_base_url,
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
Utils::Inreplace.inreplace_pairs(cask.sourcefile_path,
|
|
|
|
replacement_pairs.uniq.compact,
|
|
|
|
read_only_run: args.dry_run?,
|
|
|
|
silent: args.quiet?)
|
|
|
|
|
|
|
|
run_cask_audit(cask, old_contents, args: args)
|
|
|
|
run_cask_style(cask, old_contents, args: args)
|
|
|
|
|
2020-12-08 12:42:02 -08:00
|
|
|
branch_name = "bump-#{cask.token}"
|
|
|
|
commit_message = "Update #{cask.token}"
|
|
|
|
if new_version.present?
|
2021-11-08 10:07:27 -08:00
|
|
|
if new_version.before_comma != old_version.before_comma
|
|
|
|
new_version = new_version.before_comma
|
|
|
|
old_version = old_version.before_comma
|
|
|
|
end
|
2020-12-08 12:42:02 -08:00
|
|
|
branch_name += "-#{new_version.tr(",:", "-")}"
|
|
|
|
commit_message += " from #{old_version} to #{new_version}"
|
|
|
|
end
|
2020-09-04 16:58:31 -07:00
|
|
|
pr_info = {
|
|
|
|
sourcefile_path: cask.sourcefile_path,
|
|
|
|
old_contents: old_contents,
|
2020-12-08 12:42:02 -08:00
|
|
|
branch_name: branch_name,
|
|
|
|
commit_message: commit_message,
|
2020-09-04 16:58:31 -07:00
|
|
|
tap: cask.tap,
|
|
|
|
pr_message: "Created with `brew bump-cask-pr`.",
|
|
|
|
}
|
|
|
|
GitHub.create_bump_pr(pr_info, args: args)
|
|
|
|
end
|
|
|
|
|
2023-01-19 17:06:42 -08:00
|
|
|
def check_pull_requests(cask, state:, args:, version: nil)
|
2023-03-29 20:05:16 +08:00
|
|
|
tap_remote_repo = cask.tap.full_name || cask.tap.remote_repo
|
2021-03-21 12:35:45 -04:00
|
|
|
GitHub.check_for_duplicate_pull_requests(cask.token, tap_remote_repo,
|
2023-01-19 17:06:42 -08:00
|
|
|
state: state,
|
|
|
|
version: version,
|
|
|
|
file: cask.sourcefile_path.relative_path_from(cask.tap.path).to_s,
|
|
|
|
args: args)
|
2020-09-04 16:58:31 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_cask_audit(cask, old_contents, args:)
|
|
|
|
if args.dry_run?
|
|
|
|
if args.no_audit?
|
2020-11-18 08:10:21 +01:00
|
|
|
ohai "Skipping `brew audit`"
|
2020-09-16 10:56:54 -07:00
|
|
|
elsif args.online?
|
2020-11-18 08:10:21 +01:00
|
|
|
ohai "brew audit --cask --online #{cask.sourcefile_path.basename}"
|
2020-09-04 16:58:31 -07:00
|
|
|
else
|
2020-11-18 08:10:21 +01:00
|
|
|
ohai "brew audit --cask #{cask.sourcefile_path.basename}"
|
2020-09-04 16:58:31 -07:00
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
failed_audit = false
|
|
|
|
if args.no_audit?
|
2020-11-18 08:10:21 +01:00
|
|
|
ohai "Skipping `brew audit`"
|
2020-09-16 10:56:54 -07:00
|
|
|
elsif args.online?
|
2022-12-28 16:29:33 -05:00
|
|
|
system HOMEBREW_BREW_FILE, "audit", "--cask", "--online", cask.full_name
|
2020-09-16 10:56:54 -07:00
|
|
|
failed_audit = !$CHILD_STATUS.success?
|
2020-09-04 16:58:31 -07:00
|
|
|
else
|
2022-12-28 16:29:33 -05:00
|
|
|
system HOMEBREW_BREW_FILE, "audit", "--cask", cask.full_name
|
2020-09-04 16:58:31 -07:00
|
|
|
failed_audit = !$CHILD_STATUS.success?
|
|
|
|
end
|
|
|
|
return unless failed_audit
|
|
|
|
|
|
|
|
cask.sourcefile_path.atomic_write(old_contents)
|
2020-11-18 08:10:21 +01:00
|
|
|
odie "`brew audit` failed!"
|
2020-09-04 16:58:31 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_cask_style(cask, old_contents, args:)
|
|
|
|
if args.dry_run?
|
|
|
|
if args.no_style?
|
2020-11-18 08:10:21 +01:00
|
|
|
ohai "Skipping `brew style --fix`"
|
2020-09-04 16:58:31 -07:00
|
|
|
else
|
2020-11-18 08:10:21 +01:00
|
|
|
ohai "brew style --fix #{cask.sourcefile_path.basename}"
|
2020-09-04 16:58:31 -07:00
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
failed_style = false
|
|
|
|
if args.no_style?
|
2020-11-18 08:10:21 +01:00
|
|
|
ohai "Skipping `brew style --fix`"
|
2020-09-04 16:58:31 -07:00
|
|
|
else
|
2020-11-18 08:10:21 +01:00
|
|
|
system HOMEBREW_BREW_FILE, "style", "--fix", cask.sourcefile_path
|
2020-09-04 16:58:31 -07:00
|
|
|
failed_style = !$CHILD_STATUS.success?
|
|
|
|
end
|
|
|
|
return unless failed_style
|
|
|
|
|
|
|
|
cask.sourcefile_path.atomic_write(old_contents)
|
2020-11-18 08:10:21 +01:00
|
|
|
odie "`brew style --fix` failed!"
|
2020-09-04 16:58:31 -07:00
|
|
|
end
|
|
|
|
end
|