Skip to content

Commit

Permalink
create tsconfig based on config - named_options
Browse files Browse the repository at this point in the history
  • Loading branch information
millergarym committed May 1, 2023
1 parent 3a5ed05 commit 976e9c3
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 101 deletions.
29 changes: 20 additions & 9 deletions adl/adlc/adlc/packaging.adl
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,23 @@ struct Payload2 {
struct AdlWorkspace<T> {
String adlc;
Vector<T> use;
StringMap<NamedOpts> named_options = {};
Vector<RuntimeOpts> runtimes = [];
// Maybe<T> embedded_sys_loader;
};

struct NamedOpts {
TsNamedOpts ts_opts = {};
};

struct TsNamedOpts {
Nullable<TsRuntimeOpt> runtime_opts = null;
Nullable<StringMap<String>> scripts = null;
Nullable<StringMap<String>> dependencies = null;
Nullable<StringMap<String>> dev_dependencies = null;
Nullable<TsConfig> tsconfig = null;
};

union RuntimeOpts {
TsWriteRuntime ts_runtime;
};
Expand Down Expand Up @@ -77,6 +90,7 @@ union InjectAnnotation {

struct AdlPackageRef {
AdlPackageRefType ref;
Nullable<String> named_opts = null;
Nullable<TypescriptGenOptions> ts_opts = null;
};

Expand Down Expand Up @@ -106,6 +120,8 @@ struct TypescriptGenOptions {
StringMap<VersionSpec> extra_dependencies = {};
StringMap<VersionSpec> extra_dev_dependencies = {};
Nullable<OutputOpts> outputs = null;
Nullable<TsConfig> tsconfig = null;
StringMap<String> scripts = {};
TsRuntimeOpt runtime_opts = {"package_ref": {"name": "@adl-lang/runtime", "version": "^1.0.0"}};
Bool generate_transitive = false;
Bool include_resolver = false;
Expand Down Expand Up @@ -222,15 +238,10 @@ struct NpmPackage {

// 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"
]
};
Nullable<String> extends;
Vector<String> include;
Vector<String> exclude;
TsCompilerOptions compilerOptions;
};

struct TsCompilerOptions {
Expand Down
134 changes: 107 additions & 27 deletions rust/compiler/src/adlgen/adlc/packaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ impl Payload2 {
/**
* Expected to live in a file named `adl.work.json`
*/
#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)]
#[derive(Clone,Debug,Deserialize,Eq,PartialEq,Serialize)]
pub struct AdlWorkspace<T> {
pub adlc: String,

#[serde(rename="use")]
pub r#use: Vec<T>,

#[serde(default="AdlWorkspace::<T>::def_named_options")]
pub named_options: std::collections::HashMap<String,NamedOpts>,

#[serde(default="AdlWorkspace::<T>::def_runtimes")]
pub runtimes: Vec<RuntimeOpts>,
}
Expand All @@ -70,15 +73,88 @@ impl<T> AdlWorkspace<T> {
AdlWorkspace {
adlc: adlc,
r#use: r#use,
named_options: AdlWorkspace::<T>::def_named_options(),
runtimes: AdlWorkspace::<T>::def_runtimes(),
}
}

pub fn def_named_options() -> std::collections::HashMap<String,NamedOpts> {
[].iter().cloned().collect()
}

pub fn def_runtimes() -> Vec<RuntimeOpts> {
vec![]
}
}

#[derive(Clone,Debug,Deserialize,Eq,PartialEq,Serialize)]
pub struct NamedOpts {
#[serde(default="NamedOpts::def_ts_opts")]
pub ts_opts: TsNamedOpts,
}

impl NamedOpts {
pub fn new() -> NamedOpts {
NamedOpts {
ts_opts: NamedOpts::def_ts_opts(),
}
}

pub fn def_ts_opts() -> TsNamedOpts {
TsNamedOpts{runtime_opts : None, scripts : None, dependencies : None, dev_dependencies : None, tsconfig : None}
}
}

#[derive(Clone,Debug,Deserialize,Eq,PartialEq,Serialize)]
pub struct TsNamedOpts {
#[serde(default="TsNamedOpts::def_runtime_opts")]
pub runtime_opts: Option<TsRuntimeOpt>,

#[serde(default="TsNamedOpts::def_scripts")]
pub scripts: Option<std::collections::HashMap<String,String>>,

#[serde(default="TsNamedOpts::def_dependencies")]
pub dependencies: Option<std::collections::HashMap<String,String>>,

#[serde(default="TsNamedOpts::def_dev_dependencies")]
pub dev_dependencies: Option<std::collections::HashMap<String,String>>,

#[serde(default="TsNamedOpts::def_tsconfig")]
pub tsconfig: Option<TsConfig>,
}

impl TsNamedOpts {
pub fn new() -> TsNamedOpts {
TsNamedOpts {
runtime_opts: TsNamedOpts::def_runtime_opts(),
scripts: TsNamedOpts::def_scripts(),
dependencies: TsNamedOpts::def_dependencies(),
dev_dependencies: TsNamedOpts::def_dev_dependencies(),
tsconfig: TsNamedOpts::def_tsconfig(),
}
}

pub fn def_runtime_opts() -> Option<TsRuntimeOpt> {
None
}

pub fn def_scripts() -> Option<std::collections::HashMap<String,String>> {
None
}

pub fn def_dependencies() -> Option<std::collections::HashMap<String,String>> {
None
}

pub fn def_dev_dependencies() -> Option<std::collections::HashMap<String,String>> {
None
}

pub fn def_tsconfig() -> Option<TsConfig> {
None
}
}

#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)]
pub enum RuntimeOpts {
#[serde(rename="ts_runtime")]
Expand Down Expand Up @@ -211,6 +287,9 @@ pub struct AdlPackageRef {
#[serde(rename="ref")]
pub r#ref: AdlPackageRefType,

#[serde(default="AdlPackageRef::def_named_opts")]
pub named_opts: Option<String>,

#[serde(default="AdlPackageRef::def_ts_opts")]
pub ts_opts: Option<TypescriptGenOptions>,
}
Expand All @@ -219,10 +298,15 @@ impl AdlPackageRef {
pub fn new(r#ref: AdlPackageRefType) -> AdlPackageRef {
AdlPackageRef {
r#ref: r#ref,
named_opts: AdlPackageRef::def_named_opts(),
ts_opts: AdlPackageRef::def_ts_opts(),
}
}

pub fn def_named_opts() -> Option<String> {
None
}

pub fn def_ts_opts() -> Option<TypescriptGenOptions> {
None
}
Expand Down Expand Up @@ -291,6 +375,12 @@ pub struct TypescriptGenOptions {
#[serde(default="TypescriptGenOptions::def_outputs")]
pub outputs: Option<OutputOpts>,

#[serde(default="TypescriptGenOptions::def_tsconfig")]
pub tsconfig: Option<TsConfig>,

#[serde(default="TypescriptGenOptions::def_scripts")]
pub scripts: std::collections::HashMap<String,String>,

#[serde(default="TypescriptGenOptions::def_runtime_opts")]
pub runtime_opts: TsRuntimeOpt,

Expand Down Expand Up @@ -321,6 +411,8 @@ impl TypescriptGenOptions {
extra_dependencies: TypescriptGenOptions::def_extra_dependencies(),
extra_dev_dependencies: TypescriptGenOptions::def_extra_dev_dependencies(),
outputs: TypescriptGenOptions::def_outputs(),
tsconfig: TypescriptGenOptions::def_tsconfig(),
scripts: TypescriptGenOptions::def_scripts(),
runtime_opts: TypescriptGenOptions::def_runtime_opts(),
generate_transitive: TypescriptGenOptions::def_generate_transitive(),
include_resolver: TypescriptGenOptions::def_include_resolver(),
Expand All @@ -347,6 +439,14 @@ impl TypescriptGenOptions {
None
}

pub fn def_tsconfig() -> Option<TsConfig> {
None
}

pub fn def_scripts() -> std::collections::HashMap<String,String> {
[].iter().cloned().collect()
}

pub fn def_runtime_opts() -> TsRuntimeOpt {
TsRuntimeOpt::PackageRef(NpmPackageRef{name : "@adl-lang/runtime".to_string(), version : "^1.0.0".to_string()})
}
Expand Down Expand Up @@ -685,45 +785,25 @@ impl NpmPackage {

#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)]
pub struct TsConfig {
#[serde(default="TsConfig::def_extends")]
pub extends: String,
pub extends: Option<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 {
pub fn new(extends: Option<String>, include: Vec<String>, exclude: Vec<String>, compiler_options: TsCompilerOptions) -> TsConfig {
TsConfig {
extends: TsConfig::def_extends(),
include: TsConfig::def_include(),
exclude: TsConfig::def_exclude(),
compiler_options: TsConfig::def_compiler_options(),
extends: extends,
include: include,
exclude: exclude,
compiler_options: 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)]
Expand Down
2 changes: 2 additions & 0 deletions rust/compiler/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ pub fn run_cli() -> i32 {
npm_version: TypescriptGenOptions::def_npm_version(),
extra_dependencies: TypescriptGenOptions::def_extra_dependencies(),
extra_dev_dependencies: TypescriptGenOptions::def_extra_dev_dependencies(),
tsconfig: TypescriptGenOptions::def_tsconfig(),
scripts: TypescriptGenOptions::def_scripts(),
outputs: Some(crate::adlgen::adlc::packaging::OutputOpts::Gen(GenOutput {
referenceable: ReferenceableScopeOption::Local,
output_dir: opts.output.outputdir.to_str().unwrap().to_string(),
Expand Down
17 changes: 9 additions & 8 deletions rust/compiler/src/cli/tsgen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use genco::fmt::{self, Indentation};
use genco::prelude::*;

use crate::adlgen::adlc::packaging::{
AdlPackageRefType, AdlWorkspace, ModuleSrc, NpmPackage, Payload1, TsConfig, TsRuntimeOpt,
AdlPackageRefType, AdlWorkspace, ModuleSrc, NpmPackage, Payload1, TsRuntimeOpt,
TsStyle, TsWriteRuntime, TypescriptGenOptions,
};
use crate::adlgen::sys::adlast2::Module1;
Expand Down Expand Up @@ -281,9 +281,9 @@ pub fn gen_npm_package(payload: &Payload1, wrk1: &AdlWorkspace<Payload1>) -> any
}
TsRuntimeOpt::Generate(_) => {}
};
npm_package
.scripts
.insert("tsc".to_string(), "tsc".to_string());
for (k,v) in &opts.scripts {
npm_package.scripts.entry(k.clone()).or_insert(v.clone());
}

for d in &opts.extra_dependencies {
npm_package.dependencies.insert(d.0.clone(), d.1.clone());
Expand Down Expand Up @@ -354,10 +354,11 @@ pub fn gen_npm_package(payload: &Payload1, wrk1: &AdlWorkspace<Payload1>) -> any
writer.write(Path::new("package.json"), content)?;
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"));
if let Some(ts_config) = &opts.tsconfig {
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
Loading

0 comments on commit 976e9c3

Please sign in to comment.