refactor(build): fill settings.gradle and build.gradle.kts on app build (#6342)

This commit is contained in:
Lucas Fernandes Nogueira
2023-02-22 08:57:19 -08:00
committed by GitHub
parent 1035a83187
commit 105fe3fa24
2 changed files with 50 additions and 34 deletions

View File

@@ -4,6 +4,7 @@
#![cfg_attr(doc_cfg, feature(doc_cfg))]
use anyhow::Context;
pub use anyhow::Result;
use cargo_toml::{Dependency, Manifest};
use heck::AsShoutySnakeCase;
@@ -13,7 +14,11 @@ use tauri_utils::{
resources::{external_binaries, resource_relpath, ResourcePaths},
};
use std::path::{Path, PathBuf};
use std::{
env::var_os,
fs::{read_dir, write},
path::{Path, PathBuf},
};
#[cfg(feature = "codegen")]
mod codegen;
@@ -282,6 +287,43 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
android_package_prefix.pop();
println!("cargo:rustc-env=TAURI_ANDROID_PACKAGE_PREFIX={android_package_prefix}");
if let Some(project_dir) = var_os("TAURI_ANDROID_PROJECT_PATH").map(PathBuf::from) {
let gradle_settings_path = project_dir.join("tauri.settings.gradle");
let app_build_gradle_path = project_dir.join("app").join("tauri.build.gradle.kts");
let mut gradle_settings =
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n".to_string();
let mut app_build_gradle = "// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
val implementation by configurations
dependencies {"
.to_string();
for entry in read_dir(project_dir.join("tauri-plugins"))? {
let pkg_name = entry?
.path()
.file_name()
.unwrap()
.to_string_lossy()
.into_owned();
gradle_settings.push_str(&format!("include ':{pkg_name}'"));
gradle_settings.push('\n');
gradle_settings.push_str(&format!(
"project(':{pkg_name}').projectDir = new File('./tauri-plugins/{pkg_name}')"
));
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("\n}");
write(&gradle_settings_path, gradle_settings)
.context("failed to write tauri.settings.gradle")?;
write(&app_build_gradle_path, app_build_gradle)
.context("failed to write tauri.build.gradle.kts")?;
}
cfg_alias("dev", !has_feature("custom-protocol"));
let ws_path = get_workspace_dir()?;
@@ -361,7 +403,6 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
}
if target_triple.contains("windows") {
use anyhow::Context;
use semver::Version;
use tauri_winres::{VersionInfo, WindowsResource};

View File

@@ -4,7 +4,7 @@
use std::{
env::{var, var_os},
fs::{copy, create_dir, read_to_string, remove_dir_all, rename, write},
fs::{copy, create_dir, remove_dir_all, rename},
path::{Path, PathBuf},
};
@@ -60,38 +60,13 @@ impl PluginBuilder {
if let Some(project_dir) = var_os("TAURI_ANDROID_PROJECT_PATH").map(PathBuf::from) {
let pkg_name = var("CARGO_PKG_NAME").unwrap();
println!("cargo:rerun-if-env-changed=TAURI_ANDROID_PROJECT_PATH");
let android_plugin_project_path = project_dir.join("tauri-plugins").join(&pkg_name);
inject_android_project(&source, android_plugin_project_path, &["tauri-api"])
.context("failed to inject plugin Android project")?;
let gradle_settings_path = project_dir.join("tauri.settings.gradle");
let gradle_settings = read_to_string(&gradle_settings_path)
.context("failed to read tauri.settings.gradle")?;
let include = format!(
"include ':{pkg_name}'
project(':{pkg_name}').projectDir = new File('./tauri-plugins/{pkg_name}')"
);
if !gradle_settings.contains(&include) {
write(
&gradle_settings_path,
format!("{gradle_settings}\n{include}"),
)
.context("failed to write tauri.settings.gradle")?;
}
let app_build_gradle_path = project_dir.join("app").join("tauri.build.gradle.kts");
let app_build_gradle = read_to_string(&app_build_gradle_path)
.context("failed to read tauri.build.gradle.kts")?;
let implementation = format!(r#"implementation(project(":{pkg_name}"))"#);
let target = "dependencies {";
if !app_build_gradle.contains(&implementation) {
write(
&app_build_gradle_path,
app_build_gradle.replace(target, &format!("{target}\n {implementation}")),
)
.context("failed to write tauri.build.gradle.kts")?;
}
inject_android_project(
&source,
project_dir.join("tauri-plugins").join(pkg_name),
&["tauri-api"],
)
.context("failed to inject plugin Android project")?;
}
}
}