settings.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. use crate::{Result, TraceSrc};
  2. use once_cell::sync::Lazy;
  3. use serde::{Deserialize, Serialize};
  4. use std::{fs, path::PathBuf, sync::Arc};
  5. use tracing::{error, trace, warn};
  6. const GLOBAL_SETTINGS_FILE_NAME: &str = "dioxus/settings.toml";
  7. /// Describes cli settings from project or global level.
  8. /// The order of priority goes:
  9. /// 1. CLI Flags/Arguments
  10. /// 2. Project-level Settings
  11. /// 3. Global-level settings.
  12. ///
  13. /// This allows users to control the cli settings with ease.
  14. #[derive(Debug, Clone, Serialize, Deserialize, Default)]
  15. pub(crate) struct CliSettings {
  16. /// Describes whether hot reload should always be on.
  17. pub(crate) always_hot_reload: Option<bool>,
  18. /// Describes whether the CLI should always open the browser for Web targets.
  19. pub(crate) always_open_browser: Option<bool>,
  20. /// Describes whether desktop apps in development will be pinned always-on-top.
  21. pub(crate) always_on_top: Option<bool>,
  22. /// Describes the interval in seconds that the CLI should poll for file changes on WSL.
  23. #[serde(default = "default_wsl_file_poll_interval")]
  24. pub(crate) wsl_file_poll_interval: Option<u16>,
  25. /// Use tooling from path rather than downloading them.
  26. pub(crate) no_downloads: Option<bool>,
  27. /// Ignore updates for this version
  28. pub(crate) ignore_version_update: Option<String>,
  29. }
  30. impl CliSettings {
  31. /// Load the settings from the local, global, or default config in that order
  32. pub(crate) fn load() -> Arc<Self> {
  33. static SETTINGS: Lazy<Arc<CliSettings>> =
  34. Lazy::new(|| Arc::new(CliSettings::global_or_default()));
  35. SETTINGS.clone()
  36. }
  37. pub fn global_or_default() -> Self {
  38. CliSettings::from_global().unwrap_or_default()
  39. }
  40. /// Get the current settings structure from global.
  41. pub(crate) fn from_global() -> Option<Self> {
  42. let Some(path) = dirs::data_local_dir() else {
  43. warn!("failed to get local data directory, some config keys may be missing");
  44. return None;
  45. };
  46. let path = path.join(GLOBAL_SETTINGS_FILE_NAME);
  47. let Some(data) = fs::read_to_string(path).ok() else {
  48. // We use a debug here because we expect the file to not exist.
  49. trace!("failed to read `{}` config file", GLOBAL_SETTINGS_FILE_NAME);
  50. return None;
  51. };
  52. let data = toml::from_str::<CliSettings>(&data).ok();
  53. if data.is_none() {
  54. warn!(
  55. "failed to parse `{}` config file",
  56. GLOBAL_SETTINGS_FILE_NAME
  57. );
  58. }
  59. data
  60. }
  61. /// Save the current structure to the global settings toml.
  62. /// This does not save to project-level settings.
  63. pub(crate) fn save(&self) -> Result<()> {
  64. let path = Self::get_settings_path().ok_or_else(|| {
  65. error!(dx_src = ?TraceSrc::Dev, "failed to get settings path");
  66. anyhow::anyhow!("failed to get settings path")
  67. })?;
  68. let data = toml::to_string_pretty(&self).map_err(|e| {
  69. error!(dx_src = ?TraceSrc::Dev, ?self, "failed to parse config into toml");
  70. anyhow::anyhow!("failed to parse config into toml: {e}")
  71. })?;
  72. // Create the directory structure if it doesn't exist.
  73. let parent_path = path.parent().unwrap();
  74. if let Err(e) = fs::create_dir_all(parent_path) {
  75. error!(
  76. dx_src = ?TraceSrc::Dev,
  77. ?data,
  78. ?path,
  79. "failed to create directories for settings file"
  80. );
  81. return Err(
  82. anyhow::anyhow!("failed to create directories for settings file: {e}").into(),
  83. );
  84. }
  85. // Write the data.
  86. let result = fs::write(&path, data.clone());
  87. if let Err(e) = result {
  88. error!(?data, ?path, "failed to save global cli settings");
  89. return Err(anyhow::anyhow!("failed to save global cli settings: {e}").into());
  90. }
  91. Ok(())
  92. }
  93. /// Get the path to the settings toml file.
  94. pub(crate) fn get_settings_path() -> Option<PathBuf> {
  95. let Some(path) = dirs::data_local_dir() else {
  96. warn!("failed to get local data directory, some config keys may be missing");
  97. return None;
  98. };
  99. Some(path.join(GLOBAL_SETTINGS_FILE_NAME))
  100. }
  101. /// Modify the settings toml file - doesn't change the settings for this session
  102. pub(crate) fn modify_settings(with: impl FnOnce(&mut CliSettings)) -> Result<()> {
  103. let mut _settings = CliSettings::load();
  104. let settings: &mut CliSettings = Arc::make_mut(&mut _settings);
  105. with(settings);
  106. settings.save()?;
  107. Ok(())
  108. }
  109. /// Check if we should prefer to use the no-downloads feature
  110. pub(crate) fn prefer_no_downloads() -> bool {
  111. if cfg!(feature = "no-downloads") && !cfg!(debug_assertions) {
  112. return true;
  113. }
  114. if crate::devcfg::no_downloads() {
  115. return true;
  116. }
  117. CliSettings::load().no_downloads.unwrap_or_default()
  118. }
  119. }
  120. fn default_wsl_file_poll_interval() -> Option<u16> {
  121. Some(2)
  122. }