mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-09-11 07:55:34 +03:00
refactor(core): inject API project on plugin's Android and iOS projects (#6246)
This commit is contained in:
parent
0b51e2f657
commit
96b5e92169
4
.gitignore
vendored
4
.gitignore
vendored
@ -87,7 +87,3 @@ test_video.mp4
|
|||||||
# old cli directories
|
# old cli directories
|
||||||
/tooling/cli.js
|
/tooling/cli.js
|
||||||
/tooling/cli.rs
|
/tooling/cli.rs
|
||||||
|
|
||||||
# Swift
|
|
||||||
Package.resolved
|
|
||||||
.build
|
|
||||||
|
@ -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"
|
|
||||||
),
|
|
||||||
]
|
|
||||||
)
|
|
@ -1,5 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
env::var,
|
env::{var, var_os},
|
||||||
fs::{self, rename},
|
fs::{self, rename},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
@ -36,16 +36,30 @@ impl PluginBuilder {
|
|||||||
match target_os.as_str() {
|
match target_os.as_str() {
|
||||||
"android" => {
|
"android" => {
|
||||||
if let Some(path) = self.android_path {
|
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") {
|
if let Ok(project_dir) = var("TAURI_ANDROID_PROJECT_PATH") {
|
||||||
let source = manifest_dir.join(path);
|
|
||||||
let pkg_name = var("CARGO_PKG_NAME").unwrap();
|
let pkg_name = var("CARGO_PKG_NAME").unwrap();
|
||||||
|
|
||||||
println!("cargo:rerun-if-env-changed=TAURI_ANDROID_PROJECT_PATH");
|
println!("cargo:rerun-if-env-changed=TAURI_ANDROID_PROJECT_PATH");
|
||||||
|
|
||||||
let project_dir = PathBuf::from(project_dir);
|
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_path = project_dir.join("tauri.settings.gradle");
|
||||||
let gradle_settings = fs::read_to_string(&gradle_settings_path)?;
|
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")]
|
#[cfg(target_os = "macos")]
|
||||||
"ios" => {
|
"ios" => {
|
||||||
if let Some(path) = self.ios_path {
|
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")
|
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.");
|
.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);
|
copy_folder(
|
||||||
std::fs::create_dir_all(&project_path)?;
|
&Path::new(&tauri_library_path),
|
||||||
copy_folder(&path, &project_path.join("ios"))?;
|
&path.join("tauri-api"),
|
||||||
|
&[".build", "Package.resolved", "Tests"],
|
||||||
let package_swift_file = include_str!("../templates/Package.swift")
|
)?;
|
||||||
.replace("$PLUGIN_PACKAGE_NAME", &package_name)
|
link_swift_library(&var("CARGO_PKG_NAME").unwrap(), manifest_dir.join(path));
|
||||||
.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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
@ -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>) {
|
pub fn link_swift_library(name: &str, source: impl AsRef<Path>) {
|
||||||
let source = source.as_ref();
|
let source = source.as_ref();
|
||||||
println!("cargo:rerun-if-changed={}", source.display());
|
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")
|
swift_rs::build::SwiftLinker::new("10.13")
|
||||||
.with_ios("11")
|
.with_ios("11")
|
||||||
.with_package(name, source)
|
.with_package(name, source)
|
||||||
.link();
|
.link();
|
||||||
|
std::env::set_current_dir(&curr_dir).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[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 source = source.as_ref();
|
||||||
let target = target.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
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
copy_folder(source, target)?;
|
copy_folder(source, target, ignore_paths)?;
|
||||||
|
|
||||||
if let Some(out_dir) = out_dir {
|
if let Some(out_dir) = out_dir {
|
||||||
rename(out_dir, &build_path)?;
|
rename(out_dir, &build_path)?;
|
||||||
@ -136,12 +151,19 @@ pub fn inject_android_project(source: impl AsRef<Path>, target: impl AsRef<Path>
|
|||||||
Ok(())
|
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);
|
let _ = fs::remove_dir_all(target);
|
||||||
|
|
||||||
for entry in walkdir::WalkDir::new(source) {
|
for entry in walkdir::WalkDir::new(source) {
|
||||||
let entry = entry?;
|
let entry = entry?;
|
||||||
let rel_path = entry.path().strip_prefix(source)?;
|
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);
|
let dest_path = target.join(rel_path);
|
||||||
if entry.file_type().is_dir() {
|
if entry.file_type().is_dir() {
|
||||||
fs::create_dir(dest_path)?;
|
fs::create_dir(dest_path)?;
|
||||||
|
@ -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"
|
|
||||||
),
|
|
||||||
]
|
|
||||||
)
|
|
@ -150,16 +150,20 @@ fn main() {
|
|||||||
tauri_build::mobile::inject_android_project(
|
tauri_build::mobile::inject_android_project(
|
||||||
"./mobile/android",
|
"./mobile/android",
|
||||||
project_dir.join("tauri-api"),
|
project_dir.join("tauri-api"),
|
||||||
|
&[],
|
||||||
)
|
)
|
||||||
.expect("failed to copy tauri-api Android project");
|
.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")]
|
#[cfg(target_os = "macos")]
|
||||||
{
|
{
|
||||||
if target_os == "ios" {
|
if target_os == "ios" {
|
||||||
let lib_path =
|
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);
|
tauri_build::mobile::link_swift_library("Tauri", &lib_path);
|
||||||
println!("cargo:ios_library_path={}", lib_path.display());
|
println!("cargo:ios_library_path={}", lib_path.display());
|
||||||
}
|
}
|
||||||
|
1
core/tauri/mobile/ios-api/.gitignore
vendored
1
core/tauri/mobile/ios-api/.gitignore
vendored
@ -7,3 +7,4 @@ DerivedData/
|
|||||||
.swiftpm/config/registries.json
|
.swiftpm/config/registries.json
|
||||||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
|
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
|
||||||
.netrc
|
.netrc
|
||||||
|
Package.resolved
|
||||||
|
464
examples/api/src-tauri/tauri-plugin-sample/Cargo.lock
generated
464
examples/api/src-tauri/tauri-plugin-sample/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -1 +1,2 @@
|
|||||||
/build
|
/build
|
||||||
|
/tauri-api
|
||||||
|
@ -8,3 +8,4 @@ DerivedData/
|
|||||||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
|
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
|
||||||
.netrc
|
.netrc
|
||||||
Package.resolved
|
Package.resolved
|
||||||
|
/tauri-api
|
||||||
|
@ -17,8 +17,7 @@ let package = Package(
|
|||||||
],
|
],
|
||||||
dependencies: [
|
dependencies: [
|
||||||
// Dependencies declare other packages that this package depends on.
|
// 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")
|
||||||
.package(name: "Tauri", path: "../../../../../core/tauri/mobile/ios-api")
|
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
// 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(
|
.target(
|
||||||
name: "tauri-plugin-sample",
|
name: "tauri-plugin-sample",
|
||||||
dependencies: [
|
dependencies: [
|
||||||
//.product(name: "Tauri", package: "tauri"),
|
|
||||||
.byName(name: "Tauri")
|
.byName(name: "Tauri")
|
||||||
],
|
],
|
||||||
path: "Sources")
|
path: "Sources")
|
||||||
|
@ -1 +1,2 @@
|
|||||||
/build
|
/build
|
||||||
|
/tauri-api
|
||||||
|
1
tooling/cli/templates/plugin/ios/.gitignore
vendored
1
tooling/cli/templates/plugin/ios/.gitignore
vendored
@ -8,3 +8,4 @@ DerivedData/
|
|||||||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
|
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
|
||||||
.netrc
|
.netrc
|
||||||
Package.resolved
|
Package.resolved
|
||||||
|
/tauri-api
|
||||||
|
@ -16,8 +16,7 @@ let package = Package(
|
|||||||
targets: ["tauri-plugin-{{ plugin_name }}"]),
|
targets: ["tauri-plugin-{{ plugin_name }}"]),
|
||||||
],
|
],
|
||||||
dependencies: [
|
dependencies: [
|
||||||
// Dependencies declare other packages that this package depends on.
|
.package(name: "Tauri", path: "./tauri-api")
|
||||||
.package(url: "https://github.com/tauri-apps/tauri", branch: "next"),
|
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
// 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(
|
.target(
|
||||||
name: "tauri-plugin-{{ plugin_name }}",
|
name: "tauri-plugin-{{ plugin_name }}",
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.product(name: "Tauri", package: "tauri")
|
.byName(name: "Tauri")
|
||||||
],
|
],
|
||||||
path: "Sources")
|
path: "Sources")
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user