Skip to content

Commit a4b9047

Browse files
committed
Merge branch 'main' into local-npm-registry
# Conflicts: # poetry.lock
2 parents 2bcd01b + 1c6c8af commit a4b9047

30 files changed

+2128
-1963
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ jobs:
340340
cargo test --locked --package meta-cli
341341
342342
# from old test-libs
343-
cargo test --locked --exclude meta-cli --exclude typegate --exclude typegraph_engine --exclude typegraph_core --exclude metagen_mdk_rust_static --workspace
343+
cargo test --locked --exclude meta-cli --exclude typegate --exclude typegate_engine --exclude typegraph_core --exclude metagen_mdk_rust_static --workspace
344344
345345
deno run -A dev/update.ts --cache-only || deno run -A dev/update.ts --cache-only
346346
cargo build -p meta-cli

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ members = [
88
"meta-cli",
99
"typegraph/core", ]
1010

11-
[workspace.package]
11+
[workspace.package]
1212
version = "0.3.7-0"
1313
edition = "2021"
1414

@@ -20,6 +20,7 @@ deno = { git = "https://github.com/metatypedev/deno", branch = "v1.41.0-embeddab
2020
mt_deno = { path = "libs/deno/" }
2121
common = { path = "libs/common/" }
2222
typescript = { path = "libs/typescript/" }
23+
metagen = { path = "libs/metagen/" }
2324
typegate_engine = { path = "typegate/engine" }
2425

2526
# pinned to match version brought in

libs/metagen/src/config.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,34 @@
1616
//! annotate_debug: true
1717
use crate::interlude::*;
1818

19-
#[derive(Deserialize, Debug)]
19+
#[derive(Deserialize, Debug, Clone)]
2020
pub struct Config {
2121
pub targets: HashMap<String, Target>,
2222
}
2323

24-
#[derive(Deserialize, Debug)]
24+
#[derive(Deserialize, Debug, Clone)]
2525
pub struct Target(pub HashMap<String, serde_json::Value>);
2626

27+
/// If both name and path are set, name is used to disambiguate
28+
/// from multiple typegrpahs loaded from file at path.
2729
#[derive(Serialize, Deserialize, Debug, garde::Validate)]
2830
pub struct MdkGeneratorConfigBase {
2931
#[garde(length(min = 1))]
3032
#[serde(rename = "typegraph")]
31-
pub typegraph_name: String,
33+
#[garde(custom(|_, _| either_typegraph_name_or_path(self)))]
34+
pub typegraph_name: Option<String>,
35+
#[garde(skip)]
36+
pub typegraph_path: Option<PathBuf>,
3237
#[garde(skip)]
3338
pub path: PathBuf,
3439
}
40+
41+
fn either_typegraph_name_or_path(config: &MdkGeneratorConfigBase) -> garde::Result {
42+
if config.typegraph_name.is_none() && config.typegraph_path.is_none() {
43+
Err(garde::Error::new(
44+
"either typegraph or typegraph_path must be set",
45+
))
46+
} else {
47+
Ok(())
48+
}
49+
}

libs/metagen/src/lib.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@ mod utils;
2828

2929
use crate::interlude::*;
3030

31+
pub use config::*;
32+
3133
#[derive(Debug)]
3234
pub enum GeneratorInputOrder {
33-
TypegraphDesc { name: String },
35+
TypegraphFromTypegate { name: String },
36+
TypegraphFromPath { path: PathBuf, name: Option<String> },
3437
}
3538

3639
#[derive(Debug)]
3740
pub enum GeneratorInputResolved {
38-
TypegraphDesc { raw: Typegraph },
41+
TypegraphFromTypegate { raw: Typegraph },
42+
TypegraphFromPath { raw: Typegraph },
3943
}
4044

4145
pub trait InputResolver {
@@ -48,20 +52,6 @@ pub trait InputResolver {
4852
#[derive(Debug)]
4953
pub struct GeneratorOutput(pub HashMap<PathBuf, String>);
5054

51-
impl InputResolver for Ctx {
52-
async fn resolve(&self, order: GeneratorInputOrder) -> anyhow::Result<GeneratorInputResolved> {
53-
Ok(match order {
54-
GeneratorInputOrder::TypegraphDesc { name } => GeneratorInputResolved::TypegraphDesc {
55-
raw: self
56-
.typegate
57-
.typegraph(&name)
58-
.await?
59-
.with_context(|| format!("no typegraph found under \"{name}\""))?,
60-
},
61-
})
62-
}
63-
}
64-
6555
trait Plugin: Send + Sync {
6656
fn bill_of_inputs(&self) -> HashMap<String, GeneratorInputOrder>;
6757
fn generate(
@@ -70,11 +60,9 @@ trait Plugin: Send + Sync {
7060
) -> anyhow::Result<GeneratorOutput>;
7161
}
7262

73-
#[derive(Clone)]
74-
struct Ctx {
75-
typegate: Arc<common::node::Node>,
76-
}
77-
63+
/// This function makes use of a JoinSet to process
64+
/// items in parallel. This makes using actix workers in InputResolver
65+
/// is a no no.
7866
pub async fn generate_target(
7967
config: &config::Config,
8068
target_name: &str,

libs/metagen/src/mdk_rust/mod.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,17 @@ impl crate::Plugin for Generator {
3838
fn bill_of_inputs(&self) -> HashMap<String, GeneratorInputOrder> {
3939
[(
4040
Self::INPUT_TG.to_string(),
41-
GeneratorInputOrder::TypegraphDesc {
42-
name: self.config.base.typegraph_name.clone(),
41+
if let Some(tg_name) = &self.config.base.typegraph_name {
42+
GeneratorInputOrder::TypegraphFromTypegate {
43+
name: tg_name.clone(),
44+
}
45+
} else if let Some(tg_path) = &self.config.base.typegraph_path {
46+
GeneratorInputOrder::TypegraphFromPath {
47+
path: tg_path.clone(),
48+
name: self.config.base.typegraph_name.clone(),
49+
}
50+
} else {
51+
unreachable!()
4352
},
4453
)]
4554
.into_iter()
@@ -51,17 +60,22 @@ impl crate::Plugin for Generator {
5160
inputs: HashMap<String, GeneratorInputResolved>,
5261
) -> anyhow::Result<GeneratorOutput> {
5362
// return Ok(GeneratorOutput(Default::default()))
54-
let GeneratorInputResolved::TypegraphDesc { raw: tg } = inputs
63+
let tg = match inputs
5564
.get(Self::INPUT_TG)
56-
.context("missing generator input")?;
65+
.context("missing generator input")?
66+
{
67+
GeneratorInputResolved::TypegraphFromTypegate { raw } => raw,
68+
GeneratorInputResolved::TypegraphFromPath { raw } => raw,
69+
};
5770
let mut out = HashMap::new();
5871
out.insert(
5972
self.config.base.path.join("mod.rs"),
6073
gen_mod_rs(&self.config, tg)?,
6174
);
6275
if self.config.no_crate_manifest.unwrap_or(true) {
6376
use heck::ToSnekCase;
64-
let crate_name = format!("{}_mdk", self.config.base.typegraph_name.to_snek_case());
77+
let tg_name = tg.name().unwrap_or_else(|_| "generated".to_string());
78+
let crate_name = format!("{}_mdk", tg_name.to_snek_case());
6579
out.insert(
6680
self.config.base.path.join("Cargo.toml"),
6781
gen_cargo_toml(Some(&crate_name)),
@@ -155,7 +169,8 @@ fn mdk_rs_e2e() -> anyhow::Result<()> {
155169
stubbed_runtimes: None,
156170
no_crate_manifest: None,
157171
base: config::MdkGeneratorConfigBase {
158-
typegraph_name: tg_name.into(),
172+
typegraph_name: Some(tg_name.into()),
173+
typegraph_path: None,
159174
// NOTE: root will map to the test's tempdir
160175
path: "./gen/".into(),
161176
},
@@ -181,11 +196,7 @@ fn mdk_rs_e2e() -> anyhow::Result<()> {
181196
build_fn: |args| {
182197
Box::pin(async move {
183198
let status = tokio::process::Command::new("cargo")
184-
.args(
185-
"build --target wasm32-wasi --target-dir target/wasi"
186-
.split(' ')
187-
.collect::<Vec<_>>(),
188-
)
199+
.args("build --target wasm32-wasi".split(' ').collect::<Vec<_>>())
189200
.current_dir(args.path)
190201
.kill_on_drop(true)
191202
.spawn()?

libs/metagen/src/mdk_rust/stubs.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,16 @@ mod test {
108108
let generator = Generator::new(MdkRustGenConfig {
109109
base: crate::config::MdkGeneratorConfigBase {
110110
path: "/tmp".into(),
111-
typegraph_name: tg_name.clone(),
111+
typegraph_name: Some(tg_name.clone()),
112+
typegraph_path: None,
112113
},
113114
stubbed_runtimes: Some(vec!["wasm".into()]),
114115
no_crate_manifest: None,
115116
})?;
116117
let out = generator.generate(
117118
[(
118119
Generator::INPUT_TG.to_owned(),
119-
GeneratorInputResolved::TypegraphDesc { raw: tg },
120+
GeneratorInputResolved::TypegraphFromTypegate { raw: tg },
120121
)]
121122
.into_iter()
122123
.collect(),

libs/metagen/src/tests/fixtures.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use common::typegraph::*;
77
pub async fn test_typegraph_1() -> anyhow::Result<Typegraph> {
88
let out = tokio::process::Command::new("cargo")
99
.args(
10-
// "run -p meta-cli -- serialize -f tests/tg.ts"
11-
"run -p meta-cli -- serialize -f ../../examples/typegraphs/reduce.py"
10+
"run -p meta-cli -- serialize -f tests/tg.ts"
11+
// "run -p meta-cli -- serialize -f ../../examples/typegraphs/reduce.py"
1212
.split(' ')
1313
.collect::<Vec<_>>(),
1414
)

libs/metagen/src/tests/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ struct TestCtx {
1414
impl InputResolver for TestCtx {
1515
async fn resolve(&self, order: GeneratorInputOrder) -> anyhow::Result<GeneratorInputResolved> {
1616
match order {
17-
GeneratorInputOrder::TypegraphDesc { name } => {
18-
Ok(GeneratorInputResolved::TypegraphDesc {
17+
GeneratorInputOrder::TypegraphFromTypegate { name } => {
18+
Ok(GeneratorInputResolved::TypegraphFromTypegate {
1919
raw: self.typegraphs.get(&name).unwrap().clone(),
2020
})
2121
}
22+
GeneratorInputOrder::TypegraphFromPath { .. } => unimplemented!(),
2223
}
2324
}
2425
}
@@ -78,7 +79,7 @@ pub async fn e2e_test(cases: Vec<E2eTestCase>) -> anyhow::Result<()> {
7879
// TODO: query generated stub functions
7980

8081
// cleanup
81-
tokio::fs::remove_dir_all(tmp_dir).await?;
82+
// tokio::fs::remove_dir_all(tmp_dir).await?;
8283
// node.try_undeploy(&typegraphs.keys().cloned().collect::<Vec<_>>()).await?;
8384
}
8485
Ok(())

meta-cli/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ dialoguer = "0.11.0"
5858
flate2 = "1.0.28"
5959
tar = "0.4.40"
6060
base64 = "0.21.5"
61-
common = { path = "../libs/common" }
62-
typescript = { path = "../libs/typescript" }
61+
common.workspace = true
62+
typescript.workspace = true
63+
metagen.workspace = true
6364
serde_yaml = "0.9.27"
6465
pathdiff = "0.2.1"
6566
openssl = { version = "0.10.59", features = ["vendored"] }
@@ -95,6 +96,7 @@ num_cpus = "1.16.0"
9596
assert_cmd = "2.0.12"
9697
insta = "1.34.0"
9798
project-root = "0.2.2"
99+
tempfile.workspace = true
98100

99101
[build-dependencies]
100102
shadow-rs.workspace = true

meta-cli/src/cli/codegen.rs

Lines changed: 0 additions & 71 deletions
This file was deleted.

meta-cli/src/cli/completion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use itertools::Itertools;
1313

1414
use super::Action;
1515
use super::Args;
16-
use super::GenArgs;
16+
use super::ConfigArgs;
1717

1818
#[derive(Parser, Debug)]
1919
pub struct Completion {
@@ -23,7 +23,7 @@ pub struct Completion {
2323

2424
#[async_trait]
2525
impl Action for Completion {
26-
async fn run(&self, _args: GenArgs, _: Option<ServerHandle>) -> Result<()> {
26+
async fn run(&self, _args: ConfigArgs, _: Option<ServerHandle>) -> Result<()> {
2727
let mut cmd = Args::command();
2828
let name = cmd.get_name().to_string();
2929
match self.shell.or_else(Shell::from_env) {

0 commit comments

Comments
 (0)