Refactor bump-cask-pr.

Co-authored-by: Markus Reiter <me@reitermark.us>
This commit is contained in:
Seeker 2020-12-08 12:42:02 -08:00
parent fc921bb640
commit d291b3c185

View File

@ -68,11 +68,15 @@ module Homebrew
cask = args.named.to_casks.first cask = args.named.to_casks.first
new_version = args.version new_version = args.version
new_version = "latest" if new_version == ":latest" new_version = :latest if ["latest", ":latest"].include?(new_version)
new_version = Cask::DSL::Version.new(new_version) new_version = Cask::DSL::Version.new(new_version) if new_version.present?
new_base_url = args.url new_base_url = args.url
new_hash = args.sha256 new_hash = args.sha256
new_hash = :no_check if new_hash == ":no_check" new_hash = :no_check if ["no_check", ":no_check"].include? new_hash
if new_version.nil? && new_base_url.nil? && new_hash.nil?
raise UsageError, "No --version=/--url=/--sha256= argument specified!"
end
old_version = cask.version old_version = cask.version
old_hash = cask.sha256 old_hash = cask.sha256
@ -84,44 +88,20 @@ module Homebrew
check_open_pull_requests(cask, tap_full_name, args: args) check_open_pull_requests(cask, tap_full_name, args: args)
odie "#{cask}: no --version= argument specified!" if new_version.empty? if new_version.present? && !new_version.latest?
check_closed_pull_requests(cask, tap_full_name, version: new_version, args: args)
check_closed_pull_requests(cask, tap_full_name, version: new_version, args: args) unless new_version.latest?
if new_version == old_version
odie <<~EOS
You need to bump this cask manually since the new version
and old version are both #{new_version}.
EOS
elsif old_version.latest?
opoo "No --url= argument specified!" unless new_base_url
elsif new_version.latest?
opoo "Ignoring specified --sha256= argument." if new_hash && new_check != :no_check
elsif Version.new(new_version) < Version.new(old_version)
odie <<~EOS
You need to bump this cask manually since changing the
version from #{old_version} to #{new_version} would be a downgrade.
EOS
end end
old_contents = File.read(cask.sourcefile_path) old_contents = File.read(cask.sourcefile_path)
replacement_pairs = [] replacement_pairs = []
replacement_pairs << if old_version.latest? if new_version.present?
[ old_version_regex = old_version.latest? ? ":latest" : "[\"']#{Regexp.escape(old_version.to_s)}[\"']"
"version :latest",
"version \"#{new_version}\"", replacement_pairs << [
] /version\s+#{old_version_regex}/m,
elsif new_version.latest? "version #{new_version.latest? ? ":latest" : "\"#{new_version}\""}",
[
"version \"#{old_version}\"",
"version :latest",
]
else
[
old_version,
new_version,
] ]
end end
@ -137,58 +117,54 @@ module Homebrew
] ]
end end
if new_version.latest? if new_version.present?
new_hash = :no_check if new_version.latest?
elsif new_hash.nil? || cask.languages.present? opoo "Ignoring specified --sha256= argument." if new_hash.present?
tmp_contents = Utils::Inreplace.inreplace_pairs(cask.sourcefile_path, new_hash = :no_check
replacement_pairs.uniq.compact, elsif new_hash.nil? || cask.languages.present?
read_only_run: true, tmp_contents = Utils::Inreplace.inreplace_pairs(cask.sourcefile_path,
silent: true) replacement_pairs.uniq.compact,
read_only_run: true,
silent: true)
tmp_cask = Cask::CaskLoader.load(tmp_contents) tmp_cask = Cask::CaskLoader.load(tmp_contents)
tmp_config = cask.config tmp_config = cask.config
tmp_url = tmp_cask.url.to_s tmp_url = tmp_cask.url.to_s
if new_hash.nil? if new_hash.nil?
resource_path = fetch_resource(cask, new_version, tmp_url) resource_path = fetch_resource(cask, new_version, tmp_url)
Utils::Tar.validate_file(resource_path) Utils::Tar.validate_file(resource_path)
new_hash = resource_path.sha256 new_hash = resource_path.sha256
end end
cask.languages.each do |language| cask.languages.each do |language|
next if language == cask.language next if language == cask.language
lang_config = tmp_config.merge(Cask::Config.new(explicit: { languages: [language] })) lang_config = tmp_config.merge(Cask::Config.new(explicit: { languages: [language] }))
lang_cask = Cask::CaskLoader.load(tmp_contents) lang_cask = Cask::CaskLoader.load(tmp_contents)
lang_cask.config = lang_config lang_cask.config = lang_config
lang_url = lang_cask.url.to_s lang_url = lang_cask.url.to_s
lang_old_hash = lang_cask.sha256.to_s lang_old_hash = lang_cask.sha256.to_s
resource_path = fetch_resource(cask, new_version, lang_url) resource_path = fetch_resource(cask, new_version, lang_url)
Utils::Tar.validate_file(resource_path) Utils::Tar.validate_file(resource_path)
lang_new_hash = resource_path.sha256 lang_new_hash = resource_path.sha256
replacement_pairs << [ replacement_pairs << [
lang_old_hash, lang_old_hash,
lang_new_hash, lang_new_hash,
] ]
end
end end
end end
p old_hash if new_hash.present?
replacement_pairs << if old_version.latest? || new_version.latest? || new_hash == :no_check
hash_regex = old_hash == :no_check ? ":no_check" : "[\"']#{Regexp.escape(old_hash.to_s)}[\"']" hash_regex = old_hash == :no_check ? ":no_check" : "[\"']#{Regexp.escape(old_hash.to_s)}[\"']"
[ replacement_pairs << [
/sha256\s+#{hash_regex}/m, /sha256\s+#{hash_regex}/m,
"sha256 #{new_hash == :no_check ? ":no_check" : "\"#{new_hash}\""}", "sha256 #{new_hash == :no_check ? ":no_check" : "\"#{new_hash}\""}",
] ]
else
[
old_hash,
new_hash,
]
end end
Utils::Inreplace.inreplace_pairs(cask.sourcefile_path, Utils::Inreplace.inreplace_pairs(cask.sourcefile_path,
@ -199,12 +175,18 @@ module Homebrew
run_cask_audit(cask, old_contents, args: args) run_cask_audit(cask, old_contents, args: args)
run_cask_style(cask, old_contents, args: args) run_cask_style(cask, old_contents, args: args)
branch_name = "bump-#{cask.token}"
commit_message = "Update #{cask.token}"
if new_version.present?
branch_name += "-#{new_version.tr(",:", "-")}"
commit_message += " from #{old_version} to #{new_version}"
end
pr_info = { pr_info = {
sourcefile_path: cask.sourcefile_path, sourcefile_path: cask.sourcefile_path,
old_contents: old_contents, old_contents: old_contents,
remote_branch: default_remote_branch, remote_branch: default_remote_branch,
branch_name: "bump-#{cask.token}-#{new_version.tr(",:", "-")}", branch_name: branch_name,
commit_message: "Update #{cask.token} from #{old_version} to #{new_version}", commit_message: commit_message,
previous_branch: previous_branch, previous_branch: previous_branch,
tap: cask.tap, tap: cask.tap,
tap_full_name: tap_full_name, tap_full_name: tap_full_name,