brew/Library/Homebrew/uninstall.rb

161 lines
5.1 KiB
Ruby
Raw Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
2020-11-01 14:45:11 -05:00
# frozen_string_literal: true
require "installed_dependents"
2020-11-01 14:45:11 -05:00
module Homebrew
# Helper module for uninstalling kegs.
module Uninstall
def self.uninstall_kegs(kegs_by_rack, casks: [], force: false, ignore_dependencies: false, named_args: [])
2020-11-01 14:45:11 -05:00
handle_unsatisfied_dependents(kegs_by_rack,
2024-03-07 16:20:20 +00:00
casks:,
ignore_dependencies:,
named_args:)
2020-11-01 14:45:11 -05:00
return if Homebrew.failed?
kegs_by_rack.each do |rack, kegs|
if force
name = rack.basename
if rack.directory?
puts "Uninstalling #{name}... (#{rack.abv})"
kegs.each do |keg|
keg.unlink
keg.uninstall
end
end
rm_pin rack
else
kegs.each do |keg|
begin
f = Formulary.from_rack(rack)
if f.pinned?
onoe "#{f.full_name} is pinned. You must unpin it to uninstall."
break # exit keg loop and move on to next rack
2020-11-01 14:45:11 -05:00
end
rescue
nil
end
keg.lock do
puts "Uninstalling #{keg}... (#{keg.abv})"
keg.unlink
keg.uninstall
rack = keg.rack
rm_pin rack
if rack.directory?
2020-11-04 18:53:03 -05:00
versions = rack.subdirs.map(&:basename)
puts <<~EOS
2023-02-27 21:33:16 -08:00
#{keg.name} #{versions.to_sentence} #{(versions.count == 1) ? "is" : "are"} still installed.
To remove all versions, run:
brew uninstall --force #{keg.name}
EOS
2020-11-01 14:45:11 -05:00
end
next unless f
paths = f.pkgetc.find.map(&:to_s) if f.pkgetc.exist?
if paths.present?
puts
opoo <<~EOS
The following #{f.name} configuration files have not been removed!
If desired, remove them manually with `rm -rf`:
#{paths.sort.uniq.join("\n ")}
EOS
end
unversioned_name = f.name.gsub(/@.+$/, "")
maybe_paths = Dir.glob("#{f.etc}/#{unversioned_name}*")
excluded_names = Homebrew::API::Formula.all_formulae.keys
maybe_paths = maybe_paths.reject do |path|
# Remove extension only if a file
# (f.e. directory with name "openssl@1.1" will be trimmed to "openssl@1")
basename = if File.directory?(path)
File.basename(path)
else
File.basename(path, ".*")
end
excluded_names.include?(basename)
end
2020-11-01 14:45:11 -05:00
maybe_paths -= paths if paths.present?
if maybe_paths.present?
puts
opoo <<~EOS
The following may be #{f.name} configuration files and have not been removed!
If desired, remove them manually with `rm -rf`:
#{maybe_paths.sort.uniq.join("\n ")}
EOS
end
end
end
end
2020-11-19 12:57:10 +01:00
end
rescue MultipleVersionsInstalledError => e
ofail e
ensure
# If we delete Cellar/newname, then Cellar/oldname symlink
# can become broken and we have to remove it.
if HOMEBREW_CELLAR.directory?
HOMEBREW_CELLAR.children.each do |rack|
rack.unlink if rack.symlink? && !rack.resolved_path_exists?
end
2020-11-01 14:45:11 -05:00
end
end
def self.handle_unsatisfied_dependents(kegs_by_rack, casks: [], ignore_dependencies: false, named_args: [])
2020-11-01 14:45:11 -05:00
return if ignore_dependencies
2020-11-04 18:53:03 -05:00
2020-11-01 14:45:11 -05:00
all_kegs = kegs_by_rack.values.flatten(1)
2024-03-07 16:20:20 +00:00
check_for_dependents(all_kegs, casks:, named_args:)
2020-11-01 14:45:11 -05:00
rescue MethodDeprecatedError
# Silently ignore deprecations when uninstalling.
nil
end
def self.check_for_dependents(kegs, casks: [], named_args: [])
2024-03-07 16:20:20 +00:00
return false unless (result = InstalledDependents.find_some_installed_dependents(kegs, casks:))
2020-11-04 18:53:03 -05:00
DependentsMessage.new(*result, named_args:).output
2020-11-01 14:45:11 -05:00
true
end
class DependentsMessage
attr_reader :reqs, :deps, :named_args
2020-11-04 18:53:03 -05:00
2020-11-01 14:45:11 -05:00
def initialize(requireds, dependents, named_args: [])
@reqs = requireds
@deps = dependents
@named_args = named_args
end
2020-11-04 18:53:03 -05:00
def output
ofail <<~EOS
Refusing to uninstall #{reqs.to_sentence}
because #{(reqs.count == 1) ? "it" : "they"} #{are_required_by_deps}.
You can override this and force removal with:
#{sample_command}
EOS
end
2020-11-01 14:45:11 -05:00
protected
2020-11-04 18:53:03 -05:00
2020-11-01 14:45:11 -05:00
def sample_command
"brew uninstall --ignore-dependencies #{named_args.join(" ")}"
end
2020-11-04 18:53:03 -05:00
2020-11-01 14:45:11 -05:00
def are_required_by_deps
2023-02-27 21:33:16 -08:00
"#{(reqs.count == 1) ? "is" : "are"} required by #{deps.to_sentence}, " \
"which #{(deps.count == 1) ? "is" : "are"} currently installed"
2020-11-01 14:45:11 -05:00
end
end
2020-11-04 18:53:03 -05:00
def self.rm_pin(rack)
2020-11-01 14:45:11 -05:00
Formulary.from_rack(rack).unpin
rescue
nil
end
end
end