Project

General

Profile

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

haketilo / lib_build.sh @ e7c425cc

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
    GECKO_APPLICATIONS=''
189

    
190
    if [ "$BROWSER" = "chromium" ]; then
191
	CHROMIUM_KEY="$(dd if=/dev/urandom bs=32 count=1 2>/dev/null | base64)"
192
	CHROMIUM_KEY=$(echo chromium-key-dummy-file-$CHROMIUM_KEY | tr / -)
193
	touch $BUILDDIR/$CHROMIUM_KEY
194

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

    
213
    sed "\
214
s^_GECKO_APPLICATIONS_^$GECKO_APPLICATIONS^
215
s^_CHROMIUM_KEY_^$CHROMIUM_KEY^
216
s^_BGSCRIPTS_^$BGSCRIPTS^
217
s^_CONTENTSCRIPTS_^$CONTENTSCRIPTS^" \
218
	< manifest.json > $BUILDDIR/manifest.json
219

    
220
    ./process_html_file.sh html/display-panel.html |
221
	sed "s^_POPUPSCRIPTS_^$POPUPSCRIPTS^" \
222
	    > $BUILDDIR/html/display-panel.html
223

    
224
    ./process_html_file.sh html/options.html |
225
	sed "s^_OPTIONSSCRIPTS_^$OPTIONSSCRIPTS^" \
226
	    > $BUILDDIR/html/options.html
227

    
228
    for FILE in $SCRIPTS; do
229
	FILEKEY=$(sanitize "$FILE")
230
	if [ "xyes" != "x$(map_get USED $FILEKEY)" ]; then
231
	    errcho "WARNING! $FILE not used"
232
	else
233
	    (echo "\
234
\"use strict\";
235

    
236
({fun: (function() {
237
$(map_get IMPORTCODES $FILEKEY)
238

    
239
";
240

    
241
# A hack to insert the contents of default_settings.json at the appropriate location in background/main.js
242
if [ "$FILE" = "background/main.js" ]; then
243
    # Uses an internal sed expression to escape and indent the JSON file for use in the external sed expression
244
    sed 's/^        `DEFAULT SETTINGS`$/'"$(sed -E 's/([\\\&\/])/\\\1/g; s/^/        /; s/$/\\/' < default_settings.json) "/g < "$FILE"
245
else
246
    cat $FILE
247
fi
248

    
249
echo "
250

    
251
$(map_get EXPORTCODES $FILEKEY)
252
})}).fun();") > $BUILDDIR/$FILE
253
	fi
254
    done
255

    
256
    if [ "$BROWSER" = "chromium" ]; then
257
	cat > $BUILDDIR/exports_init.js <<EOF
258
window.killtheweb={is_chrome: true, browser: window.chrome};
259
EOF
260
    else
261
	cat > $BUILDDIR/exports_init.js <<EOF
262
/* Polyfill for IceCat 60. */
263
String.prototype.matchAll = String.prototype.matchAll || function(regex) {
264
    if (regex.flags.search("g") === -1)
265
        throw new TypeError("String.prototype.matchAll called with a non-global RegExp argument");
266

    
267
    for (const matches = [];;) {
268
        if (matches[matches.push(regex.exec(this)) - 1] === null)
269
	    return matches.splice(0, matches.length - 1);
270
    }
271
}
272

    
273
window.killtheweb={is_mozilla: true, browser: this.browser};
274
EOF
275
    fi
276

    
277
    cp -r copyright licenses/ $BUILDDIR
278
    cp html/*.css $BUILDDIR/html
279
    mkdir $BUILDDIR/icons
280
    cp icons/*.png $BUILDDIR/icons
281

    
282
    if [ "$BROWSER" = "chromium" ]; then
283
	for MOZILLA_FILE in $(find $BUILDDIR -name "MOZILLA_*"); do
284
	    echo > "$MOZILLA_FILE"
285
	done
286
    fi
287
    if [ "$BROWSER" = "mozilla" ]; then
288
	for CHROMIUM_FILE in $(find $BUILDDIR -name "CHROMIUM_*"); do
289
	    echo > "$CHROMIUM_FILE"
290
	done
291
    fi
292
}
293

    
294
make_zip() (
295
    cd "$BUILDDIR"
296
    case $BROWSER in
297
	mozilla) $ZIP_COMMAND build.zip *;;
298
	chromium) exit 92;; # Still to do
299
    esac
300
)
(5-5/9)