2020-10-10 14:16:11 +02:00
|
|
|
# typed: false
|
2020-08-13 08:55:55 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "delegate"
|
2020-09-29 23:46:30 +02:00
|
|
|
|
|
|
|
require "cask/cask_loader"
|
|
|
|
require "cli/args"
|
2020-08-18 00:23:23 +01:00
|
|
|
require "formulary"
|
2020-11-18 16:39:07 +01:00
|
|
|
require "keg"
|
2020-08-30 17:43:54 +02:00
|
|
|
require "missing_formula"
|
2020-08-13 08:55:55 -04:00
|
|
|
|
|
|
|
module Homebrew
|
|
|
|
module CLI
|
2020-08-17 19:10:06 +02:00
|
|
|
# Helper class for loading formulae/casks from named arguments.
|
|
|
|
#
|
|
|
|
# @api private
|
2020-11-16 01:52:57 +01:00
|
|
|
class NamedArgs < Array
|
2020-11-18 16:39:07 +01:00
|
|
|
extend T::Sig
|
|
|
|
|
2020-09-29 23:46:30 +02:00
|
|
|
def initialize(*args, parent: Args.new, override_spec: nil, force_bottle: false, flags: [])
|
2020-08-13 08:55:55 -04:00
|
|
|
@args = args
|
|
|
|
@override_spec = override_spec
|
|
|
|
@force_bottle = force_bottle
|
|
|
|
@flags = flags
|
2020-09-29 23:46:30 +02:00
|
|
|
@parent = parent
|
2020-08-13 08:55:55 -04:00
|
|
|
|
2020-08-19 17:12:32 +01:00
|
|
|
super(@args)
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
|
|
|
|
2020-09-28 02:10:17 +02:00
|
|
|
def to_casks
|
|
|
|
@to_casks ||= to_formulae_and_casks(only: :cask).freeze
|
|
|
|
end
|
|
|
|
|
2020-08-13 08:55:55 -04:00
|
|
|
def to_formulae
|
2020-09-26 02:24:16 +02:00
|
|
|
@to_formulae ||= to_formulae_and_casks(only: :formula).freeze
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
|
|
|
|
2020-11-19 12:57:10 +01:00
|
|
|
def to_formulae_and_casks(only: nil, ignore_unavailable: nil, method: nil)
|
2020-09-25 21:13:26 +02:00
|
|
|
@to_formulae_and_casks ||= {}
|
|
|
|
@to_formulae_and_casks[only] ||= begin
|
2020-11-19 12:57:10 +01:00
|
|
|
to_objects(only: only, ignore_unavailable: ignore_unavailable, method: method).freeze
|
2020-09-25 21:13:26 +02:00
|
|
|
end
|
|
|
|
end
|
2020-08-17 12:40:23 -04:00
|
|
|
|
2020-11-18 16:39:07 +01:00
|
|
|
def to_formulae_to_casks(only: nil, method: nil)
|
2020-10-09 21:09:07 -04:00
|
|
|
@to_formulae_to_casks ||= {}
|
2020-11-18 16:39:07 +01:00
|
|
|
@to_formulae_to_casks[[method, only]] = to_formulae_and_casks(only: only, method: method)
|
2020-10-09 21:09:07 -04:00
|
|
|
.partition { |o| o.is_a?(Formula) }
|
|
|
|
.map(&:freeze).freeze
|
|
|
|
end
|
|
|
|
|
2020-10-12 09:22:29 -04:00
|
|
|
def to_formulae_and_casks_and_unavailable(method: nil)
|
2020-10-09 21:09:07 -04:00
|
|
|
@to_formulae_casks_unknowns ||= {}
|
|
|
|
@to_formulae_casks_unknowns[method] = downcased_unique_named.map do |name|
|
2020-10-08 19:55:24 -04:00
|
|
|
load_formula_or_cask(name, method: method)
|
|
|
|
rescue FormulaOrCaskUnavailableError => e
|
|
|
|
e
|
|
|
|
end.uniq.freeze
|
|
|
|
end
|
|
|
|
|
2020-09-28 02:10:17 +02:00
|
|
|
def load_formula_or_cask(name, only: nil, method: nil)
|
2020-09-25 21:13:26 +02:00
|
|
|
if only != :cask
|
|
|
|
begin
|
2020-09-28 02:10:17 +02:00
|
|
|
formula = case method
|
|
|
|
when nil, :factory
|
|
|
|
Formulary.factory(name, *spec, force_bottle: @force_bottle, flags: @flags)
|
|
|
|
when :resolve
|
2020-11-18 16:39:07 +01:00
|
|
|
resolve_formula(name)
|
|
|
|
when :keg
|
|
|
|
resolve_keg(name)
|
2020-11-19 12:57:10 +01:00
|
|
|
when :kegs
|
|
|
|
rack = Formulary.to_rack(name)
|
|
|
|
rack.directory? ? rack.subdirs.map { |d| Keg.new(d) } : []
|
2020-09-28 02:10:17 +02:00
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
2020-09-25 21:13:26 +02:00
|
|
|
warn_if_cask_conflicts(name, "formula") unless only == :formula
|
|
|
|
return formula
|
2020-11-18 16:39:07 +01:00
|
|
|
rescue NoSuchKegError, FormulaUnavailableError => e
|
2020-09-25 21:13:26 +02:00
|
|
|
raise e if only == :formula
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
2020-09-25 21:13:26 +02:00
|
|
|
end
|
2020-08-13 08:55:55 -04:00
|
|
|
|
2020-09-25 21:13:26 +02:00
|
|
|
if only != :formula
|
|
|
|
begin
|
2020-09-28 02:10:17 +02:00
|
|
|
return Cask::CaskLoader.load(name, config: Cask::Config.from_args(@parent))
|
2020-10-01 04:20:58 +02:00
|
|
|
rescue Cask::CaskUnavailableError => e
|
2020-09-25 21:13:26 +02:00
|
|
|
raise e if only == :cask
|
|
|
|
end
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
2020-09-25 21:13:26 +02:00
|
|
|
|
|
|
|
raise FormulaOrCaskUnavailableError, name
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
2020-09-25 21:13:26 +02:00
|
|
|
private :load_formula_or_cask
|
|
|
|
|
|
|
|
def resolve_formula(name)
|
2020-09-28 02:10:17 +02:00
|
|
|
Formulary.resolve(name, spec: spec, force_bottle: @force_bottle, flags: @flags)
|
2020-09-25 21:13:26 +02:00
|
|
|
end
|
|
|
|
private :resolve_formula
|
2020-08-13 08:55:55 -04:00
|
|
|
|
|
|
|
def to_resolved_formulae
|
2020-09-28 02:10:17 +02:00
|
|
|
@to_resolved_formulae ||= to_formulae_and_casks(only: :formula, method: :resolve)
|
|
|
|
.freeze
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
|
|
|
|
2020-10-08 13:43:15 +02:00
|
|
|
def to_resolved_formulae_to_casks(only: nil)
|
2020-11-18 16:39:07 +01:00
|
|
|
to_formulae_to_casks(only: only, method: :resolve)
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
|
|
|
|
2020-11-17 17:31:30 +00:00
|
|
|
# Convert named arguments to {Formula} or {Cask} objects.
|
2020-09-25 21:13:26 +02:00
|
|
|
# If both a formula and cask exist with the same name, returns the
|
|
|
|
# formula and prints a warning unless `only` is specified.
|
2020-11-19 12:57:10 +01:00
|
|
|
def to_objects(only: nil, ignore_unavailable: nil, method: nil)
|
2020-09-25 21:13:26 +02:00
|
|
|
@to_objects ||= {}
|
2020-11-19 12:57:10 +01:00
|
|
|
@to_objects[only] ||= downcased_unique_named.flat_map do |name|
|
2020-09-28 02:10:17 +02:00
|
|
|
load_formula_or_cask(name, only: only, method: method)
|
2020-11-19 12:57:10 +01:00
|
|
|
rescue NoSuchKegError, FormulaUnavailableError, Cask::CaskUnavailableError
|
|
|
|
ignore_unavailable ? [] : raise
|
2020-09-25 21:13:26 +02:00
|
|
|
end.uniq.freeze
|
|
|
|
end
|
2020-09-28 02:17:31 +02:00
|
|
|
private :to_objects
|
2020-09-25 21:13:26 +02:00
|
|
|
|
2020-08-13 08:55:55 -04:00
|
|
|
def to_formulae_paths
|
2020-11-19 16:01:04 +01:00
|
|
|
to_paths(only: :formula)
|
2020-09-07 18:39:58 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Keep existing paths and try to convert others to tap, formula or cask paths.
|
|
|
|
# If a cask and formula with the same name exist, includes both their paths
|
|
|
|
# unless `only` is specified.
|
|
|
|
def to_paths(only: nil)
|
|
|
|
@to_paths ||= {}
|
|
|
|
@to_paths[only] ||= downcased_unique_named.flat_map do |name|
|
|
|
|
if File.exist?(name)
|
2020-09-09 22:10:33 +02:00
|
|
|
Pathname(name)
|
2020-11-19 16:01:04 +01:00
|
|
|
elsif name.count("/") == 1 && !name.start_with?("./", "/")
|
2020-09-07 18:39:58 +02:00
|
|
|
Tap.fetch(name).path
|
|
|
|
else
|
2020-11-19 16:01:04 +01:00
|
|
|
next Formulary.path(name) if only == :formula
|
|
|
|
next Cask::CaskLoader.path(name) if only == :cask
|
2020-09-07 18:39:58 +02:00
|
|
|
|
|
|
|
formula_path = Formulary.path(name)
|
|
|
|
cask_path = Cask::CaskLoader.path(name)
|
|
|
|
|
|
|
|
paths = []
|
|
|
|
|
|
|
|
paths << formula_path if formula_path.exist?
|
|
|
|
paths << cask_path if cask_path.exist?
|
|
|
|
|
2020-11-20 00:35:48 +01:00
|
|
|
paths.empty? ? Pathname(name) : paths
|
2020-09-07 18:39:58 +02:00
|
|
|
end
|
2020-08-13 08:55:55 -04:00
|
|
|
end.uniq.freeze
|
|
|
|
end
|
|
|
|
|
2020-11-18 16:39:07 +01:00
|
|
|
sig { returns(T::Array[Keg]) }
|
2020-08-13 08:55:55 -04:00
|
|
|
def to_kegs
|
2020-11-18 16:39:07 +01:00
|
|
|
@to_kegs ||= begin
|
|
|
|
to_formulae_and_casks(only: :formula, method: :keg).freeze
|
2020-08-13 08:55:55 -04:00
|
|
|
rescue NoSuchKegError => e
|
2020-11-18 16:39:07 +01:00
|
|
|
if (reason = Homebrew::MissingFormula.suggest_command(e.name, "uninstall"))
|
2020-08-13 08:55:55 -04:00
|
|
|
$stderr.puts reason
|
|
|
|
end
|
|
|
|
raise e
|
2020-11-18 16:39:07 +01:00
|
|
|
end
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
|
|
|
|
2020-11-19 12:57:10 +01:00
|
|
|
sig do
|
|
|
|
params(only: T.nilable(Symbol), ignore_unavailable: T.nilable(T::Boolean), all_kegs: T.nilable(T::Boolean))
|
|
|
|
.returns([T::Array[Keg], T::Array[Cask::Cask]])
|
|
|
|
end
|
|
|
|
def to_kegs_to_casks(only: nil, ignore_unavailable: nil, all_kegs: nil)
|
|
|
|
method = all_kegs ? :kegs : :keg
|
|
|
|
@to_kegs_to_casks ||= {}
|
|
|
|
@to_kegs_to_casks[method] ||=
|
|
|
|
to_formulae_and_casks(only: only, ignore_unavailable: ignore_unavailable, method: method)
|
|
|
|
.partition { |o| o.is_a?(Keg) }
|
|
|
|
.map(&:freeze).freeze
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
|
|
|
|
2020-11-18 16:39:07 +01:00
|
|
|
sig { returns(T::Array[String]) }
|
2020-08-13 08:55:55 -04:00
|
|
|
def homebrew_tap_cask_names
|
|
|
|
downcased_unique_named.grep(HOMEBREW_CASK_TAP_CASK_REGEX)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-11-18 16:39:07 +01:00
|
|
|
sig { returns(T::Array[String]) }
|
2020-08-13 08:55:55 -04:00
|
|
|
def downcased_unique_named
|
|
|
|
# Only lowercase names, not paths, bottle filenames or URLs
|
|
|
|
map do |arg|
|
|
|
|
if arg.include?("/") || arg.end_with?(".tar.gz") || File.exist?(arg)
|
|
|
|
arg
|
|
|
|
else
|
|
|
|
arg.downcase
|
|
|
|
end
|
|
|
|
end.uniq
|
|
|
|
end
|
|
|
|
|
2020-09-28 02:10:17 +02:00
|
|
|
def spec
|
|
|
|
@override_spec
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
2020-09-28 02:10:17 +02:00
|
|
|
private :spec
|
2020-08-13 08:55:55 -04:00
|
|
|
|
|
|
|
def resolve_keg(name)
|
|
|
|
raise UsageError if name.blank?
|
|
|
|
|
2020-08-18 00:23:23 +01:00
|
|
|
require "keg"
|
|
|
|
|
2020-08-13 08:55:55 -04:00
|
|
|
rack = Formulary.to_rack(name.downcase)
|
|
|
|
|
|
|
|
dirs = rack.directory? ? rack.subdirs : []
|
|
|
|
raise NoSuchKegError, rack.basename if dirs.empty?
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-08-21 12:10:44 -07:00
|
|
|
unless (prefix = f.latest_installed_prefix).directory?
|
2020-09-09 11:02:43 -07:00
|
|
|
raise MultipleVersionsInstalledError, <<~EOS
|
|
|
|
#{rack.basename} has multiple installed versions
|
|
|
|
Run `brew uninstall --force #{rack.basename}` to remove all versions.
|
|
|
|
EOS
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Keg.new(prefix)
|
|
|
|
end
|
|
|
|
rescue FormulaUnavailableError
|
|
|
|
raise MultipleVersionsInstalledError, <<~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
|
2020-08-17 12:40:23 -04:00
|
|
|
|
2020-08-25 09:23:27 -04:00
|
|
|
def warn_if_cask_conflicts(ref, loaded_type)
|
|
|
|
cask = Cask::CaskLoader.load ref
|
2020-09-01 08:50:08 +01:00
|
|
|
message = "Treating #{ref} as a #{loaded_type}."
|
|
|
|
message += " For the cask, use #{cask.tap.name}/#{cask.token}" if cask.tap.present?
|
|
|
|
opoo message.freeze
|
2020-08-17 12:40:23 -04:00
|
|
|
rescue Cask::CaskUnavailableError
|
2020-08-25 09:23:27 -04:00
|
|
|
# No ref conflict with a cask, do nothing
|
2020-08-17 12:40:23 -04:00
|
|
|
end
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|