aboutsummaryrefslogtreecommitdiff
path: root/build.sh
diff options
context:
space:
mode:
Diffstat (limited to 'build.sh')
-rwxr-xr-xbuild.sh257
1 files changed, 61 insertions, 196 deletions
diff --git a/build.sh b/build.sh
index ed6a141..3d3d4be 100755
--- a/build.sh
+++ b/build.sh
@@ -3,180 +3,70 @@
# Copyright (C) 2021 Wojtek Kosior
# Redistribution terms are gathered in the `copyright' file.
-handle_export_line() {
- if [ "x$1" = "xEXPORTS_START" ]; then
- if [ "$STATE" = "before_block" ]; then
- STATE="in_block"
- fi
- elif [ "x$1" = "xEXPORT" ]; then
- if [ "$STATE" != "in_block" ]; then
- return
- fi
-
- EXPORTCODE="${EXPORTCODE}window.killtheweb.$2 = $2;$ENDL"
-
- PREVIOUS_FILE="$(map_get EXPORTS $2)"
- if [ "x$PREVIOUS_FILE" != "x" ]; then
- errcho "export $2 present in both $PREVIOUS_FILE and $FILE"
- return 1
- fi
-
- map_set_instr EXPORTS $2 "$FILE"
-
- elif [ "x$1" = "xEXPORTS_END" ]; then
- if [ "$STATE" = "in_block" ]; then
- STATE="after_block"
- fi
- fi
-}
-
-translate_exports() {
- STATE="before_block"
- EXPORTCODE=''
-
- while read EXPORT_LINE; do
- handle_export_line $EXPORT_LINE || return 1
- done
-
- map_set_instr EXPORTCODES $FILEKEY "$EXPORTCODE"
-}
-
-add_exports() {
- FILE="$1"
- FILEKEY="$(sanitize "$FILE")"
-
- eval "$(grep -o 'EXPORT.\+' "$1" | translate_exports || exit 1)"
-}
-
-handle_import_line() {
- if [ "x$1" = "xIMPORTS_START" ]; then
- if [ "$STATE" = "before_block" ]; then
- STATE="in_block"
- fi
- elif [ "x$1" = "xIMPORT" ]; then
- if [ "$STATE" != "in_block" ]; then
- return
- fi
-
- IMPORTCODE="${IMPORTCODE}const $2 = window.killtheweb.$2;$ENDL"
-
- IMPORTS="$IMPORTS $2"
-
- elif [ "x$1" = "xIMPORTS_END" ]; then
- if [ "$STATE" = "in_block" ]; then
- STATE="after_block"
- fi
- fi
-}
-
-translate_imports() {
- STATE="before_block"
- IMPORTCODE=''
- IMPORTS=''
-
- while read IMPORT_LINE; do
- handle_import_line $IMPORT_LINE || return 1
- done
-
- map_set_instr IMPORTCODES $FILEKEY "$IMPORTCODE"
- map_set_instr IMPORTS $FILEKEY "$IMPORTS"
-}
-
-add_imports() {
- FILE="$1"
- FILEKEY="$(sanitize "$FILE")"
-
- eval "$(grep -o 'IMPORT.\+' "$1" | translate_imports || exit 1)"
-}
+set -e
-compute_scripts_list_rec() {
- local FILE="$1"
- local FILEKEY=$(sanitize "$1")
-
- local FILESTATE="$(map_get FILESTATES $FILEKEY)"
- if [ "xprocessed" = "x$FILESTATE" ]; then
- return
- fi
- if [ "xprocessing" = "x$FILESTATE" ]; then
- errcho "import loop on $FILE"
- return 1
- fi
-
- USED="$USED $FILEKEY"
-
- map_set FILESTATES $FILEKEY "processing"
-
- local IMPORT
- for IMPORT in $(map_get IMPORTS $FILEKEY); do
- NEXT_FILE="$(map_get EXPORTS $IMPORT)"
- if [ "x" = "x$NEXT_FILE" ]; then
- errcho "nothing exports $IMPORT, required by $FILE"
- return 1
- fi
- if ! compute_scripts_list_rec "$NEXT_FILE"; then
- errcho "when satisfying $IMPORT for $FILE"
- return 1
- fi
- done
-
- [ "x$FILE" = "xexports_init.js" ] || echo $FILE # exports_init.js is hardcoded to load first; the entire export system depends on it
- map_set FILESTATES $FILEKEY "processed"
-}
-
-compute_scripts_list() {
- USED=''
- echo COMPUTED_SCRIPTS=\"exports_init.js
- compute_scripts_list_rec "$1"
- echo \"
-
- for FILEKEY in $USED; do
- map_set_instr USED $FILEKEY yes
- done
-}
+. ./shell_utils.sh
as_json_list() {
while true; do
if [ "x" = "x$2" ]; then
- echo -n '\\n'"\t\t\"$1\""'\\n\t'
+ printf '\\n\t\t"%s"\\n\t' "$1"
return
fi
- echo -n '\\n'"\t\t\"$1\","
+ printf '\\n\t\t"%s",' "$1"
shift
done
}
as_html_list() {
while [ "x" != "x$1" ]; do
- echo -n '\\n'" <script src=\"/$1\"></script>"
+ printf '\\n <script src="/%s"></script>' "$1"
shift
done
}
-build_main() {
- # placate importers of these, as they are exported by the yet-to-be-created exports_init.js
- EXPORTS__browser=exports_init.js
- EXPORTS__is_chrome=exports_init.js
- EXPORTS__is_mozilla=exports_init.js
+compute_scripts() {
+ local DIRS="$1"
+ local ROOT_SCRIPT="$2"
+
+ local AVAILABLE="$(find $DIRS -name '[^.#]*.js')"
+
+ awk -f compute_scripts.awk script_dependencies "$ROOT_SCRIPT" $AVAILABLE
+}
- SCRIPTDIRS='background html common content'
+build_main() {
+ local ALL_SCRIPTDIRS='background html common content'
- SCRIPTS=$(find $SCRIPTDIRS -name '[^.#]*.js')
+ local ALL_SCRIPTS_AVAILABLE="$(find $ALL_SCRIPTDIRS -name '[^.#]*.js')"
- for SCRIPT in $SCRIPTS; do
- add_exports $SCRIPT
- add_imports $SCRIPT
+ local SCRIPT
+ for SCRIPT in $ALL_SCRIPTS_AVAILABLE; do
+ map_set SCRIPTS_UNUSED $(sanitize $SCRIPT) yes
done
- eval "$(compute_scripts_list background/main.js || exit 1)"
- BGSCRIPTS="$(as_json_list $COMPUTED_SCRIPTS)"
- eval "$(compute_scripts_list content/main.js || exit 1)"
- CONTENTSCRIPTS="$(as_json_list $COMPUTED_SCRIPTS)"
- eval "$(compute_scripts_list html/display-panel.js || exit 1)"
- POPUPSCRIPTS="$(as_html_list $COMPUTED_SCRIPTS)"
- eval "$(compute_scripts_list html/options_main.js || exit 1)"
- OPTIONSSCRIPTS="$(as_html_list $COMPUTED_SCRIPTS)"
+ local ROOT=background/main.js
+ local SCRIPTS_BG="$( compute_scripts 'common/ background/' $ROOT)"
+
+ local ROOT=content/main.js
+ local SCRIPTS_CONTENT="$( compute_scripts 'common/ content/' $ROOT)"
+
+ local ROOT=html/display-panel.js
+ local SCRIPTS_POPUP="$( compute_scripts 'common/ html/' $ROOT)"
+
+ local ROOT=html/options_main.js
+ local SCRIPTS_OPTIONS="$( compute_scripts 'common/ html/' $ROOT)"
- for DIR in $(find $SCRIPTDIRS -type d); do
+ local BGSCRIPTS="$( as_json_list $SCRIPTS_BG )"
+ local CONTENTSCRIPTS="$( as_json_list $SCRIPTS_CONTENT )"
+ local POPUPSCRIPTS="$( as_html_list $SCRIPTS_POPUP )"
+ local OPTIONSSCRIPTS="$( as_html_list $SCRIPTS_OPTIONS )"
+
+ for SCRIPT in $SCRIPTS_BG $SCRIPTS_CONTENT $SCRIPTS_POPUP $SCRIPTS_OPTIONS
+ do
+ map_del SCRIPTS_UNUSED $(sanitize $SCRIPT)
+ done
+
+ for DIR in $(find $ALL_SCRIPTDIRS -type d); do
mkdir -p "$BUILDDIR"/$DIR
done
@@ -214,53 +104,24 @@ s^_CONTENTSCRIPTS_^$CONTENTSCRIPTS^" \
sed "s^_OPTIONSSCRIPTS_^$OPTIONSSCRIPTS^" \
> "$BUILDDIR"/html/options.html
- for FILE in $SCRIPTS; do
+ for FILE in $ALL_SCRIPTS_AVAILABLE; do
FILEKEY=$(sanitize "$FILE")
- if [ "xyes" != "x$(map_get USED $FILEKEY)" ]; then
- errcho "WARNING! $FILE not used"
+ if [ "x$(map_get SCRIPTS_UNUSED $FILEKEY)" = "xyes" ]; then
+ printf 'WARNING! %s not used\n' "$FILE" >&2
else
- (echo "\
-\"use strict\";
-
-({fun: (function() {
-$(map_get IMPORTCODES $FILEKEY)
-
-";
-
-# A hack to insert the contents of default_settings.json at the appropriate location in background/main.js
-if [ "$FILE" = "background/main.js" ]; then
- # Uses an internal sed expression to escape and indent the JSON file for use in the external sed expression
- sed 's/^ `DEFAULT SETTINGS`$/'"$(sed -E 's/([\\\&\/])/\\\1/g; s/^/ /; s/$/\\/' < default_settings.json) "/g < "$FILE"
-else
- cat $FILE
-fi
-
-echo "
-
-$(map_get EXPORTCODES $FILEKEY)
-})}).fun();") > "$BUILDDIR"/$FILE
+ awk -f compute_scripts.awk wrapped_code "$FILE" > "$BUILDDIR"/$FILE
fi
done
+ # A hack to insert the contents of default_settings.json at the appropriate
+ # location in background/main.js. Uses an internal sed expression to escape
+ # and indent the JSON file for use in the external sed expression.
+ sed -i 's/^ `DEFAULT SETTINGS`$/'"$(sed -E 's/([\\\&\/])/\\\1/g; s/^/ /; s/$/\\/' < default_settings.json) "/g "$BUILDDIR"/background/main.js
+
if [ "$BROWSER" = "chromium" ]; then
- cat > "$BUILDDIR"/exports_init.js <<EOF
-window.killtheweb={is_chrome: true, browser: window.chrome};
-EOF
+ cp CHROMIUM_exports_init.js "$BUILDDIR"/exports_init.js
else
- cat > "$BUILDDIR"/exports_init.js <<EOF
-/* Polyfill for IceCat 60. */
-String.prototype.matchAll = String.prototype.matchAll || function(regex) {
- if (regex.flags.search("g") === -1)
- throw new TypeError("String.prototype.matchAll called with a non-global RegExp argument");
-
- for (const matches = [];;) {
- if (matches[matches.push(regex.exec(this)) - 1] === null)
- return matches.splice(0, matches.length - 1);
- }
-}
-
-window.killtheweb={is_mozilla: true, browser: this.browser};
-EOF
+ cp MOZILLA_exports_init.js "$BUILDDIR"/exports_init.js
fi
cp -r copyright licenses/ "$BUILDDIR"
@@ -271,21 +132,26 @@ EOF
if [ "$BROWSER" = "chromium" ]; then
for MOZILLA_FILE in $(find "$BUILDDIR" -name "MOZILLA_*"); do
- echo > "$MOZILLA_FILE"
+ printf '\n' > "$MOZILLA_FILE"
done
fi
if [ "$BROWSER" = "mozilla" ]; then
for CHROMIUM_FILE in $(find "$BUILDDIR" -name "CHROMIUM_*"); do
- echo > "$CHROMIUM_FILE"
+ printf '\n' > "$CHROMIUM_FILE"
done
fi
}
+print_usage() {
+ printf 'usage: %s mozilla|chromium [source directory] [update url]\n' \
+ "$0" >&2
+}
+
main() {
if [ "x$1" = "xmozilla" -o "x$1" = "xchromium" ]; then
BROWSER=$1
else
- errcho "usage: $0 mozilla|chromium [source directory] [update url]"
+ print_usage
exit 1
fi
@@ -296,13 +162,12 @@ main() {
mkdir "$BUILDDIR"
cd "$SRCDIR"
else
- errcho "usage: $0 mozilla|chromium [source directory] [update url]"
+ print_usage
exit 2
fi
UPDATE_URL="$3"
- . ./shell_utils.sh
build_main
}