From 57970c38186ea58a7282c93f5d3ba885b190a6df Mon Sep 17 00:00:00 2001 From: Troels Thomsen Date: Mon, 11 Nov 2024 08:51:39 +0100 Subject: [PATCH 1/5] Extract tap name --- spec/stub/formula.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spec/stub/formula.rb b/spec/stub/formula.rb index 71abb55cd..6a96ab26c 100644 --- a/spec/stub/formula.rb +++ b/spec/stub/formula.rb @@ -3,10 +3,16 @@ require "ostruct" require "pathname" +# This pattern is a slight remix of `HOMEBREW_TAP_FORMULA_NAME_REGEX` and +# `HOMEBREW_TAP_FORMULA_REGEX` +# https://github.com/Homebrew/brew/blob/4.4.4/Library/Homebrew/tap_constants.rb#L4-L10 +HOMEBREW_TAP_NAME_REGEX = %r{\A(?(?:[^/]+)/(?:[^/]+))/(?:[\w+\-.@]+)\Z} + class Formula def initialize(name) @prefix = Pathname("/usr/local") @name = name + @tap_name = (match = HOMEBREW_TAP_NAME_REGEX.match(name)) && match[:tap] end def opt_prefix @@ -92,7 +98,7 @@ def any_installed_prefix end def tap - OpenStruct.new(official?: true) + OpenStruct.new(official?: true, name: @tap_name) end def stable From c828368fef0166b644c9c933871fb9d81efe52cd Mon Sep 17 00:00:00 2001 From: Troels Thomsen Date: Mon, 11 Nov 2024 08:51:53 +0100 Subject: [PATCH 2/5] Verify that implicit tap is kept --- spec/bundle/commands/cleanup_command_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/bundle/commands/cleanup_command_spec.rb b/spec/bundle/commands/cleanup_command_spec.rb index 8f68a2f47..149cb5ec0 100644 --- a/spec/bundle/commands/cleanup_command_spec.rb +++ b/spec/bundle/commands/cleanup_command_spec.rb @@ -68,7 +68,7 @@ end it "computes which tap to untap" do - allow(Bundle::TapDumper).to receive(:tap_names).and_return(%w[z homebrew/bundle homebrew/core]) + allow(Bundle::TapDumper).to receive(:tap_names).and_return(%w[z homebrew/bundle homebrew/core homebrew/tap]) expect(described_class.taps_to_untap).to eql(%w[z]) end From eaed8c4dfff7da63f218531334711dcc74037827 Mon Sep 17 00:00:00 2001 From: Troels Thomsen Date: Thu, 14 Nov 2024 11:18:57 +0100 Subject: [PATCH 3/5] Inline method call --- lib/bundle/commands/cleanup.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/bundle/commands/cleanup.rb b/lib/bundle/commands/cleanup.rb index 1e82149ed..32507d68a 100644 --- a/lib/bundle/commands/cleanup.rb +++ b/lib/bundle/commands/cleanup.rb @@ -92,8 +92,7 @@ def casks_to_uninstall(global: false, file: nil) def formulae_to_uninstall(global: false, file: nil) @dsl ||= Brewfile.read(global:, file:) kept_formulae = @dsl.entries.select { |e| e.type == :brew }.map(&:name) - kept_cask_formula_dependencies = Bundle::CaskDumper.formula_dependencies(kept_casks) - kept_formulae += kept_cask_formula_dependencies + kept_formulae += Bundle::CaskDumper.formula_dependencies(kept_casks) kept_formulae.map! do |f| Bundle::BrewDumper.formula_aliases[f] || Bundle::BrewDumper.formula_oldnames[f] || From 228ef40c2819dd84b033cf540befeecac8a6db8b Mon Sep 17 00:00:00 2001 From: Troels Thomsen Date: Thu, 14 Nov 2024 11:19:09 +0100 Subject: [PATCH 4/5] Extract method --- lib/bundle/commands/cleanup.rb | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/bundle/commands/cleanup.rb b/lib/bundle/commands/cleanup.rb index 32507d68a..5176b7d8c 100644 --- a/lib/bundle/commands/cleanup.rb +++ b/lib/bundle/commands/cleanup.rb @@ -11,6 +11,7 @@ module Cleanup def reset! @dsl = nil @kept_casks = nil + @kept_formulae = nil Bundle::CaskDumper.reset! Bundle::BrewDumper.reset! Bundle::TapDumper.reset! @@ -90,23 +91,31 @@ def casks_to_uninstall(global: false, file: nil) end def formulae_to_uninstall(global: false, file: nil) - @dsl ||= Brewfile.read(global:, file:) - kept_formulae = @dsl.entries.select { |e| e.type == :brew }.map(&:name) - kept_formulae += Bundle::CaskDumper.formula_dependencies(kept_casks) - kept_formulae.map! do |f| - Bundle::BrewDumper.formula_aliases[f] || - Bundle::BrewDumper.formula_oldnames[f] || - f - end + kept_formulae = self.kept_formulae(global:, file:) current_formulae = Bundle::BrewDumper.formulae - kept_formulae += recursive_dependencies(current_formulae, kept_formulae) current_formulae.reject! do |f| Bundle::BrewInstaller.formula_in_array?(f[:full_name], kept_formulae) end current_formulae.map { |f| f[:full_name] } end + def kept_formulae(global: false, file: nil) + @kept_formulae ||= begin + @dsl ||= Brewfile.read(global:, file:) + + kept_formulae = @dsl.entries.select { |e| e.type == :brew }.map(&:name) + kept_formulae += Bundle::CaskDumper.formula_dependencies(kept_casks) + kept_formulae.map! do |f| + Bundle::BrewDumper.formula_aliases[f] || + Bundle::BrewDumper.formula_oldnames[f] || + f + end + + kept_formulae + recursive_dependencies(Bundle::BrewDumper.formulae, kept_formulae) + end + end + def kept_casks(global: false, file: nil) return @kept_casks if @kept_casks From f958db771f0c177a6846c3dd76aa2125dfc8b050 Mon Sep 17 00:00:00 2001 From: Troels Thomsen Date: Mon, 11 Nov 2024 08:52:09 +0100 Subject: [PATCH 5/5] Keep implicit taps --- lib/bundle/commands/cleanup.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/bundle/commands/cleanup.rb b/lib/bundle/commands/cleanup.rb index 5176b7d8c..a5c06c645 100644 --- a/lib/bundle/commands/cleanup.rb +++ b/lib/bundle/commands/cleanup.rb @@ -153,7 +153,9 @@ def recursive_dependencies(current_formulae, formulae_names, top_level: true) def taps_to_untap(global: false, file: nil) @dsl ||= Brewfile.read(global:, file:) + kept_formulae = self.kept_formulae(global:, file:).map(&Formulary.method(:factory)) kept_taps = @dsl.entries.select { |e| e.type == :tap }.map(&:name) + kept_taps += kept_formulae.filter_map(&:tap).map(&:name) current_taps = Bundle::TapDumper.tap_names current_taps - kept_taps - IGNORED_TAPS end