1#[cfg(feature = "build")]
7pub use build::*;
8
9#[cfg(feature = "build")]
10mod build {
11 use std::{
12 env::vars_os,
13 fs,
14 path::{Path, PathBuf},
15 };
16
17 const GLOBAL_API_SCRIPT_PATH_KEY: &str = "GLOBAL_API_SCRIPT_PATH";
18 pub const GLOBAL_API_SCRIPT_FILE_LIST_PATH: &str = "__global-api-script.js";
20
21 pub fn define_global_api_script_path(path: &Path) {
23 println!(
24 "cargo:{GLOBAL_API_SCRIPT_PATH_KEY}={}",
25 path
26 .canonicalize()
27 .expect("failed to canonicalize global API script path")
28 .display()
29 )
30 }
31
32 pub fn save_global_api_scripts_paths(out_dir: &Path, mut tauri_global_scripts: Option<PathBuf>) {
38 let mut scripts = Vec::new();
39
40 for (key, value) in vars_os() {
41 let key = key.to_string_lossy();
42
43 if key == format!("DEP_TAURI_{GLOBAL_API_SCRIPT_PATH_KEY}") {
44 tauri_global_scripts = Some(PathBuf::from(value));
45 } else if key.starts_with("DEP_") && key.ends_with(GLOBAL_API_SCRIPT_PATH_KEY) {
46 let script_path = PathBuf::from(value);
47 scripts.push(script_path);
48 }
49 }
50
51 if let Some(tauri_global_scripts) = tauri_global_scripts {
52 scripts.insert(0, tauri_global_scripts);
53 }
54
55 fs::write(
56 out_dir.join(GLOBAL_API_SCRIPT_FILE_LIST_PATH),
57 serde_json::to_string(&scripts).expect("failed to serialize global API script paths"),
58 )
59 .expect("failed to write global API script");
60 }
61
62 pub fn read_global_api_scripts(out_dir: &Path) -> Option<Vec<String>> {
64 let global_scripts_path = out_dir.join(GLOBAL_API_SCRIPT_FILE_LIST_PATH);
65 if !global_scripts_path.exists() {
66 return None;
67 }
68
69 let global_scripts_str = fs::read_to_string(global_scripts_path)
70 .expect("failed to read plugin global API script paths");
71 let global_scripts = serde_json::from_str::<Vec<PathBuf>>(&global_scripts_str)
72 .expect("failed to parse plugin global API script paths");
73
74 Some(
75 global_scripts
76 .into_iter()
77 .map(|p| {
78 fs::read_to_string(&p).unwrap_or_else(|e| {
79 panic!(
80 "failed to read plugin global API script {}: {e}",
81 p.display()
82 )
83 })
84 })
85 .collect(),
86 )
87 }
88}