Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Stub generator fixes #18

Merged
merged 2 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions wasm-rpc-stubgen/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ pub fn is_cargo_workspace_toml(path: &Path) -> anyhow::Result<bool> {
}
}

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<MetadataRoot> = Manifest::from_path_with_metadata(cargo_path)?;
if let Some(ref mut package) = manifest.package {
Expand Down
2 changes: 1 addition & 1 deletion wasm-rpc-stubgen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
&[],
));
}
Expand Down
28 changes: 18 additions & 10 deletions wasm-rpc-stubgen/src/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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."))
Expand All @@ -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!(
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand Down
7 changes: 5 additions & 2 deletions wasm-rpc-stubgen/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);

Expand Down
Loading