|
| 1 | +# typed: strict |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "abstract_command" |
| 5 | +require "open3" |
| 6 | +require "rexml/document" |
| 7 | + |
| 8 | +module Homebrew |
| 9 | + module Cmd |
| 10 | + class ListIdsInPkgCmd < AbstractCommand |
| 11 | + cmd_args do |
| 12 | + usage_banner <<~EOS |
| 13 | + `list-ids-in-pkg` [`--lax`] <file.pkg> |
| 14 | +
|
| 15 | + Given a package file, extract a list of candidate Package IDs |
| 16 | + which may be useful in a Cask uninstall stanza, eg |
| 17 | +
|
| 18 | + uninstall pkgutil: "package.id.goes.here" |
| 19 | +
|
| 20 | + The given package file need not be installed. |
| 21 | +
|
| 22 | + The output of this script should be overly inclusive -- not |
| 23 | + every candidate package id in the output will be needed at |
| 24 | + uninstall time. |
| 25 | +
|
| 26 | + Package IDs designated by Apple or common development frameworks |
| 27 | + will be excluded from the output. This can be turned off with |
| 28 | + the -lax argument. |
| 29 | +
|
| 30 | + If a package id is already installed, it will be followed by |
| 31 | + a plus symbol '(+)' in the output. This can be verified via |
| 32 | + the command |
| 33 | +
|
| 34 | + /usr/sbin/pkgutil --pkg-info "package.id.goes.here" |
| 35 | +
|
| 36 | + See CONTRIBUTING.md and 'man pkgutil' for more information. |
| 37 | + EOS |
| 38 | + |
| 39 | + switch "-l", "--lax", description: <<~EOS |
| 40 | + Turn off filtering of Apple or common development |
| 41 | + framework package IDs from the output. |
| 42 | + EOS |
| 43 | + |
| 44 | + named_args :pkg, number: 1 |
| 45 | + |
| 46 | + hide_from_man_page! |
| 47 | + end |
| 48 | + |
| 49 | + sig { override.returns(T.nilable(String)) } |
| 50 | + def run |
| 51 | + pkg = Pathname args.named.last || Dir.pwd |
| 52 | + raise ArgumentError, "No such pkg file: #{pkg}" unless pkg.exist? |
| 53 | + |
| 54 | + pkgdir = pkg if pkg.directory? |
| 55 | + mktemp = Mktemp.new("list-apps-in-pkg", retain_in_cache: true) |
| 56 | + mktemp.quiet! |
| 57 | + pkgdir ||= mktemp.run(chdir: false) do |
| 58 | + Open3.capture3("/usr/sbin/pkgutil --expand '#{pkg}' #{it.tmpdir}/unpack") |
| 59 | + Pathname "#{it.tmpdir}/unpack" |
| 60 | + end |
| 61 | + info = pkgdir.glob("**/{Package,}Info{.plist,}") |
| 62 | + |
| 63 | + bundle_ids = info.flat_map do |
| 64 | + if it.extname == ".plist" |
| 65 | + Plist.parse_xml(it)["CFBundleIdentifier"] |
| 66 | + else |
| 67 | + xml = REXML::Document.new(it.read) |
| 68 | + attributes = "id", "identifier" |
| 69 | + REXML::XPath |
| 70 | + .match(xml.root, ". | ./bundle") |
| 71 | + .map(&:attributes) |
| 72 | + .reduce(&:merge) |
| 73 | + .values_at(*attributes) |
| 74 | + .map(&:value) |
| 75 | + end |
| 76 | + end |
| 77 | + filter = T.unsafe(args).lax? ? :itself : :reject |
| 78 | + puts bundle_ids |
| 79 | + .uniq.sort |
| 80 | + .send(filter) { it.start_with?("com.apple.") || it.end_with?("sparkle.finish-installation") } |
| 81 | + .map { |
| 82 | + installed, = Open3.capture3("/usr/sbin/pkgutil --pkg-info #{it}") |
| 83 | + installed.present? ? "#{it} (+)" : it |
| 84 | + } |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | +end |
0 commit comments