mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00

- 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 ```
133 lines
3.9 KiB
Ruby
133 lines
3.9 KiB
Ruby
# typed: true # rubocop:todo Sorbet/StrictSigil
|
|
# frozen_string_literal: true
|
|
|
|
require "cask/macos"
|
|
|
|
module Cask
|
|
# Helper class for uninstalling `.pkg` installers.
|
|
class Pkg
|
|
sig { params(regexp: String, command: T.class_of(SystemCommand)).returns(T::Array[Pkg]) }
|
|
def self.all_matching(regexp, command)
|
|
command.run("/usr/sbin/pkgutil", args: ["--pkgs=#{regexp}"]).stdout.split("\n").map do |package_id|
|
|
new(package_id.chomp, command)
|
|
end
|
|
end
|
|
|
|
sig { returns(String) }
|
|
attr_reader :package_id
|
|
|
|
sig { params(package_id: String, command: T.class_of(SystemCommand)).void }
|
|
def initialize(package_id, command = SystemCommand)
|
|
@package_id = package_id
|
|
@command = command
|
|
end
|
|
|
|
sig { void }
|
|
def uninstall
|
|
unless pkgutil_bom_files.empty?
|
|
odebug "Deleting pkg files"
|
|
@command.run!(
|
|
"/usr/bin/xargs",
|
|
args: ["-0", "--", "/bin/rm", "--"],
|
|
input: pkgutil_bom_files.join("\0"),
|
|
sudo: true,
|
|
sudo_as_root: true,
|
|
)
|
|
end
|
|
|
|
unless pkgutil_bom_specials.empty?
|
|
odebug "Deleting pkg symlinks and special files"
|
|
@command.run!(
|
|
"/usr/bin/xargs",
|
|
args: ["-0", "--", "/bin/rm", "--"],
|
|
input: pkgutil_bom_specials.join("\0"),
|
|
sudo: true,
|
|
sudo_as_root: true,
|
|
)
|
|
end
|
|
|
|
unless pkgutil_bom_dirs.empty?
|
|
odebug "Deleting pkg directories"
|
|
rmdir(deepest_path_first(pkgutil_bom_dirs))
|
|
end
|
|
|
|
rmdir(root) unless MacOS.undeletable?(root)
|
|
|
|
forget
|
|
end
|
|
|
|
sig { void }
|
|
def forget
|
|
odebug "Unregistering pkg receipt (aka forgetting)"
|
|
@command.run!(
|
|
"/usr/sbin/pkgutil",
|
|
args: ["--forget", package_id],
|
|
sudo: true,
|
|
sudo_as_root: true,
|
|
)
|
|
end
|
|
|
|
sig { returns(T::Array[Pathname]) }
|
|
def pkgutil_bom_files
|
|
@pkgutil_bom_files ||= pkgutil_bom_all.select(&:file?) - pkgutil_bom_specials
|
|
end
|
|
|
|
sig { returns(T::Array[Pathname]) }
|
|
def pkgutil_bom_specials
|
|
@pkgutil_bom_specials ||= pkgutil_bom_all.select { special?(_1) }
|
|
end
|
|
|
|
sig { returns(T::Array[Pathname]) }
|
|
def pkgutil_bom_dirs
|
|
@pkgutil_bom_dirs ||= pkgutil_bom_all.select(&:directory?) - pkgutil_bom_specials
|
|
end
|
|
|
|
sig { returns(T::Array[Pathname]) }
|
|
def pkgutil_bom_all
|
|
@pkgutil_bom_all ||= @command.run!("/usr/sbin/pkgutil", args: ["--files", package_id])
|
|
.stdout
|
|
.split("\n")
|
|
.map { |path| root.join(path) }
|
|
.reject { MacOS.undeletable?(_1) }
|
|
end
|
|
|
|
sig { returns(Pathname) }
|
|
def root
|
|
@root ||= Pathname.new(info.fetch("volume")).join(info.fetch("install-location"))
|
|
end
|
|
|
|
def info
|
|
@info ||= @command.run!("/usr/sbin/pkgutil", args: ["--pkg-info-plist", package_id])
|
|
.plist
|
|
end
|
|
|
|
private
|
|
|
|
sig { params(path: Pathname).returns(T::Boolean) }
|
|
def special?(path)
|
|
path.symlink? || path.chardev? || path.blockdev?
|
|
end
|
|
|
|
# Helper script to delete empty directories after deleting `.DS_Store` files and broken symlinks.
|
|
# Needed in order to execute all file operations with `sudo`.
|
|
RMDIR_SH = (HOMEBREW_LIBRARY_PATH/"cask/utils/rmdir.sh").freeze
|
|
private_constant :RMDIR_SH
|
|
|
|
sig { params(path: T.any(Pathname, T::Array[Pathname])).void }
|
|
def rmdir(path)
|
|
@command.run!(
|
|
"/usr/bin/xargs",
|
|
args: ["-0", "--", RMDIR_SH.to_s],
|
|
input: Array(path).join("\0"),
|
|
sudo: true,
|
|
sudo_as_root: true,
|
|
)
|
|
end
|
|
|
|
sig { params(paths: T::Array[Pathname]).returns(T::Array[Pathname]) }
|
|
def deepest_path_first(paths)
|
|
paths.sort_by { |path| -path.to_s.split(File::SEPARATOR).count }
|
|
end
|
|
end
|
|
end
|