|
| 1 | +#: * `list-ids-in-pkg` [ -lax ] <file.pkg> |
| 2 | +#: |
| 3 | +#: Given a package file, extract a list of candidate Package IDs |
| 4 | +#: which may be useful in a Cask uninstall stanza, eg |
| 5 | +#: |
| 6 | +#: uninstall pkgutil: "package.id.goes.here" |
| 7 | +#: |
| 8 | +#: The given package file need not be installed. |
| 9 | +#: |
| 10 | +#: The output of this script should be overly inclusive -- not |
| 11 | +#: every candidate package id in the output will be needed at |
| 12 | +#: uninstall time. |
| 13 | +#: |
| 14 | +#: Package IDs designated by Apple or common development frameworks |
| 15 | +#: will be excluded from the output. This can be turned off with |
| 16 | +#: the -lax argument. |
| 17 | +#: |
| 18 | +#: If a package id is already installed, it will be followed by |
| 19 | +#: a plus symbol '(+)' in the output. This can be verified via |
| 20 | +#: the command |
| 21 | +#: |
| 22 | +#: /usr/sbin/pkgutil --pkg-info "package.id.goes.here" |
| 23 | +#: |
| 24 | +#: Arguments |
| 25 | +#: |
| 26 | +#: -lax Turn off filtering of Apple or common development |
| 27 | +#: framework package IDs from the output. |
| 28 | +#: |
| 29 | +#: -h, --help Show this message. |
| 30 | +#: |
| 31 | +#: See CONTRIBUTING.md and 'man pkgutil' for more information. |
| 32 | + |
| 33 | +set -e # exit on any uncaught error |
| 34 | +set +o histexpand # don't expand history expressions |
| 35 | +shopt -s nocasematch # case-insensitive regular expressions |
| 36 | + |
| 37 | +opt_lax='' |
| 38 | +opt_pkg='' |
| 39 | +pkgdir='' |
| 40 | + |
| 41 | +# prefer GNU xargs |
| 42 | +xargs="$(/usr/bin/which gxargs || printf '/usr/bin/xargs')" |
| 43 | + |
| 44 | +warn() { |
| 45 | + local message="$@" |
| 46 | + message="${message//\\t/$'\011'}" |
| 47 | + message="${message//\\n/$'\012'}" |
| 48 | + message="${message%"${message##*[![:space:]]}"}" |
| 49 | + printf "%s\n" "$message" 1>&2 |
| 50 | +} |
| 51 | + |
| 52 | +die() { |
| 53 | + warn "$@" |
| 54 | + exit 1 |
| 55 | +} |
| 56 | + |
| 57 | +bundle_id_source_1 () { |
| 58 | + /usr/bin/find "$pkgdir" -name PackageInfo -print0 | |
| 59 | + "$xargs" -0 /usr/bin/perl -0777 -ne \ |
| 60 | + 'while (m{<pkg-info.*?\sid(?:entifier)?\s*=\s*"([^"]*?)"}sg) { print "$1\n" }' |
| 61 | +} |
| 62 | + |
| 63 | +bundle_id_source_2 () { |
| 64 | + /usr/bin/find "$pkgdir" -name Info.plist -print0 | |
| 65 | + "$xargs" -0 -I {} /usr/libexec/PlistBuddy {} -c 'Print :CFBundleIdentifier' |
| 66 | +} |
| 67 | + |
| 68 | +merge_sources () { |
| 69 | + /usr/bin/sort | /usr/bin/uniq |
| 70 | +} |
| 71 | + |
| 72 | +clean_sources () { |
| 73 | + /usr/bin/egrep -v '^com\.apple\.' | |
| 74 | + /usr/bin/egrep -v 'sparkle\.finish-installation$' |
| 75 | +} |
| 76 | + |
| 77 | +mark_up_sources () { |
| 78 | + /usr/bin/perl -pe 's{\n}{\000}sg' | |
| 79 | + "$xargs" -0 -I{} -n1 /bin/bash -c \ |
| 80 | + 'printf "{}"; /usr/sbin/pkgutil --pkg-info "{}" >/dev/null 2>&1 && printf " (+)"; printf "\n"' |
| 81 | +} |
| 82 | + |
| 83 | +process_args () { |
| 84 | + local arg |
| 85 | + if [[ "$#" -eq 0 ]]; then |
| 86 | + die "ERROR: A file argument is required" |
| 87 | + else |
| 88 | + opt_pkg="${!#}" # last arg |
| 89 | + fi |
| 90 | + for arg in "$@"; do |
| 91 | + if [[ $arg =~ ^-+lax$ ]]; then |
| 92 | + opt_lax='true' |
| 93 | + elif [[ "$arg" = "$opt_pkg" ]]; then |
| 94 | + true |
| 95 | + else |
| 96 | + die "ERROR: Unknown argument '$arg'" |
| 97 | + fi |
| 98 | + done |
| 99 | + if [[ -h "$opt_pkg" ]]; then |
| 100 | + opt_pkg="$(/usr/bin/readlink "$opt_pkg")" |
| 101 | + fi |
| 102 | + if ! [[ -e "$opt_pkg" ]]; then |
| 103 | + die "ERROR: No such pkg file: '$opt_pkg'" |
| 104 | + fi |
| 105 | +} |
| 106 | + |
| 107 | +_list_ids_in_pkg () { |
| 108 | + if [[ -d "$opt_pkg" ]]; then |
| 109 | + pkgdir="$opt_pkg" |
| 110 | + else |
| 111 | + local tmpdir="$(/usr/bin/mktemp -d -t list_ids_in_pkg)" |
| 112 | + trap "/bin/rm -rf -- '$tmpdir'" EXIT |
| 113 | + pkgdir="$tmpdir/unpack" |
| 114 | + /usr/sbin/pkgutil --expand "$opt_pkg" "$tmpdir/unpack" "$pkgdir" |
| 115 | + fi |
| 116 | + |
| 117 | + { |
| 118 | + # emit strings that look like bundle ids |
| 119 | + bundle_id_source_1; |
| 120 | + bundle_id_source_2; |
| 121 | + } | |
| 122 | + merge_sources | |
| 123 | + ( [[ ! "$opt_lax" ]] && clean_sources || cat ) | |
| 124 | + mark_up_sources |
| 125 | +} |
| 126 | + |
| 127 | +process_args "${@}" |
| 128 | + |
| 129 | +_list_ids_in_pkg |
0 commit comments