2023-02-27 11:43:16 -08:00
|
|
|
# typed: true
|
2020-08-13 08:55:55 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "delegate"
|
2021-08-06 02:30:44 -04:00
|
|
|
require "api"
|
2020-09-29 23:46:30 +02:00
|
|
|
require "cli/args"
|
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
|
2023-04-14 15:33:40 +02:00
|
|
|
sig {
|
|
|
|
params(
|
|
|
|
args: String,
|
|
|
|
parent: Args,
|
|
|
|
override_spec: Symbol,
|
|
|
|
force_bottle: T::Boolean,
|
|
|
|
flags: T::Array[String],
|
|
|
|
cask_options: T::Boolean,
|
2023-06-19 03:57:52 +01:00
|
|
|
without_api: T::Boolean,
|
2023-04-14 15:33:40 +02:00
|
|
|
).void
|
|
|
|
}
|
|
|
|
def initialize(
|
|
|
|
*args,
|
|
|
|
parent: Args.new,
|
|
|
|
override_spec: T.unsafe(nil),
|
|
|
|
force_bottle: T.unsafe(nil),
|
|
|
|
flags: T.unsafe(nil),
|
2023-06-19 03:57:52 +01:00
|
|
|
cask_options: false,
|
|
|
|
without_api: false
|
2023-04-14 15:33:40 +02:00
|
|
|
)
|
2020-11-30 00:54:27 +01:00
|
|
|
require "cask/cask"
|
|
|
|
require "cask/cask_loader"
|
|
|
|
require "formulary"
|
|
|
|
require "keg"
|
|
|
|
require "missing_formula"
|
|
|
|
|
2020-08-13 08:55:55 -04:00
|
|
|
@args = args
|
|
|
|
@override_spec = override_spec
|
|
|
|
@force_bottle = force_bottle
|
|
|
|
@flags = flags
|
2021-03-18 14:46:48 +00:00
|
|
|
@cask_options = cask_options
|
2023-06-19 03:57:52 +01:00
|
|
|
@without_api = without_api
|
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-12-17 21:14:18 +09:00
|
|
|
attr_reader :parent
|
|
|
|
|
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-12-03 04:17:02 +01:00
|
|
|
# Convert named arguments to {Formula} or {Cask} objects.
|
|
|
|
# If both a formula and cask with the same name exist, returns
|
|
|
|
# the formula and prints a warning unless `only` is specified.
|
2021-01-17 22:45:55 -08:00
|
|
|
sig {
|
2021-04-08 22:05:02 +01:00
|
|
|
params(
|
2022-06-14 17:19:29 -04:00
|
|
|
only: T.nilable(Symbol),
|
|
|
|
ignore_unavailable: T.nilable(T::Boolean),
|
|
|
|
method: T.nilable(Symbol),
|
|
|
|
uniq: T::Boolean,
|
2023-04-08 14:10:58 +02:00
|
|
|
warn: T::Boolean,
|
2021-04-08 22:05:02 +01:00
|
|
|
).returns(T::Array[T.any(Formula, Keg, Cask::Cask)])
|
2021-01-17 22:45:55 -08:00
|
|
|
}
|
2023-04-14 15:33:40 +02:00
|
|
|
def to_formulae_and_casks(
|
|
|
|
only: parent&.only_formula_or_cask,
|
|
|
|
ignore_unavailable: nil,
|
|
|
|
method: T.unsafe(nil),
|
|
|
|
uniq: true,
|
|
|
|
warn: T.unsafe(nil)
|
|
|
|
)
|
2020-09-25 21:13:26 +02:00
|
|
|
@to_formulae_and_casks ||= {}
|
2020-12-03 04:17:02 +01:00
|
|
|
@to_formulae_and_casks[only] ||= downcased_unique_named.flat_map do |name|
|
2023-04-14 15:33:40 +02:00
|
|
|
options = { warn: warn }.compact
|
|
|
|
load_formula_or_cask(name, only: only, method: method, **options)
|
2021-01-24 09:04:10 -08:00
|
|
|
rescue FormulaUnreadableError, FormulaClassUnavailableError,
|
|
|
|
TapFormulaUnreadableError, TapFormulaClassUnavailableError,
|
|
|
|
Cask::CaskUnreadableError
|
|
|
|
# Need to rescue before `*UnavailableError` (superclass of this)
|
|
|
|
# The formula/cask was found, but there's a problem with its implementation
|
|
|
|
raise
|
2021-03-25 19:34:34 +01:00
|
|
|
rescue NoSuchKegError, FormulaUnavailableError, Cask::CaskUnavailableError, FormulaOrCaskUnavailableError
|
2020-12-05 13:57:41 -05:00
|
|
|
ignore_unavailable ? [] : raise
|
2021-04-08 22:05:02 +01:00
|
|
|
end.freeze
|
|
|
|
|
|
|
|
if uniq
|
|
|
|
@to_formulae_and_casks[only].uniq.freeze
|
|
|
|
else
|
|
|
|
@to_formulae_and_casks[only]
|
|
|
|
end
|
2020-09-25 21:13:26 +02:00
|
|
|
end
|
2020-08-17 12:40:23 -04:00
|
|
|
|
2020-12-17 21:14:18 +09:00
|
|
|
def to_formulae_to_casks(only: parent&.only_formula_or_cask, 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)
|
2021-09-09 22:16:45 +09:00
|
|
|
.partition { |o| o.is_a?(Formula) || o.is_a?(Keg) }
|
2020-10-09 21:09:07 -04:00
|
|
|
.map(&:freeze).freeze
|
|
|
|
end
|
|
|
|
|
2020-12-17 21:14:18 +09:00
|
|
|
def to_formulae_and_casks_and_unavailable(only: parent&.only_formula_or_cask, 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-11-20 10:26:10 +01:00
|
|
|
load_formula_or_cask(name, only: only, method: method)
|
2020-10-08 19:55:24 -04:00
|
|
|
rescue FormulaOrCaskUnavailableError => e
|
|
|
|
e
|
|
|
|
end.uniq.freeze
|
|
|
|
end
|
|
|
|
|
2023-04-14 15:33:40 +02:00
|
|
|
def load_formula_or_cask(name, only: nil, method: nil, warn: nil)
|
2023-06-19 03:57:52 +01:00
|
|
|
Homebrew.with_no_api_env_if_needed(@without_api) do
|
|
|
|
unreadable_error = nil
|
|
|
|
|
|
|
|
if only != :cask
|
|
|
|
begin
|
|
|
|
formula = case method
|
|
|
|
when nil, :factory
|
|
|
|
options = { warn: warn, force_bottle: @force_bottle, flags: @flags }.compact
|
|
|
|
Formulary.factory(name, *@override_spec, **options)
|
|
|
|
when :resolve
|
|
|
|
resolve_formula(name)
|
|
|
|
when :latest_kegs
|
|
|
|
resolve_latest_keg(name)
|
|
|
|
when :default_kegs
|
|
|
|
resolve_default_keg(name)
|
|
|
|
when :kegs
|
|
|
|
_, kegs = resolve_kegs(name)
|
|
|
|
kegs
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
2020-09-28 02:10:17 +02:00
|
|
|
|
2023-06-19 03:57:52 +01:00
|
|
|
warn_if_cask_conflicts(name, "formula") if only != :formula
|
|
|
|
return formula
|
|
|
|
rescue FormulaUnreadableError, FormulaClassUnavailableError,
|
|
|
|
TapFormulaUnreadableError, TapFormulaClassUnavailableError => e
|
|
|
|
# Need to rescue before `FormulaUnavailableError` (superclass of this)
|
|
|
|
# The formula was found, but there's a problem with its implementation
|
|
|
|
unreadable_error ||= e
|
|
|
|
rescue NoSuchKegError, FormulaUnavailableError => e
|
|
|
|
raise e if only == :formula
|
|
|
|
end
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
|
|
|
|
2023-06-19 03:57:52 +01:00
|
|
|
if only != :formula
|
|
|
|
want_keg_like_cask = [:latest_kegs, :default_kegs, :kegs].include?(method)
|
2022-05-30 16:39:00 +01:00
|
|
|
|
2023-06-19 03:57:52 +01:00
|
|
|
begin
|
|
|
|
config = Cask::Config.from_args(@parent) if @cask_options
|
|
|
|
options = { warn: warn }.compact
|
|
|
|
cask = Cask::CaskLoader.load(name, config: config, **options)
|
2021-01-27 10:03:44 -08:00
|
|
|
|
2023-06-19 03:57:52 +01:00
|
|
|
if unreadable_error.present?
|
|
|
|
onoe <<~EOS
|
|
|
|
Failed to load formula: #{name}
|
|
|
|
#{unreadable_error}
|
|
|
|
EOS
|
|
|
|
opoo "Treating #{name} as a cask."
|
|
|
|
end
|
2022-05-30 16:39:00 +01:00
|
|
|
|
2023-06-19 03:57:52 +01:00
|
|
|
# If we're trying to get a keg-like Cask, do our best to use the same cask
|
|
|
|
# file that was used for installation, if possible.
|
|
|
|
if want_keg_like_cask && (installed_caskfile = cask.installed_caskfile) && installed_caskfile.exist?
|
|
|
|
cask = Cask::CaskLoader.load(installed_caskfile)
|
2022-05-16 17:27:13 -04:00
|
|
|
end
|
2023-06-19 03:57:52 +01:00
|
|
|
|
2022-05-16 17:27:13 -04:00
|
|
|
return cask
|
2023-06-19 03:57:52 +01:00
|
|
|
rescue Cask::CaskUnreadableError, Cask::CaskInvalidError => e
|
|
|
|
# If we're trying to get a keg-like Cask, do our best to handle it
|
|
|
|
# not being readable and return something that can be used.
|
|
|
|
if want_keg_like_cask
|
|
|
|
cask_version = Cask::Cask.new(name, config: config).installed_version
|
|
|
|
cask = Cask::Cask.new(name, config: config) do
|
|
|
|
version cask_version if cask_version
|
|
|
|
end
|
|
|
|
return cask
|
|
|
|
end
|
2022-05-16 17:27:13 -04:00
|
|
|
|
2023-06-19 03:57:52 +01:00
|
|
|
# Need to rescue before `CaskUnavailableError` (superclass of this)
|
|
|
|
# The cask was found, but there's a problem with its implementation
|
|
|
|
unreadable_error ||= e
|
|
|
|
rescue Cask::CaskUnavailableError => e
|
|
|
|
raise e if only == :cask
|
|
|
|
end
|
2020-09-25 21:13:26 +02:00
|
|
|
end
|
|
|
|
|
2023-06-19 03:57:52 +01:00
|
|
|
raise unreadable_error if unreadable_error.present?
|
2021-01-24 09:04:10 -08:00
|
|
|
|
2023-06-19 03:57:52 +01:00
|
|
|
user, repo, short_name = name.downcase.split("/", 3)
|
|
|
|
if repo.present? && short_name.present?
|
|
|
|
tap = Tap.fetch(user, repo)
|
|
|
|
raise TapFormulaOrCaskUnavailableError.new(tap, short_name)
|
|
|
|
end
|
2021-03-11 21:09:42 +05:30
|
|
|
|
2023-06-19 03:57:52 +01:00
|
|
|
raise NoSuchKegError, name if resolve_formula(name)
|
2021-11-11 13:52:46 -05:00
|
|
|
|
2023-06-19 03:57:52 +01:00
|
|
|
raise FormulaOrCaskUnavailableError, name
|
|
|
|
end
|
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)
|
2023-04-14 15:33:40 +02:00
|
|
|
Formulary.resolve(name, **{ spec: @override_spec, force_bottle: @force_bottle, flags: @flags }.compact)
|
2020-09-25 21:13:26 +02:00
|
|
|
end
|
|
|
|
private :resolve_formula
|
2020-08-13 08:55:55 -04:00
|
|
|
|
2021-04-08 22:05:02 +01:00
|
|
|
sig { params(uniq: T::Boolean).returns(T::Array[Formula]) }
|
|
|
|
def to_resolved_formulae(uniq: true)
|
|
|
|
@to_resolved_formulae ||= to_formulae_and_casks(only: :formula, method: :resolve, uniq: uniq)
|
2020-09-28 02:10:17 +02:00
|
|
|
.freeze
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
|
|
|
|
2020-12-17 21:14:18 +09:00
|
|
|
def to_resolved_formulae_to_casks(only: parent&.only_formula_or_cask)
|
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-09-07 18:39:58 +02:00
|
|
|
# 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.
|
2020-12-08 01:03:39 +01:00
|
|
|
sig { params(only: T.nilable(Symbol), recurse_tap: T::Boolean).returns(T::Array[Pathname]) }
|
2020-12-17 21:14:18 +09:00
|
|
|
def to_paths(only: parent&.only_formula_or_cask, recurse_tap: false)
|
2020-09-07 18:39:58 +02:00
|
|
|
@to_paths ||= {}
|
2023-08-04 16:21:31 +01:00
|
|
|
@to_paths[only] ||= Homebrew.with_no_api_env_if_needed(@without_api) do
|
|
|
|
downcased_unique_named.flat_map do |name|
|
|
|
|
path = Pathname(name)
|
|
|
|
if File.exist?(name)
|
|
|
|
path
|
|
|
|
elsif name.count("/") == 1 && !name.start_with?("./", "/")
|
|
|
|
tap = Tap.fetch(name)
|
|
|
|
|
|
|
|
if recurse_tap
|
|
|
|
next tap.formula_files if only == :formula
|
|
|
|
next tap.cask_files if only == :cask
|
|
|
|
end
|
2020-12-08 01:03:39 +01:00
|
|
|
|
2023-08-04 16:21:31 +01:00
|
|
|
tap.path
|
|
|
|
else
|
|
|
|
next Formulary.path(name) if only == :formula
|
|
|
|
next Cask::CaskLoader.path(name) if only == :cask
|
2020-09-07 18:39:58 +02:00
|
|
|
|
2023-08-04 16:21:31 +01:00
|
|
|
formula_path = Formulary.path(name)
|
|
|
|
cask_path = Cask::CaskLoader.path(name)
|
2020-09-07 18:39:58 +02:00
|
|
|
|
2023-08-04 16:21:31 +01:00
|
|
|
paths = []
|
2020-09-07 18:39:58 +02:00
|
|
|
|
2023-08-04 16:21:31 +01:00
|
|
|
if formula_path.exist? ||
|
|
|
|
(!CoreTap.instance.installed? && Homebrew::API::Formula.all_formulae.key?(path.basename))
|
|
|
|
paths << formula_path
|
|
|
|
end
|
|
|
|
if cask_path.exist? ||
|
|
|
|
(!CoreCaskTap.instance.installed? && Homebrew::API::Cask.all_casks.key?(path.basename))
|
|
|
|
paths << cask_path
|
|
|
|
end
|
2020-09-07 18:39:58 +02:00
|
|
|
|
2023-08-04 16:21:31 +01:00
|
|
|
paths.empty? ? path : paths
|
|
|
|
end
|
|
|
|
end.uniq.freeze
|
|
|
|
end
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
|
|
|
|
2020-11-18 16:39:07 +01:00
|
|
|
sig { returns(T::Array[Keg]) }
|
2021-05-19 09:34:18 -04:00
|
|
|
def to_default_kegs
|
|
|
|
@to_default_kegs ||= begin
|
2021-05-19 10:07:03 -04:00
|
|
|
to_formulae_and_casks(only: :formula, method: :default_kegs).freeze
|
2020-08-13 08:55:55 -04:00
|
|
|
rescue NoSuchKegError => e
|
2020-11-30 00:54:27 +01:00
|
|
|
if (reason = 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
|
|
|
|
|
2021-05-20 12:58:23 -04:00
|
|
|
sig { returns(T::Array[Keg]) }
|
|
|
|
def to_latest_kegs
|
2021-05-20 15:10:20 -04:00
|
|
|
@to_latest_kegs ||= begin
|
2021-05-20 12:58:23 -04:00
|
|
|
to_formulae_and_casks(only: :formula, method: :latest_kegs).freeze
|
|
|
|
rescue NoSuchKegError => e
|
|
|
|
if (reason = MissingFormula.suggest_command(e.name, "uninstall"))
|
|
|
|
$stderr.puts reason
|
|
|
|
end
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-18 10:07:35 -04:00
|
|
|
sig { returns(T::Array[Keg]) }
|
|
|
|
def to_kegs
|
|
|
|
@to_kegs ||= begin
|
|
|
|
to_formulae_and_casks(only: :formula, method: :kegs).freeze
|
|
|
|
rescue NoSuchKegError => e
|
|
|
|
if (reason = MissingFormula.suggest_command(e.name, "uninstall"))
|
|
|
|
$stderr.puts reason
|
|
|
|
end
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-17 22:45:55 -08:00
|
|
|
sig {
|
2020-11-19 12:57:10 +01:00
|
|
|
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]])
|
2021-01-17 22:45:55 -08:00
|
|
|
}
|
2020-12-17 21:14:18 +09:00
|
|
|
def to_kegs_to_casks(only: parent&.only_formula_or_cask, ignore_unavailable: nil, all_kegs: nil)
|
2021-05-19 10:07:03 -04:00
|
|
|
method = all_kegs ? :kegs : :default_kegs
|
2020-11-19 12:57:10 +01:00
|
|
|
@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
|
|
|
|
|
2021-01-10 14:26:40 -05:00
|
|
|
sig { returns(T::Array[Tap]) }
|
|
|
|
def to_taps
|
|
|
|
@to_taps ||= downcased_unique_named.map { |name| Tap.fetch name }.uniq.freeze
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { returns(T::Array[Tap]) }
|
|
|
|
def to_installed_taps
|
|
|
|
@to_installed_taps ||= to_taps.each do |tap|
|
|
|
|
raise TapUnavailableError, tap.name unless tap.installed?
|
|
|
|
end.uniq.freeze
|
|
|
|
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
|
|
|
|
|
2021-03-25 19:11:52 +01:00
|
|
|
def resolve_kegs(name)
|
2020-08-13 08:55:55 -04:00
|
|
|
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)
|
|
|
|
|
2021-03-25 19:11:52 +01:00
|
|
|
kegs = rack.directory? ? rack.subdirs.map { |d| Keg.new(d) } : []
|
2021-11-24 11:37:34 -08:00
|
|
|
raise NoSuchKegError, name if kegs.none?
|
2021-03-25 19:11:52 +01:00
|
|
|
|
|
|
|
[rack, kegs]
|
|
|
|
end
|
|
|
|
|
2021-05-20 12:58:23 -04:00
|
|
|
def resolve_latest_keg(name)
|
|
|
|
_, kegs = resolve_kegs(name)
|
|
|
|
|
2021-05-21 11:55:11 -04:00
|
|
|
# Return keg if it is the only installed keg
|
2021-05-20 12:58:23 -04:00
|
|
|
return kegs if kegs.length == 1
|
|
|
|
|
2021-08-06 21:27:21 -04:00
|
|
|
stable_kegs = kegs.reject { |k| k.version.head? }
|
2021-08-04 18:51:39 -04:00
|
|
|
|
2021-08-11 20:07:28 -04:00
|
|
|
if stable_kegs.blank?
|
|
|
|
return kegs.max_by do |keg|
|
2021-08-12 09:16:23 -04:00
|
|
|
[Tab.for_keg(keg).source_modified_time, keg.version.revision]
|
|
|
|
end
|
2021-08-11 20:07:28 -04:00
|
|
|
end
|
2021-08-06 21:27:21 -04:00
|
|
|
|
|
|
|
stable_kegs.max_by(&:version)
|
2021-05-20 12:58:23 -04:00
|
|
|
end
|
|
|
|
|
2021-05-19 10:29:40 -04:00
|
|
|
def resolve_default_keg(name)
|
2021-03-25 19:11:52 +01:00
|
|
|
rack, kegs = resolve_kegs(name)
|
2020-08-13 08:55:55 -04:00
|
|
|
|
|
|
|
linked_keg_ref = HOMEBREW_LINKED_KEGS/rack.basename
|
|
|
|
opt_prefix = HOMEBREW_PREFIX/"opt/#{rack.basename}"
|
|
|
|
|
|
|
|
begin
|
2021-03-01 09:10:26 +09:00
|
|
|
return Keg.new(opt_prefix.resolved_path) if opt_prefix.symlink? && opt_prefix.directory?
|
|
|
|
return Keg.new(linked_keg_ref.resolved_path) if linked_keg_ref.symlink? && linked_keg_ref.directory?
|
2021-03-25 19:11:52 +01:00
|
|
|
return kegs.first if kegs.length == 1
|
2020-08-13 08:55:55 -04:00
|
|
|
|
2021-03-01 09:10:26 +09:00
|
|
|
f = if name.include?("/") || File.exist?(name)
|
|
|
|
Formulary.factory(name)
|
|
|
|
else
|
|
|
|
Formulary.from_rack(rack)
|
|
|
|
end
|
2020-08-13 08:55:55 -04:00
|
|
|
|
2021-03-01 09:10:26 +09:00
|
|
|
unless (prefix = f.latest_installed_prefix).directory?
|
|
|
|
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
|
2021-03-01 09:10:26 +09:00
|
|
|
|
|
|
|
Keg.new(prefix)
|
2020-08-13 08:55:55 -04:00
|
|
|
rescue FormulaUnavailableError
|
|
|
|
raise MultipleVersionsInstalledError, <<~EOS
|
|
|
|
Multiple kegs installed to #{rack}
|
|
|
|
However we don't know which one you refer to.
|
2023-02-10 23:15:40 -05:00
|
|
|
Please delete (with `rm -rf`!) all but one and then try again.
|
2020-08-13 08:55:55 -04:00
|
|
|
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)
|
2020-09-01 08:50:08 +01:00
|
|
|
message = "Treating #{ref} as a #{loaded_type}."
|
2021-01-27 10:03:44 -08:00
|
|
|
begin
|
|
|
|
cask = Cask::CaskLoader.load ref
|
|
|
|
message += " For the cask, use #{cask.tap.name}/#{cask.token}" if cask.tap.present?
|
|
|
|
rescue Cask::CaskUnreadableError => e
|
|
|
|
# Need to rescue before `CaskUnavailableError` (superclass of this)
|
|
|
|
# The cask was found, but there's a problem with its implementation
|
|
|
|
onoe <<~EOS
|
|
|
|
Failed to load cask: #{ref}
|
|
|
|
#{e}
|
|
|
|
EOS
|
|
|
|
rescue Cask::CaskUnavailableError
|
|
|
|
# No ref conflict with a cask, do nothing
|
|
|
|
return
|
|
|
|
end
|
2020-09-01 08:50:08 +01:00
|
|
|
opoo message.freeze
|
2020-08-17 12:40:23 -04:00
|
|
|
end
|
2020-08-13 08:55:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|