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
|
2020-05-10 15:12:25 +01:00
|
|
|
attr_reader :options_only, :flags_only
|
|
|
|
|
2019-04-17 19:42:27 +09:00
|
|
|
# undefine tap to allow --tap argument
|
|
|
|
undef tap
|
|
|
|
|
2020-05-05 12:50:41 +01:00
|
|
|
def initialize(argv = ARGV.dup.freeze, set_default_args: false)
|
|
|
|
super()
|
2020-04-18 21:14:35 +01:00
|
|
|
|
2019-09-22 20:13:11 +05:30
|
|
|
@processed_options = []
|
2020-05-10 15:12:25 +01:00
|
|
|
@options_only = args_options_only(argv)
|
|
|
|
@flags_only = args_flags_only(argv)
|
2019-09-22 20:13:11 +05:30
|
|
|
|
2020-05-07 10:33:02 +01:00
|
|
|
# Can set these because they will be overwritten by freeze_named_args!
|
|
|
|
# (whereas other values below will only be overwritten if passed).
|
|
|
|
self[:named_args] = argv.reject { |arg| arg.start_with?("-") }
|
|
|
|
|
2020-05-05 12:50:41 +01:00
|
|
|
# Set values needed before Parser#parse has been run.
|
|
|
|
return unless set_default_args
|
2019-12-11 00:23:51 +05:30
|
|
|
|
2020-05-05 12:50:41 +01:00
|
|
|
self[:build_from_source?] = argv.include?("--build-from-source") || argv.include?("-s")
|
|
|
|
self[:build_bottle?] = argv.include?("--build-bottle")
|
|
|
|
self[:force_bottle?] = argv.include?("--force-bottle")
|
|
|
|
self[:HEAD?] = argv.include?("--HEAD")
|
|
|
|
self[:devel?] = argv.include?("--devel")
|
|
|
|
self[:universal?] = argv.include?("--universal")
|
2019-09-22 20:13:11 +05:30
|
|
|
end
|
|
|
|
|
2020-05-05 12:50:41 +01:00
|
|
|
def freeze_named_args!(named_args)
|
2020-05-07 10:33:02 +01:00
|
|
|
# Reset cache values reliant on named_args
|
|
|
|
@formulae = nil
|
|
|
|
@resolved_formulae = nil
|
|
|
|
@formulae_paths = nil
|
|
|
|
@casks = nil
|
|
|
|
@kegs = nil
|
|
|
|
|
2020-05-05 12:50:41 +01:00
|
|
|
self[:named_args] = named_args
|
|
|
|
self[:named_args].freeze
|
|
|
|
end
|
2019-09-22 20:13:11 +05:30
|
|
|
|
2020-05-05 12:50:41 +01:00
|
|
|
def freeze_processed_options!(processed_options)
|
2020-05-07 10:33:02 +01:00
|
|
|
# Reset cache values reliant on processed_options
|
|
|
|
@cli_args = nil
|
|
|
|
|
2020-05-05 12:50:41 +01:00
|
|
|
@processed_options += processed_options
|
|
|
|
@processed_options.freeze
|
2019-09-22 20:13:11 +05:30
|
|
|
|
2020-05-10 15:12:25 +01:00
|
|
|
@options_only = args_options_only(cli_args)
|
|
|
|
@flags_only = args_flags_only(cli_args)
|
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
|
2020-05-05 12:50:41 +01:00
|
|
|
named_args || []
|
2019-09-08 19:56:24 +05:30
|
|
|
end
|
|
|
|
|
2020-03-04 17:23:20 +00:00
|
|
|
def no_named?
|
|
|
|
named.blank?
|
|
|
|
end
|
|
|
|
|
2020-03-24 19:44:05 +05:30
|
|
|
# If the user passes any flags that trigger building over installing from
|
|
|
|
# a bottle, they are collected here and returned as an Array for checking.
|
|
|
|
def collect_build_args
|
|
|
|
build_flags = []
|
|
|
|
|
2020-05-05 12:50:41 +01:00
|
|
|
build_flags << "--HEAD" if HEAD?
|
|
|
|
build_flags << "--universal" if build_universal?
|
|
|
|
build_flags << "--build-bottle" if build_bottle?
|
|
|
|
build_flags << "--build-from-source" if build_from_source?
|
2020-03-24 19:44:05 +05:30
|
|
|
|
|
|
|
build_flags
|
|
|
|
end
|
|
|
|
|
2019-09-08 19:56:24 +05:30
|
|
|
def formulae
|
|
|
|
require "formula"
|
2020-05-05 12:50:41 +01:00
|
|
|
|
2019-09-08 19:56:24 +05:30
|
|
|
@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
|
2020-05-07 10:02:34 +01:00
|
|
|
end.uniq(&:name).freeze
|
2019-09-08 19:56:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def resolved_formulae
|
|
|
|
require "formula"
|
2020-05-05 12:50:41 +01:00
|
|
|
|
2019-09-08 19:56:24 +05:30
|
|
|
@resolved_formulae ||= (downcased_unique_named - casks).map do |name|
|
|
|
|
Formulary.resolve(name, spec: spec(nil))
|
2020-05-07 10:02:34 +01:00
|
|
|
end.uniq(&:name).freeze
|
2019-09-08 19:56:24 +05:30
|
|
|
end
|
|
|
|
|
2020-04-14 14:20:43 +01:00
|
|
|
def formulae_paths
|
|
|
|
@formulae_paths ||= (downcased_unique_named - casks).map do |name|
|
|
|
|
Formulary.path(name)
|
2020-05-07 10:02:34 +01:00
|
|
|
end.uniq.freeze
|
2020-04-14 14:20:43 +01:00
|
|
|
end
|
|
|
|
|
2019-09-08 19:56:24 +05:30
|
|
|
def casks
|
2020-05-05 12:50:41 +01:00
|
|
|
@casks ||= downcased_unique_named.grep(HOMEBREW_CASK_TAP_CASK_REGEX)
|
|
|
|
.freeze
|
2019-10-20 21:00:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def kegs
|
|
|
|
require "keg"
|
|
|
|
require "formula"
|
|
|
|
require "missing_formula"
|
2020-05-05 12:50:41 +01:00
|
|
|
|
2019-10-20 21:00:05 -04:00
|
|
|
@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
|
2020-05-05 12:50:41 +01:00
|
|
|
end.freeze
|
2019-10-20 21:00:05 -04:00
|
|
|
end
|
2019-09-08 19:56:24 +05:30
|
|
|
|
2020-03-29 19:36:51 +01:00
|
|
|
def build_stable?
|
|
|
|
!(HEAD? || devel?)
|
|
|
|
end
|
|
|
|
|
2020-04-11 18:50:24 +05:30
|
|
|
# Whether a given formula should be built from source during the current
|
|
|
|
# installation run.
|
|
|
|
def build_formula_from_source?(f)
|
2020-05-05 12:50:41 +01:00
|
|
|
return false if !build_from_source? && !build_bottle?
|
2020-04-11 18:50:24 +05:30
|
|
|
|
|
|
|
formulae.any? { |args_f| args_f.full_name == f.full_name }
|
|
|
|
end
|
|
|
|
|
2020-05-05 12:50:41 +01:00
|
|
|
private
|
2020-04-25 21:57:21 +05:30
|
|
|
|
2020-05-05 12:50:41 +01:00
|
|
|
def option_to_name(option)
|
|
|
|
option.sub(/\A--?/, "")
|
|
|
|
.tr("-", "_")
|
2020-04-25 21:57:21 +05:30
|
|
|
end
|
|
|
|
|
2020-05-05 12:50:41 +01:00
|
|
|
def cli_args
|
|
|
|
return @cli_args if @cli_args
|
2020-04-05 17:39:30 +05:30
|
|
|
|
2020-05-05 12:50:41 +01:00
|
|
|
@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
|
|
|
|
if @table[switch] == true || @table[flag] == true
|
|
|
|
@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.freeze
|
2020-04-05 17:39:30 +05:30
|
|
|
end
|
|
|
|
|
2020-05-10 15:12:25 +01:00
|
|
|
def args_options_only(args)
|
|
|
|
args.select { |arg| arg.start_with?("-") }
|
|
|
|
.freeze
|
|
|
|
end
|
|
|
|
|
|
|
|
def args_flags_only(args)
|
|
|
|
args.select { |arg| arg.start_with?("--") }
|
|
|
|
.freeze
|
|
|
|
end
|
|
|
|
|
2019-09-08 19:56:24 +05:30
|
|
|
def downcased_unique_named
|
|
|
|
# Only lowercase names, not paths, bottle filenames or URLs
|
2020-05-05 12:50:41 +01:00
|
|
|
named.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
|
|
|
|
|
|
|
|
def spec(default = :stable)
|
2020-05-05 12:50:41 +01:00
|
|
|
if HEAD?
|
2019-09-08 19:56:24 +05:30
|
|
|
:head
|
2020-05-05 12:50:41 +01:00
|
|
|
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
|