cmd/untap: refactor logic into methods to facilitate testing

This commit is contained in:
apainintheneck 2024-03-10 13:06:13 -07:00
parent 819af3cbdd
commit 46a32f90c4

View File

@ -1,13 +1,11 @@
# typed: strict
# typed: true
# frozen_string_literal: true
require "cli/parser"
module Homebrew
module_function
sig { returns(CLI::Parser) }
def untap_args
def self.untap_args
Homebrew::CLI::Parser.new do
description <<~EOS
Remove a tapped formula repository.
@ -20,46 +18,15 @@ module Homebrew
end
sig { void }
def untap
def self.untap
args = untap_args.parse
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.core_cask_tap?)
installed_formula_names = T.let(nil, T.nilable(T::Set[String]))
installed_tap_formulae = tap.formula_names.filter_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?(formula_name.split("/").last)
formula = begin
Formulary.factory(formula_name)
rescue
# Don't blow up because of a single unavailable formula.
next
end
# Can't use Formula#any_version_installed? because it doesn't consider
# taps correctly.
formula if formula.installed_kegs.any? { |keg| keg.tab.tap == tap }
end
installed_cask_tokens = T.let(nil, T.nilable(T::Set[String]))
installed_tap_casks = tap.cask_tokens.filter_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.split("/").last)
cask = begin
Cask::CaskLoader.load(cask_token)
rescue
# Don't blow up because of a single unavailable cask.
next
end
cask if cask.installed?
end
installed_tap_formulae = installed_formulae_for(tap: tap)
installed_tap_casks = installed_casks_for(tap: tap)
if installed_tap_formulae.present? || installed_tap_casks.present?
installed_names = (installed_tap_formulae + installed_tap_casks.map(&:token)).join("\n")
@ -80,4 +47,48 @@ module Homebrew
tap.uninstall manual: true
end
end
sig { params(tap: Tap).returns(T::Array[Formula]) }
def self.installed_formulae_for(tap:)
tap.formula_names.filter_map do |formula_name|
next unless installed_formulae_names.include?(T.must(formula_name.split("/").last))
formula = begin
Formulary.factory(formula_name)
rescue
# Don't blow up because of a single unavailable formula.
next
end
# Can't use Formula#any_version_installed? because it doesn't consider
# taps correctly.
formula if formula.installed_kegs.any? { |keg| keg.tab.tap == tap }
end
end
sig { returns(T::Set[String]) }
def self.installed_formulae_names
@installed_formulae_names ||= Formula.installed_formula_names.to_set
end
sig { params(tap: Tap).returns(T::Array[Cask::Cask]) }
def self.installed_casks_for(tap:)
tap.cask_tokens.filter_map do |cask_token|
next unless installed_cask_tokens.include?(T.must(cask_token.split("/").last))
cask = begin
Cask::CaskLoader.load(cask_token)
rescue
# Don't blow up because of a single unavailable cask.
next
end
cask if cask.installed?
end
end
sig { returns(T::Set[String]) }
def self.installed_cask_tokens
@installed_cask_tokens ||= Cask::Caskroom.tokens.to_set
end
end