Skip to content

Commit

Permalink
using rust_embed for ts runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
millergarym committed Apr 24, 2023
1 parent 9007c6f commit 72067b0
Show file tree
Hide file tree
Showing 22 changed files with 74 additions and 211 deletions.
57 changes: 33 additions & 24 deletions rust/compiler/src/cli/tsgen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'";
Expand Down Expand Up @@ -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())?;
Expand All @@ -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())?;
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions rust/compiler/src/cli/tsgen/ts-runtime/package-dist.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
13 changes: 13 additions & 0 deletions rust/compiler/src/cli/tsgen/ts-runtime/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "tsconfig/base.json",
"include": ["."],
"exclude": ["dist", "build", "node_modules"],
"compilerOptions": {
"outDir": "dist",
// "target": "es2020",
// "module": "es2020",
"lib": [
"es2020",
]
}
}
18 changes: 17 additions & 1 deletion rust/compiler/src/main.rs
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion rust/compiler/src/processing/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
5 changes: 0 additions & 5 deletions rust/compiler/tests/test1/rs-output/.adl-manifest

This file was deleted.

2 changes: 0 additions & 2 deletions rust/compiler/tests/test1/rs-output/adl/mod.rs

This file was deleted.

23 changes: 0 additions & 23 deletions rust/compiler/tests/test1/rs-output/adl/sys/annotations.rs

This file was deleted.

1 change: 0 additions & 1 deletion rust/compiler/tests/test1/rs-output/adl/sys/mod.rs

This file was deleted.

Empty file.
2 changes: 0 additions & 2 deletions rust/compiler/tests/test2/rs-output/adl/mod.rs

This file was deleted.

23 changes: 0 additions & 23 deletions rust/compiler/tests/test2/rs-output/adl/sys/annotations.rs

This file was deleted.

1 change: 0 additions & 1 deletion rust/compiler/tests/test2/rs-output/adl/sys/mod.rs

This file was deleted.

128 changes: 0 additions & 128 deletions rust/compiler/tests/test2/rs-output/adl/test2.rs

This file was deleted.

0 comments on commit 72067b0

Please sign in to comment.