From 39b37d077795f1429f3676c9f79ae0d42686055b Mon Sep 17 00:00:00 2001 From: Tomas Olvecky Date: Fri, 26 Jan 2024 01:09:19 +0100 Subject: [PATCH] Use `cargo add cargo-component-bindings` in the `new` command (#209) --- src/commands/new.rs | 25 +++++++++++++++++-------- tests/build.rs | 15 ++++++++------- tests/check.rs | 15 ++++++++------- tests/clippy.rs | 15 ++++++++------- tests/metadata.rs | 15 ++++++++------- 5 files changed, 49 insertions(+), 36 deletions(-) diff --git a/src/commands/new.rs b/src/commands/new.rs index 98eb1c4d..b8912a5d 100644 --- a/src/commands/new.rs +++ b/src/commands/new.rs @@ -14,11 +14,10 @@ use std::{ path::{Path, PathBuf}, process::Command, }; -use toml_edit::{table, value, Array, Document, InlineTable, Item, Table, Value}; +use toml_edit::{table, value, Document, Item, Table, Value}; use url::Url; const WIT_BINDGEN_CRATE: &str = "wit-bindgen"; -const WIT_BINDGEN_VERSION: &str = "0.16.0"; fn escape_wit(s: &str) -> Cow { match s { @@ -282,13 +281,7 @@ impl NewCommand { metadata.set_implicit(true); metadata.set_position(doc.len()); metadata["component"] = Item::Table(component); - doc["package"]["metadata"] = Item::Table(metadata); - doc["dependencies"][WIT_BINDGEN_CRATE] = value(InlineTable::from_iter([ - ("version", Value::from(WIT_BINDGEN_VERSION)), - ("default-features", Value::from(false)), - ("features", Value::from(Array::from_iter(["realloc"]))), - ])); fs::write(&manifest_path, doc.to_string()).with_context(|| { format!( @@ -297,6 +290,22 @@ impl NewCommand { ) })?; + // Run cargo add for wit-bindgen + let mut cargo_add_command = std::process::Command::new("cargo"); + cargo_add_command.arg("add"); + cargo_add_command.arg("--quiet"); + cargo_add_command.arg("--no-default-features"); + cargo_add_command.arg("--features"); + cargo_add_command.arg("realloc"); + cargo_add_command.arg(WIT_BINDGEN_CRATE); + cargo_add_command.current_dir(out_dir); + let status = cargo_add_command + .status() + .context("failed to execute `cargo add` command")?; + if !status.success() { + bail!("`cargo add` command exited with non-zero status"); + } + config.terminal().status( "Updated", format!("manifest of package `{name}`", name = name.display), diff --git a/tests/build.rs b/tests/build.rs index ea5f979f..03985cd0 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -50,13 +50,6 @@ fn it_builds_a_workspace() -> Result<()> { root: dir.path().to_owned(), }; - project.file( - "Cargo.toml", - r#"[workspace] -members = ["foo", "bar", "baz"] -"#, - )?; - project.file( "baz/Cargo.toml", r#"[package] @@ -82,6 +75,14 @@ edition = "2021" .stderr(contains("Updated manifest of package `bar`")) .success(); + // Add the workspace after all of the projects have been created + project.file( + "Cargo.toml", + r#"[workspace] + members = ["foo", "bar", "baz"] + "#, + )?; + project .cargo_component("build") .assert() diff --git a/tests/check.rs b/tests/check.rs index 8284dfea..54359016 100644 --- a/tests/check.rs +++ b/tests/check.rs @@ -46,13 +46,6 @@ fn it_checks_a_workspace() -> Result<()> { root: dir.path().to_owned(), }; - project.file( - "Cargo.toml", - r#"[workspace] -members = ["foo", "bar", "baz"] -"#, - )?; - project.file( "baz/Cargo.toml", r#"[package] @@ -78,6 +71,14 @@ edition = "2021" .stderr(contains("Updated manifest of package `bar`")) .success(); + // Add the workspace after all of the packages have been created. + project.file( + "Cargo.toml", + r#"[workspace] + members = ["foo", "bar", "baz"] + "#, + )?; + project .cargo_component("check") .assert() diff --git a/tests/clippy.rs b/tests/clippy.rs index 8331269d..75f77330 100644 --- a/tests/clippy.rs +++ b/tests/clippy.rs @@ -67,13 +67,6 @@ fn it_checks_a_workspace() -> Result<()> { root: dir.path().to_owned(), }; - project.file( - "Cargo.toml", - r#"[workspace] -members = ["foo", "bar", "baz"] -"#, - )?; - project.file( "baz/Cargo.toml", r#"[package] @@ -99,6 +92,14 @@ edition = "2021" .stderr(contains("Updated manifest of package `bar`")) .success(); + // Add the workspace after all of the packages have been created. + project.file( + "Cargo.toml", + r#"[workspace] +members = ["foo", "bar", "baz"] +"#, + )?; + project .cargo_component("clippy") .assert() diff --git a/tests/metadata.rs b/tests/metadata.rs index 2fb09026..e233dd1f 100644 --- a/tests/metadata.rs +++ b/tests/metadata.rs @@ -44,13 +44,6 @@ fn it_prints_workspace_metadata() -> Result<()> { root: dir.path().to_owned(), }; - project.file( - "Cargo.toml", - r#"[workspace] -members = ["foo", "bar", "baz"] -"#, - )?; - project.file( "baz/Cargo.toml", r#"[package] @@ -76,6 +69,14 @@ edition = "2021" .stderr(contains("Updated manifest of package `bar`")) .success(); + // Add the workspace after all of the packages have been created. + project.file( + "Cargo.toml", + r#"[workspace] +members = ["foo", "bar", "baz"] +"#, + )?; + project .cargo_component("metadata --format-version 1") .assert()