Revision 6106c789
Added by koszko almost 2 years ago
build.sh | ||
---|---|---|
3 | 3 |
# Copyright (C) 2021 Wojtek Kosior |
4 | 4 |
# Redistribution terms are gathered in the `copyright' file. |
5 | 5 |
|
6 |
handle_export_line() { |
|
7 |
if [ "x$1" = "xEXPORTS_START" ]; then |
|
8 |
if [ "$STATE" = "before_block" ]; then |
|
9 |
STATE="in_block" |
|
10 |
fi |
|
11 |
elif [ "x$1" = "xEXPORT" ]; then |
|
12 |
if [ "$STATE" != "in_block" ]; then |
|
13 |
return |
|
14 |
fi |
|
15 |
|
|
16 |
EXPORTCODE="${EXPORTCODE}window.killtheweb.$2 = $2;$ENDL" |
|
17 |
|
|
18 |
PREVIOUS_FILE="$(map_get EXPORTS $2)" |
|
19 |
if [ "x$PREVIOUS_FILE" != "x" ]; then |
|
20 |
errcho "export $2 present in both $PREVIOUS_FILE and $FILE" |
|
21 |
return 1 |
|
22 |
fi |
|
23 |
|
|
24 |
map_set_instr EXPORTS $2 "$FILE" |
|
25 |
|
|
26 |
elif [ "x$1" = "xEXPORTS_END" ]; then |
|
27 |
if [ "$STATE" = "in_block" ]; then |
|
28 |
STATE="after_block" |
|
29 |
fi |
|
30 |
fi |
|
31 |
} |
|
32 |
|
|
33 |
translate_exports() { |
|
34 |
STATE="before_block" |
|
35 |
EXPORTCODE='' |
|
36 |
|
|
37 |
while read EXPORT_LINE; do |
|
38 |
handle_export_line $EXPORT_LINE || return 1 |
|
39 |
done |
|
40 |
|
|
41 |
map_set_instr EXPORTCODES $FILEKEY "$EXPORTCODE" |
|
42 |
} |
|
43 |
|
|
44 |
add_exports() { |
|
45 |
FILE="$1" |
|
46 |
FILEKEY="$(sanitize "$FILE")" |
|
47 |
|
|
48 |
eval "$(grep -o 'EXPORT.\+' "$1" | translate_exports || exit 1)" |
|
49 |
} |
|
50 |
|
|
51 |
handle_import_line() { |
|
52 |
if [ "x$1" = "xIMPORTS_START" ]; then |
|
53 |
if [ "$STATE" = "before_block" ]; then |
|
54 |
STATE="in_block" |
|
55 |
fi |
|
56 |
elif [ "x$1" = "xIMPORT" ]; then |
|
57 |
if [ "$STATE" != "in_block" ]; then |
|
58 |
return |
|
59 |
fi |
|
60 |
|
|
61 |
IMPORTCODE="${IMPORTCODE}const $2 = window.killtheweb.$2;$ENDL" |
|
62 |
|
|
63 |
IMPORTS="$IMPORTS $2" |
|
64 |
|
|
65 |
elif [ "x$1" = "xIMPORTS_END" ]; then |
|
66 |
if [ "$STATE" = "in_block" ]; then |
|
67 |
STATE="after_block" |
|
68 |
fi |
|
69 |
fi |
|
70 |
} |
|
71 |
|
|
72 |
translate_imports() { |
|
73 |
STATE="before_block" |
|
74 |
IMPORTCODE='' |
|
75 |
IMPORTS='' |
|
76 |
|
|
77 |
while read IMPORT_LINE; do |
|
78 |
handle_import_line $IMPORT_LINE || return 1 |
|
79 |
done |
|
80 |
|
|
81 |
map_set_instr IMPORTCODES $FILEKEY "$IMPORTCODE" |
|
82 |
map_set_instr IMPORTS $FILEKEY "$IMPORTS" |
|
83 |
} |
|
84 |
|
|
85 |
add_imports() { |
|
86 |
FILE="$1" |
|
87 |
FILEKEY="$(sanitize "$FILE")" |
|
88 |
|
|
89 |
eval "$(grep -o 'IMPORT.\+' "$1" | translate_imports || exit 1)" |
|
90 |
} |
|
6 |
set -e |
|
91 | 7 |
|
92 |
compute_scripts_list_rec() { |
|
93 |
local FILE="$1" |
|
94 |
local FILEKEY=$(sanitize "$1") |
|
95 |
|
|
96 |
local FILESTATE="$(map_get FILESTATES $FILEKEY)" |
|
97 |
if [ "xprocessed" = "x$FILESTATE" ]; then |
|
98 |
return |
|
99 |
fi |
|
100 |
if [ "xprocessing" = "x$FILESTATE" ]; then |
|
101 |
errcho "import loop on $FILE" |
|
102 |
return 1 |
|
103 |
fi |
|
104 |
|
|
105 |
USED="$USED $FILEKEY" |
|
106 |
|
|
107 |
map_set FILESTATES $FILEKEY "processing" |
|
108 |
|
|
109 |
local IMPORT |
|
110 |
for IMPORT in $(map_get IMPORTS $FILEKEY); do |
|
111 |
NEXT_FILE="$(map_get EXPORTS $IMPORT)" |
|
112 |
if [ "x" = "x$NEXT_FILE" ]; then |
|
113 |
errcho "nothing exports $IMPORT, required by $FILE" |
|
114 |
return 1 |
|
115 |
fi |
|
116 |
if ! compute_scripts_list_rec "$NEXT_FILE"; then |
|
117 |
errcho "when satisfying $IMPORT for $FILE" |
|
118 |
return 1 |
|
119 |
fi |
|
120 |
done |
|
121 |
|
|
122 |
[ "x$FILE" = "xexports_init.js" ] || echo $FILE # exports_init.js is hardcoded to load first; the entire export system depends on it |
|
123 |
map_set FILESTATES $FILEKEY "processed" |
|
124 |
} |
|
125 |
|
|
126 |
compute_scripts_list() { |
|
127 |
USED='' |
|
128 |
echo COMPUTED_SCRIPTS=\"exports_init.js |
|
129 |
compute_scripts_list_rec "$1" |
|
130 |
echo \" |
|
131 |
|
|
132 |
for FILEKEY in $USED; do |
|
133 |
map_set_instr USED $FILEKEY yes |
|
134 |
done |
|
135 |
} |
|
8 |
. ./shell_utils.sh |
|
136 | 9 |
|
137 | 10 |
as_json_list() { |
138 | 11 |
while true; do |
139 | 12 |
if [ "x" = "x$2" ]; then |
140 |
echo -n '\\n'"\t\t\"$1\""'\\n\t'
|
|
13 |
printf '\\n\t\t"%s"\\n\t' "$1"
|
|
141 | 14 |
return |
142 | 15 |
fi |
143 |
echo -n '\\n'"\t\t\"$1\","
|
|
16 |
printf '\\n\t\t"%s",' "$1"
|
|
144 | 17 |
shift |
145 | 18 |
done |
146 | 19 |
} |
147 | 20 |
|
148 | 21 |
as_html_list() { |
149 | 22 |
while [ "x" != "x$1" ]; do |
150 |
echo -n '\\n'" <script src=\"/$1\"></script>"
|
|
23 |
printf '\\n <script src="/%s"></script>' "$1"
|
|
151 | 24 |
shift |
152 | 25 |
done |
153 | 26 |
} |
154 | 27 |
|
155 |
build_main() { |
|
156 |
# placate importers of these, as they are exported by the yet-to-be-created exports_init.js |
|
157 |
EXPORTS__browser=exports_init.js |
|
158 |
EXPORTS__is_chrome=exports_init.js |
|
159 |
EXPORTS__is_mozilla=exports_init.js |
|
28 |
compute_scripts() { |
|
29 |
local DIRS="$1" |
|
30 |
local ROOT_SCRIPT="$2" |
|
31 |
|
|
32 |
local AVAILABLE="$(find $DIRS -name '[^.#]*.js')" |
|
33 |
|
|
34 |
awk -f compute_scripts.awk script_dependencies "$ROOT_SCRIPT" $AVAILABLE |
|
35 |
} |
|
160 | 36 |
|
161 |
SCRIPTDIRS='background html common content' |
|
37 |
build_main() { |
|
38 |
local ALL_SCRIPTDIRS='background html common content' |
|
162 | 39 |
|
163 |
SCRIPTS=$(find $SCRIPTDIRS -name '[^.#]*.js')
|
|
40 |
local ALL_SCRIPTS_AVAILABLE="$(find $ALL_SCRIPTDIRS -name '[^.#]*.js')"
|
|
164 | 41 |
|
165 |
for SCRIPT in $SCRIPTS; do
|
|
166 |
add_exports $SCRIPT
|
|
167 |
add_imports $SCRIPT
|
|
42 |
local SCRIPT
|
|
43 |
for SCRIPT in $ALL_SCRIPTS_AVAILABLE; do
|
|
44 |
map_set SCRIPTS_UNUSED $(sanitize $SCRIPT) yes
|
|
168 | 45 |
done |
169 | 46 |
|
170 |
eval "$(compute_scripts_list background/main.js || exit 1)" |
|
171 |
BGSCRIPTS="$(as_json_list $COMPUTED_SCRIPTS)" |
|
172 |
eval "$(compute_scripts_list content/main.js || exit 1)" |
|
173 |
CONTENTSCRIPTS="$(as_json_list $COMPUTED_SCRIPTS)" |
|
174 |
eval "$(compute_scripts_list html/display-panel.js || exit 1)" |
|
175 |
POPUPSCRIPTS="$(as_html_list $COMPUTED_SCRIPTS)" |
|
176 |
eval "$(compute_scripts_list html/options_main.js || exit 1)" |
|
177 |
OPTIONSSCRIPTS="$(as_html_list $COMPUTED_SCRIPTS)" |
|
47 |
local ROOT=background/main.js |
|
48 |
local SCRIPTS_BG="$( compute_scripts 'common/ background/' $ROOT)" |
|
49 |
|
|
50 |
local ROOT=content/main.js |
|
51 |
local SCRIPTS_CONTENT="$( compute_scripts 'common/ content/' $ROOT)" |
|
52 |
|
|
53 |
local ROOT=html/display-panel.js |
|
54 |
local SCRIPTS_POPUP="$( compute_scripts 'common/ html/' $ROOT)" |
|
55 |
|
|
56 |
local ROOT=html/options_main.js |
|
57 |
local SCRIPTS_OPTIONS="$( compute_scripts 'common/ html/' $ROOT)" |
|
178 | 58 |
|
179 |
for DIR in $(find $SCRIPTDIRS -type d); do |
|
59 |
local BGSCRIPTS="$( as_json_list $SCRIPTS_BG )" |
|
60 |
local CONTENTSCRIPTS="$( as_json_list $SCRIPTS_CONTENT )" |
|
61 |
local POPUPSCRIPTS="$( as_html_list $SCRIPTS_POPUP )" |
|
62 |
local OPTIONSSCRIPTS="$( as_html_list $SCRIPTS_OPTIONS )" |
|
63 |
|
|
64 |
for SCRIPT in $SCRIPTS_BG $SCRIPTS_CONTENT $SCRIPTS_POPUP $SCRIPTS_OPTIONS |
|
65 |
do |
|
66 |
map_del SCRIPTS_UNUSED $(sanitize $SCRIPT) |
|
67 |
done |
|
68 |
|
|
69 |
for DIR in $(find $ALL_SCRIPTDIRS -type d); do |
|
180 | 70 |
mkdir -p "$BUILDDIR"/$DIR |
181 | 71 |
done |
182 | 72 |
|
... | ... | |
214 | 104 |
sed "s^_OPTIONSSCRIPTS_^$OPTIONSSCRIPTS^" \ |
215 | 105 |
> "$BUILDDIR"/html/options.html |
216 | 106 |
|
217 |
for FILE in $SCRIPTS; do
|
|
107 |
for FILE in $ALL_SCRIPTS_AVAILABLE; do
|
|
218 | 108 |
FILEKEY=$(sanitize "$FILE") |
219 |
if [ "xyes" != "x$(map_get USED $FILEKEY)" ]; then
|
|
220 |
errcho "WARNING! $FILE not used"
|
|
109 |
if [ "x$(map_get SCRIPTS_UNUSED $FILEKEY)" = "xyes" ]; then
|
|
110 |
printf 'WARNING! %s not used\n' "$FILE" >&2
|
|
221 | 111 |
else |
222 |
(echo "\ |
|
223 |
\"use strict\"; |
|
224 |
|
|
225 |
({fun: (function() { |
|
226 |
$(map_get IMPORTCODES $FILEKEY) |
|
227 |
|
|
228 |
"; |
|
229 |
|
|
230 |
# A hack to insert the contents of default_settings.json at the appropriate location in background/main.js |
|
231 |
if [ "$FILE" = "background/main.js" ]; then |
|
232 |
# Uses an internal sed expression to escape and indent the JSON file for use in the external sed expression |
|
233 |
sed 's/^ `DEFAULT SETTINGS`$/'"$(sed -E 's/([\\\&\/])/\\\1/g; s/^/ /; s/$/\\/' < default_settings.json) "/g < "$FILE" |
|
234 |
else |
|
235 |
cat $FILE |
|
236 |
fi |
|
237 |
|
|
238 |
echo " |
|
239 |
|
|
240 |
$(map_get EXPORTCODES $FILEKEY) |
|
241 |
})}).fun();") > "$BUILDDIR"/$FILE |
|
112 |
awk -f compute_scripts.awk wrapped_code "$FILE" > "$BUILDDIR"/$FILE |
|
242 | 113 |
fi |
243 | 114 |
done |
244 | 115 |
|
116 |
# A hack to insert the contents of default_settings.json at the appropriate |
|
117 |
# location in background/main.js. Uses an internal sed expression to escape |
|
118 |
# and indent the JSON file for use in the external sed expression. |
|
119 |
sed -i 's/^ `DEFAULT SETTINGS`$/'"$(sed -E 's/([\\\&\/])/\\\1/g; s/^/ /; s/$/\\/' < default_settings.json) "/g "$BUILDDIR"/background/main.js |
|
120 |
|
|
245 | 121 |
if [ "$BROWSER" = "chromium" ]; then |
246 |
cat > "$BUILDDIR"/exports_init.js <<EOF |
|
247 |
window.killtheweb={is_chrome: true, browser: window.chrome}; |
|
248 |
EOF |
|
122 |
cp CHROMIUM_exports_init.js "$BUILDDIR"/exports_init.js |
|
249 | 123 |
else |
250 |
cat > "$BUILDDIR"/exports_init.js <<EOF |
|
251 |
/* Polyfill for IceCat 60. */ |
|
252 |
String.prototype.matchAll = String.prototype.matchAll || function(regex) { |
|
253 |
if (regex.flags.search("g") === -1) |
|
254 |
throw new TypeError("String.prototype.matchAll called with a non-global RegExp argument"); |
|
255 |
|
|
256 |
for (const matches = [];;) { |
|
257 |
if (matches[matches.push(regex.exec(this)) - 1] === null) |
|
258 |
return matches.splice(0, matches.length - 1); |
|
259 |
} |
|
260 |
} |
|
261 |
|
|
262 |
window.killtheweb={is_mozilla: true, browser: this.browser}; |
|
263 |
EOF |
|
124 |
cp MOZILLA_exports_init.js "$BUILDDIR"/exports_init.js |
|
264 | 125 |
fi |
265 | 126 |
|
266 | 127 |
cp -r copyright licenses/ "$BUILDDIR" |
... | ... | |
271 | 132 |
|
272 | 133 |
if [ "$BROWSER" = "chromium" ]; then |
273 | 134 |
for MOZILLA_FILE in $(find "$BUILDDIR" -name "MOZILLA_*"); do |
274 |
echo > "$MOZILLA_FILE"
|
|
135 |
printf '\n' > "$MOZILLA_FILE"
|
|
275 | 136 |
done |
276 | 137 |
fi |
277 | 138 |
if [ "$BROWSER" = "mozilla" ]; then |
278 | 139 |
for CHROMIUM_FILE in $(find "$BUILDDIR" -name "CHROMIUM_*"); do |
279 |
echo > "$CHROMIUM_FILE"
|
|
140 |
printf '\n' > "$CHROMIUM_FILE"
|
|
280 | 141 |
done |
281 | 142 |
fi |
282 | 143 |
} |
283 | 144 |
|
145 |
print_usage() { |
|
146 |
printf 'usage: %s mozilla|chromium [source directory] [update url]\n' \ |
|
147 |
"$0" >&2 |
|
148 |
} |
|
149 |
|
|
284 | 150 |
main() { |
285 | 151 |
if [ "x$1" = "xmozilla" -o "x$1" = "xchromium" ]; then |
286 | 152 |
BROWSER=$1 |
287 | 153 |
else |
288 |
errcho "usage: $0 mozilla|chromium [source directory] [update url]"
|
|
154 |
print_usage
|
|
289 | 155 |
exit 1 |
290 | 156 |
fi |
291 | 157 |
|
... | ... | |
296 | 162 |
mkdir "$BUILDDIR" |
297 | 163 |
cd "$SRCDIR" |
298 | 164 |
else |
299 |
errcho "usage: $0 mozilla|chromium [source directory] [update url]"
|
|
165 |
print_usage
|
|
300 | 166 |
exit 2 |
301 | 167 |
fi |
302 | 168 |
|
303 | 169 |
UPDATE_URL="$3" |
304 | 170 |
|
305 |
. ./shell_utils.sh |
|
306 | 171 |
build_main |
307 | 172 |
} |
308 | 173 |
|
Also available in: Unified diff
rewrite parts of build script in awk