Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spawn cargo add in the new command #209

Merged
merged 1 commit into from
Jan 26, 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
25 changes: 17 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,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),
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
Loading