-
Notifications
You must be signed in to change notification settings - Fork 1
/
pkgapp.sh
executable file
·154 lines (118 loc) · 3.68 KB
/
pkgapp.sh
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
#!/bin/bash
set -eu
IFS=$'\n'
declare TMPDIR=$(mktemp -d)
declare OUTDIR
declare RED="\e[1;91m"
declare GREEN="\e[1;92m"
declare BLUE="\e[1;34m"
declare RESET="\e[0m"
warn() {
printf "${RED}"
printf "$@" 1>&2
printf "${RESET}"
}
usage() {
printf "usage: %s -o OUTDIR FILE ...\n\n" "$0"
printf "$@"
exit
}
cleanup() {
[[ -z "${TMPDIR:-}" ]] && exit
for mountpoint in $(mount | awk ' $1 != "map" { print $3 }' | grep "${TMPDIR}"); do
hdiutil detach "${mountpoint}"
done
rm -r "${TMPDIR}" 2>/dev/null
}
parse_json() {
python -c "import json,sys;print(json.load(sys.stdin)$1)"
}
package_app() {
local app="${1}"
local name=$(basename "${app}" ".app" | tr 'A-Z' 'a-z' | sed -e 's/[^a-z]//g' )
local iden=$(plutil -convert json -r -o - -- "${app}/Contents/Info.plist" | parse_json "['CFBundleIdentifier']")
local vers=$(plutil -convert json -r -o - -- "${app}/Contents/Info.plist" | parse_json "['CFBundleShortVersionString']")
printf "${BLUE}packaging '%s'\n${RESET}" $(basename "${app}")
pkgbuild \
--install-location "/Applications" \
--identifier "${iden}" \
--version "${vers}" \
--component "${app}" \
"${TMPDIR}/${name}-${vers}.pkg"
productbuild \
--synthesize \
--package "${TMPDIR}/${name}-${vers}.pkg" \
"${TMPDIR}/${name}-${vers}.xml"
productbuild \
--distribution "${TMPDIR}/${name}-${vers}.xml" \
--package-path "${TMPDIR}" \
"${OUTDIR}/${name}-${vers}.pkg"
rm "${TMPDIR}/${name}-${vers}.pkg" "${TMPDIR}/${name}-${vers}.xml"
}
mount_dmg() {
local dmg="${1}"
local mountpoint
mountpoint=$(hdiutil attach "${dmg}" -nobrowse -mountrandom "${TMPDIR}" <<< Y | awk -F '\t' '{print $3}' | grep -v '^$')
printf "%s" "${mountpoint}"
}
umount_dmg() {
local mountpoint="${1}"
hdiutil detach "${mountpoint}" -quiet
}
handle_dmg() {
local dmg="${1}"
local mountpoint=$(mount_dmg "${dmg}")
while read -rd '' app; do
package_app "${app}"
done < <(find "${mountpoint}" -depth 1 -name '.*' -prune -o -depth 1 -name '*.app' -print0)
umount_dmg "${mountpoint}"
}
handle_zip() {
local file="${1}"
local tmpdir=$(mktemp -d)
unzip -q "${file}" -d "${tmpdir}"
while read -rd '' app; do
package_app "${app}"
done < <(find "${tmpdir}" -maxdepth 2 -name '.*' -prune -o -maxdepth 2 -name '*.app' -print0)
rm -r "${tmpdir}"
}
main() {
trap cleanup 1 2 3 6 ERR
while getopts "o:" opt; do
case "${opt}" in
o)
OUTDIR="${OPTARG}"
;;
*)
usage
;;
esac
done
if [[ -z "${OUTDIR:-}" ]]; then
usage "no output directory specified\n"
fi
if [[ ! -d "${OUTDIR}" ]]; then
usage "'%s' is not a directory\n" "$OUTDIR"
fi
shift $((${OPTIND}-1))
if [[ ${#@} -lt 1 ]]; then
usage "no input files specified\n"
fi
for file in "$@"; do
case "${file}" in
*.dmg)
printf "${GREEN}mounting '%s'\n${RESET}" "${file}"
handle_dmg "${file}"
;;
*.zip)
printf "${GREEN}exploding '%s'\n${RESET}" "${file}"
handle_zip "${file}"
;;
*)
warn "unhandled file: '%s'\n" "${file}"
;;
esac
done
rm -r "${TMPDIR}"
}
main "$@"