Selaa lähdekoodia

fix(cli/plugin/new): construct path from components iterator instead of `PathBuf::join` (#10128)

fixes #10082

The problem that we were calling `PathBuf::join()` with value of collecting an empty iterator of path components which was equivalent to `PathBuf::from("ios").join("")` which will result in `ios/` with a trailing slash.

This is fixed by chaining iterators of path components and collecting only once into `PathBuf`, which will never append empy path component and will never append trailing slash.
```rs
[
  Component::Normal(OsStr::new("ios")),
  Component::Normal(&some_folder_name),
]
.into_iter()
.chain(other_components_iterator)
.collect::<PathBuf>()
```

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Amr Bashir 1 vuosi sitten
vanhempi
sitoutus
eb76df4c4f
1 muutettua tiedostoa jossa 11 lisäystä ja 6 poistoa
  1. 11 6
      tooling/cli/src/plugin/init.rs

+ 11 - 6
tooling/cli/src/plugin/init.rs

@@ -13,6 +13,7 @@ use clap::{Parser, ValueEnum};
 use handlebars::{to_json, Handlebars};
 use heck::{ToKebabCase, ToPascalCase, ToSnakeCase};
 use include_dir::{include_dir, Dir};
+use std::ffi::{OsStr, OsString};
 use std::{
   collections::BTreeMap,
   env::current_dir,
@@ -211,12 +212,16 @@ pub fn command(mut options: Options) -> Result<()> {
             "ios-xcode" if !matches!(ios_framework, IosFrameworkKind::Xcode) => return Ok(None),
             "ios-spm" | "ios-xcode" => {
               let folder_name = components.next().unwrap().as_os_str().to_string_lossy();
+              let new_folder_name = folder_name.replace("{{ plugin_name }}", &plugin_name);
+              let new_folder_name = OsString::from(&new_folder_name);
 
-              path = Path::new("ios")
-                .join(Component::Normal(&std::ffi::OsString::from(
-                  &folder_name.replace("{{ plugin_name }}", &plugin_name),
-                )))
-                .join(components.collect::<PathBuf>());
+              path = [
+                Component::Normal(OsStr::new("ios")),
+                Component::Normal(&new_folder_name),
+              ]
+              .into_iter()
+              .chain(components)
+              .collect::<PathBuf>();
             }
             "guest-js" | "rollup.config.js" | "tsconfig.json" | "package.json"
               if options.no_api =>
@@ -236,7 +241,7 @@ pub fn command(mut options: Options) -> Result<()> {
         File::create(path).map(Some)
       },
     )
-    .with_context(|| "failed to render plugin Android template")?;
+    .with_context(|| "failed to render plugin template")?;
   }
 
   let permissions_dir = template_target_path.join("permissions");