Class: Homebrew::Cmd::Bundle::CleanupSubcommand Private

Inherits:
AbstractSubcommand show all
Extended by:
Utils::Output::Mixin
Defined in:
bundle/subcommand/cleanup.rb,
sorbet/rbi/dsl/homebrew/cmd/bundle/cleanup_subcommand.rbi

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

Defined Under Namespace

Modules: Args

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

Instance Attribute Summary

Attributes inherited from AbstractSubcommand

#cleanup, #context, #quiet, #targets

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Output::Mixin

issue_reporting_message, odebug, odeprecated, odie, odisabled, ofail, oh1, oh1_title, ohai, ohai_title, onoe, opoo, opoo_once, opoo_outside_github_actions, opoo_without_github_actions_annotation, pretty_cannot_install, pretty_deprecated, pretty_disabled, pretty_duration, pretty_install_status, pretty_installed, pretty_uninstalled, pretty_unmarked, pretty_upgradable, pretty_warning

Methods inherited from AbstractSubcommand

args_module, command, define, define_all, #initialize, subcommand_name, subcommands_for

Constructor Details

This class inherits a constructor from Homebrew::AbstractSubcommand

Class Method Details

.casks_to_uninstall(global: false, file: nil) ⇒ Array<String>

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.

Parameters:

  • global (Boolean) (defaults to: false)
  • file (String, nil) (defaults to: nil)

Returns:

Raises:

  • (ArgumentError)


260
261
262
263
264
265
# File 'bundle/subcommand/cleanup.rb', line 260

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

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

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.

This method returns an undefined value.

Parameters:

  • global (Boolean) (defaults to: false)
  • file (String, nil) (defaults to: nil)
  • force (Boolean) (defaults to: false)
  • zap (Boolean) (defaults to: false)
  • dsl (Homebrew::Bundle::Dsl, nil) (defaults to: nil)
  • formulae (Boolean) (defaults to: true)
  • casks (Boolean) (defaults to: true)
  • taps (Boolean) (defaults to: true)
  • ask (Boolean) (defaults to: false)
  • extension_types (Homebrew::Bundle::ExtensionTypes) (defaults to: {})


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'bundle/subcommand/cleanup.rb', line 123

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

  cleanup_formulae = formulae
  cleanup_casks = casks
  cleanup_taps = taps
  extension_types = Homebrew::Bundle.extensions.select(&:cleanup_supported?).to_h do |extension|
    [extension.type, true]
  end.merge(extension_types)
  casks = if casks
    casks_to_uninstall(global:, file:)
  else
    []
  end
  formulae = if formulae
    formulae_to_uninstall(global:, file:)
  else
    []
  end
  taps = if taps
    taps_to_untap(global:, file:)
  else
    []
  end
  cleanup_extensions = Homebrew::Bundle.extensions.select(&:cleanup_supported?).filter_map do |extension|
    next unless extension_types.fetch(extension.type, false)
    raise ArgumentError, "dsl is unset!" unless @dsl

    [extension, extension.cleanup_items(@dsl.entries)]
  end
  if force
    dsl = @dsl
    raise ArgumentError, "dsl is unset!" unless dsl

    Homebrew::Trust.replace!(Homebrew::Bundle::Trust.entries(dsl.entries))

    # Protect Brewfile formulae from autoremove during uninstall and cleanup.
    Homebrew::Bundle.mark_as_installed_on_request!(dsl.entries)

    if casks.any?
      args = if zap
        ["--zap"]
      else
        []
      end
      if Kernel.system HOMEBREW_BREW_FILE, "uninstall", "--cask", *args, "--force", *casks
        puts "Uninstalled #{Utils.pluralize("cask", casks.size, include_count: true)}"
      else
        ofail "Failed to uninstall casks. Some may have been removed."
      end
    end

    if formulae.any?
      if Kernel.system HOMEBREW_BREW_FILE, "uninstall", "--formula", "--force", *formulae
        puts "Uninstalled #{Utils.pluralize("formula", formulae.size, include_count: true)}"
      else
        ofail "Failed to uninstall formulae. Some may have been removed."
      end
    end

    if taps.any? && !Kernel.system(HOMEBREW_BREW_FILE, "untap", *taps)
      ofail "Failed to untap. Some taps may have been removed."
    end

    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.map { |item| extension.cleanup_item_name(item) }
      would_uninstall = true
    end

    would_cleanup = Cleanup.printed_dry_run_output?(Cleanup.dry_run_output(quiet: true))
    would_change = would_uninstall || would_cleanup

    # `Ask.confirm?` only prints a prompt on a TTY; when it does, don't
    # also tell the user to rerun with `--force`.
    if ask && would_change && Homebrew::Ask.confirm?(action: "cleanup")
      cleanup(global:, file:, force: true, zap:, dsl: @dsl, formulae: cleanup_formulae, casks: cleanup_casks,
              taps: cleanup_taps, extension_types:)
      return
    end

    puts "Run `brew bundle cleanup --force` to make these changes." if would_change
    exit 1 if would_uninstall
  end
end

.dsl ⇒ Homebrew::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:



255
256
257
# File 'bundle/subcommand/cleanup.rb', line 255

def self.dsl
  T.let(@dsl, T.nilable(Homebrew::Bundle::Dsl))
end

.formulae_to_uninstall(global: false, file: nil) ⇒ Array<String>

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.

Parameters:

  • global (Boolean) (defaults to: false)
  • file (String, nil) (defaults to: nil)

Returns:

Raises:

  • (ArgumentError)


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'bundle/subcommand/cleanup.rb', line 268

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) ⇒ void

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.

This method returns an undefined value.

Parameters:



242
243
244
245
246
247
248
249
250
251
252
# File 'bundle/subcommand/cleanup.rb', line 242

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

.reset! ⇒ void

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.

This method returns an undefined value.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'bundle/subcommand/cleanup.rb', line 102

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

  @dsl = T.let(nil, T.nilable(Homebrew::Bundle::Dsl))
  @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

.system_output_no_stderr(cmd, *args) ⇒ String

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.

Parameters:

Returns:



390
391
392
# File 'bundle/subcommand/cleanup.rb', line 390

def self.system_output_no_stderr(cmd, *args)
  Utils.safe_popen_read(cmd, *args, err: File::NULL)
end

.taps_to_untap(global: false, file: nil) ⇒ Array<String>

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.

Parameters:

  • global (Boolean) (defaults to: false)
  • file (String, nil) (defaults to: nil)

Returns:

Raises:

  • (ArgumentError)


361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'bundle/subcommand/cleanup.rb', line 361

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 += @dsl.entries.filter_map do |entry|
    case entry.type
    when :brew
      Utils.tap_from_full_name(entry.name)
    when :cask
      Utils.tap_from_full_name(T.cast(entry.options.fetch(:full_name, entry.name), String))
    end
  end
  kept_taps += kept_formulae.filter_map(&:tap).map(&:name)
  current_taps = Homebrew::Bundle::Tap.tap_names
  current_taps - kept_taps - IGNORED_TAPS
end

Instance Method Details

#args ⇒ T.all(Homebrew::Cmd::Bundle::Args, Homebrew::Cmd::Bundle::CleanupSubcommand::Args)

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.



10
# File 'sorbet/rbi/dsl/homebrew/cmd/bundle/cleanup_subcommand.rbi', line 10

def args; end

#run ⇒ void

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.

This method returns an undefined value.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'bundle/subcommand/cleanup.rb', line 80

def run
  core_type_options = context.core_type_options(args, "cleanup", all: args.all?)
  self.class.cleanup(
    global:          context.global,
    file:            context.file,
    force:           context.force,
    zap:             context.zap,
    ask:             context.ask || !context.force,
    formulae:        core_type_options.fetch(:formulae),
    casks:           core_type_options.fetch(:casks),
    taps:            core_type_options.fetch(:taps),
    extension_types: context.extensions.select(&:cleanup_supported?).to_h do |extension|
      [
        extension.type,
        !context.extension_disabled?(args, extension) &&
          (context.extension_selected?(args, extension) || args.all? || context.no_type_args),
      ]
    end,
  )
end