Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cask/artifact/abstract_uninstall: allow wildcard entries for launchctl #14123

Merged
merged 10 commits into from
Dec 29, 2022
25 changes: 25 additions & 0 deletions Library/Homebrew/cask/artifact/abstract_uninstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,22 @@ def uninstall_early_script(directives, **options)
# :launchctl must come before :quit/:signal for cases where app would instantly re-launch
def uninstall_launchctl(*services, command: nil, **_)
booleans = [false, true]

all_services = []

# if launchctl item contains a wildcard, find matching process(es)
services.each do |service|
all_services.push(service)
next unless /\*/.match?(service)

find_launchctl_with_wildcard(service)
.map { |_, _, id| id }
.each do |match|
all_services.push(match)
end
end

all_services.each do |service|
ohai "Removing launchctl service #{service}"
booleans.each do |with_sudo|
plist_status = command.run(
Expand Down Expand Up @@ -268,6 +283,16 @@ def uninstall_login_item(*login_items, command: nil, upgrade: false, **_)
end
end

def find_launchctl_with_wildcard(search)
system_command!("/bin/launchctl", args: ["list"])
.stdout.lines.drop(1)
.map { |line| line.chomp.split("\t") }
.map { |pid, state, id| [pid.to_i, state.to_i, id] }
.select do |(pid, _, id)|
pid.nonzero? && /\A#{Regexp.escape(search).gsub("\\*", ".*")}\Z/.match?(id)
end
end

# :kext should be unloaded before attempting to delete the relevant file
def uninstall_kext(*kexts, command: nil, **_)
kexts.each do |kext|
Expand Down