Skip to content

Commit

Permalink
Merge pull request #6857 from GauthamGoli/argv-cleanup-7
Browse files Browse the repository at this point in the history
ARGV: Replace usages of ARGV.resolved_formulae with Homebrew.args
  • Loading branch information
MikeMcQuaid authored Jan 2, 2020
2 parents 5baf68a + d50a8c4 commit c0b9167
Show file tree
Hide file tree
Showing 19 changed files with 48 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Library/Homebrew/cmd/--cellar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __cellar
if Homebrew.args.named.blank?
puts HOMEBREW_CELLAR
else
puts ARGV.resolved_formulae.map(&:rack)
puts Homebrew.args.resolved_formulae.map(&:rack)
end
end
end
2 changes: 1 addition & 1 deletion Library/Homebrew/cmd/--prefix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __prefix
if Homebrew.args.named.blank?
puts HOMEBREW_PREFIX
else
puts ARGV.resolved_formulae.map { |f|
puts Homebrew.args.resolved_formulae.map { |f|
f.opt_prefix.exist? ? f.opt_prefix : f.installed_prefix
}
end
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/cmd/gist-logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ def create_issue(repo, title, body)
end

def gist_logs
raise FormulaUnspecifiedError if ARGV.resolved_formulae.length != 1
raise FormulaUnspecifiedError if Homebrew.args.resolved_formulae.length != 1

Install.perform_preinstall_checks(all_fatal: true)
Install.perform_build_from_source_checks(all_fatal: true)
gistify_logs(ARGV.resolved_formulae.first)
gistify_logs(Homebrew.args.resolved_formulae.first)
end
end
2 changes: 1 addition & 1 deletion Library/Homebrew/cmd/migrate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def migrate

raise FormulaUnspecifiedError if Homebrew.args.named.blank?

ARGV.resolved_formulae.each do |f|
Homebrew.args.resolved_formulae.each do |f|
if f.oldname
unless (rack = HOMEBREW_CELLAR/f.oldname).exist? && !rack.subdirs.empty?
raise NoSuchKegError, f.oldname
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cmd/missing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def missing
ff = if Homebrew.args.named.blank?
Formula.installed.sort
else
ARGV.resolved_formulae.sort
Homebrew.args.resolved_formulae.sort
end

ff.each do |f|
Expand Down
6 changes: 3 additions & 3 deletions Library/Homebrew/cmd/outdated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def outdated_args
def outdated
outdated_args.parse

formulae = if ARGV.resolved_formulae.empty?
formulae = if Homebrew.args.resolved_formulae.blank?
Formula.installed
else
ARGV.resolved_formulae
Homebrew.args.resolved_formulae
end
if args.json
raise UsageError, "Invalid JSON version: #{args.json}" unless ["v1", true].include? args.json
Expand All @@ -47,7 +47,7 @@ def outdated
else
outdated = print_outdated(formulae)
end
Homebrew.failed = !ARGV.resolved_formulae.empty? && !outdated.empty?
Homebrew.failed = Homebrew.args.resolved_formulae.present? && !outdated.empty?
end

def print_outdated(formulae)
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cmd/pin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def pin

raise FormulaUnspecifiedError if args.remaining.empty?

ARGV.resolved_formulae.each do |f|
Homebrew.args.resolved_formulae.each do |f|
if f.pinned?
opoo "#{f.name} already pinned"
elsif !f.pinnable?
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cmd/postinstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def postinstall

raise KegUnspecifiedError if args.remaining.empty?

ARGV.resolved_formulae.each do |f|
Homebrew.args.resolved_formulae.each do |f|
ohai "Postinstalling #{f}"
fi = FormulaInstaller.new(f)
fi.post_install
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cmd/reinstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def reinstall

Install.perform_preinstall_checks

ARGV.resolved_formulae.each do |f|
Homebrew.args.resolved_formulae.each do |f|
if f.pinned?
onoe "#{f.full_name} is pinned. You must unpin it to reinstall."
next
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cmd/unpin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def unpin

raise FormulaUnspecifiedError if args.remaining.empty?

ARGV.resolved_formulae.each do |f|
Homebrew.args.resolved_formulae.each do |f|
if f.pinned?
f.unpin
elsif !f.pinnable?
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/cmd/upgrade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ def upgrade

exit 0 if outdated.empty?
else
outdated = ARGV.resolved_formulae.select do |f|
outdated = Homebrew.args.resolved_formulae.select do |f|
f.outdated?(fetch_head: args.fetch_HEAD?)
end

(ARGV.resolved_formulae - outdated).each do |f|
(Homebrew.args.resolved_formulae - outdated).each do |f|
versions = f.installed_kegs.map(&:version)
if versions.empty?
ofail "#{f.full_specified_name} not installed"
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/dev-cmd/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def audit
ff = Formula
files = Tap.map(&:formula_dir)
else
ff = ARGV.resolved_formulae
files = ARGV.resolved_formulae.map(&:path)
ff = Homebrew.args.resolved_formulae
files = Homebrew.args.resolved_formulae.map(&:path)
end

only_cops = args.only_cops
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/dev-cmd/bottle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def bottle
raise KegUnspecifiedError if args.remaining.empty?

ensure_relocation_formulae_installed! unless args.skip_relocation?
ARGV.resolved_formulae.each do |f|
Homebrew.args.resolved_formulae.each do |f|
bottle_formula f
end
end
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/dev-cmd/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def formula

raise FormulaUnspecifiedError if Homebrew.args.named.blank?

ARGV.resolved_formulae.each { |f| puts f.path }
Homebrew.args.resolved_formulae.each { |f| puts f.path }
end
end
2 changes: 1 addition & 1 deletion Library/Homebrew/dev-cmd/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test

require "formula_assertions"

ARGV.resolved_formulae.each do |f|
Homebrew.args.resolved_formulae.each do |f|
# Cannot test uninstalled formulae
unless f.latest_version_installed?
ofail "Testing requires the latest version of #{f.full_name}"
Expand Down
28 changes: 8 additions & 20 deletions Library/Homebrew/extend/ARGV.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ def named
self - options_only
end

def options_only
select { |arg| arg.start_with?("-") }
end

def flags_only
select { |arg| arg.start_with?("--") }
end
Expand All @@ -26,14 +22,6 @@ def formulae
end.uniq(&:name)
end

def resolved_formulae
require "formula"
# TODO: use @instance variable to ||= cache when moving to CLI::Parser
(downcased_unique_named - casks).map do |name|
Formulary.resolve(name, spec: spec(nil))
end.uniq(&:name)
end

def casks
# TODO: use @instance variable to ||= cache when moving to CLI::Parser
downcased_unique_named.grep HOMEBREW_CASK_TAP_CASK_REGEX
Expand Down Expand Up @@ -118,18 +106,10 @@ def build_formula_from_source?(f)
formulae.any? { |argv_f| argv_f.full_name == f.full_name }
end

def flag?(flag)
options_only.include?(flag) || switch?(flag[2, 1])
end

def force_bottle?
include?("--force-bottle")
end

def fetch_head?
include? "--fetch-HEAD"
end

def cc
value "cc"
end
Expand All @@ -153,6 +133,14 @@ def collect_build_flags

private

def options_only
select { |arg| arg.start_with?("-") }
end

def flag?(flag)
options_only.include?(flag) || switch?(flag[2, 1])
end

# e.g. `foo -ns -i --bar` has three switches: `n`, `s` and `i`
def switch?(char)
return false if char.length > 1
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/postinstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def postinstall_args
trap("INT", old_trap)

formula = Homebrew.args.resolved_formulae.first
formula.extend(Debrew::Formula) if Homebrew.args.debug?
formula.run_post_install
rescue Exception => e # rubocop:disable Lint/RescueException
error_pipe.puts e.to_json
Expand Down
14 changes: 12 additions & 2 deletions Library/Homebrew/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@
require "formula_assertions"
require "fcntl"
require "socket"
require "cli/parser"

def test_args
Homebrew::CLI::Parser.new do
switch :force
switch :verbose
switch :debug
end
end

TEST_TIMEOUT_SECONDS = 5 * 60

begin
test_args.parse
error_pipe = UNIXSocket.open(ENV["HOMEBREW_ERROR_PIPE"], &:recv_io)
error_pipe.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)

Expand All @@ -21,9 +31,9 @@

trap("INT", old_trap)

formula = ARGV.resolved_formulae.first
formula = Homebrew.args.resolved_formulae.first
formula.extend(Homebrew::Assertions)
formula.extend(Debrew::Formula) if ARGV.debug?
formula.extend(Debrew::Formula) if Homebrew.args.debug?

# tests can also return false to indicate failure
Timeout.timeout TEST_TIMEOUT_SECONDS do
Expand Down
14 changes: 7 additions & 7 deletions Library/Homebrew/test/ARGV_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
let(:argv) { ["--foo", "-vds", "a", "b", "cdefg"] }

it "returns an array of option arguments" do
expect(subject.options_only).to eq ["--foo", "-vds"]
expect(subject.send("options_only")).to eq ["--foo", "-vds"]
end
end

Expand Down Expand Up @@ -87,18 +87,18 @@
let(:argv) { ["--foo", "-bq", "--bar"] }

it "returns true if the given string is a flag" do
expect(subject.flag?("--foo")).to eq true
expect(subject.flag?("--bar")).to eq true
expect(subject.send("flag?", "--foo")).to eq true
expect(subject.send("flag?", "--bar")).to eq true
end

it "returns true if there is a switch with the same initial character" do
expect(subject.flag?("--baz")).to eq true
expect(subject.flag?("--qux")).to eq true
expect(subject.send("flag?", "--baz")).to eq true
expect(subject.send("flag?", "--qux")).to eq true
end

it "returns false if there is no matching flag" do
expect(subject.flag?("--frotz")).to eq false
expect(subject.flag?("--debug")).to eq false
expect(subject.send("flag?", "--frotz")).to eq false
expect(subject.send("flag?", "--debug")).to eq false
end
end

Expand Down

0 comments on commit c0b9167

Please sign in to comment.