diff --git a/wasm-rpc-stubgen/src/cargo.rs b/wasm-rpc-stubgen/src/cargo.rs index 366c3876..1a9217a5 100644 --- a/wasm-rpc-stubgen/src/cargo.rs +++ b/wasm-rpc-stubgen/src/cargo.rs @@ -198,6 +198,19 @@ pub fn is_cargo_workspace_toml(path: &Path) -> anyhow::Result { } } +pub fn add_workspace_members(path: &Path, members: &[String]) -> anyhow::Result<()> { + let mut manifest = Manifest::from_path(path)?; + if let Some(workspace) = manifest.workspace.as_mut() { + workspace.members.extend(members.iter().cloned()); + } + + let cargo_toml = toml::to_string(&manifest)?; + + println!("Writing updated Cargo.toml to {:?}", path); + fs::write(path, cargo_toml)?; + Ok(()) +} + pub fn add_dependencies_to_cargo_toml(cargo_path: &Path, names: &[String]) -> anyhow::Result<()> { let mut manifest: Manifest = Manifest::from_path_with_metadata(cargo_path)?; if let Some(ref mut package) = manifest.package { diff --git a/wasm-rpc-stubgen/src/main.rs b/wasm-rpc-stubgen/src/main.rs index d51dbb08..51e8a6f0 100644 --- a/wasm-rpc-stubgen/src/main.rs +++ b/wasm-rpc-stubgen/src/main.rs @@ -35,7 +35,7 @@ async fn main() { Command::InitializeWorkspace(init_workspace_args) => { let _ = render_error(initialize_workspace( init_workspace_args, - "golem-wasm-rpc-stubgen", + "wasm-rpc-stubgen", &[], )); } diff --git a/wasm-rpc-stubgen/src/make.rs b/wasm-rpc-stubgen/src/make.rs index cd484557..a6a18e96 100644 --- a/wasm-rpc-stubgen/src/make.rs +++ b/wasm-rpc-stubgen/src/make.rs @@ -13,6 +13,7 @@ // limitations under the License. use crate::{cargo, GenerateArgs}; +use heck::ToSnakeCase; use std::fs; use std::process::Command; use toml::map::Map; @@ -26,7 +27,8 @@ pub fn initialize_workspace( stubgen_prefix: &[&str], ) -> anyhow::Result<()> { let cwd = std::env::current_dir()?; - if cargo::is_cargo_workspace_toml(&cwd.join("Cargo.toml"))? { + let workspace_cargo = cwd.join("Cargo.toml"); + if cargo::is_cargo_workspace_toml(&workspace_cargo)? { let makefile_path = cwd.join("Makefile.toml"); if makefile_path.exists() && makefile_path.is_file() { Err(anyhow::anyhow!("Makefile.toml already exists. Modifying existing cargo-make projects is currently not supported.")) @@ -41,18 +43,24 @@ pub fn initialize_workspace( println!("Writing cargo-make Makefile to {:?}", makefile_path); fs::write(makefile_path, makefile)?; + let mut new_members = Vec::new(); for target in targets { println!("Generating initial stub for {target}"); + let stub_name = format!("{target}-stub"); crate::generate(GenerateArgs { source_wit_root: cwd.join(format!("{target}/wit")), - dest_crate_root: cwd.join(format!("{target}-stub")), + dest_crate_root: cwd.join(stub_name.clone()), world: None, stub_crate_version: "0.0.1".to_string(), wasm_rpc_path_override: wasm_rpc_path_override.clone(), })?; + + new_members.push(stub_name); } + cargo::add_workspace_members(&workspace_cargo, &new_members)?; + Ok(()) } else { Err(anyhow::anyhow!( @@ -237,19 +245,19 @@ fn generate_makefile( args.push(Value::String("--source-wasm".to_string())); args.push(Value::String(format!( "target/wasm32-wasi/debug/{}.wasm", - caller + caller.to_snake_case() ))); for target in targets { args.push(Value::String("--stub-wasm".to_string())); args.push(Value::String(format!( "target/wasm32-wasi/debug/{}_stub.wasm", - target + target.to_snake_case() ))); } args.push(Value::String("--dest-wasm".to_string())); args.push(Value::String(format!( - "target/wasm32-wasi/debug/{}-composed.wasm", - caller + "target/wasm32-wasi/debug/{}_composed.wasm", + caller.to_snake_case() ))); compose.insert("args".to_string(), Value::Array(args)); @@ -270,19 +278,19 @@ fn generate_makefile( args.push(Value::String("--source-wasm".to_string())); args.push(Value::String(format!( "target/wasm32-wasi/release/{}.wasm", - caller + caller.to_snake_case() ))); for target in targets { args.push(Value::String("--stub-wasm".to_string())); args.push(Value::String(format!( "target/wasm32-wasi/release/{}_stub.wasm", - target + target.to_snake_case() ))); } args.push(Value::String("--dest-wasm".to_string())); args.push(Value::String(format!( - "target/wasm32-wasi/release/{}-composed.wasm", - caller + "target/wasm32-wasi/release/{}_composed.wasm", + caller.to_snake_case() ))); compose_release.insert("args".to_string(), Value::Array(args)); diff --git a/wasm-rpc-stubgen/src/rust.rs b/wasm-rpc-stubgen/src/rust.rs index c432abdf..5a80ca5b 100644 --- a/wasm-rpc-stubgen/src/rust.rs +++ b/wasm-rpc-stubgen/src/rust.rs @@ -25,9 +25,12 @@ use wit_parser::{ }; pub fn generate_stub_source(def: &StubDefinition) -> anyhow::Result<()> { - let root_ns = Ident::new(&def.root_package_name.namespace, Span::call_site()); + let root_ns = Ident::new( + &def.root_package_name.namespace.to_snake_case(), + Span::call_site(), + ); let root_name = Ident::new( - &format!("{}_stub", def.root_package_name.name), + &format!("{}_stub", def.root_package_name.name.to_snake_case()), Span::call_site(), );