Skip to content

Commit

Permalink
Use cargo add cargo-component-bindings in the new command
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasol authored and peterhuene committed Jan 25, 2024
1 parent 5cf73a6 commit 07ed8c9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
23 changes: 15 additions & 8 deletions src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str> {
match s {
Expand Down Expand Up @@ -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!(
Expand All @@ -297,6 +290,20 @@ impl NewCommand {
)
})?;

// Run cargo add cargo-component-bindings
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),
Expand Down
15 changes: 8 additions & 7 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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()
Expand Down
15 changes: 8 additions & 7 deletions tests/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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()
Expand Down
15 changes: 8 additions & 7 deletions tests/clippy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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()
Expand Down
15 changes: 8 additions & 7 deletions tests/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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()
Expand Down

0 comments on commit 07ed8c9

Please sign in to comment.