From dc28baa43e0a5465c5072cda1a993fe7abe86104 Mon Sep 17 00:00:00 2001 From: Seto Elkahfi Date: Wed, 28 Aug 2024 11:38:46 +0200 Subject: [PATCH] prompt xcframework_name to avoid duplicate target names error in Xcode --- README.md | 2 +- src/commands/package.rs | 30 +++++++++++++++++++++++++----- src/main.rs | 4 ++-- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 657df2d..b6dd844 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ To bundle the previously created Rust library as Swift Package, run: ``` cargo swift package ``` -This command interactively prompts you for swift package name and target platforms. +This command interactively prompts you for swift package name, xcframework name, and target platforms. If some required toolchains for the selected target platforms are missing, cargo swift will ask you if it should install them automatically. That's it! You can now include the created package in an iOS or macOS app via Swift Package Manager. diff --git a/src/commands/package.rs b/src/commands/package.rs index 40c43fd..ba89c06 100644 --- a/src/commands/package.rs +++ b/src/commands/package.rs @@ -60,7 +60,7 @@ pub fn run( platforms: Option>, build_target: Option<&str>, package_name: Option, - xcframework_name: String, + xcframework_name: Option, disable_warnings: bool, config: Config, mode: Mode, @@ -101,7 +101,7 @@ pub fn run( platforms.clone(), build_target, None, - xcframework_name.clone(), + None, disable_warnings, &config, mode, @@ -121,7 +121,7 @@ fn run_for_crate( platforms: Option>, build_target: Option<&str>, package_name: Option, - xcframework_name: String, + xcframework_name: Option, disable_warnings: bool, config: &Config, mode: Mode, @@ -149,8 +149,12 @@ fn run_for_crate( } let crate_name = current_crate.name.to_lowercase(); - let package_name = - package_name.unwrap_or_else(|| prompt_package_name(&crate_name, config.accept_all)); + let package_name = package_name.unwrap_or_else(|| prompt_package_name(&crate_name, config.accept_all)); + let xcframework_name = xcframework_name.unwrap_or_else(|| prompt_xcframework_name(&crate_name, config.accept_all)); + + if package_name == xcframework_name { + Err("Package name and XCFramework name must be different to avoid duplicate target names in XCode!")?; + } let platforms = platforms.unwrap_or_else(|| prompt_platforms(config.accept_all)); @@ -482,6 +486,22 @@ fn prompt_package_name(crate_name: &str, accept_all: bool) -> String { .unwrap() } +fn prompt_xcframework_name(crate_name: &str, accept_all: bool) -> String { + let crate_name = format!("{}XCFramework", crate_name); + let default = crate_name.to_case(Case::UpperCamel); + + if accept_all { + return default; + } + + let theme = prompt_theme(); + Input::with_theme(&theme) + .with_prompt("XCFramework Name") + .default(default) + .interact_text() + .unwrap() +} + fn pick_lib_type( options: &[LibType], suggested: Option, diff --git a/src/main.rs b/src/main.rs index b56d11d..b2c25e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,8 +81,8 @@ enum Action { #[arg(short = 'n', long = "name")] package_name: Option, - #[arg(long, default_value = "RustFramework")] - xcframework_name: String, + #[arg(short ='f', long = "xcframework")] + xcframework_name: Option, #[arg(short, long)] /// Build package optimized for release (default: debug)