named_args: better handle keg-like casks.

Need to ensure we're using the installed caskfile when it's requested
and readable.

Follow-up from https://github.com/Homebrew/brew/pull/13289 addressing
https://github.com/Homebrew/brew/pull/13289#discussion_r884853652
This commit is contained in:
Mike McQuaid 2022-05-30 16:39:00 +01:00
parent 2d80148683
commit fdefffc0a5
No known key found for this signature in database
GPG Key ID: 3338A31AFDB1D829

View File

@ -134,6 +134,8 @@ module Homebrew
contents = Homebrew::API::CaskSource.fetch(name)
end
want_keg_like_cask = [:latest_kegs, :default_kegs, :kegs].include?(method)
begin
config = Cask::Config.from_args(@parent) if @cask_options
cask = Cask::CaskLoader.load(contents || name, config: config)
@ -146,11 +148,17 @@ module Homebrew
opoo "Treating #{name} as a cask."
end
# 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)
end
return cask
rescue Cask::CaskUnreadableError => 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 [:latest_kegs, :default_kegs, :kegs].include?(method)
if want_keg_like_cask
cask_version = Cask::Cask.new(name, config: config).versions.first
cask = Cask::Cask.new(name, config: config) do
version cask_version if cask_version