Class: Homebrew::Cmd::UninstallCmd Private

Inherits:
AbstractCommand show all
Defined in:
cmd/uninstall.rb,
sorbet/rbi/dsl/homebrew/cmd/uninstall_cmd.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

Classes: Args

Instance Method Summary collapse

Methods inherited from AbstractCommand

command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?

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_removed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#args ⇒ Homebrew::Cmd::UninstallCmd::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/uninstall_cmd.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.



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
111
112
113
114
115
116
117
118
119
120
121
122
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
# File 'cmd/uninstall.rb', line 46

def run
  method = args.force? ? :kegs : :default_kegs
  results = args.named.to_formulae_and_casks_and_unavailable(method:)

  unavailable_errors = T.let([], T::Array[T.any(FormulaOrCaskUnavailableError, NoSuchKegError)])
  all_kegs = T.let([], T::Array[Keg])
  casks = T.let([], T::Array[Cask::Cask])
  trusted_items_to_remove = T.let([], T::Array[[Symbol, String]])

  results.each do |item|
    case item
    when FormulaOrCaskUnavailableError, NoSuchKegError
      unavailable_errors << item
    when Cask::Cask
      casks << item
      trusted_items_to_remove << [:cask, item.full_name]
    when Keg
      all_kegs << item
      single_keg_tap = item.tab.tap
      trusted_items_to_remove << [:formula, "#{single_keg_tap.name}/#{item.name}"] if single_keg_tap
    when Array
      all_kegs += item
      item.each do |keg|
        array_keg_tap = keg.tab.tap
        trusted_items_to_remove << [:formula, "#{array_keg_tap.name}/#{keg.name}"] if array_keg_tap
      end
    end
  end

  return if all_kegs.blank? && casks.blank? && unavailable_errors.blank?

  dry_run = args.dry_run?
  kegs_by_rack = all_kegs.group_by(&:rack)
  if dry_run && !args.ignore_dependencies?
    Cask::Uninstall.check_dependent_casks(*casks, named_args: args.named)
    return if Homebrew.failed?
  end

  kegs_to_uninstall = Uninstall.uninstall_kegs(
    kegs_by_rack,
    casks:,
    force:               args.force?,
    ignore_dependencies: args.ignore_dependencies?,
    named_args:          args.named,
    dry_run:,
  )
  return if dry_run && kegs_to_uninstall.nil?

  if !dry_run && !args.ignore_dependencies?
    Cask::Uninstall.check_dependent_casks(*casks, named_args: args.named)
  end

  return if Homebrew.failed? && !dry_run

  casks_to_uninstall = T.let([], T::Array[Cask::Cask])
  begin
    if args.zap?
      caught_exceptions = []

      casks.each do |cask|
        odebug "Zapping Cask #{cask}"

        if !cask.installed? && !args.force?
          if dry_run
            ofail Cask::CaskNotInstalledError.new(cask)
            next
          end

          raise Cask::CaskNotInstalledError, cask
        end

        next unless Cask::Uninstall.unpin_for_removal?(cask, force: args.force?, dry_run:)

        Cask::Installer.new(cask, verbose: args.verbose?, force: args.force?).zap(dry_run:)
        casks_to_uninstall << cask
      rescue => e
        if dry_run
          ofail e
        else
          caught_exceptions << e
        end
        next
      end

      if caught_exceptions.count > 1
        raise Cask::MultipleCaskErrors, caught_exceptions
      elsif caught_exceptions.one?
        raise caught_exceptions.fetch(0)
      end
    else
      casks_to_uninstall = Cask::Uninstall.uninstall_casks(
        *casks,
        verbose: args.verbose?,
        force:   args.force?,
        dry_run:,
      )
    end
  rescue => e
    ofail e
  end

  if dry_run
    if (kegs_to_uninstall.present? || casks_to_uninstall.present?) && !Homebrew::EnvConfig.no_autoremove?
      require "cleanup"
      Cleanup.autoremove(dry_run: true, removed_kegs: kegs_to_uninstall, removed_casks: casks_to_uninstall)
    end

    unavailable_errors.each { |e| ofail e } unless args.force?
    return
  end

  trusted_items_to_remove.uniq.each do |type, name|
    next unless (tap_name = Utils.tap_from_full_name(name))
    next if Homebrew::Trust.trusted?(:tap, tap_name)

    Homebrew::Trust.untrust!(type, name)
  end

  if ENV["HOMEBREW_AUTOREMOVE"].present?
    opoo "`$HOMEBREW_AUTOREMOVE` is now a no-op as it is the default behaviour. " \
         "Set `HOMEBREW_NO_AUTOREMOVE=1` to disable it."
  end
  unless Homebrew::EnvConfig.no_autoremove?
    require "cleanup"
    Cleanup.autoremove
  end

  unavailable_errors.each { |e| ofail e } unless args.force?
end