Extract the crate name from the path argument of `cargo osdk new`

This commit is contained in:
John Hughes 2025-12-11 17:19:32 +01:00 committed by GitHub
parent ad7253e93d
commit a91c7403c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 2 deletions

View File

@ -12,11 +12,21 @@ use crate::{
pub fn execute_new_command(args: &NewArgs) {
cargo_new_lib(&args.crate_name);
// Extract the actual crate name from the path if a full path was provided.
// `cargo new` accepts both "my-crate" and "/path/to/my-crate", and in the
// latter case, it uses the last path component as the crate name.
let crate_name = PathBuf::from(&args.crate_name)
.file_name()
.and_then(|name| name.to_str())
.map(|name| name.to_string())
.unwrap_or_else(|| args.crate_name.clone());
let cargo_metadata = get_cargo_metadata(Some(&args.crate_name), None::<&[&str]>).unwrap();
add_manifest_dependencies(&cargo_metadata, &args.crate_name);
add_manifest_dependencies(&cargo_metadata, &crate_name);
create_osdk_manifest(&cargo_metadata, &args.project_type());
exclude_osdk_base(&cargo_metadata);
write_src_template(&cargo_metadata, &args.crate_name, &args.project_type());
write_src_template(&cargo_metadata, &crate_name, &args.project_type());
add_rust_toolchain(&cargo_metadata);
}