lib.rs 2.7 KB

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