use std::fs;
use std::path::Path;
fn main() {
let module = std::env::var_os("DEP_PUSHUPS_ANDROID")
.expect("pushups is a direct dependency, whose build script sets DEP_PUSHUPS_ANDROID");
let out = std::env::var_os("OUT_DIR").expect("cargo sets OUT_DIR");
let copy = Path::new(&out).join("pushups-android");
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("android") {
copy_dir(Path::new(&module), ©).expect("the Android module is copied");
if let Ok(library) = std::env::var("WRY_ANDROID_LIBRARY") {
tauri_plugin::mobile::update_android_manifest(
"PUSHUPS LIBRARY",
"application",
format!(
r#"<meta-data android:name="pushups.lib_name" android:value="{library}" />"#
),
)
.expect("the app's manifest is updated");
}
}
#[cfg(target_os = "macos")]
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("ios") {
tauri_plugin::mobile::update_entitlements(|entitlements| {
if !entitlements.contains_key("aps-environment") {
entitlements.insert("aps-environment".into(), "development".into());
}
})
.expect("the app's entitlements are updated");
tauri_plugin::mobile::update_info_plist(|info| {
let remote = plist::Value::String("remote-notification".to_owned());
match info.get_mut("UIBackgroundModes") {
Some(plist::Value::Array(modes)) => {
if !modes.contains(&remote) {
modes.push(remote);
}
}
_ => {
info.insert(
"UIBackgroundModes".into(),
plist::Value::Array(vec![remote]),
);
}
}
})
.expect("the app's Info.plist is updated");
}
println!("cargo::rerun-if-env-changed=DEP_PUSHUPS_ANDROID");
println!("cargo::rerun-if-env-changed=WRY_ANDROID_LIBRARY");
rerun_with_tauri_projects();
tauri_plugin::Builder::new(&[]).android_path(copy).build();
}
fn rerun_with_tauri_projects() {
println!("cargo::rerun-if-env-changed=TAURI_ANDROID_PROJECT_PATH");
println!("cargo::rerun-if-env-changed=TAURI_IOS_PROJECT_PATH");
println!("cargo::rerun-if-env-changed=TAURI_IOS_APP_NAME");
let mut edited = Vec::new();
if let Some(project) = std::env::var_os("TAURI_ANDROID_PROJECT_PATH") {
edited.push(Path::new(&project).join("app/src/main/AndroidManifest.xml"));
}
if let (Some(project), Ok(app)) = (
std::env::var_os("TAURI_IOS_PROJECT_PATH"),
std::env::var("TAURI_IOS_APP_NAME"),
) {
let ios = Path::new(&project).join(format!("{app}_iOS"));
edited.push(ios.join(format!("{app}_iOS.entitlements")));
edited.push(ios.join("Info.plist"));
}
for file in edited.iter().filter(|file| file.exists()) {
println!("cargo::rerun-if-changed={}", file.display());
}
}
fn copy_dir(from: &Path, to: &Path) -> std::io::Result<()> {
fs::create_dir_all(to)?;
for entry in fs::read_dir(from)? {
let entry = entry?;
let target = to.join(entry.file_name());
if entry.file_type()?.is_dir() {
if entry.file_name() != "build" {
copy_dir(&entry.path(), &target)?;
}
} else {
let content = fs::read(entry.path())?;
if fs::read(&target).ok().as_deref() != Some(content.as_slice()) {
fs::write(&target, content)?;
}
println!("cargo::rerun-if-changed={}", entry.path().display());
}
}
Ok(())
}