From c71ebff86fa79b20388749dd4781fd96fcc5c63a Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Mon, 3 Jan 2022 16:25:59 +0100 Subject: more general way to provide additional lines of code that compute_scripts.awk should process as part of a js file --- compute_scripts.awk | 69 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 27 deletions(-) (limited to 'compute_scripts.awk') 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() -- cgit v1.2.3