Skip to content

Commit

Permalink
feat: Add hint for adding members to workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai committed Feb 6, 2024
1 parent bb9b25f commit fb5e9c3
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,11 +844,24 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
}

// Try to add the new package to the workspace members.
update_manifest_with_new_member(
if update_manifest_with_new_member(
&root_manifest_path,
&mut workspace_document,
&display_path,
)?;
)? {
config.shell().status(
"Adding",
format!(
"`{}` as member of workspace at `{}`",
PathBuf::from(&display_path)
.file_name()
.unwrap()
.to_str()
.unwrap(),
root_manifest_path.parent().unwrap().display()
),
)?
}
}
}
}
Expand Down Expand Up @@ -965,7 +978,7 @@ fn update_manifest_with_new_member(
root_manifest_path: &Path,
workspace_document: &mut toml_edit::Document,
display_path: &str,
) -> CargoResult<()> {
) -> CargoResult<bool> {
// If the members element already exist, check if one of the patterns
// in the array already includes the new package's relative path.
// - Add the relative path if the members don't match the new package's path.
Expand All @@ -983,7 +996,7 @@ fn update_manifest_with_new_member(
.with_context(|| format!("cannot build glob pattern from `{}`", pat))?;

if pattern.matches(&display_path) {
return Ok(());
return Ok(false);
}
}

Expand All @@ -1003,7 +1016,8 @@ fn update_manifest_with_new_member(
write_atomic(
&root_manifest_path,
workspace_document.to_string().to_string().as_bytes(),
)
)?;
Ok(true)
}

fn get_display_path(root_manifest_path: &Path, package_path: &Path) -> CargoResult<String> {
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_init/workspace_add_member/stderr.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Creating binary (application) package
Adding `foo` as member of workspace at `[ROOT]/case`
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Creating binary (application) `foo` package
Adding `foo` as member of workspace at `[ROOT]/case`
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Creating binary (application) `foo` package
Adding `foo` as member of workspace at `[ROOT]/case`
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Creating binary (application) `foo` package
Adding `foo` as member of workspace at `[ROOT]/case`
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Creating binary (application) `foo` package
Adding `foo` as member of workspace at `[ROOT]/case`
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Creating library `bar` package
Adding `bar` as member of workspace at `[ROOT]/case`
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1 change: 1 addition & 0 deletions tests/testsuite/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,7 @@ fn new_creates_members_list() {
p.cargo("new --lib bar")
.with_stderr("\
[CREATING] library `bar` package
[ADDING] `bar` as member of workspace at `[ROOT]/foo`
[NOTE] see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
")
.run();
Expand Down

0 comments on commit fb5e9c3

Please sign in to comment.