Skip to content

Commit

Permalink
wip gen index.ts(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
millergarym committed Apr 24, 2023
1 parent 72067b0 commit 2e2b00f
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 25 deletions.
5 changes: 3 additions & 2 deletions adl/adl.work.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"runtimes": [
{
"ts_runtime": {
"output_dir": "../generated/typescript/tests"
"output_dir": "../generated/typescript/tests",
"strip_first": false
}
}
],
Expand Down Expand Up @@ -49,7 +50,7 @@
},
"outputs": {
"gen": {
"output_dir": "../generated/typescript/tests/test31/src",
"output_dir": "../generated/typescript/tests/test31",
"manifest": "../generated/typescript/tests/test31/.adl-manifest"
}
},
Expand Down
18 changes: 18 additions & 0 deletions adl/adlc/adlc/packaging.adl
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,24 @@ struct NpmPackage {
StringMap<String> dev_dependencies = {};
};

// the bits of tsconfig we need
struct TsConfig {
String extends = "tsconfig/base.json";
Vector<String> include = ["."];
Vector<String> exclude = ["dist", "build", "node_modules"];
TsCompilerOptions compilerOptions = {
"outDir": "dist",
"lib": [
"es2020"
]
};
};

struct TsCompilerOptions {
String outDir;
Vector<String> lib;
};

// union DependencySpec {
// Void workspace;
// VersionSpec semverSpec;
Expand Down
60 changes: 60 additions & 0 deletions rust/compiler/src/adlgen/adlc/packaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,66 @@ impl NpmPackage {
}
}

#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)]
pub struct TsConfig {
#[serde(default="TsConfig::def_extends")]
pub extends: String,

#[serde(default="TsConfig::def_include")]
pub include: Vec<String>,

#[serde(default="TsConfig::def_exclude")]
pub exclude: Vec<String>,

#[serde(default="TsConfig::def_compiler_options")]
#[serde(rename="compilerOptions")]
pub compiler_options: TsCompilerOptions,
}

impl TsConfig {
pub fn new() -> TsConfig {
TsConfig {
extends: TsConfig::def_extends(),
include: TsConfig::def_include(),
exclude: TsConfig::def_exclude(),
compiler_options: TsConfig::def_compiler_options(),
}
}

pub fn def_extends() -> String {
"tsconfig/base.json".to_string()
}

pub fn def_include() -> Vec<String> {
vec![".".to_string()]
}

pub fn def_exclude() -> Vec<String> {
vec!["dist".to_string(), "build".to_string(), "node_modules".to_string()]
}

pub fn def_compiler_options() -> TsCompilerOptions {
TsCompilerOptions{out_dir : "dist".to_string(), lib : vec!["es2020".to_string()]}
}
}

#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)]
pub struct TsCompilerOptions {
#[serde(rename="outDir")]
pub out_dir: String,

pub lib: Vec<String>,
}

impl TsCompilerOptions {
pub fn new(out_dir: String, lib: Vec<String>) -> TsCompilerOptions {
TsCompilerOptions {
out_dir: out_dir,
lib: lib,
}
}
}

#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)]
pub struct NpmPackageRef {
pub name: String,
Expand Down
2 changes: 1 addition & 1 deletion rust/compiler/src/cli/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn rust(opts: &RustOpts) -> anyhow::Result<()> {
Err(e) => return Err(anyhow!("Failed to load module {}: {:?}", m, e)),
}
}
let modules: Vec<&Module1> = resolver
let modules: Vec<Module1> = resolver
.get_module_names()
.into_iter()
.map(|mn| resolver.get_module(&mn).unwrap())
Expand Down
92 changes: 82 additions & 10 deletions rust/compiler/src/cli/tsgen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use regex;

use std::fmt::Write as _;
use std::io::Write as _;

use regex::bytes::Regex;

use std::collections::HashMap;
Expand All @@ -13,12 +16,12 @@ use genco::fmt::{self, Indentation};
use genco::prelude::*;

use crate::adlgen::adlc::packaging::{
AdlWorkspace, NpmPackage, NpmPackageRef, Payload1, TsGenRuntime, TsRuntimeOpt, TsStyle,
TsWriteRuntime, TypescriptGenOptions,
AdlWorkspace, NpmPackage, Payload1, TsConfig, TsRuntimeOpt, TsStyle, TsWriteRuntime,
TypescriptGenOptions,
};
use crate::adlgen::sys::adlast2::Module1;
use crate::adlgen::sys::adlast2::{self as adlast};
use crate::cli::tsgen::utils::{get_npm_pkg, npm_pkg_import};
use crate::cli::tsgen::utils::{get_npm_pkg, npm_pkg_import, IndexEntry};
use crate::processing::loader::AdlLoader;
use crate::processing::resolver::Resolver;
use crate::processing::writer::TreeWriter;
Expand Down Expand Up @@ -144,18 +147,46 @@ pub fn tsgen(
let parent = outputdir.file_name().unwrap().to_str().unwrap().to_string();
println!("!!!'{}'", parent);

let modules: Vec<&Module1> = resolver
let modules: Vec<Module1> = resolver
.get_module_names()
.into_iter()
.map(|mn| resolver.get_module(&mn).unwrap())
.collect();

// struct Index {
// files: Vec<String>,
// dirs: Vec<(String,Index)>,
// }

// enum Indexable {
// File(String),
// Dir(String),
// }

// let i_f = HashMap::new();
// let i_d = HashMap::new();

// let mut index = Index{files: vec![], dirs: vec![]};
// let mut indicies: &mut HashMap<PathBuf, Index> = &mut HashMap::new();

let index_map = &mut HashMap::new();

for m in &modules {
if opts.generate_transitive || module_names.contains(&m.name) {
let does_contain = module_names.contains(&m.name);
if opts.generate_transitive || does_contain {
let path = path_from_module_name(strip_first, m.name.to_owned());
println!("~~~{} - {:?}", m.name.to_owned(), path);

utils::collect_indexes(path.clone(), index_map);

println!("~~~{} - {:?}", m.name.to_owned(), &path.clone());
let path = path.as_path();
if None == path.components().next() {
return Err(anyhow!("Output module name was empty. Potential config issue 'strip_first' might need to be false. Module: '{}'. strip_first: '{}'", m.name, strip_first));
}
let code = gen_ts_module(m, &resolver, opts)?;
writer.write(path.as_path(), code)?;
writer
.write(path, code)
.map_err(|e| anyhow!("Error write to path {:?}. Error: {}", path, e.to_string()))?;
}
}

Expand Down Expand Up @@ -191,9 +222,44 @@ pub fn tsgen(
gen_runtime(strip_first, &opts.ts_style, &mut writer)?
}

println!("INDEX MAP");
let mut keys: Vec<&PathBuf> = index_map.keys().collect();
keys.sort();
for k in keys {
let mut out = Vec::new();
let vs = index_map.get(k).unwrap();
let mut v1: Vec<IndexEntry> = vec![];
for v in vs {
v1.push(v.to_owned());
}
v1.sort();
for v in &v1 {
match v {
IndexEntry::Dir(ie) => {
write!(&mut out, "export * as {} from './{}/index';\n", ie, ie)?;
},
IndexEntry::Leaf(ie) => {
let iep: Vec<&str> = ie.split(".").collect();
write!(&mut out, "export * from './{}';\n", iep[0])?;
},
}
}
let path_ind = k.join("index.ts");
let code = std::str::from_utf8(&out)?;
writer.write(path_ind.as_path(), code.to_string())?;

println!(" {:?}: {:?}", k, v1);
}
Ok(())
}

// export * as db from "./db/index";
// export * as strings from "./strings/index";
// export * as tabular from "./tabular/index";
// export * as ui from "./ui/index";
// export * from "./common";


pub fn gen_npm_package(pkg_path: String, wrk1: &AdlWorkspace<Payload1>) -> anyhow::Result<()> {
let payload = wrk1
.r#use
Expand Down Expand Up @@ -296,7 +362,12 @@ pub fn gen_npm_package(pkg_path: String, wrk1: &AdlWorkspace<Payload1>) -> anyho
}
let content = serde_json::to_string_pretty(&npm_package)?;
writer.write(Path::new("package.json"), content)?;
eprintln!("generated {:?}", outputdir.clone().join("package.json"));
log::info!("generated {:?}", outputdir.clone().join("package.json"));

let ts_config = TsConfig::new();
let content = serde_json::to_string_pretty(&ts_config)?;
writer.write(Path::new("tsconfig.json"), content)?;
log::info!("generated {:?}", outputdir.clone().join("tsconfig.json"));

Ok(())
}
Expand Down Expand Up @@ -355,7 +426,8 @@ fn gen_ts_module(

fn path_from_module_name(strip_first: bool, mname: adlast::ModuleName) -> PathBuf {
let mut path = PathBuf::new();
for (i, el) in mname.split(".").enumerate() {
let mut iter = mname.split(".").enumerate(); //.peekable();
while let Some((i, el)) = iter.next() {
if i == 0 && strip_first {
continue;
}
Expand All @@ -371,7 +443,7 @@ fn gen_resolver(
generate_transitive: bool,
runtime_opts: &TsRuntimeOpt,
resolver: &Resolver,
modules: &Vec<&Module1>,
modules: &Vec<Module1>,
) -> anyhow::Result<()> {
// TODO remote or local imports
let mut m_imports = vec![];
Expand Down
2 changes: 1 addition & 1 deletion rust/compiler/src/cli/tsgen/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Deserialize;

use crate::{
adlgen::adlc::{
packaging::{GenOutput, ModuleSrc, ReferenceableScopeOption},
packaging::{GenOutput, ModuleSrc, ReferenceableScopeOption, TsGenRuntime},
testing_table::TestFilesMetaData,
},
processing::loader::loader_from_search_paths,
Expand Down
2 changes: 0 additions & 2 deletions rust/compiler/src/cli/tsgen/ts-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
"license": "CLOSED",
"private": true,
"scripts": {
"build": "tsc && cp package-dist.json ./dist/package.json",
"dev": "cp package-dist.json ./dist/package.json && pnpm run tsc --watch",
"tsc": "tsc"
},
"dependencies": {
Expand Down
Loading

0 comments on commit 2e2b00f

Please sign in to comment.