untap: add new module to make these refactored methods easier to test

This commit is contained in:
apainintheneck 2024-03-10 13:21:40 -07:00
parent 46a32f90c4
commit ff495a2e44
2 changed files with 60 additions and 47 deletions

View File

@ -1,7 +1,8 @@
# typed: true
# typed: strict
# frozen_string_literal: true
require "cli/parser"
require "untap"
module Homebrew
sig { returns(CLI::Parser) }
@ -25,8 +26,8 @@ module Homebrew
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_tap_formulae = installed_formulae_for(tap: tap)
installed_tap_casks = installed_casks_for(tap: tap)
installed_tap_formulae = Untap.installed_formulae_for(tap:)
installed_tap_casks = Untap.installed_casks_for(tap:)
if installed_tap_formulae.present? || installed_tap_casks.present?
installed_names = (installed_tap_formulae + installed_tap_casks.map(&:token)).join("\n")
@ -47,48 +48,4 @@ 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

56
Library/Homebrew/untap.rb Normal file
View File

@ -0,0 +1,56 @@
# typed: true
# frozen_string_literal: true
module Homebrew
# Helpers for the `brew untap` command.
# @api private
module Untap
# All installed formulae currently available in a tap by formula full name.
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.freeze
end
private_class_method :installed_formulae_names
# All installed casks currently available in a tap by cask full name.
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.freeze
end
private_class_method :installed_cask_tokens
end
end