From 22e25dd593c7952fe509c52a291f36620bc268a1 Mon Sep 17 00:00:00 2001
From: Gautham Goli <gauthamgoli@gautham2.local>
Date: Sun, 8 Sep 2019 19:56:24 +0530
Subject: [PATCH] argv: move formulae to cli/args

---
 Library/Homebrew/cli/args.rb                | 53 +++++++++++++++++----
 Library/Homebrew/cli/parser.rb              |  3 +-
 Library/Homebrew/cmd/--cache.rb             |  2 +-
 Library/Homebrew/cmd/--env.rb               |  2 +-
 Library/Homebrew/cmd/cat.rb                 |  2 +-
 Library/Homebrew/cmd/deps.rb                | 12 ++---
 Library/Homebrew/cmd/desc.rb                |  2 +-
 Library/Homebrew/cmd/fetch.rb               |  4 +-
 Library/Homebrew/cmd/home.rb                |  2 +-
 Library/Homebrew/cmd/info.rb                |  4 +-
 Library/Homebrew/cmd/install.rb             |  2 +-
 Library/Homebrew/cmd/options.rb             |  2 +-
 Library/Homebrew/cmd/style.rb               |  2 +-
 Library/Homebrew/cmd/unpack.rb              |  2 +-
 Library/Homebrew/cmd/uses.rb                |  2 +-
 Library/Homebrew/dev-cmd/bump-formula-pr.rb |  2 +-
 Library/Homebrew/dev-cmd/bump-revision.rb   |  7 +--
 Library/Homebrew/dev-cmd/mirror.rb          |  2 +-
 Library/Homebrew/test/cli/parser_spec.rb    | 25 ++++++++++
 19 files changed, 98 insertions(+), 34 deletions(-)

diff --git a/Library/Homebrew/cli/args.rb b/Library/Homebrew/cli/args.rb
index 4032bb9dab..61ffb1d722 100644
--- a/Library/Homebrew/cli/args.rb
+++ b/Library/Homebrew/cli/args.rb
@@ -51,22 +51,36 @@ module Homebrew
         options_only - CLI::Parser.global_options.values.map(&:first).flatten
       end
 
-      def downcased_unique_named
-        # Only lowercase names, not paths, bottle filenames or URLs
-        @downcased_unique_named ||= remaining.map do |arg|
-          if arg.include?("/") || arg.end_with?(".tar.gz") || File.exist?(arg)
-            arg
+      def named
+        remaining
+      end
+
+      def formulae
+        require "formula"
+        @formulae ||= (downcased_unique_named - casks).map do |name|
+          if name.include?("/") || File.exist?(name)
+            Formulary.factory(name, spec)
           else
-            arg.downcase
+            Formulary.find_with_priority(name, spec)
           end
-        end.uniq
+        end.uniq(&:name)
+      end
+
+      def resolved_formulae
+        require "formula"
+        @resolved_formulae ||= (downcased_unique_named - casks).map do |name|
+          Formulary.resolve(name, spec: spec(nil))
+        end.uniq(&:name)
+      end
+
+      def casks
+        @casks ||= downcased_unique_named.grep HOMEBREW_CASK_TAP_CASK_REGEX
       end
 
       def kegs
         require "keg"
         require "formula"
         require "missing_formula"
-
         @kegs ||= downcased_unique_named.map do |name|
           raise UsageError if name.empty?
 
@@ -113,6 +127,29 @@ module Homebrew
           end
         end
       end
+
+      private
+
+      def downcased_unique_named
+        # Only lowercase names, not paths, bottle filenames or URLs
+        remaining.map do |arg|
+          if arg.include?("/") || arg.end_with?(".tar.gz") || File.exist?(arg)
+            arg
+          else
+            arg.downcase
+          end
+        end.uniq
+      end
+
+      def spec(default = :stable)
+        if HEAD?
+          :head
+        elsif devel?
+          :devel
+        else
+          default
+        end
+      end
     end
   end
 end
diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb
index 1e76709170..3992537466 100644
--- a/Library/Homebrew/cli/parser.rb
+++ b/Library/Homebrew/cli/parser.rb
@@ -28,6 +28,7 @@ module Homebrew
       def initialize(&block)
         @parser = OptionParser.new
         @args = Homebrew::CLI::Args.new(argv: ARGV_WITHOUT_MONKEY_PATCHING)
+        @args[:remaining] = []
         @constraints = []
         @conflicts = []
         @switch_sources = {}
@@ -159,7 +160,7 @@ module Homebrew
       end
 
       def formula_options
-        ARGV.formulae.each do |f|
+        @args.formulae.each do |f|
           next if f.options.empty?
 
           f.options.each do |o|
diff --git a/Library/Homebrew/cmd/--cache.rb b/Library/Homebrew/cmd/--cache.rb
index dc253a37ed..5e72c776cc 100644
--- a/Library/Homebrew/cmd/--cache.rb
+++ b/Library/Homebrew/cmd/--cache.rb
@@ -29,7 +29,7 @@ module Homebrew
     if ARGV.named.empty?
       puts HOMEBREW_CACHE
     else
-      ARGV.formulae.each do |f|
+      Homebrew.args.formulae.each do |f|
         if Fetch.fetch_bottle?(f)
           puts f.bottle.cached_download
         else
diff --git a/Library/Homebrew/cmd/--env.rb b/Library/Homebrew/cmd/--env.rb
index f1c3e3e94f..8da098ea26 100644
--- a/Library/Homebrew/cmd/--env.rb
+++ b/Library/Homebrew/cmd/--env.rb
@@ -30,7 +30,7 @@ module Homebrew
     __env_args.parse
 
     ENV.activate_extensions!
-    ENV.deps = ARGV.formulae if superenv?
+    ENV.deps = Homebrew.args.formulae if superenv?
     ENV.setup_build_environment
     ENV.universal_binary if ARGV.build_universal?
 
diff --git a/Library/Homebrew/cmd/cat.rb b/Library/Homebrew/cmd/cat.rb
index c7f2f4c790..1f2386da67 100644
--- a/Library/Homebrew/cmd/cat.rb
+++ b/Library/Homebrew/cmd/cat.rb
@@ -20,7 +20,7 @@ module Homebrew
     # do not "fix" this to support multiple arguments, the output would be
     # unparsable, if the user wants to cat multiple formula they can call
     # brew cat multiple times.
-    formulae = ARGV.formulae
+    formulae = Homebrew.args.formulae
     raise FormulaUnspecifiedError if formulae.empty?
     raise "`brew cat` doesn't support multiple arguments" if args.remaining.size > 1
 
diff --git a/Library/Homebrew/cmd/deps.rb b/Library/Homebrew/cmd/deps.rb
index fe6484a025..3031f9d2aa 100644
--- a/Library/Homebrew/cmd/deps.rb
+++ b/Library/Homebrew/cmd/deps.rb
@@ -67,16 +67,16 @@ module Homebrew
       if args.installed?
         puts_deps_tree Formula.installed.sort, recursive
       else
-        raise FormulaUnspecifiedError if args.remaining.empty?
+        raise FormulaUnspecifiedError if Homebrew.args.remaining.empty?
 
-        puts_deps_tree ARGV.formulae, recursive
+        puts_deps_tree Homebrew.args.formulae, recursive
       end
       return
     elsif args.all?
       puts_deps Formula.sort, recursive
       return
-    elsif !args.remaining.empty? && args.for_each?
-      puts_deps ARGV.formulae, recursive
+    elsif !Homebrew.args.remaining.empty? && args.for_each?
+      puts_deps Homebrew.args.formulae, recursive
       return
     end
 
@@ -88,14 +88,14 @@ module Homebrew
                                 !args.include_optional? &&
                                 !args.skip_recommended?
 
-    if args.remaining.empty?
+    if Homebrew.args.remaining.empty?
       raise FormulaUnspecifiedError unless args.installed?
 
       puts_deps Formula.installed.sort, recursive
       return
     end
 
-    all_deps = deps_for_formulae(ARGV.formulae, recursive, &(args.union? ? :| : :&))
+    all_deps = deps_for_formulae(Homebrew.args.formulae, recursive, &(args.union? ? :| : :&))
     all_deps = condense_requirements(all_deps)
     all_deps.select!(&:installed?) if args.installed?
     all_deps.map!(&method(:dep_display_name))
diff --git a/Library/Homebrew/cmd/desc.rb b/Library/Homebrew/cmd/desc.rb
index 3512712047..46f50430a6 100644
--- a/Library/Homebrew/cmd/desc.rb
+++ b/Library/Homebrew/cmd/desc.rb
@@ -50,7 +50,7 @@ module Homebrew
       raise FormulaUnspecifiedError if ARGV.named.empty?
 
       desc = {}
-      ARGV.formulae.each { |f| desc[f.full_name] = f.desc }
+      Homebrew.args.formulae.each { |f| desc[f.full_name] = f.desc }
       Descriptions.new(desc)
     else
       arg = ARGV.named.join(" ")
diff --git a/Library/Homebrew/cmd/fetch.rb b/Library/Homebrew/cmd/fetch.rb
index e9159f504d..ee91163281 100644
--- a/Library/Homebrew/cmd/fetch.rb
+++ b/Library/Homebrew/cmd/fetch.rb
@@ -49,13 +49,13 @@ module Homebrew
 
     if args.deps?
       bucket = []
-      ARGV.formulae.each do |f|
+      Homebrew.args.formulae.each do |f|
         bucket << f
         bucket.concat f.recursive_dependencies.map(&:to_formula)
       end
       bucket.uniq!
     else
-      bucket = ARGV.formulae
+      bucket = Homebrew.args.formulae
     end
 
     puts "Fetching: #{bucket * ", "}" if bucket.size > 1
diff --git a/Library/Homebrew/cmd/home.rb b/Library/Homebrew/cmd/home.rb
index 8746c0feed..98099c090a 100644
--- a/Library/Homebrew/cmd/home.rb
+++ b/Library/Homebrew/cmd/home.rb
@@ -23,7 +23,7 @@ module Homebrew
     if args.remaining.empty?
       exec_browser HOMEBREW_WWW
     else
-      exec_browser(*ARGV.formulae.map(&:homepage))
+      exec_browser(*Homebrew.args.formulae.map(&:homepage))
     end
   end
 end
diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb
index ad04e74dac..94a82c68b0 100644
--- a/Library/Homebrew/cmd/info.rb
+++ b/Library/Homebrew/cmd/info.rb
@@ -80,7 +80,7 @@ module Homebrew
 
       print_json
     elsif args.github?
-      exec_browser(*ARGV.formulae.map { |f| github_info(f) })
+      exec_browser(*Homebrew.args.formulae.map { |f| github_info(f) })
     else
       print_info
     end
@@ -129,7 +129,7 @@ module Homebrew
     elsif args.installed?
       Formula.installed.sort
     else
-      ARGV.formulae
+      Homebrew.args.formulae
     end
     json = ff.map(&:to_hash)
     puts JSON.generate(json)
diff --git a/Library/Homebrew/cmd/install.rb b/Library/Homebrew/cmd/install.rb
index 8c61e87d64..a3c428c754 100644
--- a/Library/Homebrew/cmd/install.rb
+++ b/Library/Homebrew/cmd/install.rb
@@ -130,7 +130,7 @@ module Homebrew
     # developer tools are available, we need to stop them early on
     FormulaInstaller.prevent_build_flags unless DevelopmentTools.installed?
 
-    ARGV.formulae.each do |f|
+    Homebrew.args.formulae.each do |f|
       # head-only without --HEAD is an error
       if !Homebrew.args.HEAD? && f.stable.nil? && f.devel.nil?
         raise <<~EOS
diff --git a/Library/Homebrew/cmd/options.rb b/Library/Homebrew/cmd/options.rb
index 99948ef6f7..c42f9f0661 100644
--- a/Library/Homebrew/cmd/options.rb
+++ b/Library/Homebrew/cmd/options.rb
@@ -35,7 +35,7 @@ module Homebrew
     else
       raise FormulaUnspecifiedError if args.remaining.empty?
 
-      puts_options ARGV.formulae
+      puts_options Homebrew.args.formulae
     end
   end
 
diff --git a/Library/Homebrew/cmd/style.rb b/Library/Homebrew/cmd/style.rb
index 77c9a0d67f..560329f679 100644
--- a/Library/Homebrew/cmd/style.rb
+++ b/Library/Homebrew/cmd/style.rb
@@ -45,7 +45,7 @@ module Homebrew
     elsif ARGV.named.any? { |tap| tap.count("/") == 1 }
       ARGV.named.map { |tap| Tap.fetch(tap).path }
     else
-      ARGV.formulae.map(&:path)
+      Homebrew.args.formulae.map(&:path)
     end
 
     only_cops = args.only_cops
diff --git a/Library/Homebrew/cmd/unpack.rb b/Library/Homebrew/cmd/unpack.rb
index 8b49a963bb..522fc5a891 100644
--- a/Library/Homebrew/cmd/unpack.rb
+++ b/Library/Homebrew/cmd/unpack.rb
@@ -32,7 +32,7 @@ module Homebrew
   def unpack
     unpack_args.parse
 
-    formulae = ARGV.formulae
+    formulae = Homebrew.args.formulae
     raise FormulaUnspecifiedError if formulae.empty?
 
     if dir = args.destdir
diff --git a/Library/Homebrew/cmd/uses.rb b/Library/Homebrew/cmd/uses.rb
index ef083c5055..9f6e235bf2 100644
--- a/Library/Homebrew/cmd/uses.rb
+++ b/Library/Homebrew/cmd/uses.rb
@@ -50,7 +50,7 @@ module Homebrew
 
     used_formulae_missing = false
     used_formulae = begin
-      ARGV.formulae
+      Homebrew.args.formulae
     rescue FormulaUnavailableError => e
       opoo e
       used_formulae_missing = true
diff --git a/Library/Homebrew/dev-cmd/bump-formula-pr.rb b/Library/Homebrew/dev-cmd/bump-formula-pr.rb
index dfd59f3a23..e984152e78 100644
--- a/Library/Homebrew/dev-cmd/bump-formula-pr.rb
+++ b/Library/Homebrew/dev-cmd/bump-formula-pr.rb
@@ -114,7 +114,7 @@ module Homebrew
     # Use the user's browser, too.
     ENV["BROWSER"] = ENV["HOMEBREW_BROWSER"]
 
-    formula = ARGV.formulae.first
+    formula = Homebrew.args.formulae.first
 
     if formula
       tap_full_name, origin_branch, previous_branch = use_correct_linux_tap(formula)
diff --git a/Library/Homebrew/dev-cmd/bump-revision.rb b/Library/Homebrew/dev-cmd/bump-revision.rb
index af34df763a..ef5f4aee7b 100644
--- a/Library/Homebrew/dev-cmd/bump-revision.rb
+++ b/Library/Homebrew/dev-cmd/bump-revision.rb
@@ -32,10 +32,11 @@ module Homebrew
     # user path, too.
     ENV["PATH"] = ENV["HOMEBREW_PATH"]
 
-    raise FormulaUnspecifiedError if ARGV.formulae.empty?
-    raise "Multiple formulae given, only one is allowed." if ARGV.formulae.length > 1
+    formulae = Homebrew.args.formulae
+    raise FormulaUnspecifiedError if formulae.empty?
+    raise "Multiple formulae given, only one is allowed." if formulae.length > 1
 
-    formula = ARGV.formulae.first
+    formula = formulae.first
     current_revision = formula.revision
 
     if current_revision.zero?
diff --git a/Library/Homebrew/dev-cmd/mirror.rb b/Library/Homebrew/dev-cmd/mirror.rb
index c90c9f98cb..464d96b5b1 100644
--- a/Library/Homebrew/dev-cmd/mirror.rb
+++ b/Library/Homebrew/dev-cmd/mirror.rb
@@ -27,7 +27,7 @@ module Homebrew
     bintray_key = ENV["HOMEBREW_BINTRAY_KEY"]
     raise "Missing HOMEBREW_BINTRAY_USER or HOMEBREW_BINTRAY_KEY variables!" if !bintray_user || !bintray_key
 
-    ARGV.formulae.each do |f|
+    Homebrew.args.formulae.each do |f|
       bintray_package = Utils::Bottles::Bintray.package f.name
       bintray_repo_url = "https://api.bintray.com/packages/homebrew/mirror"
       package_url = "#{bintray_repo_url}/#{bintray_package}"
diff --git a/Library/Homebrew/test/cli/parser_spec.rb b/Library/Homebrew/test/cli/parser_spec.rb
index 8a326d52a7..3061e484e8 100644
--- a/Library/Homebrew/test/cli/parser_spec.rb
+++ b/Library/Homebrew/test/cli/parser_spec.rb
@@ -236,6 +236,21 @@ describe Homebrew::CLI::Parser do
       expect(Homebrew.args.passthrough).to eq %w[--foo --bar=value -s]
     end
 
+    it "#formulae raises an error when a Formula is unavailable" do
+      parser.parse(["mxcl"])
+      expect { Homebrew.args.formulae }.to raise_error FormulaUnavailableError
+    end
+
+    it "#formulae returns an empty array when there are no Formulae" do
+      parser.parse([])
+      expect(Homebrew.args.formulae).to be_empty
+    end
+
+    it "#casks returns an empty array when there are no matching casks" do
+      parser.parse([])
+      expect(Homebrew.args.casks).to eq []
+    end
+
     context "kegs" do
       before do
         keg = HOMEBREW_CELLAR + "mxcl/10.0"
@@ -252,5 +267,15 @@ describe Homebrew::CLI::Parser do
         expect(Homebrew.args.kegs).to be_empty
       end
     end
+
+    it "#named returns an array of non-option arguments" do
+      parser.parse(["foo", "-v", "-s"])
+      expect(Homebrew.args.named).to eq ["foo"]
+    end
+
+    it "#named returns an empty array when there are no named arguments" do
+      parser.parse([])
+      expect(Homebrew.args.named).to be_empty
+    end
   end
 end
-- 
GitLab