mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00
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.
This commit is contained in:
parent
cb8007ce30
commit
9f5b7e79cc
5 changed files with 41 additions and 40 deletions
|
@ -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(())
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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)
|
34
ts/tools/markpure.ts
Normal file
34
ts/tools/markpure.ts
Normal file
|
@ -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");
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
Loading…
Reference in a new issue