From af009793a1c2d2f5d6b77f71b7f60cfd4c8aad18 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Fri, 7 Mar 2014 09:58:25 -0500 Subject: [PATCH] devscript: add list_pkg_ids_by_regexp Inspired by @alebcay in #3431 . --- developer/bin/list_pkg_ids_by_regexp | 83 ++++++++++++++++++++++++++++ doc/CASK_LANGUAGE_REFERENCE.md | 7 +++ 2 files changed, 90 insertions(+) create mode 100755 developer/bin/list_pkg_ids_by_regexp diff --git a/developer/bin/list_pkg_ids_by_regexp b/developer/bin/list_pkg_ids_by_regexp new file mode 100755 index 0000000000..8ff6f76885 --- /dev/null +++ b/developer/bin/list_pkg_ids_by_regexp @@ -0,0 +1,83 @@ +#!/bin/bash +# +# list_pkg_ids_by_regexp +# + +### +### settings +### + +set -e # exit on any uncaught error +set +o histexpand # don't expand history expressions +shopt -s nocasematch # case-insensitive regular expressions + +### +### functions +### + +warn () { + local message="$@" + if ! [[ $message =~ "\n"$ ]]; then + message="${message}\n" + fi + printf "$message" 1>&2 +} + +die () { + warn "$@" + exit 1 +} + +fail_informatively () { + local message="No match." + if ! [[ "$1" =~ '*' ]]; then + message="$message Suggestion: try '${1}.*'" + fi + die "$message" +} + +analyze_regexp () { + if [[ "$1" =~ ^\^ ]]; then + warn "Note: pkgutil regular expressions are implicitly anchored with '^' at start" + fi + if [[ "$1" =~ \$$ ]]; then + warn "Note: pkgutil regular expressions are implicitly anchored with '$' at end" + fi +} + +### +### main +### + +_list_pkg_ids_by_regexp () { + analyze_regexp "$1" + if ! /usr/sbin/pkgutil --pkgs="$1"; then + fail_informatively "$1" + fi +} + +# process args +if [[ $1 =~ ^-+h(elp)?$ || $# -ne 1 ]]; then + printf "list_pkg_ids_by_regexp + +Print pkg receipt IDs for installed packages matching a regular +expression, which may be useful in a Cask uninstall stanza, eg + + uninstall :pkgutil => 'pkg.regexp.goes.here' + +Unlike most other scripts in this directory, package IDs attributed to +Apple are NOT excluded from the output. This is to avoid uninstalling +essential system files due to an exuberant regexp. + +For more information, see + + https://github.com/phinze/homebrew-cask/blob/master/doc/CASK_LANGUAGE_REFERENCE.md#uninstall-stanza-details + +" + exit +fi + +# dispatch main +_list_pkg_ids_by_regexp "${@}" + +# diff --git a/doc/CASK_LANGUAGE_REFERENCE.md b/doc/CASK_LANGUAGE_REFERENCE.md index daddb37bd6..689a2caf0a 100644 --- a/doc/CASK_LANGUAGE_REFERENCE.md +++ b/doc/CASK_LANGUAGE_REFERENCE.md @@ -327,6 +327,13 @@ command $ ./developer/bin/list_recent_pkg_ids ``` +`:pkgutil` also accepts a regular expression to match multiple package +IDs. To test a regular expression against currently-installed packages, +use the command +```bash +$ ./developer/bin/list_pkg_ids_by_regexp +``` + ### List Files Associated With a `pkg` Once you know the ID for an installed package, (above), you can list