Skip to content

Commit

Permalink
Merge pull request #1495 from tt/keep-implicit-taps
Browse files Browse the repository at this point in the history
Keep implicit taps
  • Loading branch information
MikeMcQuaid authored Nov 15, 2024
2 parents 709f68c + f958db7 commit 144757e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
30 changes: 20 additions & 10 deletions lib/bundle/commands/cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -90,24 +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_cask_formula_dependencies = Bundle::CaskDumper.formula_dependencies(kept_casks)
kept_formulae += kept_cask_formula_dependencies
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

Expand Down Expand Up @@ -145,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
Expand Down
2 changes: 1 addition & 1 deletion spec/bundle/commands/cleanup_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion spec/stub/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(?<tap>(?:[^/]+)/(?:[^/]+))/(?:[\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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 144757e

Please sign in to comment.