From 9f5b7e79cce22d24e89d2628459be2ff0c1c415f Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 3 Jul 2023 16:40:33 +1000 Subject: [PATCH] Move markpure to TypeScript Combined with the previous changes, this allows the mobile clients to build the web components without having to set up a Python environment, and should speed up AnkiDroid CI. --- build/configure/src/web.rs | 9 ++----- build/ninja_gen/src/node.rs | 10 ++++---- pylib/tools/markpure.py | 28 --------------------- ts/tools/markpure.ts | 34 ++++++++++++++++++++++++++ ts/{sql_format => tools}/sql_format.ts | 0 5 files changed, 41 insertions(+), 40 deletions(-) delete mode 100644 pylib/tools/markpure.py create mode 100644 ts/tools/markpure.ts rename ts/{sql_format => tools}/sql_format.ts (100%) diff --git a/build/configure/src/web.rs b/build/configure/src/web.rs index b56014e82..1371d48f0 100644 --- a/build/configure/src/web.rs +++ b/build/configure/src/web.rs @@ -125,7 +125,7 @@ fn build_and_check_tslib(build: &mut Build) -> Result<()> { path.replace("proto/", "ts/lib/") .replace("proto\\", "ts/lib\\") }, - py_transform_script: "pylib/tools/markpure.py", + ts_transform_script: "ts/tools/markpure.ts", }, )?; // ensure _service files are generated by rslib @@ -186,12 +186,7 @@ fn declare_and_check_other_libraries(build: &mut Build) -> Result<()> { } } - eslint( - build, - "sql_format", - "ts/sql_format", - inputs![glob!("ts/sql_format/**")], - )?; + eslint(build, "scripts", "ts/tools", inputs![glob!("ts/tools/*")])?; Ok(()) } diff --git a/build/ninja_gen/src/node.rs b/build/ninja_gen/src/node.rs index 82f30e0c6..af92ecc24 100644 --- a/build/ninja_gen/src/node.rs +++ b/build/ninja_gen/src/node.rs @@ -322,7 +322,7 @@ impl BuildAction for SqlFormat { fn files(&mut self, build: &mut impl build::FilesHandle) { build.add_inputs("tsx", inputs![":node_modules:tsx"]); - build.add_inputs("sql_format", inputs!["ts/sql_format/sql_format.ts"]); + build.add_inputs("sql_format", inputs!["ts/tools/sql_format.ts"]); build.add_inputs("in", &self.inputs); let mode = if self.check_only { "check" } else { "fix" }; build.add_variable("mode", mode); @@ -338,14 +338,14 @@ pub struct GenTypescriptProto<'a> { /// Can be used to adjust the output js/dts files to point to out_dir. pub out_path_transform: fn(&str) -> String, /// Script to apply modifications to the generated files. - pub py_transform_script: &'static str, + pub ts_transform_script: &'static str, } impl BuildAction for GenTypescriptProto<'_> { fn command(&self) -> &str { "$protoc $includes $in \ --plugin $gen-es --es_out $out_dir && \ - $pyenv_bin $script $out_dir" + $tsx $transform_script $out_dir" } fn files(&mut self, build: &mut impl build::FilesHandle) { @@ -374,8 +374,8 @@ impl BuildAction for GenTypescriptProto<'_> { build.add_inputs("gen-es", inputs![":node_modules:protoc-gen-es"]); build.add_inputs_vec("in", proto_files); build.add_inputs("", inputs!["yarn.lock"]); - build.add_inputs("pyenv_bin", inputs![":pyenv:bin"]); - build.add_inputs("script", inputs![self.py_transform_script]); + build.add_inputs("tsx", inputs![":node_modules:tsx"]); + build.add_inputs("transform_script", inputs![self.ts_transform_script]); build.add_outputs("", output_files); } diff --git a/pylib/tools/markpure.py b/pylib/tools/markpure.py deleted file mode 100644 index cdf1f809f..000000000 --- a/pylib/tools/markpure.py +++ /dev/null @@ -1,28 +0,0 @@ -# Copyright: Ankitects Pty Ltd and contributors -# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html - -import os -import re -import sys - -root = sys.argv[1] - -type_re = re.compile(r'(make(Enum|MessageType))\(\n\s+".*",') -for dirpath, dirnames, filenames in os.walk(root): - for filename in filenames: - if filename.endswith(".js"): - file = os.path.join(dirpath, filename) - with open(file, "r", encoding="utf8") as f: - contents = f.read() - - # allow tree shaking on proto messages - contents = contents.replace( - "= proto3.make", "= /* @__PURE__ */ proto3.make" - ) - # strip out typeName info, which appears to only be required for - # certain JSON functionality (though this only saves a few hundred - # bytes) - contents = type_re.sub('\\1("",', contents) - - with open(file, "w", encoding="utf8") as f: - f.write(contents) diff --git a/ts/tools/markpure.ts b/ts/tools/markpure.ts new file mode 100644 index 000000000..6ddc53d28 --- /dev/null +++ b/ts/tools/markpure.ts @@ -0,0 +1,34 @@ +// Copyright: Ankitects Pty Ltd and contributors +// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + +import * as fs from "fs"; +import * as path from "path"; + +const root = process.argv[2]; +const typeRe = /(make(Enum|MessageType))\(\n\s+".*",/g; + +fs.readdirSync(root, { withFileTypes: true }).forEach(dirEnt => { + const dirPath = path.join(root, dirEnt.name); + + if (dirEnt.isDirectory()) { + fs.readdirSync(dirPath).forEach(fileName => { + if (fileName.endsWith(".js")) { + const file = path.join(dirPath, fileName); + let contents = fs.readFileSync(file, "utf8"); + + // allow tree shaking on proto messages + contents = contents.replace( + "= proto3.make", + "= /* @__PURE__ */ proto3.make", + ); + + // strip out typeName info, which appears to only be required for + // certain JSON functionality (though this only saves a few hundred + // bytes) + contents = contents.replace(typeRe, "$1(\"\","); + + fs.writeFileSync(file, contents, "utf8"); + } + }); + } +}); diff --git a/ts/sql_format/sql_format.ts b/ts/tools/sql_format.ts similarity index 100% rename from ts/sql_format/sql_format.ts rename to ts/tools/sql_format.ts