-
Notifications
You must be signed in to change notification settings - Fork 0
/
crxmake.sh
executable file
·96 lines (80 loc) · 2.51 KB
/
crxmake.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
#!/bin/bash -e
#
# Purpose: Pack a Chromium extension directory into crx format
#
# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
# find bash working directory and build from there
#
bash_source="${BASH_SOURCE[0]}"
# resolve $bash_source until the file is no longer a symlink
while [ -h "$bash_source" ]; do
cwd="$(cd -P "$(dirname "$bash_source")" && pwd)"
bash_source="$(readlink "$bash_source")"
# if $bash_source was a relative symlink, we need to resolve it relative to the path where the symlink file was located
[[ $bash_source != /* ]] && bash_source="$cwd/$bash_source"
done
cwd="$(cd -P "$(dirname "$bash_source" )" && pwd)"
# args
if test $# -lt 2; then
echo "Usage:"
echo "crxmake.sh <extension dir> <pem path> [output.crx]"
echo ""
echo "crxmake.sh src extension/slack.com.js.pem extension/slack.com.js.crx"
echo "crxmake.sh src extension/slack.com.js.pem extension/slack.com.js.crx version.txt"
echo ""
exit 1
else
src=$1
pem=$2
if test $# -eq 3; then
dest=$3
elif test $# -eq 4; then
dest=$3
fver=$4
fi
fi
# do build versioning
if test ${#fver} -gt 0; then
# sed increment build revision
orig_version=$(head -n 1 "$cwd/$fver")
(sed -r -i"" 's/^([0-9]+\.[0-9]\.)([0-9]+)/echo \1$((\2+1))/e' "$cwd/$fver")
new_version=$(head -n 1 "$cwd/$fver")
# bump update_manifest
sed_update_manifest="s/\.crx\" version=\".*\\?\"/\.crx\" version=\"$new_version\"/"
(sed -i"" "$sed_update_manifest" "$cwd/update_manifest.xml")
# bump manifest
sed_src_manifest="s/\"version\": \".*\\?\"/\"version\": \"$new_version\"/"
(sed -i"" "$sed_src_manifest" "$cwd/src/manifest.json")
fi
# build vars
name=$(basename "$src")
crx="$name.crx"
pub="$name.pub"
sig="$name.sig"
zip="$name.zip"
trap 'rm -f "$pub" "$sig" "$zip"' EXIT
# zip up the crx dir
(cd "$src" && zip -qr -9 -X "$cwd/$zip" . -x \*.rcs\*)
# signature
openssl sha1 -sha1 -binary -sign "$pem" < "$zip" > "$sig"
# public key
openssl rsa -pubout -outform DER < "$pem" > "$pub" 2>/dev/null
# byte swap
byte_swap () {
# take "abcdefgh" and return it as "ghefcdab"
echo "${1:6:2}${1:4:2}${1:2:2}${1:0:2}"
}
crmagic_hex="4372 3234" # Cr24
version_hex="0200 0000" # 2
pub_len_hex=$(byte_swap $(printf '%08x\n' $(ls -l "$pub" | awk '{print $5}')))
sig_len_hex=$(byte_swap $(printf '%08x\n' $(ls -l "$sig" | awk '{print $5}')))
(
echo "$crmagic_hex $version_hex $pub_len_hex $sig_len_hex" | xxd -r -p
cat "$pub" "$sig" "$zip"
) > "$crx"
if test ${#dest} -gt 0; then
mv "$crx" "$dest"
echo "Wrote $dest"
else
echo "Wrote $crx"
fi