Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
millergarym committed Apr 19, 2023
1 parent c9545fe commit a00210b
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 115 deletions.
16 changes: 15 additions & 1 deletion adl/adlc/adlc/packaging.adl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@ module adlc.packaging {

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

// AdlWorkspace0 is the serilization format
type AdlWorkspace0 = AdlWorkspace<AdlPackageRef>;
type AdlWorkspace1 = AdlWorkspace<Pair<AdlPackageRef,AdlPackage>>;
type AdlWorkspace1 = AdlWorkspace<Payload1>;
type AdlWorkspace2 = AdlWorkspace<Payload2>;

struct Payload1 {
AdlPackageRef p_ref;
AdlPackage pkg;
};

struct Payload2 {
AdlPackageRef p_ref;
AdlPackage pkg;
Vector<Module1> modules;
};

/// Expected to live in a file named `adl.work.json`
struct AdlWorkspace<T> {
Expand Down
3 changes: 3 additions & 0 deletions adl/tests/test31/proj/protoclient/protoapp/api.adl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module protoclient.protoapp.api {

import sys.types.Map;

import common.Instant;
import common.db.WithDbId;
import common.config.log.LogLevel;
Expand All @@ -18,6 +20,7 @@ import protoclient.protoapp.db.MessageId;

/// The app API
struct ApiRequests {
Map imamap;
/// Login a user
HttpPost<String, Vector<StringMap<Int64>>> with_prim = {
"path": "/login",
Expand Down
40 changes: 39 additions & 1 deletion rust/compiler/src/adlgen/adlc/packaging.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@
// @generated from adl module adlc.packaging

use crate::adlgen::sys::adlast2::Module1;
use crate::adlgen::sys::adlast2::ScopedName;
use crate::adlrt::custom::sys::types::pair::Pair;
use serde::Deserialize;
use serde::Serialize;

pub type AdlWorkspace0 = AdlWorkspace<AdlPackageRef>;

pub type AdlWorkspace1 = AdlWorkspace<Pair<AdlPackageRef, AdlPackage>>;
pub type AdlWorkspace1 = AdlWorkspace<Payload1>;

pub type AdlWorkspace2 = AdlWorkspace<Payload2>;

#[derive(Clone,Debug,Deserialize,PartialEq,Serialize)]
pub struct Payload1 {
pub p_ref: AdlPackageRef,

pub pkg: AdlPackage,
}

impl Payload1 {
pub fn new(p_ref: AdlPackageRef, pkg: AdlPackage) -> Payload1 {
Payload1 {
p_ref: p_ref,
pkg: pkg,
}
}
}

#[derive(Clone,Debug,Deserialize,PartialEq,Serialize)]
pub struct Payload2 {
pub p_ref: AdlPackageRef,

pub pkg: AdlPackage,

pub modules: Vec<Module1>,
}

impl Payload2 {
pub fn new(p_ref: AdlPackageRef, pkg: AdlPackage, modules: Vec<Module1>) -> Payload2 {
Payload2 {
p_ref: p_ref,
pkg: pkg,
modules: modules,
}
}
}

/**
* Expected to live in a file named `adl.work.json`
Expand Down
2 changes: 1 addition & 1 deletion rust/compiler/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::{Path, PathBuf};
use std::path::{PathBuf};

use anyhow::{Error, anyhow};
use clap::{Args, Parser};
Expand Down
2 changes: 1 addition & 1 deletion rust/compiler/src/cli/tsgen/defaultval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use convert_case::{Case, Casing};
use serde_json::Value;

use crate::adlgen::sys::adlast2::{
Decl, DeclType, Field, Module, PrimitiveType, ScopedName, TypeExpr, TypeRef, Union, Decl1, Module1, Field1, Union1,
DeclType, PrimitiveType, ScopedName, TypeExpr, TypeRef, Decl1, Module1, Field1, Union1,
};
use crate::processing::resolver::Resolver;
use genco::prelude::*;
Expand Down
33 changes: 4 additions & 29 deletions rust/compiler/src/cli/tsgen/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use crate::adlgen::sys::adlast2::{
use crate::parser::docstring_scoped_name;
use crate::processing::resolver::Resolver;

use super::utils::{get_npm_pkg, npm_pkg_import, rel_import};

const SP: &str = " ";
const SQ: &str = "'";

Expand Down Expand Up @@ -335,28 +337,9 @@ impl TsGenVisitor<'_> {
let npm_pkg = get_npm_pkg(self.module);
let imp = self.map.entry(scoped_name.clone()).or_insert_with(|| {
let path = if npm_pkg2 != None && npm_pkg2 != npm_pkg {
let npm_pkg2 = npm_pkg2.unwrap();
let mn_parts: Vec<&str> = scoped_name.module_name.split(".").collect();
let npm_parts: Vec<&str> = npm_pkg2.rsplit("/").collect();
let mut mn = mn_parts.iter().peekable();
let mut npm = npm_parts.iter().peekable();
while let (Some(m), Some(n)) = (&mn.peek(), &npm.peek()) {
if m != n {
break;
}
mn.next(); npm.next();
}
let mut path = npm_pkg2;
path.push_str("/");
while let Some(p) = mn.next() {
path.push_str(p);
if let Some(_) = mn.peek() {
path.push_str("/");
}
}
path
npm_pkg_import(npm_pkg2, scoped_name.module_name.clone())
} else {
crate::cli::tsgen::utils::rel_import(&self.module.name, &scoped_name.module_name)
rel_import(&self.module.name, &scoped_name.module_name)
};
let i_name = scoped_name
.module_name
Expand Down Expand Up @@ -511,14 +494,6 @@ impl TsGenVisitor<'_> {
}
}

fn get_npm_pkg(module: &Module1) -> Option<String> {
let npm_pkg = module.annotations.0.get(&ScopedName {
module_name: "adlc.config.typescript".to_string(),
name: "NpmPackage".to_string(),
});
npm_pkg.map(|p| p.as_str().unwrap().to_string())
}

fn lit(t: &mut Tokens<JavaScript>, s: &'static str) {
t.append(Item::Literal(ItemStr::Static(s)));
}
Expand Down
42 changes: 30 additions & 12 deletions rust/compiler/src/cli/tsgen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use genco::prelude::*;

use crate::adlgen::adlc::packaging::{TsGenRuntime, TsRuntimeOpt, TsStyle, TypescriptGenOptions};
use crate::adlgen::sys::adlast2::{self as adlast};
use crate::adlgen::sys::adlast2::{Module, Module1, TypeExpr, TypeRef};
use crate::processing::loader::{loader_from_search_paths, AdlLoader};
use crate::adlgen::sys::adlast2::{Module1};
use crate::cli::tsgen::utils::{get_npm_pkg, npm_pkg_import};
use crate::processing::loader::{AdlLoader};
use crate::processing::resolver::Resolver;
use crate::processing::writer::TreeWriter;

Expand Down Expand Up @@ -129,7 +130,7 @@ pub fn tsgen(
.map(|mn| resolver.get_module(&mn).unwrap())
.collect();

for m in modules {
for m in &modules {
if opts.generate_transitive || module_names.contains(&m.name) {
let path = path_from_module_name(opts, m.name.to_owned());
let code = gen_ts_module(m, &resolver, opts)?;
Expand All @@ -139,8 +140,8 @@ pub fn tsgen(

{
let tokens = &mut js::Tokens::new();
let modules = resolver.get_module_names();
gen_resolver(tokens, modules)?;
// let modules = resolver.get_module_names();
gen_resolver(tokens, opts.npm_pkg_name.clone(), &resolver, &modules)?;
let config = js::Config::default();
// let config = js::Config{
// ..Default::default()
Expand Down Expand Up @@ -179,7 +180,7 @@ fn gen_ts_module(
// sdf
js::import(pkg.clone() + "/adl", "ADL").into_wildcard()
}
TsRuntimeOpt::Generate(gen) => {
TsRuntimeOpt::Generate(_gen) => {
// TODO modify the import path with opts.runtime_dir
js::import(
utils::rel_import(&m.name, &"runtime.adl".to_string()),
Expand Down Expand Up @@ -225,19 +226,36 @@ fn path_from_module_name(_opts: &TypescriptGenOptions, mname: adlast::ModuleName
return path;
}

fn gen_resolver(t: &mut Tokens<JavaScript>, mut modules: Vec<String>) -> anyhow::Result<()> {
fn gen_resolver(t: &mut Tokens<JavaScript>, npm_pkg: Option<String>, resolver: &Resolver, modules: &Vec<&Module1>) -> anyhow::Result<()> {
// TODO remote or local imports
let mut m_imports = vec![];
for m in &modules {

for m in modules {

let npm_pkg2 = if let Some(m2) = resolver.get_module(&m.name) {
get_npm_pkg(m2)
} else {
None
};

let path = if npm_pkg2 != None && npm_pkg2 != npm_pkg {
npm_pkg_import(npm_pkg2, m.name.clone())
} else {
format!("./{}", m.name.replace(".", "/"))
};

m_imports.push(
js::import(format!("./{}", m.replace(".", "/")), "_AST_MAP")
.with_alias(m.replace(".", "_")),
js::import(path, "_AST_MAP")
.with_alias(m.name.replace(".", "_")),
);
}
let adlr1 = js::import("./runtime/adl", "declResolver");
let adlr2 = js::import("./runtime/adl", "ScopedDecl");
let gened = "/* @generated from adl */";
modules.sort();
// modules.sort_by(|a, b| a.name.cmp(&b.name));
let mut keys: Vec<String> = modules.iter().map(|m| m.name.clone()).collect();
// let m_map: HashMap<String,&Module1> = modules.iter().map(|m| (m.name.clone(),*m)).collect();
keys.sort();
quote_in! { *t =>
$gened
$(register (adlr2))
Expand All @@ -246,7 +264,7 @@ fn gen_resolver(t: &mut Tokens<JavaScript>, mut modules: Vec<String>) -> anyhow:


export const ADL: { [key: string]: ScopedDecl } = {
$(for m in &modules => ...$(m.replace(".", "_")),$['\r'])
$(for m in keys => ...$(m.replace(".", "_")),$['\r'])
};

export const RESOLVER = declResolver(ADL);
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 @@ -4,7 +4,7 @@ use std::path::PathBuf;

use serde::Deserialize;

use crate::{cli::{AdlSearchOpts, OutputOpts}, adlgen::adlc::{testing_table::TestFilesMetaData, packaging::{GenOutput, ReferenceableScopeOption, ModuleSrc}}};
use crate::{cli::{AdlSearchOpts, OutputOpts}, adlgen::adlc::{testing_table::TestFilesMetaData, packaging::{GenOutput, ReferenceableScopeOption, ModuleSrc}}, processing::loader::loader_from_search_paths};

use super::*;

Expand Down
33 changes: 33 additions & 0 deletions rust/compiler/src/cli/tsgen/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
use crate::adlgen::sys::adlast2::{Module1, ScopedName};

pub fn get_npm_pkg(module: &Module1) -> Option<String> {
let npm_pkg = module.annotations.0.get(&ScopedName {
module_name: "adlc.config.typescript".to_string(),
name: "NpmPackage".to_string(),
});
npm_pkg.map(|p| p.as_str().unwrap().to_string())
}

pub fn npm_pkg_import(npm_pkg2: Option<String>, module_name: String) -> String {
let npm_pkg2 = npm_pkg2.unwrap();
let mn_parts: Vec<&str> = module_name.split(".").collect();
let npm_parts: Vec<&str> = npm_pkg2.rsplit("/").collect();
let mut mn = mn_parts.iter().peekable();
let mut npm = npm_parts.iter().peekable();
while let (Some(m), Some(n)) = (&mn.peek(), &npm.peek()) {
if m != n {
break;
}
mn.next(); npm.next();
}
let mut path = npm_pkg2;
path.push_str("/");
while let Some(p) = mn.next() {
path.push_str(p);
if let Some(_) = mn.peek() {
path.push_str("/");
}
}
path
}

pub fn rel_import(src: &String, dst: &String) -> String {
let src_v: Vec<&str> = src.split(['.']).collect();
let src_v = &src_v[..src_v.len() - 1];
Expand Down
12 changes: 6 additions & 6 deletions rust/compiler/src/cli/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use anyhow::anyhow;

use serde::Deserialize;

use crate::adlgen::adlc::packaging::{AdlPackage, AdlWorkspace0, AdlWorkspace1, OutputOpts, TsRuntimeOpt};
use crate::adlrt::custom::sys::types::pair::Pair;
use crate::adlgen::adlc::packaging::{AdlPackage, AdlWorkspace0, AdlWorkspace1, Payload1};

use crate::processing::loader::loader_from_workspace;

use super::{tsgen};
Expand All @@ -16,13 +16,13 @@ pub(crate) fn workspace(opts: &super::GenOpts) -> Result<(), anyhow::Error> {
let wrk1 = collection_to_workspace(pkg_defs)?;
// println!("{:?}", &wrk1);
for pkg in &wrk1.1.r#use {
if let Some(opts) = &pkg.0.0.ts_opts {
if let Some(opts) = &pkg.p_ref.ts_opts {
let loader = loader_from_workspace(wrk1.0.clone(), wrk1.1.clone());
println!(
"TsGen for pkg {:?} in workspace {:?} output dir {:?}",
pkg.0 .0, wrk1.0, &opts.outputs
pkg.p_ref, wrk1.0, &opts.outputs
);
let pkg_root = wrk1.0.join(pkg.0 .0.path.clone()).canonicalize()?;
let pkg_root = wrk1.0.join(pkg.p_ref.path.clone()).canonicalize()?;
tsgen::tsgen(loader, &opts, Some(pkg_root))?;
}
}
Expand Down Expand Up @@ -55,7 +55,7 @@ fn collection_to_workspace(
let mut de = serde_json::Deserializer::from_str(&content);
let pkg = AdlPackage::deserialize(&mut de)
.map_err(|e| anyhow!("{:?}: {}", p_path, e.to_string()))?;
wrk1.r#use.push(Pair::new(p.clone(), pkg));
wrk1.r#use.push(Payload1::new(p.clone(), pkg));
}
// println!("wrk {:?}", wrk1);
return Ok((porw.1, wrk1));
Expand Down
2 changes: 2 additions & 0 deletions rust/compiler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(rust_2018_idioms)]

pub mod adlgen;
pub mod adlrt;
pub mod cli;
Expand Down
Loading

0 comments on commit a00210b

Please sign in to comment.