| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 19:42:27 +09:00
										 |  |  | require "ostruct" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							|  |  |  |   module CLI | 
					
						
							|  |  |  |     class Args < OpenStruct | 
					
						
							| 
									
										
										
										
											2019-12-11 00:23:51 +05:30
										 |  |  |       attr_reader :processed_options, :args_parsed | 
					
						
							| 
									
										
										
										
											2019-04-17 19:42:27 +09:00
										 |  |  |       # undefine tap to allow --tap argument | 
					
						
							|  |  |  |       undef tap | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def initialize(argv:) | 
					
						
							|  |  |  |         super | 
					
						
							|  |  |  |         @argv = argv | 
					
						
							| 
									
										
										
										
											2019-10-27 15:05:45 +05:30
										 |  |  |         @args_parsed = false | 
					
						
							| 
									
										
										
										
											2019-09-22 20:13:11 +05:30
										 |  |  |         @processed_options = [] | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-11 00:23:51 +05:30
										 |  |  |       def freeze_processed_options!(processed_options) | 
					
						
							|  |  |  |         @processed_options += processed_options | 
					
						
							|  |  |  |         @processed_options.freeze | 
					
						
							|  |  |  |         @args_parsed = true | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-22 20:13:11 +05:30
										 |  |  |       def option_to_name(option) | 
					
						
							|  |  |  |         option.sub(/\A--?/, "") | 
					
						
							|  |  |  |               .tr("-", "_") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def cli_args | 
					
						
							| 
									
										
										
										
											2019-09-25 14:21:06 +05:30
										 |  |  |         return @cli_args if @cli_args | 
					
						
							| 
									
										
										
										
											2019-09-22 20:13:11 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         @cli_args = [] | 
					
						
							|  |  |  |         processed_options.each do |short, long| | 
					
						
							|  |  |  |           option = long || short | 
					
						
							|  |  |  |           switch = "#{option_to_name(option)}?".to_sym | 
					
						
							|  |  |  |           flag = option_to_name(option).to_sym | 
					
						
							| 
									
										
										
										
											2019-09-25 14:21:06 +05:30
										 |  |  |           if @table[switch] == true || @table[flag] == true | 
					
						
							| 
									
										
										
										
											2019-09-22 20:13:11 +05:30
										 |  |  |             @cli_args << option | 
					
						
							|  |  |  |           elsif @table[flag].instance_of? String | 
					
						
							|  |  |  |             @cli_args << option + "=" + @table[flag] | 
					
						
							|  |  |  |           elsif @table[flag].instance_of? Array | 
					
						
							|  |  |  |             @cli_args << option + "=" + @table[flag].join(",") | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         @cli_args | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def options_only | 
					
						
							|  |  |  |         @options_only ||= cli_args.select { |arg| arg.start_with?("-") } | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def flags_only | 
					
						
							|  |  |  |         @flags_only ||= cli_args.select { |arg| arg.start_with?("--") } | 
					
						
							| 
									
										
										
										
											2019-04-17 19:42:27 +09:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2019-09-25 14:21:06 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |       def passthrough | 
					
						
							|  |  |  |         options_only - CLI::Parser.global_options.values.map(&:first).flatten | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2019-10-20 21:00:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-08 19:56:24 +05:30
										 |  |  |       def named | 
					
						
							| 
									
										
										
										
											2019-12-15 23:50:01 +05:30
										 |  |  |         return [] if remaining.nil? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-08 19:56:24 +05:30
										 |  |  |         remaining | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 17:23:20 +00:00
										 |  |  |       def no_named? | 
					
						
							|  |  |  |         named.blank? | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-08 19:56:24 +05:30
										 |  |  |       def formulae | 
					
						
							|  |  |  |         require "formula" | 
					
						
							|  |  |  |         @formulae ||= (downcased_unique_named - casks).map do |name| | 
					
						
							|  |  |  |           if name.include?("/") || File.exist?(name) | 
					
						
							|  |  |  |             Formulary.factory(name, spec) | 
					
						
							| 
									
										
										
										
											2019-10-20 21:00:05 -04:00
										 |  |  |           else | 
					
						
							| 
									
										
										
										
											2019-09-08 19:56:24 +05:30
										 |  |  |             Formulary.find_with_priority(name, spec) | 
					
						
							| 
									
										
										
										
											2019-10-20 21:00:05 -04:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2019-09-08 19:56:24 +05:30
										 |  |  |         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 | 
					
						
							| 
									
										
										
										
											2019-10-20 21:00:05 -04:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def kegs | 
					
						
							|  |  |  |         require "keg" | 
					
						
							|  |  |  |         require "formula" | 
					
						
							|  |  |  |         require "missing_formula" | 
					
						
							|  |  |  |         @kegs ||= downcased_unique_named.map do |name| | 
					
						
							|  |  |  |           raise UsageError if name.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           rack = Formulary.to_rack(name.downcase) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           dirs = rack.directory? ? rack.subdirs : [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           if dirs.empty? | 
					
						
							|  |  |  |             if (reason = Homebrew::MissingFormula.suggest_command(name, "uninstall")) | 
					
						
							|  |  |  |               $stderr.puts reason | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |             raise NoSuchKegError, rack.basename | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           linked_keg_ref = HOMEBREW_LINKED_KEGS/rack.basename | 
					
						
							|  |  |  |           opt_prefix = HOMEBREW_PREFIX/"opt/#{rack.basename}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           begin | 
					
						
							|  |  |  |             if opt_prefix.symlink? && opt_prefix.directory? | 
					
						
							|  |  |  |               Keg.new(opt_prefix.resolved_path) | 
					
						
							|  |  |  |             elsif linked_keg_ref.symlink? && linked_keg_ref.directory? | 
					
						
							|  |  |  |               Keg.new(linked_keg_ref.resolved_path) | 
					
						
							|  |  |  |             elsif dirs.length == 1
 | 
					
						
							|  |  |  |               Keg.new(dirs.first) | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |               f = if name.include?("/") || File.exist?(name) | 
					
						
							|  |  |  |                 Formulary.factory(name) | 
					
						
							|  |  |  |               else | 
					
						
							|  |  |  |                 Formulary.from_rack(rack) | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               unless (prefix = f.installed_prefix).directory? | 
					
						
							|  |  |  |                 raise MultipleVersionsInstalledError, rack.basename | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               Keg.new(prefix) | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           rescue FormulaUnavailableError | 
					
						
							|  |  |  |             raise <<~EOS | 
					
						
							|  |  |  |               Multiple kegs installed to #{rack} | 
					
						
							|  |  |  |               However we don't know which one you refer to. | 
					
						
							|  |  |  |               Please delete (with rm -rf!) all but one and then try again. | 
					
						
							|  |  |  |             EOS | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2019-09-08 19:56:24 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |       private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def downcased_unique_named | 
					
						
							|  |  |  |         # Only lowercase names, not paths, bottle filenames or URLs | 
					
						
							| 
									
										
										
										
											2019-10-27 15:05:45 +05:30
										 |  |  |         arguments = if args_parsed | 
					
						
							| 
									
										
										
										
											2020-03-04 17:26:38 +00:00
										 |  |  |           named | 
					
						
							| 
									
										
										
										
											2019-10-27 15:05:45 +05:30
										 |  |  |         else | 
					
						
							|  |  |  |           cmdline_args.reject { |arg| arg.start_with?("-") } | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         arguments.map do |arg| | 
					
						
							| 
									
										
										
										
											2019-09-08 19:56:24 +05:30
										 |  |  |           if arg.include?("/") || arg.end_with?(".tar.gz") || File.exist?(arg) | 
					
						
							|  |  |  |             arg | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             arg.downcase | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end.uniq | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-27 15:05:45 +05:30
										 |  |  |       def head | 
					
						
							|  |  |  |         (args_parsed && HEAD?) || cmdline_args.include?("--HEAD") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def devel | 
					
						
							|  |  |  |         (args_parsed && devel?) || cmdline_args.include?("--devel") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-08 19:56:24 +05:30
										 |  |  |       def spec(default = :stable) | 
					
						
							| 
									
										
										
										
											2019-10-27 15:05:45 +05:30
										 |  |  |         if head | 
					
						
							| 
									
										
										
										
											2019-09-08 19:56:24 +05:30
										 |  |  |           :head | 
					
						
							| 
									
										
										
										
											2019-10-27 15:05:45 +05:30
										 |  |  |         elsif devel | 
					
						
							| 
									
										
										
										
											2019-09-08 19:56:24 +05:30
										 |  |  |           :devel | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           default | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2019-04-17 19:42:27 +09:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |