tauri_plugin/build/
mobile.rs1use 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#[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#[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
98pub 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 let _result = update_android_manifest("test", "activity", "<test></test>".to_string());
230 }
231}