cli/named_args: raise priority of core casks
This commit is contained in:
parent
249a7beb56
commit
ae9d5f9f14
@ -138,9 +138,9 @@ module Homebrew
|
|||||||
Homebrew.with_no_api_env_if_needed(@without_api) do
|
Homebrew.with_no_api_env_if_needed(@without_api) do
|
||||||
unreadable_error = nil
|
unreadable_error = nil
|
||||||
|
|
||||||
if only != :cask
|
formula_or_kegs = if only != :cask
|
||||||
begin
|
begin
|
||||||
formula = case method
|
case method
|
||||||
when nil, :factory
|
when nil, :factory
|
||||||
options = { warn:, force_bottle: @force_bottle, flags: @flags }.compact
|
options = { warn:, force_bottle: @force_bottle, flags: @flags }.compact
|
||||||
Formulary.factory(name, *@override_spec, **options)
|
Formulary.factory(name, *@override_spec, **options)
|
||||||
@ -156,27 +156,32 @@ module Homebrew
|
|||||||
else
|
else
|
||||||
raise
|
raise
|
||||||
end
|
end
|
||||||
|
|
||||||
warn_if_cask_conflicts(name, "formula") if only != :formula
|
|
||||||
return formula
|
|
||||||
rescue FormulaUnreadableError, FormulaClassUnavailableError,
|
rescue FormulaUnreadableError, FormulaClassUnavailableError,
|
||||||
TapFormulaUnreadableError, TapFormulaClassUnavailableError,
|
TapFormulaUnreadableError, TapFormulaClassUnavailableError,
|
||||||
FormulaSpecificationError => e
|
FormulaSpecificationError => e
|
||||||
# Need to rescue before `FormulaUnavailableError` (superclass of this)
|
# Need to rescue before `FormulaUnavailableError` (superclass of this)
|
||||||
# The formula was found, but there's a problem with its implementation
|
# The formula was found, but there's a problem with its implementation
|
||||||
unreadable_error ||= e
|
unreadable_error ||= e
|
||||||
|
nil
|
||||||
rescue NoSuchKegError, FormulaUnavailableError => e
|
rescue NoSuchKegError, FormulaUnavailableError => e
|
||||||
raise e if only == :formula
|
raise e if only == :formula
|
||||||
|
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if only != :formula
|
if only == :formula
|
||||||
|
return formula_or_kegs if formula_or_kegs
|
||||||
|
elsif formula_or_kegs && (!formula_or_kegs.is_a?(Formula) || formula_or_kegs.tap&.core_tap?)
|
||||||
|
warn_if_cask_conflicts(name, "formula")
|
||||||
|
return formula_or_kegs
|
||||||
|
else
|
||||||
want_keg_like_cask = [:latest_kegs, :default_kegs, :kegs].include?(method)
|
want_keg_like_cask = [:latest_kegs, :default_kegs, :kegs].include?(method)
|
||||||
|
|
||||||
begin
|
cask = begin
|
||||||
config = Cask::Config.from_args(@parent) if @cask_options
|
config = Cask::Config.from_args(@parent) if @cask_options
|
||||||
options = { warn: }.compact
|
options = { warn: }.compact
|
||||||
cask = Cask::CaskLoader.load(name, config:, **options)
|
candidate_cask = Cask::CaskLoader.load(name, config:, **options)
|
||||||
|
|
||||||
if unreadable_error.present?
|
if unreadable_error.present?
|
||||||
onoe <<~EOS
|
onoe <<~EOS
|
||||||
@ -188,27 +193,46 @@ module Homebrew
|
|||||||
|
|
||||||
# If we're trying to get a keg-like Cask, do our best to use the same cask
|
# 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.
|
# file that was used for installation, if possible.
|
||||||
if want_keg_like_cask && (installed_caskfile = cask.installed_caskfile) && installed_caskfile.exist?
|
if want_keg_like_cask &&
|
||||||
cask = Cask::CaskLoader.load(installed_caskfile)
|
(installed_caskfile = candidate_cask.installed_caskfile) &&
|
||||||
|
installed_caskfile.exist?
|
||||||
|
Cask::CaskLoader.load(installed_caskfile)
|
||||||
|
else
|
||||||
|
candidate_cask
|
||||||
end
|
end
|
||||||
|
|
||||||
return cask
|
|
||||||
rescue Cask::CaskUnreadableError, Cask::CaskInvalidError => e
|
rescue Cask::CaskUnreadableError, Cask::CaskInvalidError => e
|
||||||
# If we're trying to get a keg-like Cask, do our best to handle it
|
# 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.
|
# not being readable and return something that can be used.
|
||||||
if want_keg_like_cask
|
if want_keg_like_cask
|
||||||
cask_version = Cask::Cask.new(name, config:).installed_version
|
cask_version = Cask::Cask.new(name, config:).installed_version
|
||||||
cask = Cask::Cask.new(name, config:) do
|
Cask::Cask.new(name, config:) do
|
||||||
version cask_version if cask_version
|
version cask_version if cask_version
|
||||||
end
|
end
|
||||||
return cask
|
else
|
||||||
|
# Need to rescue before `CaskUnavailableError` (superclass of this)
|
||||||
|
# The cask was found, but there's a problem with its implementation
|
||||||
|
unreadable_error ||= e
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# 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
|
rescue Cask::CaskUnavailableError => e
|
||||||
raise e if only == :cask
|
raise e if only == :cask
|
||||||
|
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
# Prioritise formulae unless it's a core tap cask (we already prioritised core tap formulae above)
|
||||||
|
if formula_or_kegs && !cask&.tap&.core_cask_tap?
|
||||||
|
if cask || unreadable_error
|
||||||
|
onoe <<~EOS if unreadable_error
|
||||||
|
Failed to load cask: #{name}
|
||||||
|
#{unreadable_error}
|
||||||
|
EOS
|
||||||
|
opoo package_conflicts_message(name, "formula", cask)
|
||||||
|
end
|
||||||
|
return formula_or_kegs
|
||||||
|
elsif cask
|
||||||
|
opoo package_conflicts_message(name, "cask", formula_or_kegs) if formula_or_kegs
|
||||||
|
return cask
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -438,13 +462,29 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def warn_if_cask_conflicts(ref, loaded_type)
|
def package_conflicts_message(ref, loaded_type, package)
|
||||||
message = "Treating #{ref} as a #{loaded_type}."
|
message = "Treating #{ref} as a #{loaded_type}."
|
||||||
begin
|
case package
|
||||||
cask = Cask::CaskLoader.load(ref, warn: false)
|
when Formula, Keg, Array
|
||||||
|
message += " For the formula, "
|
||||||
|
if package.is_a?(Formula) && (tap = package.tap)
|
||||||
|
message += "use #{tap.name}/#{package.name} or "
|
||||||
|
end
|
||||||
|
message += "specify the `--formula` flag."
|
||||||
|
when Cask::Cask
|
||||||
message += " For the cask, "
|
message += " For the cask, "
|
||||||
message += "use #{cask.tap.name}/#{cask.token} or " if cask.tap
|
if (tap = package.tap)
|
||||||
|
message += "use #{tap.name}/#{package.token} or "
|
||||||
|
end
|
||||||
message += "specify the `--cask` flag."
|
message += "specify the `--cask` flag."
|
||||||
|
end
|
||||||
|
message.freeze
|
||||||
|
end
|
||||||
|
|
||||||
|
def warn_if_cask_conflicts(ref, loaded_type)
|
||||||
|
available = true
|
||||||
|
cask = begin
|
||||||
|
Cask::CaskLoader.load(ref, warn: false)
|
||||||
rescue Cask::CaskUnreadableError => e
|
rescue Cask::CaskUnreadableError => e
|
||||||
# Need to rescue before `CaskUnavailableError` (superclass of this)
|
# Need to rescue before `CaskUnavailableError` (superclass of this)
|
||||||
# The cask was found, but there's a problem with its implementation
|
# The cask was found, but there's a problem with its implementation
|
||||||
@ -452,11 +492,15 @@ module Homebrew
|
|||||||
Failed to load cask: #{ref}
|
Failed to load cask: #{ref}
|
||||||
#{e}
|
#{e}
|
||||||
EOS
|
EOS
|
||||||
|
nil
|
||||||
rescue Cask::CaskUnavailableError
|
rescue Cask::CaskUnavailableError
|
||||||
# No ref conflict with a cask, do nothing
|
# No ref conflict with a cask, do nothing
|
||||||
return
|
available = false
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
opoo message.freeze
|
return unless available
|
||||||
|
|
||||||
|
opoo package_conflicts_message(ref, loaded_type, cask)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -31,7 +31,7 @@ RSpec.describe Homebrew::CLI::NamedArgs do
|
|||||||
end
|
end
|
||||||
|
|
||||||
let(:baz) do
|
let(:baz) do
|
||||||
Cask::CaskLoader.load(+<<~RUBY)
|
Cask::CaskLoader::FromContentLoader.new(+<<~RUBY, tap: CoreCaskTap.instance).load(config: nil)
|
||||||
cask "baz" do
|
cask "baz" do
|
||||||
version "1.0"
|
version "1.0"
|
||||||
end
|
end
|
||||||
@ -39,7 +39,7 @@ RSpec.describe Homebrew::CLI::NamedArgs do
|
|||||||
end
|
end
|
||||||
|
|
||||||
let(:foo_cask) do
|
let(:foo_cask) do
|
||||||
Cask::CaskLoader.load(+<<~RUBY)
|
Cask::CaskLoader::FromContentLoader.new(+<<~RUBY, tap: CoreCaskTap.instance).load(config: nil)
|
||||||
cask "foo" do
|
cask "foo" do
|
||||||
version "1.0"
|
version "1.0"
|
||||||
end
|
end
|
||||||
@ -90,6 +90,32 @@ RSpec.describe Homebrew::CLI::NamedArgs do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "when a non-core formula and a core cask are present" do
|
||||||
|
let(:non_core_formula) do
|
||||||
|
formula "foo", tap: Tap.fetch("some/tap") do
|
||||||
|
url "https://brew.sh"
|
||||||
|
version "1.0"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
stub_formula_loader non_core_formula, "foo"
|
||||||
|
stub_cask_loader foo_cask
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns the cask by default" do
|
||||||
|
expect(described_class.new("foo").to_formulae_and_casks).to eq [foo_cask]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns formula if loading formula only" do
|
||||||
|
expect(described_class.new("foo").to_formulae_and_casks(only: :formula)).to eq [non_core_formula]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns cask if loading cask only" do
|
||||||
|
expect(described_class.new("foo").to_formulae_and_casks(only: :cask)).to eq [foo_cask]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "when both formula and cask are unreadable" do
|
context "when both formula and cask are unreadable" do
|
||||||
before do
|
before do
|
||||||
setup_unredable_formula "foo"
|
setup_unredable_formula "foo"
|
||||||
|
|||||||
@ -5,8 +5,8 @@ require "formulary"
|
|||||||
module Test
|
module Test
|
||||||
module Helper
|
module Helper
|
||||||
module Formula
|
module Formula
|
||||||
def formula(name = "formula_name", path: Formulary.core_path(name), spec: :stable, alias_path: nil, tap: nil,
|
def formula(name = "formula_name", path: nil, spec: :stable, alias_path: nil, tap: nil, &block)
|
||||||
&block)
|
path ||= Formulary.find_formula_in_tap(name, tap || CoreTap.instance)
|
||||||
Class.new(::Formula, &block).new(name, path, spec, alias_path:, tap:)
|
Class.new(::Formula, &block).new(name, path, spec, alias_path:, tap:)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user