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.
refactor into multiple modules
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
- .casks_to_uninstall(global: false, file: nil) ⇒ Object private
- .flatpaks_to_uninstall(global: false, file: nil) ⇒ Object private
- .formulae_to_uninstall(global: false, file: nil) ⇒ Object private
- .lookup_formula(formula) ⇒ Object private
- .reset! ⇒ Object private
- .run(global: false, file: nil, force: false, zap: false, dsl: nil, formulae: true, casks: true, taps: true, vscode: true, flatpak: true) ⇒ Object private
- .system_output_no_stderr(cmd, *args) ⇒ Object private
- .taps_to_untap(global: false, file: nil) ⇒ Object private
- .vscode_extensions_to_uninstall(global: false, file: nil) ⇒ Object private
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.
112 113 114 115 |
# File 'bundle/commands/cleanup.rb', line 112 def self.casks_to_uninstall(global: false, file: nil) require "bundle/cask_dumper" Homebrew::Bundle::CaskDumper.cask_names - kept_casks(global:, file:) end |
.flatpaks_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.
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'bundle/commands/cleanup.rb', line 231 def self.flatpaks_to_uninstall(global: false, file: nil) return [].freeze unless Bundle.flatpak_installed? require "bundle/brewfile" @dsl ||= Brewfile.read(global:, file:) kept_flatpaks = @dsl.entries.select { |e| e.type == :flatpak }.map(&:name) # To provide a graceful migration from `Brewfile`s that don't yet or # don't want to use `flatpak`: don't remove any flatpaks if we don't # find any in the `Brewfile`. return [].freeze if kept_flatpaks.empty? require "bundle/flatpak_dumper" current_flatpaks = Homebrew::Bundle::FlatpakDumper.packages current_flatpaks - kept_flatpaks 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.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'bundle/commands/cleanup.rb', line 117 def self.formulae_to_uninstall(global: false, file: nil) kept_formulae = self.kept_formulae(global:, file:) require "bundle/formula_dumper" require "bundle/formula_installer" current_formulae = Homebrew::Bundle::FormulaDumper.formulae current_formulae.reject! do |f| Homebrew::Bundle::FormulaInstaller.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 |
.lookup_formula(formula) ⇒ 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.
209 210 211 212 213 214 |
# File 'bundle/commands/cleanup.rb', line 209 def self.lookup_formula(formula) Formulary.factory(formula) rescue TapFormulaUnavailableError # ignore these as an unavailable formula implies there is no tap to worry about nil 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.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'bundle/commands/cleanup.rb', line 11 def self.reset! require "bundle/cask_dumper" require "bundle/formula_dumper" require "bundle/tap_dumper" require "bundle/vscode_extension_dumper" require "bundle/flatpak_dumper" require "bundle/brew_services" @dsl = nil @kept_casks = nil @kept_formulae = nil Homebrew::Bundle::CaskDumper.reset! Homebrew::Bundle::FormulaDumper.reset! Homebrew::Bundle::TapDumper.reset! Homebrew::Bundle::VscodeExtensionDumper.reset! Homebrew::Bundle::FlatpakDumper.reset! Homebrew::Bundle::BrewServices.reset! end |
.run(global: false, file: nil, force: false, zap: false, dsl: nil, formulae: true, casks: true, taps: true, vscode: true, flatpak: true) ⇒ 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.
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 109 110 |
# File 'bundle/commands/cleanup.rb', line 30 def self.run(global: false, file: nil, force: false, zap: false, dsl: nil, formulae: true, casks: true, taps: true, vscode: true, flatpak: true) @dsl ||= dsl casks = casks ? casks_to_uninstall(global:, file:) : [] formulae = formulae ? formulae_to_uninstall(global:, file:) : [] taps = taps ? taps_to_untap(global:, file:) : [] vscode_extensions = vscode ? vscode_extensions_to_uninstall(global:, file:) : [] flatpaks = flatpak ? flatpaks_to_uninstall(global:, file:) : [] 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? 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? Bundle.exchange_uid_if_needed! do vscode_extensions.each do |extension| Kernel.system(T.must(Bundle.which_vscode).to_s, "--uninstall-extension", extension) end end if flatpaks.any? flatpaks.each do |flatpak_name| Kernel.system "flatpak", "uninstall", "-y", "--system", flatpak_name end puts "Uninstalled #{flatpaks.size} flatpak#{"s" if flatpaks.size != 1}" 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 if vscode_extensions.any? puts "Would uninstall VSCode extensions:" puts Formatter.columns vscode_extensions would_uninstall = true end if flatpaks.any? puts "Would uninstall flatpaks:" puts Formatter.columns flatpaks 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.
248 249 250 |
# File 'bundle/commands/cleanup.rb', line 248 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.
197 198 199 200 201 202 203 204 205 206 207 |
# File 'bundle/commands/cleanup.rb', line 197 def self.taps_to_untap(global: false, file: nil) require "bundle/brewfile" require "bundle/tap_dumper" @dsl ||= Brewfile.read(global:, file:) kept_formulae = self.kept_formulae(global:, file:).filter_map { lookup_formula(_1) } kept_taps = @dsl.entries.select { |e| e.type == :tap }.map(&:name) kept_taps += kept_formulae.filter_map(&:tap).map(&:name) current_taps = Homebrew::Bundle::TapDumper.tap_names current_taps - kept_taps - IGNORED_TAPS end |
.vscode_extensions_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.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'bundle/commands/cleanup.rb', line 216 def self.vscode_extensions_to_uninstall(global: false, file: nil) require "bundle/brewfile" @dsl ||= Brewfile.read(global:, file:) kept_extensions = @dsl.entries.select { |e| e.type == :vscode }.map { |x| x.name.downcase } # To provide a graceful migration from `Brewfile`s that don't yet or # don't want to use `vscode`: don't remove any extensions if we don't # find any in the `Brewfile`. return [].freeze if kept_extensions.empty? require "bundle/vscode_extension_dumper" current_extensions = Homebrew::Bundle::VscodeExtensionDumper.extensions current_extensions - kept_extensions end |