forked from Homebrew/homebrew-cask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_apps_in_pkg
executable file
·166 lines (135 loc) · 4.24 KB
/
list_apps_in_pkg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
#
# list_apps_in_pkg
#
###
### settings
###
set -e # exit on any uncaught error
set +o histexpand # don't expand history expressions
shopt -s nocasematch # case-insensitive regular expressions
###
### global variables
###
opt_lax=''
opt_pkg=''
pkgdir=''
# prefer GNU xargs
xargs="$(/usr/bin/which gxargs || printf '/usr/bin/xargs')"
###
### functions
###
warn () {
local message="$@"
message="${message//\\t/$'\011'}"
message="${message//\\n/$'\012'}"
message="${message%"${message##*[![:space:]]}"}"
printf "%s\n" "$message" 1>&2
}
die () {
warn "$@"
exit 1
}
app_source_1 () {
/usr/bin/find "$pkgdir" -name PackageInfo -print0 | \
"$xargs" -0 /usr/bin/perl -0777 -ne \
'while (m{<pkg-info[^\n]*install-location="/Applications".*?path\s*=\s*"([^"]+)"}sg) { my $p = $1; $p =~ s{\A.*/}{}; print "$p\n" }';
}
app_source_2 () {
/usr/bin/find "$pkgdir" -name PackageInfo -print0 | \
"$xargs" -0 /usr/bin/perl -0777 -ne \
'while (m{<pkg-info[^\n]*install-location="/".*?path\s*=\s*"./Applications/([^"]+)"}sg) { my $p = $1; $p =~ s{\A.*/}{}; print "$p\n" }';
}
app_source_3 () {
/usr/bin/find "$pkgdir" -type d -name '*.app' | \
perl -pe 's{\A.*/}{}';
}
app_source_4 () {
/usr/bin/find "$pkgdir" -name PackageInfo -print0 | \
"$xargs" -0 /usr/bin/perl -0777 -ne \
'while (m{path\s*=\s*"([^"]+\.app)"}sg) { my $p = $1; $p =~ s{\A.*/}{}; print "$p\n" }';
}
app_source_5 () {
/usr/bin/find "$pkgdir" -name Archive.pax.gz -print0 | \
"$xargs" -0 -n1 -I{} /bin/bash -c \
"/usr/bin/gunzip -c '{}' | /bin/pax | /usr/bin/egrep '\.app'" | \
/usr/bin/perl -pe 's{\A.*/([^/]+\.app).*\Z}{$1}';
}
merge_sources () {
/usr/bin/sort | /usr/bin/uniq
}
mark_up_sources () {
/usr/bin/perl -pe 's{\n}{\000}sg' | \
"$xargs" -0 -I{} -n1 /bin/bash -c \
'printf "{}"; /bin/test -n "$(/usr/bin/find /Applications -type d -maxdepth 3 -name "{}" -print0; /usr/bin/find ~/Applications -type d -maxdepth 3 -name "{}")" && printf " (+)"; printf "\n"'
}
process_args () {
local arg
if [[ "$#" -eq 0 ]]; then
die "ERROR: A file argument is required"
else
opt_pkg="${!#}" # last arg
fi
for arg in "$@"; do
if [[ $arg =~ ^-+h(elp)?$ ]]; then
printf "list_apps_in_pkg [ -lax ] <file.pkg>
Given a package file, extract a list of candidate App names from
inside the pkg, which may be useful for naming a Cask.
The given package file need not be installed.
If an App of the listed name is already installed in /Applications
or ~/Applications, it will be followed by a plus symbol '(+)' in
the output. This can be verified via 'ls' or the Finder.
Arguments
-lax Be less selective in looking for App names. Generate
more, but less accurate, guesses.
Bugs: This script is imperfect.
- It does not fully parse PackageInfo files
- An App can be hidden within a nested archive and not found
- Some pkg files simply don't contain any Apps
See CONTRIBUTING.md and 'man pkgutil' for more information.
"
exit
elif [[ $arg =~ ^-+lax$ ]]; then
opt_lax='true'
elif [[ "$arg" = "$opt_pkg" ]]; then
true
else
die "ERROR: Unknown argument '$arg'"
fi
done
if [[ -h "$opt_pkg" ]]; then
opt_pkg="$(/usr/bin/readlink "$opt_pkg")"
fi
if ! [[ -e "$opt_pkg" ]]; then
die "ERROR: No such pkg file: '$opt_pkg'"
fi
}
###
### main
###
_list_apps_in_pkg () {
if [[ -d "$opt_pkg" ]]; then
pkgdir="$opt_pkg"
else
local tmpdir="$(/usr/bin/mktemp -d -t list_ids_in_pkg)"
trap "/bin/rm -rf -- '$tmpdir'" EXIT
pkgdir="$tmpdir/unpack"
/usr/sbin/pkgutil --expand "$opt_pkg" "$tmpdir/unpack" "$pkgdir"
fi
{
# strings that look like App names (Something.app)
app_source_1;
app_source_2;
app_source_3;
if [[ -n "$opt_lax" ]]; then
app_source_4;
app_source_5;
fi
} | \
merge_sources | \
mark_up_sources
}
process_args "${@}"
# dispatch main
_list_apps_in_pkg
#