plugin.rs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. //! Compile-time and runtime types for Tauri plugins.
  5. #[cfg(feature = "build")]
  6. pub use build::*;
  7. #[cfg(feature = "build")]
  8. mod build {
  9. use std::{
  10. env::vars_os,
  11. path::{Path, PathBuf},
  12. };
  13. const GLOBAL_API_SCRIPT_PATH_KEY: &str = "GLOBAL_API_SCRIPT_PATH";
  14. /// Known file name of the file that contains an array with the path of all API scripts defined with [`define_global_api_script_path`].
  15. pub const GLOBAL_API_SCRIPT_FILE_LIST_PATH: &str = "__global-api-script.js";
  16. /// Defines the path to the global API script using Cargo instructions.
  17. pub fn define_global_api_script_path(path: PathBuf) {
  18. println!(
  19. "cargo:{GLOBAL_API_SCRIPT_PATH_KEY}={}",
  20. path
  21. .canonicalize()
  22. .expect("failed to canonicalize global API script path")
  23. .display()
  24. )
  25. }
  26. /// Collects the path of all the global API scripts defined with [`define_global_api_script_path`]
  27. /// and saves them to the out dir with filename [`GLOBAL_API_SCRIPT_FILE_LIST_PATH`].
  28. pub fn load_global_api_scripts(out_dir: &Path) {
  29. let mut scripts = Vec::new();
  30. for (key, value) in vars_os() {
  31. let key = key.to_string_lossy();
  32. if key.starts_with("DEP_") && key.ends_with(GLOBAL_API_SCRIPT_PATH_KEY) {
  33. let script_path = PathBuf::from(value);
  34. scripts.push(script_path);
  35. }
  36. }
  37. std::fs::write(
  38. out_dir.join(GLOBAL_API_SCRIPT_FILE_LIST_PATH),
  39. serde_json::to_string(&scripts).expect("failed to serialize global API script paths"),
  40. )
  41. .expect("failed to write global API script");
  42. }
  43. }