-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.sh
executable file
·208 lines (167 loc) · 4.96 KB
/
build.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/bash
# Requires jq (https://stedolan.github.io/jq/)
#
# Local installation
# Firefox: Install directly
# Chrome: Run ./build.sh -c, install tmp-chrome/carrot
#
# Preparing the zip for release
# ./build.sh -f -c -z will prepare the zips in release/
set -e
USAGE='Usage: [-f|--firefox] [-c|--chrome] [-z|--zip]
At least one of -f or -c must be present.
-f must be accompanied by -z.'
shopt -s extglob
copy_carrot() {
local copy_dir="$1"
mkdir -p "${copy_dir}/carrot"
# Copy everything except the tests.
cp -r carrot/!(tests) "${copy_dir}/carrot"
}
jq_manifest_replace() {
local jq_command="$1"
local manifest_file="$2"
local manifest_file_tmp="${manifest_file}.tmp"
jq "${jq_command}" "${manifest_file}" > "${manifest_file_tmp}"
mv "${manifest_file_tmp}" "${manifest_file}"
}
pack_firefox() {
local firefox_zip="$1"
local copy_dir="$2"
local release_dir="$3"
mkdir -p "${release_dir}"
printf "Packing ${release_dir}/${firefox_zip}..."
copy_carrot "${copy_dir}"
cd "${copy_dir}/carrot"
# Remove browser_specific_settings from manifest.
jq_manifest_replace 'del(.browser_specific_settings)' manifest.json
# Prepare the zip.
rm -f "../../${release_dir}/${firefox_zip}"
zip -q -r "../../${release_dir}/${firefox_zip}" .
# Clean up.
cd ../..
rm -r "${copy_dir}/carrot"
printf " Done!\n"
}
pack_chrome() {
local chrome_zip="$1"
local copy_dir="$2"
local release_dir="$3"
if [[ -n "${release_dir}" ]]; then
mkdir -p "${release_dir}"
printf "Packing ${release_dir}/${chrome_zip}..."
else
printf "Setting up ${copy_dir}/carrot..."
fi
copy_carrot "${copy_dir}"
cd "${copy_dir}"
# Download the polyfill.
local polyfill_url='https://unpkg.com/[email protected]/dist/browser-polyfill.min.js'
local polyfill_download_path='downloads/browser-polyfill.min.js'
local polyfill_path='polyfill/browser-polyfill.min.js'
mkdir -p downloads
if [[ ! -f "${polyfill_download_path}" ]]; then
curl -s -o "${polyfill_download_path}" "${polyfill_url}"
sed -i '/sourceMappingURL/ d' "${polyfill_download_path}"
fi
mkdir -p "carrot/$(dirname "${polyfill_path}")"
cp "${polyfill_download_path}" "carrot/${polyfill_path}"
cd carrot
# Insert the polyfill before the first script tag in every html file.
local polyfill_script="<script src=\"\/${polyfill_path/\//\\\/}\"><\/script>"
shopt -s globstar
for html_file in **/*.html; do
sed -i -r "0,/<script/ s/((\s+)<script)/\2${polyfill_script}\n\1/" "${html_file}"
done
# Add the polyfill as content script.
jq_manifest_replace ".content_scripts[].js |= [\"${polyfill_path}\"] + ." manifest.json
# Prepare png icons from svg.
icon_sizes=(16 32 48 128)
icons_json=''
for size in ${icon_sizes[@]}; do
rsvg-convert -w "${size}" -h "${size}" -o "icons/icon${size}.png" icons/icon.svg
icons_json="${icons_json},\"${size}\":\"icons/icon${size}.png\""
done
rm icons/icon.svg
icons_json="{${icons_json:1}}"
# Set png icons in the manifest.
jq_manifest_replace ".icons = ${icons_json} |
.browser_action.default_icon = \"icons/icon${icon_sizes[-1]}.png\"" manifest.json
# Final touches to manifest.json.
jq_manifest_replace 'del(.browser_specific_settings) |
if .options_ui.browser_style
then .options_ui.chrome_style = .options_ui.browser_style else . end |
del (.options_ui.browser_style) |
del (.browser_action.browser_style)' manifest.json
if [[ -n "${release_dir}" ]]; then
# Prepare the zip.
rm -f "../../${release_dir}/${chrome_zip}"
zip -q -r "../../${release_dir}/${chrome_zip}" .
fi
cd ../..
# Don't clean up copy_dir/carrot since it is used for local installation.
printf " Done!\n"
}
main() {
local help=0
local firefox=0
local chrome=0
local zip=0
local unknown=()
while (($# > 0)); do
arg="$1"
case "${arg}" in
-h|--help)
help=1
shift
;;
-f|--firefox)
firefox=1
shift
;;
-c|--chrome)
chrome=1
shift
;;
-z|--zip)
zip=1
shift
;;
*)
unknown+=("$1")
shift
;;
esac
done
if ((help != 0)); then
echo "${USAGE}"
exit
fi
if ((${#unknown[@]} != 0)); then
echo "Unknown args: ${unknown[@]}"
echo " -h to see usage"
exit 1
fi
if ((firefox == 0 && chrome == 0)); then
echo "At least one of -f|--firefox or -c|--chrome expected"
exit 1
fi
if ((firefox != 0 && zip == 0)); then
echo "-f|--firefox must be accompanied by -z|--zip"
exit 1
fi
local version=$(jq -r .version carrot/manifest.json)
local release_dir=''
if ((zip != 0)); then
release_dir='release'
fi
if ((firefox != 0)); then
firefox_zip="carrot-firefox-v${version}.zip"
pack_firefox "${firefox_zip}" tmp-firefox "${release_dir}"
fi
if ((chrome != 0)); then
chrome_zip="carrot-chrome-v${version}.zip"
pack_chrome "${chrome_zip}" tmp-chrome "${release_dir}"
fi
}
main "$@"