-
Notifications
You must be signed in to change notification settings - Fork 86
/
deploy
executable file
·128 lines (112 loc) · 2.75 KB
/
deploy
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
#!/bin/bash
# Author: Peter Wu <[email protected]>
wwwdir="$1"
key="$2"
crxdir="$1/crx"
updates="$wwwdir/updates.xml"
if [ $# -lt 2 ]; then
echo "Usage: $0 extension-www-dir key"
exit 1
fi
if [ -z "$wwwdir" -o ! -e "$updates" ]; then
echo "Cannot find $updates"
exit 1
fi
if [ -z "$key" ]; then
echo "Key is required"
exit 1
fi
key="$(readlink -f "$key")"
if ! [ -e "manifest.json" ]; then
echo "manifest.json not found in current directory"
exit 1
fi
version=$(grep -Po 'version": "\K(.+)(?=")' manifest.json)
if ! [[ $version =~ ^([0-9]+\.)*[0-9]+$ ]]; then
echo "Invalid version found: '$version'"
exit 1
fi
dat_to_id() {
sha256sum | head -c32 | tr 0-9a-f a-p
}
emptysum=$(echo -n | dat_to_id)
_emptysum=odlameecjipmbmbejkplpemijjgpljce
if [[ $emptysum != $_emptysum ]]; then
echo "Internal sanity check failed: calculated ID does not match"
echo "Expected : $_emptysum"
echo "Calculated: $emptysum"
exit 1
fi
get_pub() {
local pem="$1"
{
openssl rsa -pubout -outform DER -in "$pem" 2>&1 >&3 |
grep -xv 'writing RSA key' >&2
} 3>&1
}
calc_id() {
local pem="$1" pub emptysum
pub=$(get_pub "$pem" | dat_to_id)
# only print the processed id if openssl did not fail
if [[ $emptysum != $pub ]]; then
echo "$pub"
fi
}
get_appid() {
grep -Po "appid='\\K[a-p]{32}(?=')" "$updates"
}
appid=$(get_appid)
if grep -q "appid=''" "$updates"; then
if [ -s "$key" ]; then
echo "App ID is empty, but key already exists! Aborting."
exit 1
fi
echo "Key will be generated"
# openssl genrsa -out "$key" 1024
elif [ ! -s "$key" ]; then
echo "App ID exists, please pass the correct key"
exit 1
else
if [ -z "$appid" ]; then
echo "Cannot find app ID in $updates"
exit 1
fi
calc_extid=$(calc_id "$key")
if [ -z "$calc_extid" ]; then
echo "Cannot calculate extension ID!"
exit 1
fi
if [[ $appid != $calc_extid ]]; then
echo "App ID does not match calculated ID:"
echo "Expected : $appid"
echo "Calculated: $calc_extid"
exit 1
fi
fi
tmpdir="$(mktemp -d)"
if [ -z "$tmpdir" -o ! -d "$tmpdir" ]; then
echo "Could not create a tmpdir!"
exit 1
fi
cleanup() {
echo "Removing $tmpdir"
rm -r "$tmpdir"
}
trap cleanup EXIT
mkdir "$tmpdir/ext"
git archive HEAD -- \*.css \*.js \*.html \*.json \*.png | tar x -C "$tmpdir/ext"
cmd=(chromium --pack-extension="$tmpdir/ext")
[ -s "$key" ] && cmd+=(--pack-extension-key="$key")
"${cmd[@]}"
regexp="codebase='(.+)-[0-9.]+\\.crx' version='.*'"
repl="codebase='\\1-$version.crx' version='$version'"
sed -r "s#$regexp#$repl#" -i "$updates"
if [ -z "$appid" ]; then
mv "$tmpdir/ext.pem" "$key"
appid=$(calc_id "$key")
sed "s#appid=''#appid='$appid'#" -i "$updates"
fi
file=$(grep -Po "codebase='\\K[^']+-$version.crx" "$updates")
file="${file##*/}"
[ -d "$crxdir" ] || mkdir "$crxdir"
mv -v "$tmpdir/ext.crx" "$crxdir/$file"