lib.rs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. pub use self::context::{context_codegen, ContextData};
  5. use std::{
  6. borrow::Cow,
  7. path::{Path, PathBuf},
  8. };
  9. pub use tauri_utils::config::{parse::ConfigError, Config};
  10. mod context;
  11. pub mod embedded_assets;
  12. #[doc(hidden)]
  13. pub mod vendor;
  14. /// Represents all the errors that can happen while reading the config during codegen.
  15. #[derive(Debug, thiserror::Error)]
  16. #[non_exhaustive]
  17. pub enum CodegenConfigError {
  18. #[error("unable to access current working directory: {0}")]
  19. CurrentDir(std::io::Error),
  20. // this error should be "impossible" because we use std::env::current_dir() - cover it anyways
  21. #[error("Tauri config file has no parent, this shouldn't be possible. file an issue on https://github.com/tauri-apps/tauri - target {0}")]
  22. Parent(PathBuf),
  23. #[error("unable to parse inline JSON TAURI_CONFIG env var: {0}")]
  24. FormatInline(serde_json::Error),
  25. #[error(transparent)]
  26. Json(#[from] serde_json::Error),
  27. #[error("{0}")]
  28. ConfigError(#[from] ConfigError),
  29. }
  30. /// Get the [`Config`] from the `TAURI_CONFIG` environmental variable, or read from the passed path.
  31. ///
  32. /// If the passed path is relative, it should be relative to the current working directory of the
  33. /// compiling crate.
  34. pub fn get_config(path: &Path) -> Result<(Config, PathBuf), CodegenConfigError> {
  35. let path = if path.is_relative() {
  36. let cwd = std::env::current_dir().map_err(CodegenConfigError::CurrentDir)?;
  37. Cow::Owned(cwd.join(path))
  38. } else {
  39. Cow::Borrowed(path)
  40. };
  41. // this should be impossible because of the use of `current_dir()` above, but handle it anyways
  42. let parent = path
  43. .parent()
  44. .map(ToOwned::to_owned)
  45. .ok_or_else(|| CodegenConfigError::Parent(path.into_owned()))?;
  46. // in the future we may want to find a way to not need the TAURI_CONFIG env var so that
  47. // it is impossible for the content of two separate configs to get mixed up. The chances are
  48. // already unlikely unless the developer goes out of their way to run the cli on a different
  49. // project than the target crate.
  50. let mut config = serde_json::from_value(tauri_utils::config::parse::read_from(parent.clone())?)?;
  51. if let Ok(env) = std::env::var("TAURI_CONFIG") {
  52. let merge_config: serde_json::Value =
  53. serde_json::from_str(&env).map_err(CodegenConfigError::FormatInline)?;
  54. json_patch::merge(&mut config, &merge_config);
  55. }
  56. let old_cwd = std::env::current_dir().map_err(CodegenConfigError::CurrentDir)?;
  57. // Set working directory to where `tauri.config.json` is, so that relative paths in it are parsed correctly.
  58. std::env::set_current_dir(parent.clone()).map_err(CodegenConfigError::CurrentDir)?;
  59. let config = serde_json::from_value(config)?;
  60. // Reset working directory.
  61. std::env::set_current_dir(old_cwd).map_err(CodegenConfigError::CurrentDir)?;
  62. Ok((config, parent))
  63. }