瀏覽代碼

feat(build): use swift workspace to resolve tauri dep path (#6243)

Lucas Fernandes Nogueira 2 年之前
父節點
當前提交
98867a9680
共有 4 個文件被更改,包括 55 次插入7 次删除
  1. 27 6
      core/tauri-build/src/mobile.rs
  2. 23 0
      core/tauri-build/templates/Package.swift
  3. 1 0
      core/tauri/Cargo.toml
  4. 4 1
      core/tauri/build.rs

+ 27 - 6
core/tauri-build/src/mobile.rs

@@ -32,7 +32,7 @@ impl PluginBuilder {
 
   /// Injects the mobile templates in the given path relative to the manifest root.
   pub fn run(self) -> Result<()> {
-    let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
+    let target_os = var("CARGO_CFG_TARGET_OS").unwrap();
     match target_os.as_str() {
       "android" => {
         if let Some(path) = self.android_path {
@@ -76,7 +76,22 @@ project(':{pkg_name}').projectDir = new File('./tauri-plugins/{pkg_name}')"
       #[cfg(target_os = "macos")]
       "ios" => {
         if let Some(path) = self.ios_path {
-          link_swift_library(&std::env::var("CARGO_PKG_NAME").unwrap(), path);
+          let package_name = var("CARGO_PKG_NAME").unwrap();
+          let tauri_library_path = std::env::var("DEP_TAURI_IOS_LIBRARY_PATH")
+            .expect("missing `DEP_TAURI_IOS_LIBRARY_PATH` environment variable. Make sure `tauri` is a dependency of the plugin.");
+
+          let project_path = std::env::temp_dir().join(&package_name);
+          std::fs::create_dir_all(&project_path)?;
+          copy_folder(&path, &project_path.join("ios"))?;
+
+          let package_swift_file = include_str!("../templates/Package.swift")
+            .replace("$PLUGIN_PACKAGE_NAME", &package_name)
+            .replace("$PLUGIN_PACKAGE_SRC_PATH", "ios/Sources")
+            .replace("$TAURI_PATH", &tauri_library_path);
+
+          std::fs::write(project_path.join("Package.swift"), package_swift_file)?;
+          std::env::set_current_dir(&project_path)?;
+          link_swift_library(&var("CARGO_PKG_NAME").unwrap(), project_path);
         }
       }
       _ => (),
@@ -112,6 +127,16 @@ pub fn inject_android_project(source: impl AsRef<Path>, target: impl AsRef<Path>
     None
   };
 
+  copy_folder(source, target)?;
+
+  if let Some(out_dir) = out_dir {
+    rename(out_dir, &build_path)?;
+  }
+
+  Ok(())
+}
+
+fn copy_folder(source: &Path, target: &Path) -> Result<()> {
   let _ = fs::remove_dir_all(target);
 
   for entry in walkdir::WalkDir::new(source) {
@@ -125,9 +150,5 @@ pub fn inject_android_project(source: impl AsRef<Path>, target: impl AsRef<Path>
     }
   }
 
-  if let Some(out_dir) = out_dir {
-    rename(out_dir, &build_path)?;
-  }
-
   Ok(())
 }

+ 23 - 0
core/tauri-build/templates/Package.swift

@@ -0,0 +1,23 @@
+// swift-tools-version:5.7
+// The swift-tools-version declares the minimum version of Swift required to build this package.
+
+import PackageDescription
+
+let package = Package(
+    name: "PluginWorkspace",
+    products: [
+        .library(name: "$PLUGIN_PACKAGE_NAME", type: .static, targets: ["$PLUGIN_PACKAGE_NAME"]),
+    ],
+    dependencies: [
+        .package(name: "Tauri", path: "$TAURI_PATH"),
+    ],
+    targets: [
+        .target(
+            name: "$PLUGIN_PACKAGE_NAME",
+            dependencies: [
+                .byName(name: "Tauri")
+            ],
+            path: "$PLUGIN_PACKAGE_SRC_PATH"
+        ),
+    ]
+)

+ 1 - 0
core/tauri/Cargo.toml

@@ -11,6 +11,7 @@ name = "tauri"
 readme = "README.md"
 repository = "https://github.com/tauri-apps/tauri"
 version = "2.0.0-alpha.3"
+links = "Tauri"
 
 [package.metadata.docs.rs]
 no-default-features = true

+ 4 - 1
core/tauri/build.rs

@@ -158,7 +158,10 @@ fn main() {
   #[cfg(target_os = "macos")]
   {
     if target_os == "ios" {
-      tauri_build::mobile::link_swift_library("Tauri", "./mobile/ios-api");
+      let lib_path =
+        PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("mobile/ios-api");
+      tauri_build::mobile::link_swift_library("Tauri", &lib_path);
+      println!("cargo:ios_library_path={}", lib_path.display());
     }
   }
 }