Module: Homebrew::Bundle::Commands::Cleanup Private

Defined in:
bundle/commands/cleanup.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Uninstalls formulae, casks, taps, VSCode extensions and Flatpak packages not listed in the Brewfile.

Constant Summary collapse

IGNORED_TAPS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

%w[homebrew/core].freeze

Class Method Summary collapse

Class Method Details

.casks_to_uninstall(global: false, file: nil) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Raises:

  • (ArgumentError)


124
125
126
127
128
129
# File 'bundle/commands/cleanup.rb', line 124

def self.casks_to_uninstall(global: false, file: nil)
  raise ArgumentError, "@dsl is unset!" unless @dsl

  require "bundle/cask"
  Homebrew::Bundle::Cask.cask_names - kept_casks(global:, file:)
end

.dslHomebrew::Bundle::Dsl?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



120
121
122
# File 'bundle/commands/cleanup.rb', line 120

def self.dsl
  @dsl
end

.formulae_to_uninstall(global: false, file: nil) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Raises:

  • (ArgumentError)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'bundle/commands/cleanup.rb', line 131

def self.formulae_to_uninstall(global: false, file: nil)
  raise ArgumentError, "@dsl is unset!" unless @dsl

  kept_formulae = self.kept_formulae(global:, file:)

  require "bundle/brew"
  current_formulae = Homebrew::Bundle::Brew.formulae
  current_formulae.reject! do |f|
    Homebrew::Bundle::Brew.formula_in_array?(f[:full_name], kept_formulae)
  end

  # Don't try to uninstall formulae with keepme references
  current_formulae.reject! do |f|
    Formula[f[:full_name]].installed_kegs.any? do |keg|
      keg.keepme_refs.present?
    end
  end
  current_formulae.map { |f| f[:full_name] }
end

.read_dsl_from_brewfile!(global: false, file: nil, dsl: nil) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



110
111
112
113
114
115
116
117
# File 'bundle/commands/cleanup.rb', line 110

def self.read_dsl_from_brewfile!(global: false, file: nil, dsl: nil)
  @dsl = if dsl
    dsl
  else
    require "bundle/brewfile"
    Brewfile.read(global:, file:)
  end
end

.reset!Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'bundle/commands/cleanup.rb', line 13

def self.reset!
  require "bundle/cask"
  require "bundle/brew"
  require "bundle/tap"
  require "bundle/brew_services"

  @dsl = nil
  @kept_casks = nil
  @kept_formulae = nil
  Homebrew::Bundle::Cask.reset!
  Homebrew::Bundle::Brew.reset!
  Homebrew::Bundle::Tap.reset!
  Homebrew::Bundle::Brew::Services.reset!
  Homebrew::Bundle.extensions.each(&:reset!)
end

.run(global: false, file: nil, force: false, zap: false, dsl: nil, formulae: true, casks: true, taps: true, extension_types: {}) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'bundle/commands/cleanup.rb', line 29

def self.run(global: false, file: nil, force: false, zap: false, dsl: nil,
             formulae: true, casks: true, taps: true, extension_types: {})
  read_dsl_from_brewfile!(global:, file:, dsl:)

  extension_types = Homebrew::Bundle.extensions.select(&:cleanup_supported?).to_h do |extension|
    [extension.type, true]
  end.merge(extension_types)
  casks = casks ? casks_to_uninstall(global:, file:) : []
  formulae = formulae ? formulae_to_uninstall(global:, file:) : []
  taps = taps ? taps_to_untap(global:, file:) : []
  cleanup_extensions = Homebrew::Bundle.extensions.select(&:cleanup_supported?).filter_map do |extension|
    next unless extension_types.fetch(extension.type, false)

    [extension, extension.cleanup_items(@dsl.entries)]
  end
  if force
    if casks.any?
      args = zap ? ["--zap"] : []
      Kernel.system HOMEBREW_BREW_FILE, "uninstall", "--cask", *args, "--force", *casks
      puts "Uninstalled #{casks.size} cask#{"s" if casks.size != 1}"
    end

    if formulae.any?
      # Mark Brewfile formulae as installed_on_request to prevent autoremove
      # from removing them when their dependents are uninstalled
      Homebrew::Bundle.mark_as_installed_on_request!(@dsl.entries)

      Kernel.system HOMEBREW_BREW_FILE, "uninstall", "--formula", "--force", *formulae
      puts "Uninstalled #{formulae.size} formula#{"e" if formulae.size != 1}"
    end

    Kernel.system HOMEBREW_BREW_FILE, "untap", *taps if taps.any?

    cleanup_extensions.each do |extension, items|
      next if items.empty?

      extension.cleanup!(items)
    end

    cleanup = system_output_no_stderr(HOMEBREW_BREW_FILE, "cleanup")
    puts cleanup unless cleanup.empty?
  else
    would_uninstall = false

    if casks.any?
      puts "Would uninstall casks:"
      puts Formatter.columns casks
      would_uninstall = true
    end

    if formulae.any?
      puts "Would uninstall formulae:"
      puts Formatter.columns formulae
      would_uninstall = true
    end

    if taps.any?
      puts "Would untap:"
      puts Formatter.columns taps
      would_uninstall = true
    end

    cleanup_extensions.each do |extension, items|
      next if items.empty?

      puts "Would uninstall #{extension.cleanup_heading}:"
      puts Formatter.columns items
      would_uninstall = true
    end

    cleanup = system_output_no_stderr(HOMEBREW_BREW_FILE, "cleanup", "--dry-run")
    unless cleanup.empty?
      puts "Would `brew cleanup`:"
      puts cleanup
    end

    puts "Run `brew bundle cleanup --force` to make these changes." if would_uninstall || !cleanup.empty?
    exit 1 if would_uninstall
  end
end

.system_output_no_stderr(cmd, *args) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



226
227
228
# File 'bundle/commands/cleanup.rb', line 226

def self.system_output_no_stderr(cmd, *args)
  IO.popen([cmd, *args], err: :close).read
end

.taps_to_untap(global: false, file: nil) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Raises:

  • (ArgumentError)


207
208
209
210
211
212
213
214
215
216
217
# File 'bundle/commands/cleanup.rb', line 207

def self.taps_to_untap(global: false, file: nil)
  raise ArgumentError, "@dsl is unset!" unless @dsl

  require "bundle/tap"

  kept_formulae = self.kept_formulae(global:, file:).filter_map { lookup_formula(it) }
  kept_taps = @dsl.entries.select { |e| e.type == :tap }.map(&:name)
  kept_taps += kept_formulae.filter_map(&:tap).map(&:name)
  current_taps = Homebrew::Bundle::Tap.tap_names
  current_taps - kept_taps - IGNORED_TAPS
end