mod.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. pub mod app_paths;
  5. pub mod cargo;
  6. pub mod cargo_manifest;
  7. pub mod config;
  8. pub mod flock;
  9. pub mod framework;
  10. pub mod npm;
  11. pub mod prompts;
  12. pub mod template;
  13. pub mod updater_signature;
  14. use std::{
  15. collections::HashMap,
  16. path::{Path, PathBuf},
  17. process::Command,
  18. };
  19. use anyhow::Context;
  20. use tauri_utils::config::HookCommand;
  21. use crate::{
  22. interface::{AppInterface, Interface},
  23. CommandExt,
  24. };
  25. use self::app_paths::app_dir;
  26. pub fn command_env(debug: bool) -> HashMap<&'static str, String> {
  27. let mut map = HashMap::new();
  28. map.insert(
  29. "TAURI_ENV_PLATFORM_VERSION",
  30. os_info::get().version().to_string(),
  31. );
  32. if debug {
  33. map.insert("TAURI_ENV_DEBUG", "true".into());
  34. }
  35. map
  36. }
  37. pub fn resolve_tauri_path<P: AsRef<Path>>(path: P, crate_name: &str) -> PathBuf {
  38. let path = path.as_ref();
  39. if path.is_absolute() {
  40. path.join(crate_name)
  41. } else {
  42. PathBuf::from("..").join(path).join(crate_name)
  43. }
  44. }
  45. pub fn cross_command(bin: &str) -> Command {
  46. #[cfg(target_os = "windows")]
  47. let cmd = {
  48. let mut cmd = Command::new("cmd");
  49. cmd.arg("/c").arg(bin);
  50. cmd
  51. };
  52. #[cfg(not(target_os = "windows"))]
  53. let cmd = Command::new(bin);
  54. cmd
  55. }
  56. pub fn run_hook(
  57. name: &str,
  58. hook: HookCommand,
  59. interface: &AppInterface,
  60. debug: bool,
  61. ) -> crate::Result<()> {
  62. let (script, script_cwd) = match hook {
  63. HookCommand::Script(s) if s.is_empty() => (None, None),
  64. HookCommand::Script(s) => (Some(s), None),
  65. HookCommand::ScriptWithOptions { script, cwd } => (Some(script), cwd.map(Into::into)),
  66. };
  67. let cwd = script_cwd.unwrap_or_else(|| app_dir().clone());
  68. if let Some(script) = script {
  69. log::info!(action = "Running"; "{} `{}`", name, script);
  70. let mut env = command_env(debug);
  71. env.extend(interface.env());
  72. log::debug!("Setting environment for hook {:?}", env);
  73. #[cfg(target_os = "windows")]
  74. let status = Command::new("cmd")
  75. .arg("/S")
  76. .arg("/C")
  77. .arg(&script)
  78. .current_dir(cwd)
  79. .envs(env)
  80. .piped()
  81. .with_context(|| format!("failed to run `{}` with `cmd /C`", script))?;
  82. #[cfg(not(target_os = "windows"))]
  83. let status = Command::new("sh")
  84. .arg("-c")
  85. .arg(&script)
  86. .current_dir(cwd)
  87. .envs(env)
  88. .piped()
  89. .with_context(|| format!("failed to run `{script}` with `sh -c`"))?;
  90. if !status.success() {
  91. anyhow::bail!(
  92. "{} `{}` failed with exit code {}",
  93. name,
  94. script,
  95. status.code().unwrap_or_default()
  96. );
  97. }
  98. }
  99. Ok(())
  100. }