Skip to content

Commit

Permalink
refactor(core): use absolute path to plugin Android project (#6773)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Apr 23, 2023
1 parent dfa407f commit 2969d1c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .changes/plugin-android-project-refactor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cli.rs": patch
"tauri-build": patch
---

Use absolute path to each Android plugin project instead of copying the files to enhance developer experience.
25 changes: 14 additions & 11 deletions core/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ use tauri_utils::{
};

use std::{
collections::HashMap,
env::var_os,
fs::{read_dir, write},
fs::{read_to_string, write},
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -298,22 +299,24 @@ val implementation by configurations
dependencies {"
.to_string();

for entry in read_dir(project_dir.join(".tauri").join("plugins"))? {
let pkg_name = entry?
.path()
.file_name()
.unwrap()
.to_string_lossy()
.into_owned();
gradle_settings.push_str(&format!("include ':{pkg_name}'"));
let plugins_json_path = project_dir.join(".tauri").join("plugins.json");
let plugins: HashMap<String, mobile::PluginMetadata> = if plugins_json_path.exists() {
let s = read_to_string(&plugins_json_path)?;
serde_json::from_str(&s)?
} else {
Default::default()
};
for (plugin_name, plugin) in plugins {
gradle_settings.push_str(&format!("include ':{plugin_name}'"));
gradle_settings.push('\n');
gradle_settings.push_str(&format!(
"project(':{pkg_name}').projectDir = new File('./.tauri/plugins/{pkg_name}')"
"project(':{plugin_name}').projectDir = new File('{}')",
plugin.path.display()
));
gradle_settings.push('\n');

app_build_gradle.push('\n');
app_build_gradle.push_str(&format!(r#" implementation(project(":{pkg_name}"))"#));
app_build_gradle.push_str(&format!(r#" implementation(project(":{plugin_name}"))"#));
}
app_build_gradle.push_str("\n}");

Expand Down
25 changes: 18 additions & 7 deletions core/tauri-build/src/mobile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
// SPDX-License-Identifier: MIT

use std::{
collections::HashMap,
env::{var, var_os},
fs::{copy, create_dir, create_dir_all, remove_dir_all, rename},
fs::{copy, create_dir, create_dir_all, read_to_string, remove_dir_all, rename, write},
path::{Path, PathBuf},
};

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
pub(crate) struct PluginMetadata {
pub path: PathBuf,
}

#[derive(Default)]
pub struct PluginBuilder {
Expand Down Expand Up @@ -49,6 +56,7 @@ impl PluginBuilder {

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.");
println!("cargo:rerun-if-env-changed=DEP_TAURI_ANDROID_LIBRARY_PATH");

create_dir_all(source.join(".tauri")).context("failed to create .tauri directory")?;
copy_folder(
Expand All @@ -62,12 +70,15 @@ impl PluginBuilder {
let pkg_name = var("CARGO_PKG_NAME").unwrap();
println!("cargo:rerun-if-env-changed=TAURI_ANDROID_PROJECT_PATH");

inject_android_project(
&source,
project_dir.join(".tauri").join("plugins").join(pkg_name),
&["tauri-api"],
)
.context("failed to inject plugin Android project")?;
let plugins_json_path = project_dir.join(".tauri").join("plugins.json");
let mut plugins: HashMap<String, PluginMetadata> = if plugins_json_path.exists() {
let s = read_to_string(&plugins_json_path)?;
serde_json::from_str(&s)?
} else {
Default::default()
};
plugins.insert(pkg_name, PluginMetadata { path: source });
write(&plugins_json_path, serde_json::to_string(&plugins)?)?;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tooling/cli/src/mobile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ fn ensure_init(project_dir: PathBuf, target: Target) -> Result<()> {
)
} else {
if target == Target::Android {
create_dir_all(project_dir.join(".tauri").join("plugins"))?;
create_dir_all(project_dir.join(".tauri"))?;
}
Ok(())
}
Expand Down

0 comments on commit 2969d1c

Please sign in to comment.