Skip to content

Commit

Permalink
injecting npm_pkg_name as annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
millergarym committed Apr 18, 2023
1 parent ecd01be commit c9545fe
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 69 deletions.
7 changes: 7 additions & 0 deletions adl/adlc/adlc/packaging.adl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module adlc.packaging {

import sys.types.Pair;
import sys.adlast2.ScopedName;

type AdlWorkspace0 = AdlWorkspace<AdlPackageRef>;
type AdlWorkspace1 = AdlWorkspace<Pair<AdlPackageRef,AdlPackage>>;
Expand Down Expand Up @@ -35,6 +36,8 @@ struct TypescriptGenOptions {
ModuleSrc modules = "all";
Bool capitalize_branch_names_in_types = true;
Bool capitalize_type_names = true;

Vector<InjectAnnotation> annotate = [];
// Vector<> searchDir;
// --merge-adlext=EXT
// --verbose
Expand All @@ -43,6 +46,10 @@ struct TypescriptGenOptions {
// --excluded-ast-annotations=SCOPEDNAMES
};

union InjectAnnotation {
Pair<ScopedName,Json> module_;
};

union TsRuntimeOpt {
String packageRef;
TsGenRuntime generate;
Expand Down
19 changes: 17 additions & 2 deletions rust/compiler/src/adlgen/adlc/packaging.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @generated from adl module adlc.packaging

use crate::adlgen::sys::adlast2::ScopedName;
use crate::adlrt::custom::sys::types::pair::Pair;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -38,7 +39,7 @@ impl<T> AdlWorkspace<T> {

pub type AdlPackageRefs = Vec<AdlPackageRef>;

#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)]
#[derive(Clone,Debug,Deserialize,PartialEq,Serialize)]
pub struct AdlPackageRef {
pub path: String,

Expand All @@ -59,7 +60,7 @@ impl AdlPackageRef {
}
}

#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)]
#[derive(Clone,Debug,Deserialize,PartialEq,Serialize)]
pub struct TypescriptGenOptions {
pub npm_pkg_name: Option<String>,

Expand All @@ -86,6 +87,9 @@ pub struct TypescriptGenOptions {

#[serde(default="TypescriptGenOptions::def_capitalize_type_names")]
pub capitalize_type_names: bool,

#[serde(default="TypescriptGenOptions::def_annotate")]
pub annotate: Vec<InjectAnnotation>,
}

impl TypescriptGenOptions {
Expand All @@ -100,6 +104,7 @@ impl TypescriptGenOptions {
modules: TypescriptGenOptions::def_modules(),
capitalize_branch_names_in_types: TypescriptGenOptions::def_capitalize_branch_names_in_types(),
capitalize_type_names: TypescriptGenOptions::def_capitalize_type_names(),
annotate: TypescriptGenOptions::def_annotate(),
}
}

Expand Down Expand Up @@ -134,6 +139,16 @@ impl TypescriptGenOptions {
pub fn def_capitalize_type_names() -> bool {
true
}

pub fn def_annotate() -> Vec<InjectAnnotation> {
vec![]
}
}

#[derive(Clone,Debug,Deserialize,PartialEq,Serialize)]
pub enum InjectAnnotation {
#[serde(rename="module_")]
Module(Pair<ScopedName, serde_json::Value>),
}

#[derive(Clone,Debug,Deserialize,Eq,Hash,PartialEq,Serialize)]
Expand Down
1 change: 1 addition & 0 deletions rust/compiler/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn run_cli() -> i32 {
let loader = loader_from_search_paths(&opts.search.path);
let ts_opts = TypescriptGenOptions {
npm_pkg_name: None,
annotate: vec![],
outputs: crate::adlgen::adlc::packaging::OutputOpts::Gen(GenOutput{
referenceable: ReferenceableScopeOption::Local,
output_dir: opts.output.outputdir.to_str().unwrap().to_string(),
Expand Down
1 change: 1 addition & 0 deletions rust/compiler/src/cli/tsgen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ fn path_from_module_name(_opts: &TypescriptGenOptions, mname: adlast::ModuleName
}

fn gen_resolver(t: &mut Tokens<JavaScript>, mut modules: Vec<String>) -> anyhow::Result<()> {
// TODO remote or local imports
let mut m_imports = vec![];
for m in &modules {
m_imports.push(
Expand Down
18 changes: 1 addition & 17 deletions rust/compiler/src/cli/tsgen/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,9 @@ fn generate_ts_from_test_files() {
})
}
let modules = t.modules.clone();
// let opts = TsOpts {
// search: AdlSearchOpts { path: search_path },
// output: OutputOpts {
// outputdir: outdir,
// manifest: Some(manifest),
// },
// include_rt: true,
// runtime_dir: Some("runtime".to_string()),
// runtime_pkg: None,
// ts_style: None,
// modules: t.modules.clone(),
// capitalize_branch_names_in_types: true,
// capitalize_type_names: true,
// generate_transitive: true,
// include_resolver: true,
// };

let ts_opts = TypescriptGenOptions {
npm_pkg_name: None,
annotate: vec![],
outputs: crate::adlgen::adlc::packaging::OutputOpts::Gen(GenOutput{
referenceable: ReferenceableScopeOption::Local,
output_dir: outdir.clone(),
Expand Down
37 changes: 0 additions & 37 deletions rust/compiler/src/cli/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,6 @@ pub(crate) fn workspace(opts: &super::GenOpts) -> Result<(), anyhow::Error> {
// println!("{:?}", &wrk1);
for pkg in &wrk1.1.r#use {
if let Some(opts) = &pkg.0.0.ts_opts {
// let outputs = match &opts.outputs {
// OutputOpts::Gen(output) => output,
// OutputOpts::Ref(_) => continue,
// };
// let rt = match &opts.runtime_opts {
// TsRuntimeOpt::PackageRef(rt) => (false, None, Some(rt.clone())),
// TsRuntimeOpt::Generate(rt) => (true, Some(rt.runtime_dir.clone()), None),
// };
// let tsopts = TsOpts {
// search: super::AdlSearchOpts { path: vec![] },
// output: super::OutputOpts {
// outputdir: wrk1.0.join(outputs.output_dir.clone()),
// manifest: outputs.manifest.clone().map(|m| wrk1.0.join(m)),
// },
// include_rt: rt.0,
// runtime_dir: rt.1,
// runtime_pkg: rt.2,
// generate_transitive: opts.generate_transitive,
// include_resolver: opts.include_resolver,
// ts_style: match opts.ts_style {
// crate::adlgen::adlc::packaging::TsStyle::Tsc => Some(TsStyle::Tsc),
// crate::adlgen::adlc::packaging::TsStyle::Deno => Some(TsStyle::Deno),
// },
// modules: match &opts.modules {
// crate::adlgen::adlc::packaging::ModuleSrc::All => {
// let pkg_root = wrk1.0.join(pkg.0 .0.path.clone()).canonicalize()?;
// if let Some(pkg_root_str) = pkg_root.as_os_str().to_str() {
// walk_and_collect_adl_modules(pkg_root_str, &pkg_root)
// } else {
// return Err(anyhow!("Could get str from pkg_root"));
// }
// }
// crate::adlgen::adlc::packaging::ModuleSrc::Modules(ms) => ms.clone(),
// },
// capitalize_branch_names_in_types: opts.capitalize_branch_names_in_types,
// capitalize_type_names: opts.capitalize_type_names,
// };
let loader = loader_from_workspace(wrk1.0.clone(), wrk1.1.clone());
println!(
"TsGen for pkg {:?} in workspace {:?} output dir {:?}",
Expand Down
44 changes: 31 additions & 13 deletions rust/compiler/src/processing/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ use std::fs;
use std::io::ErrorKind;
use std::path::PathBuf;

use crate::adlgen::adlc::packaging::{AdlWorkspace1};
use crate::adlgen::adlc::packaging::AdlWorkspace1;
use crate::adlgen::sys::adlast2::{self as adlast, Module0};
use crate::parser::{convert_error, raw_module};
use crate::processing::annotations::apply_explicit_annotations_and_serialized_name;

pub fn loader_from_workspace(
root: PathBuf,
workspace: AdlWorkspace1,
) -> Box<dyn AdlLoader> {
pub fn loader_from_workspace(root: PathBuf, workspace: AdlWorkspace1) -> Box<dyn AdlLoader> {
Box::new(WorkspaceLoader {
root,
workspace,
Expand Down Expand Up @@ -52,11 +49,11 @@ impl AdlLoader for WorkspaceLoader {
}
}
for pkg in &self.workspace.r#use {
let pkg_path = pkg.0.1.path.as_str();
println!(" looking for {} in {} or {:?}", module_name, pkg_path, pkg.0.1.global_alias.clone());
let pkg_path = pkg.0 .1.path.as_str();
// println!(" looking for {} in {} or {:?}", module_name, pkg_path, pkg.0.1.global_alias.clone());
let pkg_name = if module_name.starts_with(pkg_path) {
Some(pkg.0.1.path.clone())
} else if let Some(alias) = pkg.0.1.global_alias.clone() {
Some(pkg.0 .1.path.clone())
} else if let Some(alias) = pkg.0 .1.global_alias.clone() {
if module_name.starts_with(alias.as_str()) {
Some(alias)
} else {
Expand All @@ -65,12 +62,33 @@ impl AdlLoader for WorkspaceLoader {
} else {
None
};
if let Some(name) = pkg_name {
if let Some(name) = &pkg_name {
let loader = self
.loaders
.entry(name)
.or_insert(Box::new(DirTreeLoader::new(self.root.join(&pkg.0.0.path))));
return loader.load(module_name);
.entry(name.clone())
.or_insert(Box::new(DirTreeLoader::new(self.root.join(&pkg.0 .0.path))));
let module = loader.load(module_name);
println!("--- {}", &name.clone());
match module {
Ok(module) => {
if let Some(mut moduleX) = module.clone() {
if let Some(ts_opts) = &pkg.0 .0.ts_opts {
if let Some(npm_pkg) = &ts_opts.npm_pkg_name {
moduleX.annotations.0.insert(
adlast::ScopedName {
module_name: "adlc.config.typescript".to_string(),
name: "NpmPackage".to_string(),
},
serde_json::json!(npm_pkg),
);
return Ok(Some(moduleX));
}
}
}
return Ok(module);
}
Err(_) => return module,
}
}
}
Ok(None)
Expand Down

0 comments on commit c9545fe

Please sign in to comment.