Skip to main content

tauri_plugin/build/
mobile.rs

1// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5//! Mobile-specific build utilities.
6
7use std::{
8  fs::{copy, create_dir, create_dir_all, remove_dir_all},
9  path::{Path, PathBuf},
10};
11
12use anyhow::{Context, Result};
13
14use super::{build_var, cfg_alias};
15
16/// Patches the entitlements file of the iOS project of the application that depends on this
17/// plugin, so the plugin can request the capabilities its native code needs
18/// (push notifications, app groups, HealthKit, iCloud...).
19///
20/// It edits `<project>/<app-name>_iOS/<app-name>_iOS.entitlements` of the Xcode project
21/// generated by `tauri ios init`, reading its path from the `TAURI_IOS_PROJECT_PATH` and
22/// `TAURI_IOS_APP_NAME` environment variables set by the Tauri CLI. It is a no-op when they are
23/// not set (a plain `cargo build`, or a desktop build) or when the file does not exist, and the
24/// file is only written when the closure actually changed it.
25///
26/// Must be called from the plugin `build.rs`, ideally guarded by a
27/// [`plugin_config`](super::plugin_config) value so the app can opt in to the entitlement.
28/// Only available on macOS hosts, where the iOS project can be built.
29///
30/// # Examples
31///
32/// ```rust,no_run
33/// tauri_plugin::mobile::update_entitlements(|entitlements| {
34///   entitlements.insert("aps-environment".into(), "development".into());
35/// })
36/// .expect("failed to update entitlements");
37/// ```
38#[cfg(target_os = "macos")]
39pub fn update_entitlements<F: FnOnce(&mut plist::Dictionary)>(f: F) -> Result<()> {
40  if let (Some(project_path), Ok(app_name)) = (
41    std::env::var_os("TAURI_IOS_PROJECT_PATH").map(PathBuf::from),
42    std::env::var("TAURI_IOS_APP_NAME"),
43  ) {
44    update_plist_file(
45      project_path
46        .join(format!("{app_name}_iOS"))
47        .join(format!("{app_name}_iOS.entitlements")),
48      f,
49    )?;
50  }
51
52  Ok(())
53}
54
55/// Patches the `Info.plist` of the iOS project of the application that depends on this plugin,
56/// which is where iOS expects the usage descriptions, URL schemes and background modes a
57/// plugin needs to be declared.
58///
59/// It edits `<project>/<app-name>_iOS/Info.plist` of the Xcode project generated by
60/// `tauri ios init`, reading its path from the `TAURI_IOS_PROJECT_PATH` and
61/// `TAURI_IOS_APP_NAME` environment variables set by the Tauri CLI. It is a no-op when they are
62/// not set (a plain `cargo build`, or a desktop build) or when the file does not exist, and the
63/// file is only written when the closure actually changed it.
64///
65/// Must be called from the plugin `build.rs`. Values that are specific to the application, such
66/// as the text shown on a permission prompt, should be read from the plugin configuration with
67/// [`plugin_config`](super::plugin_config) instead of hardcoded.
68/// Only available on macOS hosts, where the iOS project can be built.
69///
70/// # Examples
71///
72/// ```rust,no_run
73/// tauri_plugin::mobile::update_info_plist(|plist| {
74///   plist.insert(
75///     "NSCameraUsageDescription".into(),
76///     "This app needs the camera to scan QR codes".into(),
77///   );
78/// })
79/// .expect("failed to update Info.plist");
80/// ```
81#[cfg(target_os = "macos")]
82pub fn update_info_plist<F: FnOnce(&mut plist::Dictionary)>(f: F) -> Result<()> {
83  if let (Some(project_path), Ok(app_name)) = (
84    std::env::var_os("TAURI_IOS_PROJECT_PATH").map(PathBuf::from),
85    std::env::var("TAURI_IOS_APP_NAME"),
86  ) {
87    update_plist_file(
88      project_path
89        .join(format!("{app_name}_iOS"))
90        .join("Info.plist"),
91      f,
92    )?;
93  }
94
95  Ok(())
96}
97
98/// Updates the Android manifest by inserting XML content into a specified parent tag.
99pub fn update_android_manifest(block_identifier: &str, parent: &str, insert: String) -> Result<()> {
100  tauri_utils::build::update_android_manifest(block_identifier, parent, insert)
101}
102
103pub(crate) fn setup(
104  android_path: Option<PathBuf>,
105  #[allow(unused_variables)] ios_path: Option<PathBuf>,
106) -> Result<()> {
107  let target_os = build_var("CARGO_CFG_TARGET_OS")?;
108  let mobile = target_os == "android" || target_os == "ios";
109  cfg_alias("mobile", mobile);
110  cfg_alias("desktop", !mobile);
111
112  match target_os.as_str() {
113    "android" => {
114      if let Some(path) = android_path {
115        let manifest_dir = build_var("CARGO_MANIFEST_DIR").map(PathBuf::from)?;
116        let source = manifest_dir.join(path);
117
118        let tauri_library_path = std::env::var("DEP_TAURI_ANDROID_LIBRARY_PATH")
119            .expect("missing `DEP_TAURI_ANDROID_LIBRARY_PATH` environment variable. Make sure `tauri` is a dependency of the plugin.");
120        println!("cargo:rerun-if-env-changed=DEP_TAURI_ANDROID_LIBRARY_PATH");
121
122        create_dir_all(source.join(".tauri")).context("failed to create .tauri directory")?;
123        copy_folder(
124          Path::new(&tauri_library_path),
125          &source.join(".tauri").join("tauri-api"),
126          &[],
127        )
128        .context("failed to copy tauri-api to the plugin project")?;
129
130        println!("cargo:android_library_path={}", source.display());
131      }
132    }
133    #[cfg(target_os = "macos")]
134    "ios" => {
135      if let Some(path) = ios_path {
136        let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
137          .map(PathBuf::from)
138          .unwrap();
139        let tauri_library_path = std::env::var("DEP_TAURI_IOS_LIBRARY_PATH")
140            .expect("missing `DEP_TAURI_IOS_LIBRARY_PATH` environment variable. Make sure `tauri` is a dependency of the plugin.");
141
142        let tauri_dep_path = path.parent().unwrap().join(".tauri");
143        create_dir_all(&tauri_dep_path).context("failed to create .tauri directory")?;
144        copy_folder(
145          Path::new(&tauri_library_path),
146          &tauri_dep_path.join("tauri-api"),
147          &[".build", "Package.resolved", "Tests"],
148        )
149        .context("failed to copy tauri-api to the plugin project")?;
150        tauri_utils::build::link_apple_library(
151          &std::env::var("CARGO_PKG_NAME").unwrap(),
152          manifest_dir.join(path),
153        );
154      }
155    }
156    _ => (),
157  }
158
159  Ok(())
160}
161
162fn copy_folder(source: &Path, target: &Path, ignore_paths: &[&str]) -> Result<()> {
163  let _ = remove_dir_all(target);
164
165  for entry in walkdir::WalkDir::new(source) {
166    let entry = entry?;
167    let rel_path = entry.path().strip_prefix(source)?;
168    let rel_path_str = rel_path.to_string_lossy();
169    if ignore_paths
170      .iter()
171      .any(|path| rel_path_str.starts_with(path))
172    {
173      continue;
174    }
175    let dest_path = target.join(rel_path);
176
177    if entry.file_type().is_dir() {
178      create_dir(&dest_path)
179        .with_context(|| format!("failed to create directory {}", dest_path.display()))?;
180    } else {
181      copy(entry.path(), &dest_path).with_context(|| {
182        format!(
183          "failed to copy {} to {}",
184          entry.path().display(),
185          dest_path.display()
186        )
187      })?;
188      println!("cargo:rerun-if-changed={}", entry.path().display());
189    }
190  }
191
192  Ok(())
193}
194
195#[cfg(target_os = "macos")]
196fn update_plist_file<P: AsRef<Path>, F: FnOnce(&mut plist::Dictionary)>(
197  path: P,
198  f: F,
199) -> Result<()> {
200  use std::io::Cursor;
201
202  let path = path.as_ref();
203  if path.exists() {
204    let plist_str = std::fs::read_to_string(path)?;
205    let mut plist = plist::Value::from_reader(Cursor::new(&plist_str))?;
206    if let Some(dict) = plist.as_dictionary_mut() {
207      f(dict);
208      let mut plist_buf = Vec::new();
209      let writer = Cursor::new(&mut plist_buf);
210      plist::to_writer_xml(writer, &plist)?;
211      let new_plist_str = String::from_utf8(plist_buf)?;
212      if new_plist_str != plist_str {
213        std::fs::write(path, new_plist_str)?;
214      }
215    }
216  }
217
218  Ok(())
219}
220
221#[cfg(test)]
222mod tests {
223  #[test]
224  fn update_android_manifest() {
225    use tauri_utils::build::update_android_manifest;
226
227    // This test would require setting up the environment, so we just verify it compiles
228    // The actual implementation is tested in tauri-utils
229    let _result = update_android_manifest("test", "activity", "<test></test>".to_string());
230  }
231}