From 72067b0269021676b0433130db764dcb1484ef12 Mon Sep 17 00:00:00 2001 From: Gary Miller Date: Mon, 24 Apr 2023 11:04:05 +1000 Subject: [PATCH] using rust_embed for ts runtime --- rust/compiler/src/cli/tsgen/mod.rs | 57 ++++---- .../cli/tsgen/{runtime => ts-runtime}/adl.ts | 0 .../tsgen/{runtime => ts-runtime}/dynamic.ts | 0 .../cli/tsgen/{runtime => ts-runtime}/json.ts | 0 .../cli/tsgen/ts-runtime/package-dist.json | 10 ++ .../{runtime => ts-runtime}/package.json | 0 .../{runtime => ts-runtime}/sys/adlast.ts | 0 .../{runtime => ts-runtime}/sys/dynamic.ts | 0 .../{runtime => ts-runtime}/sys/types.ts | 0 .../src/cli/tsgen/ts-runtime/tsconfig.json | 13 ++ .../tsgen/{runtime => ts-runtime}/utils.ts | 0 rust/compiler/src/main.rs | 18 ++- rust/compiler/src/processing/loader.rs | 2 +- .../tests/test1/rs-output/.adl-manifest | 5 - .../compiler/tests/test1/rs-output/adl/mod.rs | 2 - .../test1/rs-output/adl/sys/annotations.rs | 23 ---- .../tests/test1/rs-output/adl/sys/mod.rs | 1 - .../tests/test1/rs-output/adl/test1.rs | 0 .../compiler/tests/test2/rs-output/adl/mod.rs | 2 - .../test2/rs-output/adl/sys/annotations.rs | 23 ---- .../tests/test2/rs-output/adl/sys/mod.rs | 1 - .../tests/test2/rs-output/adl/test2.rs | 128 ------------------ 22 files changed, 74 insertions(+), 211 deletions(-) rename rust/compiler/src/cli/tsgen/{runtime => ts-runtime}/adl.ts (100%) rename rust/compiler/src/cli/tsgen/{runtime => ts-runtime}/dynamic.ts (100%) rename rust/compiler/src/cli/tsgen/{runtime => ts-runtime}/json.ts (100%) create mode 100644 rust/compiler/src/cli/tsgen/ts-runtime/package-dist.json rename rust/compiler/src/cli/tsgen/{runtime => ts-runtime}/package.json (100%) rename rust/compiler/src/cli/tsgen/{runtime => ts-runtime}/sys/adlast.ts (100%) rename rust/compiler/src/cli/tsgen/{runtime => ts-runtime}/sys/dynamic.ts (100%) rename rust/compiler/src/cli/tsgen/{runtime => ts-runtime}/sys/types.ts (100%) create mode 100644 rust/compiler/src/cli/tsgen/ts-runtime/tsconfig.json rename rust/compiler/src/cli/tsgen/{runtime => ts-runtime}/utils.ts (100%) delete mode 100644 rust/compiler/tests/test1/rs-output/.adl-manifest delete mode 100644 rust/compiler/tests/test1/rs-output/adl/mod.rs delete mode 100644 rust/compiler/tests/test1/rs-output/adl/sys/annotations.rs delete mode 100644 rust/compiler/tests/test1/rs-output/adl/sys/mod.rs delete mode 100644 rust/compiler/tests/test1/rs-output/adl/test1.rs delete mode 100644 rust/compiler/tests/test2/rs-output/adl/mod.rs delete mode 100644 rust/compiler/tests/test2/rs-output/adl/sys/annotations.rs delete mode 100644 rust/compiler/tests/test2/rs-output/adl/sys/mod.rs delete mode 100644 rust/compiler/tests/test2/rs-output/adl/test2.rs diff --git a/rust/compiler/src/cli/tsgen/mod.rs b/rust/compiler/src/cli/tsgen/mod.rs index b52f3493..658f4e78 100644 --- a/rust/compiler/src/cli/tsgen/mod.rs +++ b/rust/compiler/src/cli/tsgen/mod.rs @@ -29,26 +29,31 @@ mod generate; #[cfg(test)] mod tests; mod utils; - -const RUNTIME_PACKAGE: &[u8] = include_bytes!("runtime/package.json"); -const RUNTIME_JSON: &[u8] = include_bytes!("runtime/json.ts"); -const RUNTIME_ADL: &[u8] = include_bytes!("runtime/adl.ts"); -const RUNTIME_UTILS: &[u8] = include_bytes!("runtime/utils.ts"); -const RUNTIME_DYNAMIC: &[u8] = include_bytes!("runtime/dynamic.ts"); -const RUNTIME_SYS_DYNAMIC: &[u8] = include_bytes!("runtime/sys/dynamic.ts"); -const RUNTIME_SYS_ADLAST: &[u8] = include_bytes!("runtime/sys/adlast.ts"); -const RUNTIME_SYS_TYPES: &[u8] = include_bytes!("runtime/sys/types.ts"); - -const RUNTIME: [&'static (&str, &[u8]); 8] = [ - &("package.json", RUNTIME_PACKAGE), - &("json.ts", RUNTIME_JSON), - &("adl.ts", RUNTIME_ADL), - &("utils.ts", RUNTIME_UTILS), - &("dynamic.ts", RUNTIME_DYNAMIC), - &("sys/dynamic.ts", RUNTIME_SYS_DYNAMIC), - &("sys/adlast.ts", RUNTIME_SYS_ADLAST), - &("sys/types.ts", RUNTIME_SYS_TYPES), -]; +use rust_embed::RustEmbed; + +#[derive(RustEmbed)] +#[folder = "src/cli/tsgen/ts-runtime/"] +struct Asset; + +// const RUNTIME_PACKAGE: &[u8] = include_bytes!("runtime/package.json"); +// const RUNTIME_JSON: &[u8] = include_bytes!("runtime/json.ts"); +// const RUNTIME_ADL: &[u8] = include_bytes!("runtime/adl.ts"); +// const RUNTIME_UTILS: &[u8] = include_bytes!("runtime/utils.ts"); +// const RUNTIME_DYNAMIC: &[u8] = include_bytes!("runtime/dynamic.ts"); +// const RUNTIME_SYS_DYNAMIC: &[u8] = include_bytes!("runtime/sys/dynamic.ts"); +// const RUNTIME_SYS_ADLAST: &[u8] = include_bytes!("runtime/sys/adlast.ts"); +// const RUNTIME_SYS_TYPES: &[u8] = include_bytes!("runtime/sys/types.ts"); + +// const RUNTIME: [&'static (&str, &[u8]); 8] = [ +// &("package.json", RUNTIME_PACKAGE), +// &("json.ts", RUNTIME_JSON), +// &("adl.ts", RUNTIME_ADL), +// &("utils.ts", RUNTIME_UTILS), +// &("dynamic.ts", RUNTIME_DYNAMIC), +// &("sys/dynamic.ts", RUNTIME_SYS_DYNAMIC), +// &("sys/adlast.ts", RUNTIME_SYS_ADLAST), +// &("sys/types.ts", RUNTIME_SYS_TYPES), +// ]; const TSC_B64: &[u8] = b"import {fromByteArray as b64Encode, toByteArray as b64Decode} from 'base64-js'"; @@ -444,23 +449,27 @@ fn gen_runtime( ts_style: &TsStyle, writer: &mut TreeWriter, ) -> anyhow::Result<()> { + println!("Writing Runtime to file system ..."); let re = Regex::new(r"\$TSEXT").unwrap(); let re2 = Regex::new(r"\$TSB64IMPORT").unwrap(); - for rt in RUNTIME.iter() { + for rt_name in Asset::iter() { + // println!(" '{}'", rt_name); let mut file_path = PathBuf::new(); if !strip_first { file_path.push("./runtime"); } // file_path.push(&rt_gen_opts.runtime_dir); - file_path.push(rt.0); + file_path.push(rt_name.as_ref()); let dir_path = file_path.parent().unwrap(); std::fs::create_dir_all(dir_path)?; log::info!("writing {}", file_path.display()); + let data = Asset::get(rt_name.as_ref()).unwrap(); + let content = data.data.as_ref(); match ts_style { TsStyle::Tsc => { - let content = re.replace_all(rt.1, "".as_bytes()); + let content = re.replace_all(content, "".as_bytes()); let content = re2.replace(&content, TSC_B64); let x = content.deref(); let y = String::from_utf8(x.to_vec())?; @@ -469,7 +478,7 @@ fn gen_runtime( // .map_err(|e| anyhow!("error writing runtime file {}", e.to_string()))?; } TsStyle::Deno => { - let content = re.replace_all(rt.1, ".ts".as_bytes()); + let content = re.replace_all(content, ".ts".as_bytes()); let content = re2.replace(&content, DENO_B64); let x = content.deref(); let y = String::from_utf8(x.to_vec())?; diff --git a/rust/compiler/src/cli/tsgen/runtime/adl.ts b/rust/compiler/src/cli/tsgen/ts-runtime/adl.ts similarity index 100% rename from rust/compiler/src/cli/tsgen/runtime/adl.ts rename to rust/compiler/src/cli/tsgen/ts-runtime/adl.ts diff --git a/rust/compiler/src/cli/tsgen/runtime/dynamic.ts b/rust/compiler/src/cli/tsgen/ts-runtime/dynamic.ts similarity index 100% rename from rust/compiler/src/cli/tsgen/runtime/dynamic.ts rename to rust/compiler/src/cli/tsgen/ts-runtime/dynamic.ts diff --git a/rust/compiler/src/cli/tsgen/runtime/json.ts b/rust/compiler/src/cli/tsgen/ts-runtime/json.ts similarity index 100% rename from rust/compiler/src/cli/tsgen/runtime/json.ts rename to rust/compiler/src/cli/tsgen/ts-runtime/json.ts diff --git a/rust/compiler/src/cli/tsgen/ts-runtime/package-dist.json b/rust/compiler/src/cli/tsgen/ts-runtime/package-dist.json new file mode 100644 index 00000000..13f2dd89 --- /dev/null +++ b/rust/compiler/src/cli/tsgen/ts-runtime/package-dist.json @@ -0,0 +1,10 @@ +{ + "name": "@adl-lang/runtime", + "version": "0.0.0", + "license": "MIT", + "main": "./index.js", + "types": "./index.d.ts", + "dependencies": { + "base64-js": "^1.5.1" + } +} diff --git a/rust/compiler/src/cli/tsgen/runtime/package.json b/rust/compiler/src/cli/tsgen/ts-runtime/package.json similarity index 100% rename from rust/compiler/src/cli/tsgen/runtime/package.json rename to rust/compiler/src/cli/tsgen/ts-runtime/package.json diff --git a/rust/compiler/src/cli/tsgen/runtime/sys/adlast.ts b/rust/compiler/src/cli/tsgen/ts-runtime/sys/adlast.ts similarity index 100% rename from rust/compiler/src/cli/tsgen/runtime/sys/adlast.ts rename to rust/compiler/src/cli/tsgen/ts-runtime/sys/adlast.ts diff --git a/rust/compiler/src/cli/tsgen/runtime/sys/dynamic.ts b/rust/compiler/src/cli/tsgen/ts-runtime/sys/dynamic.ts similarity index 100% rename from rust/compiler/src/cli/tsgen/runtime/sys/dynamic.ts rename to rust/compiler/src/cli/tsgen/ts-runtime/sys/dynamic.ts diff --git a/rust/compiler/src/cli/tsgen/runtime/sys/types.ts b/rust/compiler/src/cli/tsgen/ts-runtime/sys/types.ts similarity index 100% rename from rust/compiler/src/cli/tsgen/runtime/sys/types.ts rename to rust/compiler/src/cli/tsgen/ts-runtime/sys/types.ts diff --git a/rust/compiler/src/cli/tsgen/ts-runtime/tsconfig.json b/rust/compiler/src/cli/tsgen/ts-runtime/tsconfig.json new file mode 100644 index 00000000..3367d02e --- /dev/null +++ b/rust/compiler/src/cli/tsgen/ts-runtime/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "tsconfig/base.json", + "include": ["."], + "exclude": ["dist", "build", "node_modules"], + "compilerOptions": { + "outDir": "dist", + // "target": "es2020", + // "module": "es2020", + "lib": [ + "es2020", + ] + } +} diff --git a/rust/compiler/src/cli/tsgen/runtime/utils.ts b/rust/compiler/src/cli/tsgen/ts-runtime/utils.ts similarity index 100% rename from rust/compiler/src/cli/tsgen/runtime/utils.ts rename to rust/compiler/src/cli/tsgen/ts-runtime/utils.ts diff --git a/rust/compiler/src/main.rs b/rust/compiler/src/main.rs index 56bef89b..46754505 100644 --- a/rust/compiler/src/main.rs +++ b/rust/compiler/src/main.rs @@ -1,10 +1,26 @@ use compiler::cli::run_cli; +use log::LevelFilter; +use std::io::Write; fn main() { // // useful for recursive stack overflows // unsafe { backtrace_on_stack_overflow::enable() }; - env_logger::init(); + env_logger::builder() + // .format_module_path(true) + .format(|buf, record| { + if let (Some(f), Some(l)) = (record.file(), record.line()) { + writeln!(buf, "[{} {}:{}] {}", record.level(), f, l, record.args() ) + } else if let Some(m) = record.module_path() { + writeln!(buf, "[{} {}] {}", record.level(), m , record.args() ) + } else { + writeln!(buf, "[{}] {}", record.level(), record.args() ) + } + }) + .filter(None, LevelFilter::Info) + .init(); + + // env_logger::init(); let exit_code = run_cli(); std::process::exit(exit_code); } diff --git a/rust/compiler/src/processing/loader.rs b/rust/compiler/src/processing/loader.rs index e783d1e7..28814f1e 100644 --- a/rust/compiler/src/processing/loader.rs +++ b/rust/compiler/src/processing/loader.rs @@ -7,7 +7,7 @@ use std::io::ErrorKind; use std::path::PathBuf; use crate::adlgen::adlc::packaging::{ - InjectAnnotation, InjectAnnotations, LoaderRef, LoaderWorkspace, + InjectAnnotation, InjectAnnotations, LoaderWorkspace, }; use crate::adlgen::sys::adlast2::{self as adlast, Module0}; use crate::parser::{convert_error, raw_module}; diff --git a/rust/compiler/tests/test1/rs-output/.adl-manifest b/rust/compiler/tests/test1/rs-output/.adl-manifest deleted file mode 100644 index f8687930..00000000 --- a/rust/compiler/tests/test1/rs-output/.adl-manifest +++ /dev/null @@ -1,5 +0,0 @@ -# manifest @generated by the adl compiler -adl/mod.rs -adl/sys/annotations.rs -adl/sys/mod.rs -adl/test1.rs diff --git a/rust/compiler/tests/test1/rs-output/adl/mod.rs b/rust/compiler/tests/test1/rs-output/adl/mod.rs deleted file mode 100644 index c7a9fddd..00000000 --- a/rust/compiler/tests/test1/rs-output/adl/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod test1; -pub mod sys; \ No newline at end of file diff --git a/rust/compiler/tests/test1/rs-output/adl/sys/annotations.rs b/rust/compiler/tests/test1/rs-output/adl/sys/annotations.rs deleted file mode 100644 index 29e6a1ca..00000000 --- a/rust/compiler/tests/test1/rs-output/adl/sys/annotations.rs +++ /dev/null @@ -1,23 +0,0 @@ -// @generated from adl module sys.annotations - -use serde::Deserialize; -use serde::Serialize; - -pub type Doc = String; - -pub type SerializedName = String; - -#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)] -pub struct SerializedWithInternalTag { - pub tag: String, -} - -impl SerializedWithInternalTag { - pub fn new(tag: String) -> SerializedWithInternalTag { - SerializedWithInternalTag { - tag: tag, - } - } -} - -pub type CustomSerialization = bool; diff --git a/rust/compiler/tests/test1/rs-output/adl/sys/mod.rs b/rust/compiler/tests/test1/rs-output/adl/sys/mod.rs deleted file mode 100644 index adf10224..00000000 --- a/rust/compiler/tests/test1/rs-output/adl/sys/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod annotations; \ No newline at end of file diff --git a/rust/compiler/tests/test1/rs-output/adl/test1.rs b/rust/compiler/tests/test1/rs-output/adl/test1.rs deleted file mode 100644 index e69de29b..00000000 diff --git a/rust/compiler/tests/test2/rs-output/adl/mod.rs b/rust/compiler/tests/test2/rs-output/adl/mod.rs deleted file mode 100644 index 54f5d1cb..00000000 --- a/rust/compiler/tests/test2/rs-output/adl/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod sys; -pub mod test2; \ No newline at end of file diff --git a/rust/compiler/tests/test2/rs-output/adl/sys/annotations.rs b/rust/compiler/tests/test2/rs-output/adl/sys/annotations.rs deleted file mode 100644 index 29e6a1ca..00000000 --- a/rust/compiler/tests/test2/rs-output/adl/sys/annotations.rs +++ /dev/null @@ -1,23 +0,0 @@ -// @generated from adl module sys.annotations - -use serde::Deserialize; -use serde::Serialize; - -pub type Doc = String; - -pub type SerializedName = String; - -#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)] -pub struct SerializedWithInternalTag { - pub tag: String, -} - -impl SerializedWithInternalTag { - pub fn new(tag: String) -> SerializedWithInternalTag { - SerializedWithInternalTag { - tag: tag, - } - } -} - -pub type CustomSerialization = bool; diff --git a/rust/compiler/tests/test2/rs-output/adl/sys/mod.rs b/rust/compiler/tests/test2/rs-output/adl/sys/mod.rs deleted file mode 100644 index adf10224..00000000 --- a/rust/compiler/tests/test2/rs-output/adl/sys/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod annotations; \ No newline at end of file diff --git a/rust/compiler/tests/test2/rs-output/adl/test2.rs b/rust/compiler/tests/test2/rs-output/adl/test2.rs deleted file mode 100644 index 38fdc8ea..00000000 --- a/rust/compiler/tests/test2/rs-output/adl/test2.rs +++ /dev/null @@ -1,128 +0,0 @@ -// @generated from adl module test2 - -use serde::Deserialize; -use serde::Serialize; - -/** - * An empty structure. - */ -#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)] -pub struct S0 { -} - -impl S0 { - pub fn new() -> S0 { - S0 { - } - } -} - -/** - * A structure containing primitives. - * It has two fields: an integer x and a String y. - */ -#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)] -pub struct S1 { - pub x: i32, - - pub y: String, -} - -impl S1 { - pub fn new(x: i32, y: String) -> S1 { - S1 { - x: x, - y: y, - } - } -} - -/** - * A structure containing a vector. - */ -#[derive(Clone,Debug,Deserialize,PartialEq,Serialize)] -pub struct S2 { - #[serde(rename="f1")] - pub f_1: String, - - #[serde(rename="f2")] - pub f_2: f64, - - #[serde(rename="f3")] - pub f_3: Vec, -} - -impl S2 { - pub fn new(f_1: String, f_2: f64, f_3: Vec) -> S2 { - S2 { - f_1: f_1, - f_2: f_2, - f_3: f_3, - } - } -} - -/** - * A generic structure. - */ -#[derive(Clone,Debug,Deserialize,PartialEq,Serialize)] -pub struct S3 { - #[serde(rename="f1")] - pub f_1: String, - - #[serde(rename="f2")] - pub f_2: f64, - - #[serde(rename="f3")] - pub f_3: T, - - #[serde(rename="f4")] - pub f_4: Vec, -} - -impl S3 { - pub fn new(f_1: String, f_2: f64, f_3: T, f_4: Vec) -> S3 { - S3 { - f_1: f_1, - f_2: f_2, - f_3: f_3, - f_4: f_4, - } - } -} - -#[derive(Clone,Debug,Deserialize,PartialEq,Serialize)] -pub struct S4 { - #[serde(rename="f1")] - pub f_1: S3, - - #[serde(rename="f2")] - pub f_2: S3, -} - -impl S4 { - pub fn new(f_1: S3, f_2: S3) -> S4 { - S4 { - f_1: f_1, - f_2: f_2, - } - } -} - -#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)] -pub struct Tree { - pub value: T, - - pub children: Vec>, -} - -impl Tree { - pub fn new(value: T, children: Vec>) -> Tree { - Tree { - value: value, - children: children, - } - } -} - -pub type IntTree = Tree;