|
| 1 | +# typed: strict |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "abstract_command" |
| 5 | +require "open3" |
| 6 | + |
| 7 | +module Homebrew |
| 8 | + module Cmd |
| 9 | + class ListInstalledLaunchjobIdsCmd < AbstractCommand |
| 10 | + cmd_args do |
| 11 | + description <<~EOS |
| 12 | + List all installed launchjob IDs, which may be useful |
| 13 | + in a Cask uninstall stanza, e.g.: |
| 14 | +
|
| 15 | + uninstall launchctl: "job.id.goes.here" |
| 16 | +
|
| 17 | + Launchctl jobs attributed to Apple will be omitted. |
| 18 | +
|
| 19 | + If a launchctl job is currently loaded, and visible to the current |
| 20 | + user, it will be followed by a plus symbol '(+)' in the output. |
| 21 | + This can be verified via the command: |
| 22 | +
|
| 23 | + /bin/launchctl list 'job.id.goes.here' |
| 24 | +
|
| 25 | + See CONTRIBUTING.md and 'man launchctl' for more information. |
| 26 | + EOS |
| 27 | + |
| 28 | + named_args :none |
| 29 | + |
| 30 | + hide_from_man_page! |
| 31 | + end |
| 32 | + |
| 33 | + sig { override.returns(T.nilable(String)) } |
| 34 | + def run |
| 35 | + loaded = list_loaded_launchjob_ids |
| 36 | + pattern = "{#{Dir.home},}/Library/Launch{Agents,Daemons}/**.plist" |
| 37 | + puts Pathname |
| 38 | + .glob(pattern) |
| 39 | + .filter(&:readable?) |
| 40 | + .filter_map { method(:read_label).call(it) } |
| 41 | + .reject { it.start_with? "com.apple." } |
| 42 | + .uniq.sort |
| 43 | + .map { loaded.include?(it) ? "#{it} (+)" : it } |
| 44 | + end |
| 45 | + |
| 46 | + sig { params(plist: Pathname).returns(T.nilable(String)) } |
| 47 | + def read_label(plist) |
| 48 | + xml = plist.read |
| 49 | + if xml.start_with? "bplist" |
| 50 | + xml, _, status = Open3.capture3("/usr/bin/plutil -convert xml1 -o - '#{plist}'") |
| 51 | + return unless status.success? |
| 52 | + end |
| 53 | + Plist.parse_xml(xml, marshal: false)["Label"] |
| 54 | + end |
| 55 | + |
| 56 | + sig { returns(T::Array[T.nilable(String)]) } |
| 57 | + def list_loaded_launchjob_ids |
| 58 | + loaded, = Open3.capture3("/bin/launchctl list") |
| 59 | + loaded.lines.map { it.split(/\s+/).last } |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | +end |
0 commit comments