dev-cmd: use more and cleanup new args APIs.

This commit is contained in:
Mike McQuaid 2020-03-04 17:28:24 +00:00
parent a7fe0ed847
commit e3ac94fc5d
No known key found for this signature in database
GPG Key ID: 48A898132FD8EE70
14 changed files with 47 additions and 63 deletions

View File

@ -79,12 +79,12 @@ module Homebrew
ENV.activate_extensions! ENV.activate_extensions!
ENV.setup_build_environment ENV.setup_build_environment
if Homebrew.args.named.blank? if args.no_named?
ff = Formula ff = Formula
files = Tap.map(&:formula_dir) files = Tap.map(&:formula_dir)
else else
ff = Homebrew.args.resolved_formulae ff = args.resolved_formulae
files = Homebrew.args.resolved_formulae.map(&:path) files = args.resolved_formulae.map(&:path)
end end
only_cops = args.only_cops only_cops = args.only_cops

View File

@ -79,6 +79,7 @@ module Homebrew
switch :verbose switch :verbose
switch :debug switch :debug
conflicts "--no-rebuild", "--keep-old" conflicts "--no-rebuild", "--keep-old"
min_named 1
end end
end end
@ -86,10 +87,9 @@ module Homebrew
bottle_args.parse bottle_args.parse
return merge if args.merge? return merge if args.merge?
raise KegUnspecifiedError if args.remaining.empty?
ensure_relocation_formulae_installed! unless args.skip_relocation? ensure_relocation_formulae_installed! unless args.skip_relocation?
Homebrew.args.resolved_formulae.each do |f| args.resolved_formulae.each do |f|
bottle_formula f bottle_formula f
end end
end end
@ -435,10 +435,7 @@ module Homebrew
end end
def merge def merge
write = args.write? bottles_hash = args.named.reduce({}) do |hash, json_file|
raise UsageError, "--merge requires a JSON file path argument" if Homebrew.args.named.blank?
bottles_hash = Homebrew.args.named.reduce({}) do |hash, json_file|
hash.deep_merge(JSON.parse(IO.read(json_file))) do |key, first, second| hash.deep_merge(JSON.parse(IO.read(json_file))) do |key, first, second|
if key == "cellar" if key == "cellar"
# Prioritize HOMEBREW_CELLAR over :any over :any_skip_relocation # Prioritize HOMEBREW_CELLAR over :any over :any_skip_relocation
@ -478,7 +475,7 @@ module Homebrew
output = bottle_output bottle output = bottle_output bottle
if write if args.write?
path = Pathname.new((HOMEBREW_REPOSITORY/bottle_hash["formula"]["path"]).to_s) path = Pathname.new((HOMEBREW_REPOSITORY/bottle_hash["formula"]["path"]).to_s)
update_or_add = nil update_or_add = nil

View File

@ -116,7 +116,7 @@ module Homebrew
# Use the user's browser, too. # Use the user's browser, too.
ENV["BROWSER"] = ENV["HOMEBREW_BROWSER"] ENV["BROWSER"] = ENV["HOMEBREW_BROWSER"]
formula = Homebrew.args.formulae.first formula = args.formulae.first
if formula if formula
tap_full_name, origin_branch, previous_branch = use_correct_linux_tap(formula) tap_full_name, origin_branch, previous_branch = use_correct_linux_tap(formula)
@ -315,7 +315,7 @@ module Homebrew
new_formula_version = formula_version(formula, requested_spec, new_contents) new_formula_version = formula_version(formula, requested_spec, new_contents)
if !new_mirrors && !formula_spec.mirrors.empty? if !new_mirrors && !formula_spec.mirrors.empty?
if Homebrew.args.force? if args.force?
opoo "#{formula}: Removing all mirrors because a --mirror= argument was not specified." opoo "#{formula}: Removing all mirrors because a --mirror= argument was not specified."
else else
odie <<~EOS odie <<~EOS
@ -433,7 +433,7 @@ module Homebrew
contents = path.open("r") { |f| Formulary.ensure_utf8_encoding(f).read } contents = path.open("r") { |f| Formulary.ensure_utf8_encoding(f).read }
contents.extend(StringInreplaceExtension) contents.extend(StringInreplaceExtension)
replacement_pairs.each do |old, new| replacement_pairs.each do |old, new|
ohai "replace #{old.inspect} with #{new.inspect}" unless Homebrew.args.quiet? ohai "replace #{old.inspect} with #{new.inspect}" unless args.quiet?
raise "No old value for new value #{new}! Did you pass the wrong arguments?" unless old raise "No old value for new value #{new}! Did you pass the wrong arguments?" unless old
contents.gsub!(old, new) contents.gsub!(old, new)
@ -445,7 +445,7 @@ module Homebrew
else else
Utils::Inreplace.inreplace(path) do |s| Utils::Inreplace.inreplace(path) do |s|
replacement_pairs.each do |old, new| replacement_pairs.each do |old, new|
ohai "replace #{old.inspect} with #{new.inspect}" unless Homebrew.args.quiet? ohai "replace #{old.inspect} with #{new.inspect}" unless args.quiet?
raise "No old value for new value #{new}! Did you pass the wrong arguments?" unless old raise "No old value for new value #{new}! Did you pass the wrong arguments?" unless old
s.gsub!(old, new) s.gsub!(old, new)
@ -485,11 +485,11 @@ module Homebrew
#{pull_requests.map { |pr| "#{pr["title"]} #{pr["html_url"]}" }.join("\n")} #{pull_requests.map { |pr| "#{pr["title"]} #{pr["html_url"]}" }.join("\n")}
EOS EOS
error_message = "Duplicate PRs should not be opened. Use --force to override this error." error_message = "Duplicate PRs should not be opened. Use --force to override this error."
if Homebrew.args.force? && !Homebrew.args.quiet? if args.force? && !args.quiet?
opoo duplicates_message opoo duplicates_message
elsif !Homebrew.args.force? && Homebrew.args.quiet? elsif !args.force? && args.quiet?
odie error_message odie error_message
elsif !Homebrew.args.force? elsif !args.force?
odie <<~EOS odie <<~EOS
#{duplicates_message.chomp} #{duplicates_message.chomp}
#{error_message} #{error_message}

View File

@ -33,7 +33,7 @@ module Homebrew
# user path, too. # user path, too.
ENV["PATH"] = ENV["HOMEBREW_PATH"] ENV["PATH"] = ENV["HOMEBREW_PATH"]
formulae = Homebrew.args.formulae formulae = args.formulae
raise FormulaUnspecifiedError if formulae.empty? raise FormulaUnspecifiedError if formulae.empty?
formula = formulae.first formula = formulae.first
@ -64,7 +64,7 @@ module Homebrew
end end
if args.dry_run? if args.dry_run?
ohai "replace #{old.inspect} with #{replacement.inspect}" unless Homebrew.args.quiet? ohai "replace #{old.inspect} with #{replacement.inspect}" unless args.quiet?
else else
Utils::Inreplace.inreplace(formula.path) do |s| Utils::Inreplace.inreplace(formula.path) do |s|
s.gsub!(old, replacement) s.gsub!(old, replacement)

View File

@ -49,7 +49,7 @@ module Homebrew
switch :verbose switch :verbose
switch :debug switch :debug
conflicts "--autotools", "--cmake", "--go", "--meson", "--perl", "--python", "--rust" conflicts "--autotools", "--cmake", "--go", "--meson", "--perl", "--python", "--rust"
max_named 1 named 1
end end
end end
@ -57,12 +57,10 @@ module Homebrew
def create def create
create_args.parse create_args.parse
raise UsageError if ARGV.named.empty?
# Ensure that the cache exists so we can fetch the tarball # Ensure that the cache exists so we can fetch the tarball
HOMEBREW_CACHE.mkpath HOMEBREW_CACHE.mkpath
url = ARGV.named.first # Pull the first (and only) url from ARGV url = args.named.first # Pull the first (and only) url from ARGV
version = args.set_version version = args.set_version
name = args.set_name name = args.set_name

View File

@ -32,10 +32,10 @@ module Homebrew
end end
# If no brews are listed, open the project root in an editor. # If no brews are listed, open the project root in an editor.
paths = [HOMEBREW_REPOSITORY] if ARGV.named.empty? paths = [HOMEBREW_REPOSITORY] if args.no_named?
# Don't use ARGV.formulae as that will throw if the file doesn't parse # Don't use args.formulae as that will throw if the file doesn't parse
paths ||= ARGV.named.map do |name| paths ||= args.named.map do |name|
path = Formulary.path(name) path = Formulary.path(name)
raise FormulaUnavailableError, name if !path.file? && !args.force? raise FormulaUnavailableError, name if !path.file? && !args.force?

View File

@ -91,18 +91,15 @@ module Homebrew
description: "Extract the specified <version> of <formula> instead of the most recent." description: "Extract the specified <version> of <formula> instead of the most recent."
switch :force switch :force
switch :debug switch :debug
max_named 2 named 2
end end
end end
def extract def extract
extract_args.parse extract_args.parse
# Expect exactly two named arguments: formula and tap if args.named.first !~ HOMEBREW_TAP_FORMULA_REGEX
raise UsageError, "This command requires formula and tap arguments" if args.remaining.length != 2 name = args.named.first.downcase
if args.remaining.first !~ HOMEBREW_TAP_FORMULA_REGEX
name = args.remaining.first.downcase
source_tap = CoreTap.instance source_tap = CoreTap.instance
else else
name = Regexp.last_match(3).downcase name = Regexp.last_match(3).downcase
@ -110,7 +107,7 @@ module Homebrew
raise TapFormulaUnavailableError.new(source_tap, name) unless source_tap.installed? raise TapFormulaUnavailableError.new(source_tap, name) unless source_tap.installed?
end end
destination_tap = Tap.fetch(args.remaining.second) destination_tap = Tap.fetch(args.named.second)
unless ARGV.homebrew_developer? unless ARGV.homebrew_developer?
odie "Cannot extract formula to homebrew/core!" if destination_tap.core_tap? odie "Cannot extract formula to homebrew/core!" if destination_tap.core_tap?
odie "Cannot extract formula to the same tap!" if destination_tap == source_tap odie "Cannot extract formula to the same tap!" if destination_tap == source_tap
@ -190,7 +187,7 @@ module Homebrew
path = destination_tap.path/"Formula/#{name}@#{version}.rb" path = destination_tap.path/"Formula/#{name}@#{version}.rb"
if path.exist? if path.exist?
unless Homebrew.args.force? unless args.force?
odie <<~EOS odie <<~EOS
Destination formula already exists: #{path} Destination formula already exists: #{path}
To overwrite it and continue anyways, run: To overwrite it and continue anyways, run:

View File

@ -15,14 +15,13 @@ module Homebrew
EOS EOS
switch :verbose switch :verbose
switch :debug switch :debug
min_named :formula
end end
end end
def formula def formula
formula_args.parse formula_args.parse
raise FormulaUnspecifiedError if Homebrew.args.named.blank? args.resolved_formulae.each { |f| puts f.path }
Homebrew.args.resolved_formulae.each { |f| puts f.path }
end end
end end

View File

@ -33,10 +33,10 @@ module Homebrew
linkage_args.parse linkage_args.parse
CacheStoreDatabase.use(:linkage) do |db| CacheStoreDatabase.use(:linkage) do |db|
kegs = if Homebrew.args.kegs.empty? kegs = if args.kegs.empty?
Formula.installed.map(&:opt_or_installed_prefix_keg).reject(&:nil?) Formula.installed.map(&:opt_or_installed_prefix_keg).reject(&:nil?)
else else
Homebrew.args.kegs args.kegs
end end
kegs.each do |keg| kegs.each do |keg|
ohai "Checking #{keg.name} linkage" if kegs.size > 1 ohai "Checking #{keg.name} linkage" if kegs.size > 1

View File

@ -15,19 +15,18 @@ module Homebrew
switch :verbose switch :verbose
switch :debug switch :debug
hide_from_man_page! hide_from_man_page!
min_named :formula
end end
end end
def mirror def mirror
mirror_args.parse mirror_args.parse
raise FormulaUnspecifiedError if args.remaining.empty?
bintray_user = ENV["HOMEBREW_BINTRAY_USER"] bintray_user = ENV["HOMEBREW_BINTRAY_USER"]
bintray_key = ENV["HOMEBREW_BINTRAY_KEY"] bintray_key = ENV["HOMEBREW_BINTRAY_KEY"]
raise "Missing HOMEBREW_BINTRAY_USER or HOMEBREW_BINTRAY_KEY variables!" if !bintray_user || !bintray_key raise "Missing HOMEBREW_BINTRAY_USER or HOMEBREW_BINTRAY_KEY variables!" if !bintray_user || !bintray_key
Homebrew.args.formulae.each do |f| args.formulae.each do |f|
bintray_package = Utils::Bottles::Bintray.package f.name bintray_package = Utils::Bottles::Bintray.package f.name
bintray_repo_url = "https://api.bintray.com/packages/homebrew/mirror" bintray_repo_url = "https://api.bintray.com/packages/homebrew/mirror"
package_url = "#{bintray_repo_url}/#{bintray_package}" package_url = "#{bintray_repo_url}/#{bintray_package}"

View File

@ -62,6 +62,7 @@ module Homebrew
description: "Pull the bottle block commit from the specified <user> on GitHub." description: "Pull the bottle block commit from the specified <user> on GitHub."
switch :verbose switch :verbose
switch :debug switch :debug
min_named 1
end end
end end
@ -70,10 +71,6 @@ module Homebrew
pull_args.parse pull_args.parse
if ARGV.named.empty?
raise UsageError, "This command requires at least one argument containing a URL or pull request number"
end
# Passthrough Git environment variables for e.g. git am # Passthrough Git environment variables for e.g. git am
ENV["GIT_COMMITTER_NAME"] = ENV["HOMEBREW_GIT_NAME"] if ENV["HOMEBREW_GIT_NAME"] ENV["GIT_COMMITTER_NAME"] = ENV["HOMEBREW_GIT_NAME"] if ENV["HOMEBREW_GIT_NAME"]
ENV["GIT_COMMITTER_EMAIL"] = ENV["HOMEBREW_GIT_EMAIL"] if ENV["HOMEBREW_GIT_EMAIL"] ENV["GIT_COMMITTER_EMAIL"] = ENV["HOMEBREW_GIT_EMAIL"] if ENV["HOMEBREW_GIT_EMAIL"]
@ -96,7 +93,7 @@ module Homebrew
tap = nil tap = nil
ARGV.named.each do |arg| args.named.each do |arg|
arg = "#{CoreTap.instance.default_remote}/pull/#{arg}" if arg.to_i.positive? arg = "#{CoreTap.instance.default_remote}/pull/#{arg}" if arg.to_i.positive?
if (testing_match = arg.match %r{/job/Homebrew.*Testing/(\d+)}) if (testing_match = arg.match %r{/job/Homebrew.*Testing/(\d+)})
tap = ARGV.value("tap") tap = ARGV.value("tap")

View File

@ -23,13 +23,13 @@ module Homebrew
def release_notes def release_notes
release_notes_args.parse release_notes_args.parse
previous_tag = ARGV.named.first previous_tag = args.named.first
previous_tag ||= Utils.popen_read( previous_tag ||= Utils.popen_read(
"git", "-C", HOMEBREW_REPOSITORY, "tag", "--list", "--sort=-version:refname" "git", "-C", HOMEBREW_REPOSITORY, "tag", "--list", "--sort=-version:refname"
).lines.first.chomp ).lines.first.chomp
odie "Could not find any previous tags!" unless previous_tag odie "Could not find any previous tags!" unless previous_tag
end_ref = ARGV.named.second || "origin/master" end_ref = args.named.second || "origin/master"
[previous_tag, end_ref].each do |ref| [previous_tag, end_ref].each do |ref|
next if quiet_system "git", "-C", HOMEBREW_REPOSITORY, "rev-parse", "--verify", "--quiet", ref next if quiet_system "git", "-C", HOMEBREW_REPOSITORY, "rev-parse", "--verify", "--quiet", ref

View File

@ -15,16 +15,14 @@ module Homebrew
EOS EOS
switch :verbose switch :verbose
switch :debug switch :debug
max_named 1 named 1
end end
end end
def tap_new def tap_new
tap_new_args.parse tap_new_args.parse
raise UsageError, "This command requires a tap argument" if ARGV.named.empty? tap = Tap.fetch(args.named.first)
tap = Tap.fetch(ARGV.named.first)
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

View File

@ -28,17 +28,16 @@ module Homebrew
switch :verbose switch :verbose
switch :debug switch :debug
conflicts "--devel", "--HEAD" conflicts "--devel", "--HEAD"
min_named :formula
end end
end end
def test def test
test_args.parse test_args.parse
raise FormulaUnspecifiedError if ARGV.named.empty?
require "formula_assertions" require "formula_assertions"
Homebrew.args.resolved_formulae.each do |f| args.resolved_formulae.each do |f|
# Cannot test uninstalled formulae # Cannot test uninstalled formulae
unless f.latest_version_installed? unless f.latest_version_installed?
ofail "Testing requires the latest version of #{f.full_name}" ofail "Testing requires the latest version of #{f.full_name}"
@ -52,7 +51,7 @@ module Homebrew
end end
# Don't test unlinked formulae # Don't test unlinked formulae
if !Homebrew.args.force? && !f.keg_only? && !f.linked? if !args.force? && !f.keg_only? && !f.linked?
ofail "#{f.full_name} is not linked" ofail "#{f.full_name} is not linked"
next next
end end
@ -75,19 +74,19 @@ module Homebrew
env = ENV.to_hash env = ENV.to_hash
begin begin
args = %W[ exec_args = %W[
#{RUBY_PATH} #{RUBY_PATH}
-W0 -W0
-I #{$LOAD_PATH.join(File::PATH_SEPARATOR)} -I #{$LOAD_PATH.join(File::PATH_SEPARATOR)}
-- --
#{HOMEBREW_LIBRARY_PATH}/test.rb #{HOMEBREW_LIBRARY_PATH}/test.rb
#{f.path} #{f.path}
].concat(Homebrew.args.options_only) ].concat(args.options_only)
if f.head? if f.head?
args << "--HEAD" exec_args << "--HEAD"
elsif f.devel? elsif f.devel?
args << "--devel" exec_args << "--devel"
end end
Utils.safe_fork do Utils.safe_fork do
@ -102,9 +101,9 @@ module Homebrew
sandbox.allow_write_path(HOMEBREW_PREFIX/"var/homebrew/locks") sandbox.allow_write_path(HOMEBREW_PREFIX/"var/homebrew/locks")
sandbox.allow_write_path(HOMEBREW_PREFIX/"var/log") sandbox.allow_write_path(HOMEBREW_PREFIX/"var/log")
sandbox.allow_write_path(HOMEBREW_PREFIX/"var/run") sandbox.allow_write_path(HOMEBREW_PREFIX/"var/run")
sandbox.exec(*args) sandbox.exec(*exec_args)
else else
exec(*args) exec(*exec_args)
end end
end end
rescue Exception => e # rubocop:disable Lint/RescueException rescue Exception => e # rubocop:disable Lint/RescueException