Skip to content

Commit

Permalink
cmd/untap: fix untapping syntax failure.
Browse files Browse the repository at this point in the history
If an installed cask is invalid on attempting an untap: it will
prevent untapping that cask.

Fix this in two ways: one more specific to `untap` and one more
generally to other commands too:
- specific: only read the actual formulae/casks from the tap we're
  untapping instead of all of those that are installed
- general: rescue more exceptions in `Cask::Caskroom.casks` (like we
  already do for `Formula.installed`
  • Loading branch information
MikeMcQuaid committed Dec 13, 2023
1 parent cd8a9c0 commit 9811606
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
26 changes: 14 additions & 12 deletions Library/Homebrew/cask/caskroom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def self.paths
end
private_class_method :paths

# Return all tokens for installed casks.
sig { returns(T::Array[String]) }
def self.tokens
paths.map(&:basename).map(&:to_s)
end

sig { returns(T::Boolean) }
def self.any_casks_installed?
paths.any?
Expand All @@ -46,18 +52,14 @@ def self.ensure_caskroom_exists

sig { params(config: T.nilable(Config)).returns(T::Array[Cask]) }
def self.casks(config: nil)
paths.sort.map do |path|
token = path.basename.to_s

begin
CaskLoader.load(token, config: config)
rescue TapCaskAmbiguityError
tap_path = CaskLoader.tap_paths(token).first
CaskLoader::FromTapPathLoader.new(tap_path).load(config: config)
rescue CaskUnavailableError
# Don't blow up because of a single unavailable cask.
nil
end
tokens.sort.map do |token|
CaskLoader.load(token, config: config)
rescue TapCaskAmbiguityError
tap_path = CaskLoader.tap_paths(token).first
CaskLoader::FromTapPathLoader.new(tap_path).load(config: config)
rescue
# Don't blow up because of a single unavailable cask.
nil
end.compact
end
end
Expand Down
35 changes: 32 additions & 3 deletions Library/Homebrew/cmd/untap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,38 @@ def untap
args.named.to_installed_taps.each do |tap|
odie "Untapping #{tap} is not allowed" if tap.core_tap? && Homebrew::EnvConfig.no_install_from_api?

if Homebrew::EnvConfig.no_install_from_api? || (!tap.core_tap? && tap != "homebrew/cask")
installed_tap_formulae = Formula.installed.select { |formula| formula.tap == tap }
installed_tap_casks = Cask::Caskroom.casks.select { |cask| cask.tap == tap }
if Homebrew::EnvConfig.no_install_from_api? || (!tap.core_tap? && !tap.core_cask_tap?)
installed_formula_names = nil
installed_tap_formulae = tap.formula_names.map do |formula_name|
# initialise lazily in case there's no formulae in this tap
installed_formula_names ||= Set.new(Formula.installed_formula_names)
next unless installed_formula_names.include?(cask_token)

formula = begin
Formulary.factory("#{tap.name}/#{formula_name}")
rescue
# Don't blow up because of a single unavailable formula.
next
end

formula if formula.any_version_installed?
end.compact

installed_cask_tokens = nil
installed_tap_casks = tap.cask_tokens.map do |cask_token|
# initialise lazily in case there's no casks in this tap
installed_cask_tokens ||= Set.new(Cask::Caskroom.tokens)
next unless installed_cask_tokens.include?(cask_token)

cask = begin
Cask::CaskLoader.load("#{tap.name}/#{cask_token}")
rescue
# Don't blow up because of a single unavailable cask.
next
end

cask if cask.installed?
end.compact

if installed_tap_formulae.present? || installed_tap_casks.present?
installed_names = (installed_tap_formulae + installed_tap_casks.map(&:token)).join("\n")
Expand Down
6 changes: 6 additions & 0 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,12 @@ def self.racks
end
end

# An array of all currently installed formula names.
# @private
def self.installed_formula_names
racks.map(&:basename).map(&:to_s)
end

# An array of all installed {Formula}
# @private
def self.installed
Expand Down

0 comments on commit 9811606

Please sign in to comment.