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-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-08-13 08:55:55 -04:00
|
|
|
class NamedArgs < SimpleDelegator
|
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-09-28 02:10:17 +02:00
|
|
|
def to_formulae_and_casks(only: nil, method: nil)
|
2020-09-25 21:13:26 +02:00
|
|
|
@to_formulae_and_casks ||= {}
|
|
|
|
@to_formulae_and_casks[only] ||= begin
|
2020-09-28 02:10:17 +02:00
|
|
|
to_objects(only: only, method: method).reject { |o| o.is_a?(Tap) }.freeze
|
2020-09-25 21:13:26 +02:00
|
|
|
end
|
|
|
|
end
|
2020-08-17 12:40:23 -04:00
|
|
|
|
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
|
|
|
|
Formulary.resolve(name, spec: spec, force_bottle: @force_bottle, flags: @flags)
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
2020-09-25 21:13:26 +02:00
|
|
|
warn_if_cask_conflicts(name, "formula") unless only == :formula
|
|
|
|
return formula
|
|
|
|
rescue FormulaUnavailableError => e
|
|
|
|
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-09-25 21:13:26 +02:00
|
|
|
rescue Cask::CaskUnavailableError
|
|
|
|
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
|
|
|
|
|
|
|
|
def to_resolved_formulae_to_casks
|
2020-09-28 02:10:17 +02:00
|
|
|
@to_resolved_formulae_to_casks ||= to_formulae_and_casks(method: :resolve)
|
|
|
|
.partition { |o| o.is_a?(Formula) }
|
|
|
|
.map(&:freeze).freeze
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
|
|
|
|
2020-09-25 21:13:26 +02:00
|
|
|
# Convert named arguments to `Tap`, `Formula` or `Cask` objects.
|
|
|
|
# If both a formula and cask exist with the same name, returns the
|
|
|
|
# formula and prints a warning unless `only` is specified.
|
2020-09-28 02:10:17 +02:00
|
|
|
def to_objects(only: nil, method: nil)
|
2020-09-25 21:13:26 +02:00
|
|
|
@to_objects ||= {}
|
|
|
|
@to_objects[only] ||= downcased_unique_named.flat_map do |name|
|
|
|
|
next Tap.fetch(name) if only == :tap || (only.nil? && name.count("/") == 1 && !name.start_with?("./", "/"))
|
|
|
|
|
2020-09-28 02:10:17 +02:00
|
|
|
load_formula_or_cask(name, only: only, method: method)
|
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-09-07 18:39:58 +02:00
|
|
|
to_paths(only: :formulae)
|
|
|
|
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-09-07 18:39:58 +02:00
|
|
|
elsif name.count("/") == 1
|
|
|
|
Tap.fetch(name).path
|
|
|
|
else
|
|
|
|
next Formulary.path(name) if only == :formulae
|
|
|
|
next Cask::CaskLoader.path(name) if only == :casks
|
|
|
|
|
|
|
|
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?
|
|
|
|
|
|
|
|
paths.empty? ? name : paths
|
|
|
|
end
|
2020-08-13 08:55:55 -04:00
|
|
|
end.uniq.freeze
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_kegs
|
|
|
|
@to_kegs ||= downcased_unique_named.map do |name|
|
|
|
|
resolve_keg name
|
|
|
|
rescue NoSuchKegError => e
|
|
|
|
if (reason = Homebrew::MissingFormula.suggest_command(name, "uninstall"))
|
|
|
|
$stderr.puts reason
|
|
|
|
end
|
|
|
|
raise e
|
|
|
|
end.freeze
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_kegs_to_casks
|
|
|
|
@to_kegs_to_casks ||= begin
|
|
|
|
kegs = []
|
|
|
|
casks = []
|
|
|
|
|
|
|
|
downcased_unique_named.each do |name|
|
|
|
|
kegs << resolve_keg(name)
|
2020-08-17 12:40:23 -04:00
|
|
|
|
2020-08-25 09:23:27 -04:00
|
|
|
warn_if_cask_conflicts(name, "keg")
|
2020-08-13 08:55:55 -04:00
|
|
|
rescue NoSuchKegError, FormulaUnavailableError
|
|
|
|
begin
|
2020-09-29 23:46:30 +02:00
|
|
|
casks << Cask::CaskLoader.load(name, config: Cask::Config.from_args(@parent))
|
2020-08-13 08:55:55 -04:00
|
|
|
rescue Cask::CaskUnavailableError
|
|
|
|
raise "No installed keg or cask with the name \"#{name}\""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[kegs.freeze, casks.freeze].freeze
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def homebrew_tap_cask_names
|
|
|
|
downcased_unique_named.grep(HOMEBREW_CASK_TAP_CASK_REGEX)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
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
|