Pārlūkot izejas kodu

refactor(core): inject API project on plugin's Android and iOS projects (#6246)

Lucas Fernandes Nogueira 2 gadi atpakaļ
vecāks
revīzija
96b5e92169

+ 0 - 4
.gitignore

@@ -87,7 +87,3 @@ test_video.mp4
 # old cli directories
 /tooling/cli.js
 /tooling/cli.rs
-
-# Swift
-Package.resolved
-.build

+ 0 - 28
Package.swift

@@ -1,28 +0,0 @@
-// 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: "TauriWorkspace",
-    products: [
-        .library(name: "Tauri", targets: ["Tauri"]),
-    ],
-    dependencies: [
-        .package(url: "https://github.com/Brendonovich/swift-rs", revision: "eb6de914ad57501da5019154d476d45660559999"),
-    ],
-    targets: [
-        .target(
-            name: "Tauri",
-            dependencies: [
-                .product(name: "SwiftRs", package: "swift-rs"),
-            ],
-            path: "core/tauri/mobile/ios-api/Sources/Tauri"
-        ),
-        .testTarget(
-            name: "TauriTests",
-            dependencies: ["Tauri"],
-            path: "core/tauri/mobile/ios-api/Tests/TauriTests"
-        ),
-    ]
-)

+ 42 - 20
core/tauri-build/src/mobile.rs

@@ -1,5 +1,5 @@
 use std::{
-  env::var,
+  env::{var, var_os},
   fs::{self, rename},
   path::{Path, PathBuf},
 };
@@ -36,16 +36,30 @@ impl PluginBuilder {
     match target_os.as_str() {
       "android" => {
         if let Some(path) = self.android_path {
-          let manifest_dir = var("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap();
+          let manifest_dir = var_os("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap();
+          let source = manifest_dir.join(path);
+
+          let tauri_library_path = std::env::var("DEP_TAURI_ANDROID_LIBRARY_PATH")
+            .expect("missing `DEP_TAURI_ANDROID_LIBRARY_PATH` environment variable. Make sure `tauri` is a dependency of the plugin.");
+
+          copy_folder(
+            Path::new(&tauri_library_path),
+            &source.join("tauri-api"),
+            &[],
+          )?;
+
           if let Ok(project_dir) = var("TAURI_ANDROID_PROJECT_PATH") {
-            let source = manifest_dir.join(path);
             let pkg_name = var("CARGO_PKG_NAME").unwrap();
 
             println!("cargo:rerun-if-env-changed=TAURI_ANDROID_PROJECT_PATH");
 
             let project_dir = PathBuf::from(project_dir);
 
-            inject_android_project(source, project_dir.join("tauri-plugins").join(&pkg_name))?;
+            inject_android_project(
+              source,
+              project_dir.join("tauri-plugins").join(&pkg_name),
+              &["tauri-api"],
+            )?;
 
             let gradle_settings_path = project_dir.join("tauri.settings.gradle");
             let gradle_settings = fs::read_to_string(&gradle_settings_path)?;
@@ -76,22 +90,16 @@ project(':{pkg_name}').projectDir = new File('./tauri-plugins/{pkg_name}')"
       #[cfg(target_os = "macos")]
       "ios" => {
         if let Some(path) = self.ios_path {
-          let package_name = var("CARGO_PKG_NAME").unwrap();
+          let manifest_dir = var_os("CARGO_MANIFEST_DIR").map(PathBuf::from).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);
+          copy_folder(
+            &Path::new(&tauri_library_path),
+            &path.join("tauri-api"),
+            &[".build", "Package.resolved", "Tests"],
+          )?;
+          link_swift_library(&var("CARGO_PKG_NAME").unwrap(), manifest_dir.join(path));
         }
       }
       _ => (),
@@ -106,14 +114,21 @@ project(':{pkg_name}').projectDir = new File('./tauri-plugins/{pkg_name}')"
 pub fn link_swift_library(name: &str, source: impl AsRef<Path>) {
   let source = source.as_ref();
   println!("cargo:rerun-if-changed={}", source.display());
+  let curr_dir = std::env::current_dir().unwrap();
+  std::env::set_current_dir(&source).unwrap();
   swift_rs::build::SwiftLinker::new("10.13")
     .with_ios("11")
     .with_package(name, source)
     .link();
+  std::env::set_current_dir(&curr_dir).unwrap();
 }
 
 #[doc(hidden)]
-pub fn inject_android_project(source: impl AsRef<Path>, target: impl AsRef<Path>) -> Result<()> {
+pub fn inject_android_project(
+  source: impl AsRef<Path>,
+  target: impl AsRef<Path>,
+  ignore_paths: &[&str],
+) -> Result<()> {
   let source = source.as_ref();
   let target = target.as_ref();
 
@@ -127,7 +142,7 @@ pub fn inject_android_project(source: impl AsRef<Path>, target: impl AsRef<Path>
     None
   };
 
-  copy_folder(source, target)?;
+  copy_folder(source, target, ignore_paths)?;
 
   if let Some(out_dir) = out_dir {
     rename(out_dir, &build_path)?;
@@ -136,12 +151,19 @@ pub fn inject_android_project(source: impl AsRef<Path>, target: impl AsRef<Path>
   Ok(())
 }
 
-fn copy_folder(source: &Path, target: &Path) -> Result<()> {
+fn copy_folder(source: &Path, target: &Path, ignore_paths: &[&str]) -> Result<()> {
   let _ = fs::remove_dir_all(target);
 
   for entry in walkdir::WalkDir::new(source) {
     let entry = entry?;
     let rel_path = entry.path().strip_prefix(source)?;
+    let rel_path_str = rel_path.to_string_lossy();
+    if ignore_paths
+      .iter()
+      .any(|path| rel_path_str.starts_with(path))
+    {
+      continue;
+    }
     let dest_path = target.join(rel_path);
     if entry.file_type().is_dir() {
       fs::create_dir(dest_path)?;

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

@@ -1,23 +0,0 @@
-// 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"
-        ),
-    ]
-)

+ 5 - 1
core/tauri/build.rs

@@ -150,16 +150,20 @@ fn main() {
       tauri_build::mobile::inject_android_project(
         "./mobile/android",
         project_dir.join("tauri-api"),
+        &[],
       )
       .expect("failed to copy tauri-api Android project");
     }
+    let lib_path =
+      PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("mobile/android");
+    println!("cargo:android_library_path={}", lib_path.display());
   }
 
   #[cfg(target_os = "macos")]
   {
     if target_os == "ios" {
       let lib_path =
-        PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("mobile/ios-api");
+        PathBuf::from(std::env::var_os("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());
     }

+ 1 - 0
core/tauri/mobile/ios-api/.gitignore

@@ -7,3 +7,4 @@ DerivedData/
 .swiftpm/config/registries.json
 .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
 .netrc
+Package.resolved

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 217 - 221
examples/api/src-tauri/tauri-plugin-sample/Cargo.lock


+ 2 - 1
examples/api/src-tauri/tauri-plugin-sample/android/.gitignore

@@ -1 +1,2 @@
-/build
+/build
+/tauri-api

+ 1 - 0
examples/api/src-tauri/tauri-plugin-sample/ios/.gitignore

@@ -8,3 +8,4 @@ DerivedData/
 .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
 .netrc
 Package.resolved
+/tauri-api

+ 1 - 3
examples/api/src-tauri/tauri-plugin-sample/ios/Package.swift

@@ -17,8 +17,7 @@ let package = Package(
     ],
     dependencies: [
         // Dependencies declare other packages that this package depends on.
-        //.package(url: "https://github.com/tauri-apps/tauri", branch: "next"),
-        .package(name: "Tauri", path: "../../../../../core/tauri/mobile/ios-api")
+        .package(name: "Tauri", path: "./tauri-api")
     ],
     targets: [
         // Targets are the basic building blocks of a package. A target can define a module or a test suite.
@@ -26,7 +25,6 @@ let package = Package(
         .target(
             name: "tauri-plugin-sample",
             dependencies: [
-                //.product(name: "Tauri", package: "tauri"),
                 .byName(name: "Tauri")
             ],
             path: "Sources")

+ 2 - 1
tooling/cli/templates/plugin/android/.gitignore

@@ -1 +1,2 @@
-/build
+/build
+/tauri-api

+ 1 - 0
tooling/cli/templates/plugin/ios/.gitignore

@@ -8,3 +8,4 @@ DerivedData/
 .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
 .netrc
 Package.resolved
+/tauri-api

+ 2 - 3
tooling/cli/templates/plugin/ios/Package.swift

@@ -16,8 +16,7 @@ let package = Package(
             targets: ["tauri-plugin-{{ plugin_name }}"]),
     ],
     dependencies: [
-        // Dependencies declare other packages that this package depends on.
-        .package(url: "https://github.com/tauri-apps/tauri", branch: "next"),
+        .package(name: "Tauri", path: "./tauri-api")
     ],
     targets: [
         // Targets are the basic building blocks of a package. A target can define a module or a test suite.
@@ -25,7 +24,7 @@ let package = Package(
         .target(
             name: "tauri-plugin-{{ plugin_name }}",
             dependencies: [
-                .product(name: "Tauri", package: "tauri")
+                .byName(name: "Tauri")
             ],
             path: "Sources")
     ]

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels