path.rs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. //! Types and functions related to file system path operations.
  5. use std::{
  6. env::temp_dir,
  7. path::{Component, Path, PathBuf},
  8. };
  9. use crate::{Config, Env, PackageInfo};
  10. use serde_repr::{Deserialize_repr, Serialize_repr};
  11. // we have to wrap the BaseDirectory enum in a module for #[allow(deprecated)]
  12. // to work, because the procedural macros on the enum prevent it from working directly
  13. // TODO: remove this workaround in v2 along with deprecated variants
  14. #[allow(deprecated)]
  15. mod base_directory {
  16. use super::*;
  17. /// A base directory to be used in [`resolve_path`].
  18. ///
  19. /// The base directory is the optional root of a file system operation.
  20. /// If informed by the API call, all paths will be relative to the path of the given directory.
  21. ///
  22. /// For more information, check the [`dirs_next` documentation](https://docs.rs/dirs_next/).
  23. #[derive(Serialize_repr, Deserialize_repr, Clone, Copy, Debug)]
  24. #[repr(u16)]
  25. #[non_exhaustive]
  26. pub enum BaseDirectory {
  27. /// The Audio directory.
  28. Audio = 1,
  29. /// The Cache directory.
  30. Cache,
  31. /// The Config directory.
  32. Config,
  33. /// The Data directory.
  34. Data,
  35. /// The LocalData directory.
  36. LocalData,
  37. /// The Desktop directory.
  38. Desktop,
  39. /// The Document directory.
  40. Document,
  41. /// The Download directory.
  42. Download,
  43. /// The Executable directory.
  44. Executable,
  45. /// The Font directory.
  46. Font,
  47. /// The Home directory.
  48. Home,
  49. /// The Picture directory.
  50. Picture,
  51. /// The Public directory.
  52. Public,
  53. /// The Runtime directory.
  54. Runtime,
  55. /// The Template directory.
  56. Template,
  57. /// The Video directory.
  58. Video,
  59. /// The Resource directory.
  60. Resource,
  61. /// The default app config directory.
  62. /// Resolves to [`BaseDirectory::Config`]`/{bundle_identifier}`.
  63. #[deprecated(
  64. since = "1.2.0",
  65. note = "Will be removed in 2.0.0. Use `BaseDirectory::AppConfig` or BaseDirectory::AppData` instead."
  66. )]
  67. App,
  68. /// The default app log directory.
  69. /// Resolves to [`BaseDirectory::Home`]`/Library/Logs/{bundle_identifier}` on macOS
  70. /// and [`BaseDirectory::Config`]`/{bundle_identifier}/logs` on linux and Windows.
  71. #[deprecated(
  72. since = "1.2.0",
  73. note = "Will be removed in 2.0.0. Use `BaseDirectory::AppLog` instead."
  74. )]
  75. Log,
  76. /// A temporary directory.
  77. /// Resolves to [`temp_dir`].
  78. Temp,
  79. /// The default app config directory.
  80. /// Resolves to [`BaseDirectory::Config`]`/{bundle_identifier}`.
  81. AppConfig,
  82. /// The default app data directory.
  83. /// Resolves to [`BaseDirectory::Data`]`/{bundle_identifier}`.
  84. AppData,
  85. /// The default app local data directory.
  86. /// Resolves to [`BaseDirectory::LocalData`]`/{bundle_identifier}`.
  87. AppLocalData,
  88. /// The default app cache directory.
  89. /// Resolves to [`BaseDirectory::Cache`]`/{bundle_identifier}`.
  90. AppCache,
  91. /// The default app log directory.
  92. /// Resolves to [`BaseDirectory::Home`]`/Library/Logs/{bundle_identifier}` on macOS
  93. /// and [`BaseDirectory::Config`]`/{bundle_identifier}/logs` on linux and Windows.
  94. AppLog,
  95. }
  96. }
  97. pub use base_directory::BaseDirectory;
  98. impl BaseDirectory {
  99. /// Gets the variable that represents this [`BaseDirectory`] for string paths.
  100. pub fn variable(self) -> &'static str {
  101. match self {
  102. Self::Audio => "$AUDIO",
  103. Self::Cache => "$CACHE",
  104. Self::Config => "$CONFIG",
  105. Self::Data => "$DATA",
  106. Self::LocalData => "$LOCALDATA",
  107. Self::Desktop => "$DESKTOP",
  108. Self::Document => "$DOCUMENT",
  109. Self::Download => "$DOWNLOAD",
  110. Self::Executable => "$EXE",
  111. Self::Font => "$FONT",
  112. Self::Home => "$HOME",
  113. Self::Picture => "$PICTURE",
  114. Self::Public => "$PUBLIC",
  115. Self::Runtime => "$RUNTIME",
  116. Self::Template => "$TEMPLATE",
  117. Self::Video => "$VIDEO",
  118. Self::Resource => "$RESOURCE",
  119. #[allow(deprecated)]
  120. Self::App => "$APP",
  121. #[allow(deprecated)]
  122. Self::Log => "$LOG",
  123. Self::Temp => "$TEMP",
  124. Self::AppConfig => "$APPCONFIG",
  125. Self::AppData => "$APPDATA",
  126. Self::AppLocalData => "$APPLOCALDATA",
  127. Self::AppCache => "$APPCACHE",
  128. Self::AppLog => "$APPLOG",
  129. }
  130. }
  131. /// Gets the [`BaseDirectory`] associated with the given variable, or [`None`] if the variable doesn't match any.
  132. pub fn from_variable(variable: &str) -> Option<Self> {
  133. let res = match variable {
  134. "$AUDIO" => Self::Audio,
  135. "$CACHE" => Self::Cache,
  136. "$CONFIG" => Self::Config,
  137. "$DATA" => Self::Data,
  138. "$LOCALDATA" => Self::LocalData,
  139. "$DESKTOP" => Self::Desktop,
  140. "$DOCUMENT" => Self::Document,
  141. "$DOWNLOAD" => Self::Download,
  142. "$EXE" => Self::Executable,
  143. "$FONT" => Self::Font,
  144. "$HOME" => Self::Home,
  145. "$PICTURE" => Self::Picture,
  146. "$PUBLIC" => Self::Public,
  147. "$RUNTIME" => Self::Runtime,
  148. "$TEMPLATE" => Self::Template,
  149. "$VIDEO" => Self::Video,
  150. "$RESOURCE" => Self::Resource,
  151. #[allow(deprecated)]
  152. "$APP" => Self::App,
  153. #[allow(deprecated)]
  154. "$LOG" => Self::Log,
  155. "$TEMP" => Self::Temp,
  156. "$APPCONFIG" => Self::AppConfig,
  157. "$APPDATA" => Self::AppData,
  158. "$APPLOCALDATA" => Self::AppLocalData,
  159. "$APPCACHE" => Self::AppCache,
  160. "$APPLOG" => Self::AppLog,
  161. _ => return None,
  162. };
  163. Some(res)
  164. }
  165. }
  166. /// Parse the given path, resolving a [`BaseDirectory`] variable if the path starts with one.
  167. ///
  168. /// # Examples
  169. ///
  170. /// ```rust,no_run
  171. /// use tauri::Manager;
  172. /// tauri::Builder::default()
  173. /// .setup(|app| {
  174. /// let path = tauri::api::path::parse(&app.config(), app.package_info(), &app.env(), "$HOME/.bashrc")?;
  175. /// assert_eq!(path.to_str().unwrap(), "/home/${whoami}/.bashrc");
  176. /// Ok(())
  177. /// });
  178. /// ```
  179. pub fn parse<P: AsRef<Path>>(
  180. config: &Config,
  181. package_info: &PackageInfo,
  182. env: &Env,
  183. path: P,
  184. ) -> crate::api::Result<PathBuf> {
  185. let mut p = PathBuf::new();
  186. let mut components = path.as_ref().components();
  187. match components.next() {
  188. Some(Component::Normal(str)) => {
  189. if let Some(base_directory) = BaseDirectory::from_variable(&str.to_string_lossy()) {
  190. p.push(resolve_path(
  191. config,
  192. package_info,
  193. env,
  194. "",
  195. Some(base_directory),
  196. )?);
  197. } else {
  198. p.push(str);
  199. }
  200. }
  201. Some(component) => p.push(component),
  202. None => (),
  203. }
  204. for component in components {
  205. if let Component::ParentDir = component {
  206. continue;
  207. }
  208. p.push(component);
  209. }
  210. Ok(p)
  211. }
  212. /// Resolves the path with the optional base directory.
  213. ///
  214. /// This is a low level API. If the application has been built,
  215. /// prefer the [path resolver API](`crate::AppHandle#method.path_resolver`).
  216. ///
  217. /// # Examples
  218. ///
  219. /// ## Before initializing the application
  220. ///
  221. /// ```rust,no_run
  222. /// use tauri::{api::path::{BaseDirectory, resolve_path}, Env};
  223. /// // on an actual app, remove the string argument
  224. /// let context = tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json");
  225. /// let path = resolve_path(
  226. /// context.config(),
  227. /// context.package_info(),
  228. /// &Env::default(),
  229. /// "db/tauri.sqlite",
  230. /// Some(BaseDirectory::AppData))
  231. /// .expect("failed to resolve path");
  232. /// assert_eq!(path.to_str().unwrap(), "/home/${whoami}/.config/com.tauri.app/db/tauri.sqlite");
  233. ///
  234. /// tauri::Builder::default().run(context).expect("error while running tauri application");
  235. /// ```
  236. ///
  237. /// ## With an initialized app
  238. /// ```rust,no_run
  239. /// use tauri::{api::path::{BaseDirectory, resolve_path}, Manager};
  240. /// tauri::Builder::default()
  241. /// .setup(|app| {
  242. /// let path = resolve_path(
  243. /// &app.config(),
  244. /// app.package_info(),
  245. /// &app.env(),
  246. /// "path/to/something",
  247. /// Some(BaseDirectory::Config)
  248. /// )?;
  249. /// assert_eq!(path.to_str().unwrap(), "/home/${whoami}/.config/path/to/something");
  250. /// Ok(())
  251. /// });
  252. /// ```
  253. pub fn resolve_path<P: AsRef<Path>>(
  254. config: &Config,
  255. package_info: &PackageInfo,
  256. env: &Env,
  257. path: P,
  258. dir: Option<BaseDirectory>,
  259. ) -> crate::api::Result<PathBuf> {
  260. if let Some(base_dir) = dir {
  261. let resolve_resource = matches!(base_dir, BaseDirectory::Resource);
  262. let base_dir_path = match base_dir {
  263. BaseDirectory::Audio => audio_dir(),
  264. BaseDirectory::Cache => cache_dir(),
  265. BaseDirectory::Config => config_dir(),
  266. BaseDirectory::Data => data_dir(),
  267. BaseDirectory::LocalData => local_data_dir(),
  268. BaseDirectory::Desktop => desktop_dir(),
  269. BaseDirectory::Document => document_dir(),
  270. BaseDirectory::Download => download_dir(),
  271. BaseDirectory::Executable => executable_dir(),
  272. BaseDirectory::Font => font_dir(),
  273. BaseDirectory::Home => home_dir(),
  274. BaseDirectory::Picture => picture_dir(),
  275. BaseDirectory::Public => public_dir(),
  276. BaseDirectory::Runtime => runtime_dir(),
  277. BaseDirectory::Template => template_dir(),
  278. BaseDirectory::Video => video_dir(),
  279. BaseDirectory::Resource => resource_dir(package_info, env),
  280. #[allow(deprecated)]
  281. BaseDirectory::App => app_config_dir(config),
  282. #[allow(deprecated)]
  283. BaseDirectory::Log => app_log_dir(config),
  284. BaseDirectory::Temp => Some(temp_dir()),
  285. BaseDirectory::AppConfig => app_config_dir(config),
  286. BaseDirectory::AppData => app_data_dir(config),
  287. BaseDirectory::AppLocalData => app_local_data_dir(config),
  288. BaseDirectory::AppCache => app_cache_dir(config),
  289. BaseDirectory::AppLog => app_log_dir(config),
  290. };
  291. if let Some(mut base_dir_path_value) = base_dir_path {
  292. // use the same path resolution mechanism as the bundler's resource injection algorithm
  293. if resolve_resource {
  294. let mut resource_path = PathBuf::new();
  295. for component in path.as_ref().components() {
  296. match component {
  297. Component::Prefix(_) => {}
  298. Component::RootDir => resource_path.push("_root_"),
  299. Component::CurDir => {}
  300. Component::ParentDir => resource_path.push("_up_"),
  301. Component::Normal(p) => resource_path.push(p),
  302. }
  303. }
  304. base_dir_path_value.push(resource_path);
  305. } else {
  306. base_dir_path_value.push(path);
  307. }
  308. Ok(base_dir_path_value)
  309. } else {
  310. Err(crate::api::Error::Path(
  311. "unable to determine base dir path".to_string(),
  312. ))
  313. }
  314. } else {
  315. let mut dir_path = PathBuf::new();
  316. dir_path.push(path);
  317. Ok(dir_path)
  318. }
  319. }
  320. /// Returns the path to the user's audio directory.
  321. ///
  322. /// ## Platform-specific
  323. ///
  324. /// - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_MUSIC_DIR`.
  325. /// - **macOS:** Resolves to `$HOME/Music`.
  326. /// - **Windows:** Resolves to `{FOLDERID_Music}`.
  327. pub fn audio_dir() -> Option<PathBuf> {
  328. dirs_next::audio_dir()
  329. }
  330. /// Returns the path to the user's cache directory.
  331. ///
  332. /// ## Platform-specific
  333. ///
  334. /// - **Linux:** Resolves to `$XDG_CACHE_HOME` or `$HOME/.cache`.
  335. /// - **macOS:** Resolves to `$HOME/Library/Caches`.
  336. /// - **Windows:** Resolves to `{FOLDERID_LocalAppData}`.
  337. pub fn cache_dir() -> Option<PathBuf> {
  338. dirs_next::cache_dir()
  339. }
  340. /// Returns the path to the user's config directory.
  341. ///
  342. /// ## Platform-specific
  343. ///
  344. /// - **Linux:** Resolves to `$XDG_CONFIG_HOME` or `$HOME/.config`.
  345. /// - **macOS:** Resolves to `$HOME/Library/Application Support`.
  346. /// - **Windows:** Resolves to `{FOLDERID_RoamingAppData}`.
  347. pub fn config_dir() -> Option<PathBuf> {
  348. dirs_next::config_dir()
  349. }
  350. /// Returns the path to the user's data directory.
  351. ///
  352. /// ## Platform-specific
  353. ///
  354. /// - **Linux:** Resolves to `$XDG_DATA_HOME` or `$HOME/.local/share`.
  355. /// - **macOS:** Resolves to `$HOME/Library/Application Support`.
  356. /// - **Windows:** Resolves to `{FOLDERID_RoamingAppData}`.
  357. pub fn data_dir() -> Option<PathBuf> {
  358. dirs_next::data_dir()
  359. }
  360. /// Returns the path to the user's local data directory.
  361. ///
  362. /// ## Platform-specific
  363. ///
  364. /// - **Linux:** Resolves to `$XDG_DATA_HOME` or `$HOME/.local/share`.
  365. /// - **macOS:** Resolves to `$HOME/Library/Application Support`.
  366. /// - **Windows:** Resolves to `{FOLDERID_LocalAppData}`.
  367. pub fn local_data_dir() -> Option<PathBuf> {
  368. dirs_next::data_local_dir()
  369. }
  370. /// Returns the path to the user's desktop directory.
  371. ///
  372. /// ## Platform-specific
  373. ///
  374. /// - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_DESKTOP_DIR`.
  375. /// - **macOS:** Resolves to `$HOME/Desktop`.
  376. /// - **Windows:** Resolves to `{FOLDERID_Desktop}`.
  377. pub fn desktop_dir() -> Option<PathBuf> {
  378. dirs_next::desktop_dir()
  379. }
  380. /// Returns the path to the user's document directory.
  381. ///
  382. /// ## Platform-specific
  383. ///
  384. /// - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_DOCUMENTS_DIR`.
  385. /// - **macOS:** Resolves to `$HOME/Documents`.
  386. /// - **Windows:** Resolves to `{FOLDERID_Documents}`.
  387. pub fn document_dir() -> Option<PathBuf> {
  388. dirs_next::document_dir()
  389. }
  390. /// Returns the path to the user's download directory.
  391. ///
  392. /// ## Platform-specific
  393. ///
  394. /// - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_DOWNLOAD_DIR`.
  395. /// - **macOS:** Resolves to `$HOME/Downloads`.
  396. /// - **Windows:** Resolves to `{FOLDERID_Downloads}`.
  397. pub fn download_dir() -> Option<PathBuf> {
  398. dirs_next::download_dir()
  399. }
  400. /// Returns the path to the user's executable directory.
  401. ///
  402. /// ## Platform-specific
  403. ///
  404. /// - **Linux:** Resolves to `$XDG_BIN_HOME/../bin` or `$XDG_DATA_HOME/../bin` or `$HOME/.local/bin`.
  405. /// - **macOS:** Not supported.
  406. /// - **Windows:** Not supported.
  407. pub fn executable_dir() -> Option<PathBuf> {
  408. dirs_next::executable_dir()
  409. }
  410. /// Returns the path to the user's font directory.
  411. ///
  412. /// ## Platform-specific
  413. ///
  414. /// - **Linux:** Resolves to `$XDG_DATA_HOME/fonts` or `$HOME/.local/share/fonts`.
  415. /// - **macOS:** Resolves to `$HOME/Library/Fonts`.
  416. /// - **Windows:** Not supported.
  417. pub fn font_dir() -> Option<PathBuf> {
  418. dirs_next::font_dir()
  419. }
  420. /// Returns the path to the user's home directory.
  421. ///
  422. /// ## Platform-specific
  423. ///
  424. /// - **Linux:** Resolves to `$HOME`.
  425. /// - **macOS:** Resolves to `$HOME`.
  426. /// - **Windows:** Resolves to `{FOLDERID_Profile}`.
  427. pub fn home_dir() -> Option<PathBuf> {
  428. dirs_next::home_dir()
  429. }
  430. /// Returns the path to the user's picture directory.
  431. ///
  432. /// ## Platform-specific
  433. ///
  434. /// - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_PICTURES_DIR`.
  435. /// - **macOS:** Resolves to `$HOME/Pictures`.
  436. /// - **Windows:** Resolves to `{FOLDERID_Pictures}`.
  437. pub fn picture_dir() -> Option<PathBuf> {
  438. dirs_next::picture_dir()
  439. }
  440. /// Returns the path to the user's public directory.
  441. ///
  442. /// ## Platform-specific
  443. ///
  444. /// - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_PUBLICSHARE_DIR`.
  445. /// - **macOS:** Resolves to `$HOME/Public`.
  446. /// - **Windows:** Resolves to `{FOLDERID_Public}`.
  447. pub fn public_dir() -> Option<PathBuf> {
  448. dirs_next::public_dir()
  449. }
  450. /// Returns the path to the user's runtime directory.
  451. ///
  452. /// ## Platform-specific
  453. ///
  454. /// - **Linux:** Resolves to `$XDG_RUNTIME_DIR`.
  455. /// - **macOS:** Not supported.
  456. /// - **Windows:** Not supported.
  457. pub fn runtime_dir() -> Option<PathBuf> {
  458. dirs_next::runtime_dir()
  459. }
  460. /// Returns the path to the user's template directory.
  461. ///
  462. /// ## Platform-specific
  463. ///
  464. /// - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_TEMPLATES_DIR`.
  465. /// - **macOS:** Not supported.
  466. /// - **Windows:** Resolves to `{FOLDERID_Templates}`.
  467. pub fn template_dir() -> Option<PathBuf> {
  468. dirs_next::template_dir()
  469. }
  470. /// Returns the path to the user's video dir
  471. ///
  472. /// ## Platform-specific
  473. ///
  474. /// - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_VIDEOS_DIR`.
  475. /// - **macOS:** Resolves to `$HOME/Movies`.
  476. /// - **Windows:** Resolves to `{FOLDERID_Videos}`.
  477. pub fn video_dir() -> Option<PathBuf> {
  478. dirs_next::video_dir()
  479. }
  480. /// Returns the path to the resource directory of this app.
  481. ///
  482. /// See [`PathResolver::resource_dir`](crate::PathResolver#method.resource_dir) for a more convenient helper function.
  483. pub fn resource_dir(package_info: &PackageInfo, env: &Env) -> Option<PathBuf> {
  484. crate::utils::platform::resource_dir(package_info, env).ok()
  485. }
  486. /// Returns the path to the suggested directory for your app's config files.
  487. ///
  488. /// Resolves to [`config_dir`]`/${bundle_identifier}`.
  489. ///
  490. /// See [`PathResolver::app_config_dir`](crate::PathResolver#method.app_config_dir) for a more convenient helper function.
  491. pub fn app_config_dir(config: &Config) -> Option<PathBuf> {
  492. dirs_next::config_dir().map(|dir| dir.join(&config.tauri.bundle.identifier))
  493. }
  494. /// Returns the path to the suggested directory for your app's data files.
  495. ///
  496. /// Resolves to [`data_dir`]`/${bundle_identifier}`.
  497. ///
  498. /// See [`PathResolver::app_data_dir`](crate::PathResolver#method.app_data_dir) for a more convenient helper function.
  499. pub fn app_data_dir(config: &Config) -> Option<PathBuf> {
  500. dirs_next::data_dir().map(|dir| dir.join(&config.tauri.bundle.identifier))
  501. }
  502. /// Returns the path to the suggested directory for your app's local data files.
  503. ///
  504. /// Resolves to [`local_data_dir`]`/${bundle_identifier}`.
  505. ///
  506. /// See [`PathResolver::app_local_data_dir`](crate::PathResolver#method.app_local_data_dir) for a more convenient helper function.
  507. pub fn app_local_data_dir(config: &Config) -> Option<PathBuf> {
  508. dirs_next::data_local_dir().map(|dir| dir.join(&config.tauri.bundle.identifier))
  509. }
  510. /// Returns the path to the suggested directory for your app's cache files.
  511. ///
  512. /// Resolves to [`cache_dir`]`/${bundle_identifier}`.
  513. ///
  514. /// See [`PathResolver::app_cache_dir`](crate::PathResolver#method.app_cache_dir) for a more convenient helper function.
  515. pub fn app_cache_dir(config: &Config) -> Option<PathBuf> {
  516. dirs_next::cache_dir().map(|dir| dir.join(&config.tauri.bundle.identifier))
  517. }
  518. /// Returns the path to the suggested directory for your app's log files.
  519. ///
  520. /// ## Platform-specific
  521. ///
  522. /// - **Linux:** Resolves to [`config_dir`]`/${bundle_identifier}/logs`.
  523. /// - **macOS:** Resolves to [`home_dir`]`/Library/Logs/${bundle_identifier}`
  524. /// - **Windows:** Resolves to [`config_dir`]`/${bundle_identifier}/logs`.
  525. ///
  526. /// See [`PathResolver::app_log_dir`](crate::PathResolver#method.app_log_dir) for a more convenient helper function.
  527. pub fn app_log_dir(config: &Config) -> Option<PathBuf> {
  528. #[cfg(target_os = "macos")]
  529. let path = dirs_next::home_dir().map(|dir| {
  530. dir
  531. .join("Library/Logs")
  532. .join(&config.tauri.bundle.identifier)
  533. });
  534. #[cfg(not(target_os = "macos"))]
  535. let path =
  536. dirs_next::config_dir().map(|dir| dir.join(&config.tauri.bundle.identifier).join("logs"));
  537. path
  538. }
  539. /// Returns the path to the suggested directory for your app's config files.
  540. ///
  541. /// Resolves to [`config_dir`]`/${bundle_identifier}`.
  542. ///
  543. /// See [`PathResolver::app_config_dir`](crate::PathResolver#method.app_config_dir) for a more convenient helper function.
  544. #[deprecated(
  545. since = "1.2.0",
  546. note = "Will be removed in 2.0.0. Use `app_config_dir` or `app_data_dir` instead."
  547. )]
  548. pub fn app_dir(config: &Config) -> Option<PathBuf> {
  549. app_config_dir(config)
  550. }
  551. /// Returns the path to the suggested directory for your app's log files.
  552. ///
  553. /// ## Platform-specific
  554. ///
  555. /// - **Linux:** Resolves to [`config_dir`]`/${bundle_identifier}`.
  556. /// - **macOS:** Resolves to [`home_dir`]`/Library/Logs/${bundle_identifier}`
  557. /// - **Windows:** Resolves to [`config_dir`]`/${bundle_identifier}`.
  558. ///
  559. /// See [`PathResolver::app_log_dir`](crate::PathResolver#method.app_log_dir) for a more convenient helper function.
  560. #[deprecated(
  561. since = "1.2.0",
  562. note = "Will be removed in 2.0.0. Use `app_log_dir` instead."
  563. )]
  564. pub fn log_dir(config: &Config) -> Option<PathBuf> {
  565. app_log_dir(config)
  566. }