cmd/untap: refactor logic into methods to facilitate testing
This commit is contained in:
parent
819af3cbdd
commit
46a32f90c4
@ -1,13 +1,11 @@
|
|||||||
# typed: strict
|
# typed: true
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "cli/parser"
|
require "cli/parser"
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
module_function
|
|
||||||
|
|
||||||
sig { returns(CLI::Parser) }
|
sig { returns(CLI::Parser) }
|
||||||
def untap_args
|
def self.untap_args
|
||||||
Homebrew::CLI::Parser.new do
|
Homebrew::CLI::Parser.new do
|
||||||
description <<~EOS
|
description <<~EOS
|
||||||
Remove a tapped formula repository.
|
Remove a tapped formula repository.
|
||||||
@ -20,46 +18,15 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
|
|
||||||
sig { void }
|
sig { void }
|
||||||
def untap
|
def self.untap
|
||||||
args = untap_args.parse
|
args = untap_args.parse
|
||||||
|
|
||||||
args.named.to_installed_taps.each do |tap|
|
args.named.to_installed_taps.each do |tap|
|
||||||
odie "Untapping #{tap} is not allowed" if tap.core_tap? && Homebrew::EnvConfig.no_install_from_api?
|
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?)
|
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 = installed_formulae_for(tap: tap)
|
||||||
installed_tap_formulae = tap.formula_names.filter_map do |formula_name|
|
installed_tap_casks = installed_casks_for(tap: tap)
|
||||||
# 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
|
|
||||||
|
|
||||||
if installed_tap_formulae.present? || installed_tap_casks.present?
|
if installed_tap_formulae.present? || installed_tap_casks.present?
|
||||||
installed_names = (installed_tap_formulae + installed_tap_casks.map(&:token)).join("\n")
|
installed_names = (installed_tap_formulae + installed_tap_casks.map(&:token)).join("\n")
|
||||||
@ -80,4 +47,48 @@ module Homebrew
|
|||||||
tap.uninstall manual: true
|
tap.uninstall manual: true
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user