summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2022-01-03 16:25:59 +0100
committerWojtek Kosior <koszko@koszko.org>2022-01-03 16:25:59 +0100
commitc71ebff86fa79b20388749dd4781fd96fcc5c63a (patch)
treeb51e2e80c25912e33ace31fd68888d8bb4a5445c
parent0feb9db2b57725d47a7b3cc1e84ba5b9a7553b12 (diff)
downloadbrowser-extension-c71ebff86fa79b20388749dd4781fd96fcc5c63a.tar.gz
browser-extension-c71ebff86fa79b20388749dd4781fd96fcc5c63a.zip
more general way to provide additional lines of code that compute_scripts.awk should process as part of a js file
-rwxr-xr-xcompute_scripts.awk69
-rw-r--r--test/script_loader.py7
2 files changed, 47 insertions, 29 deletions
diff --git a/compute_scripts.awk b/compute_scripts.awk
index e17d12c..43d8a65 100755
--- a/compute_scripts.awk
+++ b/compute_scripts.awk
@@ -23,10 +23,11 @@ BEGIN {
}
BEGIN {
- identifier_re = "[_a-zA-Z][_a-zA-Z0-9]*"
- path_dir_re = "([-_a-zA-Z0-9][-._a-zA-Z0-9]*/)*"
- path_ext_re = "(\\.[-_.a-zA-Z0-9]*)?"
- path_re = "^" path_dir_re identifier_re path_ext_re "$"
+ identifier_re = "[_a-zA-Z][_a-zA-Z0-9]*"
+ path_dir_re = "([-_a-zA-Z0-9][-._a-zA-Z0-9]*/)*"
+ path_ext_re = "(\\.[-_.a-zA-Z0-9]*)?"
+ path_re_noanchor = path_dir_re identifier_re path_ext_re
+ path_re = "^" path_re_noanchor "$"
if_clause_re = "!?" identifier_re
if_AND_re = "([[:space:]]+&&[[:space:]]+" if_clause_re ")*"
@@ -158,11 +159,11 @@ function process_file(path, read_path, mode,
return 1
}
if (result == 0) {
- if (path != js_to_amalgamate || \
- additional_line_nr == additional_lines_count)
+ if (!(path in appended_lines_counts) || \
+ additional_line_nr == appended_lines_counts[path])
break
- line = amalgamation_additional_lines[++additional_line_nr]
+ line = appended_lines[path,++additional_line_nr]
}
if (line !~ /^#/) {
@@ -180,10 +181,9 @@ function process_file(path, read_path, mode,
return 1
}
if (result == 0) {
- if (path == js_to_amalgamate && \
- additional_line_nr < additional_lines_count) {
- line_part = \
- amalgamation_additional_lines[++additional_line_nr]
+ if (path in appended_lines_counts && \
+ additional_line_nr < appended_lines_counts[path]) {
+ line_part = appended_lines[path,++additional_line_nr]
} else {
printf "ERROR: Unexpected EOF in %s\n",
read_path > "/dev/stderr"
@@ -613,7 +613,7 @@ function print_amalgamation(js_deps, js_deps_count,
}
function print_usage() {
- printf "USAGE: %s compute_scripts.awk -- [-D PREPROCESSOR_DEFINITION]... [-M manifest/to/process/manifest.json]... [-H html/to/process.html]... [-J js/to/process.js]... [--help|-h] [--output-dir=./build] [--write-js-deps] [--write-html-deps] [--output=files-to-copy|--output=amalgamate-js:js/to/process.js[:additional-code]]\n",
+ printf "USAGE: %s compute_scripts.awk -- [-D PREPROCESSOR_DEFINITION]... [-M manifest/to/process/manifest.json]... [-H html/to/process.html]... [-J js/to/process.js]... [-A js/to/append/to.js:appended_code]... [--help|-h] [--output-dir=./build] [--write-js-deps] [--write-html-deps] [--output=files-to-copy|--output=amalgamate-js:js/to/process.js]\n",
ARGV[0] > "/dev/stderr"
}
@@ -622,18 +622,22 @@ BEGIN {
option_arg_patterns["M"] = path_re
option_arg_patterns["H"] = path_re
option_arg_patterns["J"] = path_re
+ option_arg_patterns["A"] = "^" path_re_noanchor ":"
}
-function main(i, path, letter, dir, max_line_nr, js_deps, js_deps_count,
- amalgamation_additional_code) {
+function main(i, j, path, letter, dir, max_line_nr, js_deps, js_deps_count,
+ code, lines) {
output_dir = "./build"
write_js_deps = false
write_html_deps = false
+ delete appended_lines[0]
+ delete appended_lines_counts[0]
+
+ delete lines[0]
+
output = ""
js_to_amalgamate = ""
- delete amalgamation_additional_lines[0]
- additional_lines_count = 0
delete main_js_lines[0]
delete manifests_to_process[0]
@@ -643,7 +647,7 @@ function main(i, path, letter, dir, max_line_nr, js_deps, js_deps_count,
delete explicitly_requested[0]
for (i = 1; i < ARGC; i++) {
- if (ARGV[i] ~ /^-[DMHJ]$/) {
+ if (ARGV[i] ~ /^-[DMHJA]$/) {
letter = substr(ARGV[i++], 2)
if (i == ARGC || ARGV[i] !~ option_arg_patterns[letter]) {
printf "ERROR: '-%s' option should be followed by an argument matching '%s'\n",
@@ -662,6 +666,25 @@ function main(i, path, letter, dir, max_line_nr, js_deps, js_deps_count,
html_to_process[ARGV[i]]
if (letter == "J")
js_to_process[ARGV[i]]
+
+ if (letter == "A") {
+ path = ARGV[i]
+ sub(/:.*$/, "", path)
+ if (path in appended_lines_counts) {
+ printf "ERROR: The same file %s given to the '-A' option multiple times\n",
+ path > "/dev/stderr"
+ return 1
+ }
+
+ modes[path] = "js"
+
+ clear_array(lines)
+ code = ARGV[i]
+ sub(/^[^:]+:/, "", code)
+ appended_lines_counts[path] = split(code, lines, "\n")
+ for (j = appended_lines_counts[path]; j > 0; j--)
+ appended_lines[path,j] = lines[j]
+ }
} else if (ARGV[i] ~ /^-(-help|h)$/ ) {
print_usage()
return 0
@@ -676,21 +699,13 @@ function main(i, path, letter, dir, max_line_nr, js_deps, js_deps_count,
output = "files-to-copy"
} else if (ARGV[i] ~ /^--output=amalgamate-js:/) {
output = "amalgamate-js"
- amalgamation_additional_code = ARGV[i]
- sub(/^--output=amalgamate-js:/, "", amalgamation_additional_code)
- js_to_amalgamate = amalgamation_additional_code
- sub(/:.*$/, "", js_to_amalgamate)
+ js_to_amalgamate = ARGV[i]
+ sub(/^--output=amalgamate-js:/, "", js_to_amalgamate)
if (js_to_amalgamate !~ path_re) {
printf "ERROR: amalgamate-js path does not match '%s': %s\n",
path_re, js_to_amalgamate > "/dev/stderr"
return 1
}
- sub(/^[^:]+:?/, "", amalgamation_additional_code)
- if (amalgamation_additional_code) {
- additional_lines_count = split(amalgamation_additional_code,
- amalgamation_additional_lines,
- "\n")
- }
} else {
printf "ERROR: Unknown option '%s'\n", ARGV[i] > "/dev/stderr"
print_usage()
diff --git a/test/script_loader.py b/test/script_loader.py
index 53de779..f527d9e 100644
--- a/test/script_loader.py
+++ b/test/script_loader.py
@@ -59,13 +59,16 @@ def load_script(path, code_to_add=None):
a dependency to be substituted by a mocked value.
"""
path = make_relative_path(path)
- key = f'{str(path)}:{code_to_add}' if code_to_add is not None else str(path)
+ key = (str(path), code_to_add)
if key in script_cache:
return script_cache[key]
+ append_flags = () if code_to_add is None else ('-A', ':'.join(key))
+
awk = subprocess.run(['awk', '-f', str(awk_script), '--', '-D', 'MOZILLA',
'-D', 'MV2', '-D', 'TEST', '-D', 'UNIT_TEST',
- '-D', 'DEBUG', '--output=amalgamate-js:' + key],
+ '-D', 'DEBUG', *append_flags,
+ '--output=amalgamate-js:' + str(path)],
stdout=subprocess.PIPE, cwd=script_root, check=True)
script = awk.stdout.decode()
script_cache[key] = script