config.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. #[cfg(target_os = "linux")]
  5. use heck::KebabCase;
  6. use json_patch::merge;
  7. use once_cell::sync::Lazy;
  8. use serde_json::Value as JsonValue;
  9. #[path = "../../config_definition.rs"]
  10. mod config_definition;
  11. pub use config_definition::*;
  12. #[cfg(windows)]
  13. impl From<WixConfig> for tauri_bundler::WixSettings {
  14. fn from(config: WixConfig) -> tauri_bundler::WixSettings {
  15. tauri_bundler::WixSettings {
  16. template: config.template,
  17. fragment_paths: config.fragment_paths,
  18. component_group_refs: config.component_group_refs,
  19. component_refs: config.component_refs,
  20. feature_group_refs: config.feature_group_refs,
  21. feature_refs: config.feature_refs,
  22. merge_refs: config.merge_refs,
  23. skip_webview_install: config.skip_webview_install,
  24. }
  25. }
  26. }
  27. use std::{
  28. env::set_var,
  29. fs::File,
  30. io::BufReader,
  31. process::exit,
  32. sync::{Arc, Mutex},
  33. };
  34. pub type ConfigHandle = Arc<Mutex<Option<Config>>>;
  35. fn config_handle() -> &'static ConfigHandle {
  36. static CONFING_HANDLE: Lazy<ConfigHandle> = Lazy::new(Default::default);
  37. &CONFING_HANDLE
  38. }
  39. /// Gets the static parsed config from `tauri.conf.json`.
  40. fn get_internal(merge_config: Option<&str>, reload: bool) -> crate::Result<ConfigHandle> {
  41. if !reload && config_handle().lock().unwrap().is_some() {
  42. return Ok(config_handle().clone());
  43. }
  44. let path = super::app_paths::tauri_dir().join("tauri.conf.json");
  45. let file = File::open(path)?;
  46. let buf = BufReader::new(file);
  47. let mut config: JsonValue = serde_json::from_reader(buf)?;
  48. let schema: JsonValue = serde_json::from_str(include_str!("../../schema.json"))?;
  49. let mut scope = valico::json_schema::Scope::new();
  50. let schema = scope.compile_and_return(schema, false).unwrap();
  51. let state = schema.validate(&config);
  52. if !state.errors.is_empty() {
  53. for error in state.errors {
  54. eprintln!(
  55. "`tauri.conf.json` error on `{}`: {}",
  56. error
  57. .get_path()
  58. .chars()
  59. .skip(1)
  60. .collect::<String>()
  61. .replace("/", " > "),
  62. error.get_detail().unwrap_or_else(|| error.get_title()),
  63. );
  64. }
  65. exit(1);
  66. }
  67. if let Some(merge_config) = merge_config {
  68. let merge_config: JsonValue = serde_json::from_str(&merge_config)?;
  69. merge(&mut config, &merge_config);
  70. }
  71. #[allow(unused_mut)]
  72. let mut config: Config = serde_json::from_value(config)?;
  73. #[cfg(target_os = "linux")]
  74. if let Some(product_name) = config.package.product_name.as_mut() {
  75. *product_name = product_name.to_kebab_case();
  76. }
  77. set_var("TAURI_CONFIG", serde_json::to_string(&config)?);
  78. *config_handle().lock().unwrap() = Some(config);
  79. Ok(config_handle().clone())
  80. }
  81. pub fn get(merge_config: Option<&str>) -> crate::Result<ConfigHandle> {
  82. get_internal(merge_config, false)
  83. }
  84. pub fn reload(merge_config: Option<&str>) -> crate::Result<()> {
  85. get_internal(merge_config, true)?;
  86. Ok(())
  87. }