feat: Add hint for adding members to workspace

This commit is contained in:
Lin Yihai 2024-02-06 13:10:36 +08:00
parent bb9b25f213
commit 9be95dfb1c
9 changed files with 27 additions and 5 deletions

View File

@ -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()
),
)?
}
}
}
}
@ -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.
@ -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);
}
}
@ -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> {

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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();