Project

General

Profile

Download (7.61 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / lib_build.sh @ 81910556

1
#!/bin/sh
2

    
3
# Copyright (C) 2021 Wojtek Kosior
4
# Redistribution terms are gathered in the `copyright' file.
5

    
6
. ./shell_utils.sh
7

    
8
handle_export_line() {
9
    if [ "x$1" = "xEXPORTS_START" ]; then
10
	if [ "$STATE" = "before_block" ]; then
11
	    STATE="in_block"
12
	fi
13
    elif [ "x$1" = "xEXPORT" ]; then
14
	if [ "$STATE" != "in_block" ]; then
15
	    return
16
	fi
17

    
18
	EXPORTCODE="${EXPORTCODE}window.killtheweb.$2 = $2;$ENDL"
19

    
20
	PREVIOUS_FILE="$(map_get EXPORTS $2)"
21
	if [ "x$PREVIOUS_FILE" != "x" ]; then
22
	    errcho "export $2 present in both $PREVIOUS_FILE and $FILE"
23
	    return 1
24
	fi
25

    
26
	map_set_instr EXPORTS $2 "$FILE"
27

    
28
    elif [ "x$1" = "xEXPORTS_END" ]; then
29
	if [ "$STATE" = "in_block" ]; then
30
	    STATE="after_block"
31
	fi
32
    fi
33
}
34

    
35
translate_exports() {
36
    STATE="before_block"
37
    EXPORTCODE=''
38

    
39
    while read EXPORT_LINE; do
40
	handle_export_line $EXPORT_LINE || return 1
41
    done
42

    
43
    map_set_instr EXPORTCODES $FILEKEY "$EXPORTCODE"
44
}
45

    
46
add_exports() {
47
    FILE="$1"
48
    FILEKEY="$(sanitize "$FILE")"
49

    
50
    eval "$(grep -o 'EXPORT.\+' "$1" | translate_exports || exit 1)"
51
}
52

    
53
handle_import_line() {
54
    if [ "x$1" = "xIMPORTS_START" ]; then
55
	if [ "$STATE" = "before_block" ]; then
56
	    STATE="in_block"
57
	fi
58
    elif [ "x$1" = "xIMPORT" ]; then
59
	if [ "$STATE" != "in_block" ]; then
60
	    return
61
	fi
62

    
63
	IMPORTCODE="${IMPORTCODE}const $2 = window.killtheweb.$2;$ENDL"
64

    
65
	IMPORTS="$IMPORTS $2"
66

    
67
    elif [ "x$1" = "xIMPORTS_END" ]; then
68
	if [ "$STATE" = "in_block" ]; then
69
	    STATE="after_block"
70
	fi
71
    fi
72
}
73

    
74
translate_imports() {
75
    STATE="before_block"
76
    IMPORTCODE=''
77
    IMPORTS=''
78

    
79
    while read IMPORT_LINE; do
80
	handle_import_line $IMPORT_LINE || return 1
81
    done
82

    
83
    map_set_instr IMPORTCODES $FILEKEY "$IMPORTCODE"
84
    map_set_instr IMPORTS $FILEKEY "$IMPORTS"
85
}
86

    
87
add_imports() {
88
    FILE="$1"
89
    FILEKEY="$(sanitize "$FILE")"
90

    
91
    eval "$(grep -o 'IMPORT.\+' "$1" | translate_imports || exit 1)"
92
}
93

    
94
compute_scripts_list_rec() {
95
    local FILE="$1"
96
    local FILEKEY=$(sanitize "$1")
97

    
98
    local FILESTATE="$(map_get FILESTATES $FILEKEY)"
99
    if [ "xprocessed" = "x$FILESTATE" ]; then
100
	return
101
    fi
102
    if [ "xprocessing" = "x$FILESTATE" ]; then
103
	errcho "import loop on $FILE"
104
	return 1
105
    fi
106

    
107
    USED="$USED $FILEKEY"
108

    
109
    map_set FILESTATES $FILEKEY "processing"
110

    
111
    local IMPORT
112
    for IMPORT in $(map_get IMPORTS $FILEKEY); do
113
	NEXT_FILE="$(map_get EXPORTS $IMPORT)"
114
	if [ "x" = "x$NEXT_FILE" ]; then
115
	    errcho "nothing exports $IMPORT, required by $FILE"
116
	    return 1
117
	fi
118
	if ! compute_scripts_list_rec "$NEXT_FILE"; then
119
	    errcho "when satisfying $IMPORT for $FILE"
120
	    return 1
121
	fi
122
    done
123

    
124
    [ "x$FILE" = "xexports_init.js" ] || echo $FILE # exports_init.js is hardcoded to load first; the entire export system depends on it
125
    map_set FILESTATES $FILEKEY "processed"
126
}
127

    
128
compute_scripts_list() {
129
    USED=''
130
    echo COMPUTED_SCRIPTS=\"exports_init.js
131
    compute_scripts_list_rec "$1"
132
    echo \"
133

    
134
    for FILEKEY in $USED; do
135
	map_set_instr USED $FILEKEY yes
136
    done
137
}
138

    
139
as_json_list() {
140
    while true; do
141
	if [ "x" = "x$2" ]; then
142
	    echo -n '\\n'"\t\t\"$1\""'\\n\t'
143
	    return
144
	fi
145
	echo -n '\\n'"\t\t\"$1\","
146
	shift
147
    done
148
}
149

    
150
as_html_list() {
151
    while [ "x" != "x$1" ]; do
152
	echo -n '\\n'"    <script src=\"/$1\"></script>"
153
	shift
154
    done
155
}
156

    
157
main() {
158
    # placate importers of these, as they are exported by the yet-to-be-created exports_init.js
159
    EXPORTS__browser=exports_init.js
160
    EXPORTS__is_chrome=exports_init.js
161
    EXPORTS__is_mozilla=exports_init.js
162

    
163
    SCRIPTDIRS='background html common content'
164

    
165
    SCRIPTS=$(find $SCRIPTDIRS -name '[^.#]*.js')
166

    
167
    for SCRIPT in $SCRIPTS; do
168
	add_exports $SCRIPT
169
	add_imports $SCRIPT
170
    done
171

    
172
    eval "$(compute_scripts_list background/main.js || exit 1)"
173
    BGSCRIPTS="$(as_json_list $COMPUTED_SCRIPTS)"
174
    eval "$(compute_scripts_list content/main.js || exit 1)"
175
    CONTENTSCRIPTS="$(as_json_list $COMPUTED_SCRIPTS)"
176
    eval "$(compute_scripts_list html/display-panel.js || exit 1)"
177
    POPUPSCRIPTS="$(as_html_list $COMPUTED_SCRIPTS)"
178
    eval "$(compute_scripts_list html/options_main.js || exit 1)"
179
    OPTIONSSCRIPTS="$(as_html_list $COMPUTED_SCRIPTS)"
180

    
181
    rm -rf $BUILDDIR
182
    mkdir $BUILDDIR
183
    for DIR in $(find $SCRIPTDIRS -type d); do
184
	mkdir -p $BUILDDIR/$DIR
185
    done
186

    
187
    CHROMIUM_KEY=''
188
    CHROMIUM_UPDATE_URL=''
189
    GECKO_APPLICATIONS=''
190
    
191
    if [ "x$UPDATE_URL" != x ]; then
192
	UPDATE_URL=",\n    \"update_url\": \"$UPDATE_URL\""
193
    fi
194

    
195
    if [ "$BROWSER" = "chromium" ]; then
196
	CHROMIUM_KEY="$(dd if=/dev/urandom bs=32 count=1 2>/dev/null | base64)"
197
	CHROMIUM_KEY=$(echo chromium-key-dummy-file-$CHROMIUM_KEY | tr / -)
198
	touch $BUILDDIR/$CHROMIUM_KEY
199

    
200
	CHROMIUM_UPDATE_URL="$UPDATE_URL"
201

    
202
	CHROMIUM_KEY="\n\
203
	// WARNING!!!\n\
204
	// EACH USER SHOULD REPLACE DUMMY FILE's VALUE WITH A UNIQUE ONE!!!\n\
205
	// OTHERWISE, SECURITY CAN BE TRIVIALLY COMPROMISED!\n\
206
	// Only relevant to users of chrome-based browsers.\n\
207
	// Users of Firefox forks are safe.\n\
208
	\"$CHROMIUM_KEY\"\
209
"
210
    else
211
	GECKO_APPLICATIONS="\n\
212
    \"applications\": {\n\
213
	\"gecko\": {\n\
214
	    \"id\": \"{6fe13369-88e9-440f-b837-5012fb3bedec}\",\n\
215
	    \"strict_min_version\": \"60.0\"$UPDATE_URL\n\
216
	}\n\
217
    },"
218
    fi
219

    
220
    sed "\
221
s^_GECKO_APPLICATIONS_^$GECKO_APPLICATIONS^
222
s^_CHROMIUM_KEY_^$CHROMIUM_KEY^
223
s^_CHROMIUM_UPDATE_URL_^$CHROMIUM_UPDATE_URL^
224
s^_BGSCRIPTS_^$BGSCRIPTS^
225
s^_CONTENTSCRIPTS_^$CONTENTSCRIPTS^" \
226
	< manifest.json > $BUILDDIR/manifest.json
227

    
228
    ./process_html_file.sh html/display-panel.html |
229
	sed "s^_POPUPSCRIPTS_^$POPUPSCRIPTS^" \
230
	    > $BUILDDIR/html/display-panel.html
231

    
232
    ./process_html_file.sh html/options.html |
233
	sed "s^_OPTIONSSCRIPTS_^$OPTIONSSCRIPTS^" \
234
	    > $BUILDDIR/html/options.html
235

    
236
    for FILE in $SCRIPTS; do
237
	FILEKEY=$(sanitize "$FILE")
238
	if [ "xyes" != "x$(map_get USED $FILEKEY)" ]; then
239
	    errcho "WARNING! $FILE not used"
240
	else
241
	    (echo "\
242
\"use strict\";
243

    
244
({fun: (function() {
245
$(map_get IMPORTCODES $FILEKEY)
246

    
247
";
248

    
249
# A hack to insert the contents of default_settings.json at the appropriate location in background/main.js
250
if [ "$FILE" = "background/main.js" ]; then
251
    # Uses an internal sed expression to escape and indent the JSON file for use in the external sed expression
252
    sed 's/^        `DEFAULT SETTINGS`$/'"$(sed -E 's/([\\\&\/])/\\\1/g; s/^/        /; s/$/\\/' < default_settings.json) "/g < "$FILE"
253
else
254
    cat $FILE
255
fi
256

    
257
echo "
258

    
259
$(map_get EXPORTCODES $FILEKEY)
260
})}).fun();") > $BUILDDIR/$FILE
261
	fi
262
    done
263

    
264
    if [ "$BROWSER" = "chromium" ]; then
265
	cat > $BUILDDIR/exports_init.js <<EOF
266
window.killtheweb={is_chrome: true, browser: window.chrome};
267
EOF
268
    else
269
	cat > $BUILDDIR/exports_init.js <<EOF
270
/* Polyfill for IceCat 60. */
271
String.prototype.matchAll = String.prototype.matchAll || function(regex) {
272
    if (regex.flags.search("g") === -1)
273
        throw new TypeError("String.prototype.matchAll called with a non-global RegExp argument");
274

    
275
    for (const matches = [];;) {
276
        if (matches[matches.push(regex.exec(this)) - 1] === null)
277
	    return matches.splice(0, matches.length - 1);
278
    }
279
}
280

    
281
window.killtheweb={is_mozilla: true, browser: this.browser};
282
EOF
283
    fi
284

    
285
    cp -r copyright licenses/ $BUILDDIR
286
    cp html/*.css $BUILDDIR/html
287
    mkdir $BUILDDIR/icons
288
    cp icons/*.png $BUILDDIR/icons
289

    
290
    if [ "$BROWSER" = "chromium" ]; then
291
	for MOZILLA_FILE in $(find $BUILDDIR -name "MOZILLA_*"); do
292
	    echo > "$MOZILLA_FILE"
293
	done
294
    fi
295
    if [ "$BROWSER" = "mozilla" ]; then
296
	for CHROMIUM_FILE in $(find $BUILDDIR -name "CHROMIUM_*"); do
297
	    echo > "$CHROMIUM_FILE"
298
	done
299
    fi
300
}
301

    
302
make_zip() (
303
    cd "$PACKDIR"
304
    if [ "x$KEY" = x ]; then
305
	${HAKETILO_ZIP:-zip -r} build.zip *;
306
    else
307
	${HAKETILO_CHROMIUM:-chromium} --pack-extension="$(realpath inner)" --pack-extension-key="$KEY"
308
	mv *.crx build.zip
309
    fi
310
)
(5-5/9)