Skip to content

Commit 5d89905

Browse files
caesaramrbashirlucasfernog
authored
feat(api): add app-specific directory APIs, closes #5263 (#5272)
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com> Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent eedfa5e commit 5d89905

File tree

11 files changed

+384
-134
lines changed

11 files changed

+384
-134
lines changed

.changes/app-dirs-api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": minor
3+
"api": minor
4+
---
5+
6+
Add new app-specific `BaseDirectory` enum variants `AppConfig`, `AppData`, `AppLocalData`, `AppCache` and `AppLog` along with equivalent functions in `path` module and deprecated ambiguous variants `Log` and `App` along with their equivalent functions in `path` module.

core/tauri-utils/src/config.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,8 @@ macro_rules! check_feature {
11291129
/// Each pattern can start with a variable that resolves to a system base directory.
11301130
/// The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`,
11311131
/// `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`,
1132-
/// `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`, `$LOG`, `$TEMP`.
1132+
/// `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`, `$LOG`, `$TEMP`, `$APPCONFIG`, `$APPDATA`,
1133+
/// `$APPLOCALDATA`, `$APPCACHE`, `$APPLOG`.
11331134
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
11341135
#[cfg_attr(feature = "schema", derive(JsonSchema))]
11351136
#[serde(untagged)]
@@ -1465,7 +1466,8 @@ pub struct ShellAllowedCommand {
14651466
/// It can start with a variable that resolves to a system base directory.
14661467
/// The variables are: `$AUDIO`, `$CACHE`, `$CONFIG`, `$DATA`, `$LOCALDATA`, `$DESKTOP`,
14671468
/// `$DOCUMENT`, `$DOWNLOAD`, `$EXE`, `$FONT`, `$HOME`, `$PICTURE`, `$PUBLIC`, `$RUNTIME`,
1468-
/// `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`, `$LOG`, `$TEMP`.
1469+
/// `$TEMPLATE`, `$VIDEO`, `$RESOURCE`, `$APP`, `$LOG`, `$TEMP`, `$APPCONFIG`, `$APPDATA`,
1470+
/// `$APPLOCALDATA`, `$APPCACHE`, `$APPLOG`.
14691471
#[serde(rename = "cmd", default)] // use default just so the schema doesn't flag it as required
14701472
pub command: PathBuf,
14711473

core/tauri/scripts/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/tauri/src/api/path.rs

Lines changed: 178 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,61 +13,94 @@ use crate::{Config, Env, PackageInfo};
1313

1414
use serde_repr::{Deserialize_repr, Serialize_repr};
1515

16-
/// A base directory to be used in [`resolve_path`].
17-
///
18-
/// The base directory is the optional root of a file system operation.
19-
/// If informed by the API call, all paths will be relative to the path of the given directory.
20-
///
21-
/// For more information, check the [`dirs_next` documentation](https://docs.rs/dirs_next/).
22-
#[derive(Serialize_repr, Deserialize_repr, Clone, Copy, Debug)]
23-
#[repr(u16)]
24-
#[non_exhaustive]
25-
pub enum BaseDirectory {
26-
/// The Audio directory.
27-
Audio = 1,
28-
/// The Cache directory.
29-
Cache,
30-
/// The Config directory.
31-
Config,
32-
/// The Data directory.
33-
Data,
34-
/// The LocalData directory.
35-
LocalData,
36-
/// The Desktop directory.
37-
Desktop,
38-
/// The Document directory.
39-
Document,
40-
/// The Download directory.
41-
Download,
42-
/// The Executable directory.
43-
Executable,
44-
/// The Font directory.
45-
Font,
46-
/// The Home directory.
47-
Home,
48-
/// The Picture directory.
49-
Picture,
50-
/// The Public directory.
51-
Public,
52-
/// The Runtime directory.
53-
Runtime,
54-
/// The Template directory.
55-
Template,
56-
/// The Video directory.
57-
Video,
58-
/// The Resource directory.
59-
Resource,
60-
/// The default App config directory.
61-
/// Resolves to [`BaseDirectory::Config`].
62-
App,
63-
/// The Log directory.
64-
/// Resolves to `BaseDirectory::Home/Library/Logs/{bundle_identifier}` on macOS
65-
/// and `BaseDirectory::Config/{bundle_identifier}/logs` on linux and windows.
66-
Log,
67-
/// A temporary directory.
68-
/// Resolves to [`temp_dir`].
69-
Temp,
16+
// we have to wrap the BaseDirectory enum in a module for #[allow(deprecated)]
17+
// to work, because the procedural macros on the enum prevent it from working directly
18+
// TODO: remove this workaround in v2 along with deprecated variants
19+
#[allow(deprecated)]
20+
mod base_directory {
21+
use super::*;
22+
23+
/// A base directory to be used in [`resolve_path`].
24+
///
25+
/// The base directory is the optional root of a file system operation.
26+
/// If informed by the API call, all paths will be relative to the path of the given directory.
27+
///
28+
/// For more information, check the [`dirs_next` documentation](https://docs.rs/dirs_next/).
29+
#[derive(Serialize_repr, Deserialize_repr, Clone, Copy, Debug)]
30+
#[repr(u16)]
31+
#[non_exhaustive]
32+
pub enum BaseDirectory {
33+
/// The Audio directory.
34+
Audio = 1,
35+
/// The Cache directory.
36+
Cache,
37+
/// The Config directory.
38+
Config,
39+
/// The Data directory.
40+
Data,
41+
/// The LocalData directory.
42+
LocalData,
43+
/// The Desktop directory.
44+
Desktop,
45+
/// The Document directory.
46+
Document,
47+
/// The Download directory.
48+
Download,
49+
/// The Executable directory.
50+
Executable,
51+
/// The Font directory.
52+
Font,
53+
/// The Home directory.
54+
Home,
55+
/// The Picture directory.
56+
Picture,
57+
/// The Public directory.
58+
Public,
59+
/// The Runtime directory.
60+
Runtime,
61+
/// The Template directory.
62+
Template,
63+
/// The Video directory.
64+
Video,
65+
/// The Resource directory.
66+
Resource,
67+
/// The default app config directory.
68+
/// Resolves to [`BaseDirectory::Config`]`/{bundle_identifier}`.
69+
#[deprecated(
70+
since = "1.2.0",
71+
note = "Will be removed in 2.0.0. Use `BaseDirectory::AppConfig` or BaseDirectory::AppData` instead."
72+
)]
73+
App,
74+
/// The default app log directory.
75+
/// Resolves to [`BaseDirectory::Home`]`/Library/Logs/{bundle_identifier}` on macOS
76+
/// and [`BaseDirectory::Config`]`/{bundle_identifier}/logs` on linux and Windows.
77+
#[deprecated(
78+
since = "1.2.0",
79+
note = "Will be removed in 2.0.0. Use `BaseDirectory::AppLog` instead."
80+
)]
81+
Log,
82+
/// A temporary directory.
83+
/// Resolves to [`temp_dir`].
84+
Temp,
85+
/// The default app config directory.
86+
/// Resolves to [`BaseDirectory::Config`]`/{bundle_identifier}`.
87+
AppConfig,
88+
/// The default app data directory.
89+
/// Resolves to [`BaseDirectory::Data`]`/{bundle_identifier}`.
90+
AppData,
91+
/// The default app local data directory.
92+
/// Resolves to [`BaseDirectory::LocalData`]`/{bundle_identifier}`.
93+
AppLocalData,
94+
/// The default app cache directory.
95+
/// Resolves to [`BaseDirectory::Cache`]`/{bundle_identifier}`.
96+
AppCache,
97+
/// The default app log directory.
98+
/// Resolves to [`BaseDirectory::Home`]`/Library/Logs/{bundle_identifier}` on macOS
99+
/// and [`BaseDirectory::Config`]`/{bundle_identifier}/logs` on linux and Windows.
100+
AppLog,
101+
}
70102
}
103+
pub use base_directory::BaseDirectory;
71104

72105
impl BaseDirectory {
73106
/// Gets the variable that represents this [`BaseDirectory`] for string paths.
@@ -90,9 +123,16 @@ impl BaseDirectory {
90123
Self::Template => "$TEMPLATE",
91124
Self::Video => "$VIDEO",
92125
Self::Resource => "$RESOURCE",
126+
#[allow(deprecated)]
93127
Self::App => "$APP",
128+
#[allow(deprecated)]
94129
Self::Log => "$LOG",
95130
Self::Temp => "$TEMP",
131+
Self::AppConfig => "$APPCONFIG",
132+
Self::AppData => "$APPDATA",
133+
Self::AppLocalData => "$APPLOCALDATA",
134+
Self::AppCache => "$APPCACHE",
135+
Self::AppLog => "$APPLOG",
96136
}
97137
}
98138

@@ -116,9 +156,16 @@ impl BaseDirectory {
116156
"$TEMPLATE" => Self::Template,
117157
"$VIDEO" => Self::Video,
118158
"$RESOURCE" => Self::Resource,
159+
#[allow(deprecated)]
119160
"$APP" => Self::App,
161+
#[allow(deprecated)]
120162
"$LOG" => Self::Log,
121163
"$TEMP" => Self::Temp,
164+
"$APPCONFIG" => Self::AppConfig,
165+
"$APPDATA" => Self::AppData,
166+
"$APPLOCALDATA" => Self::AppLocalData,
167+
"$APPCACHE" => Self::AppCache,
168+
"$APPLOG" => Self::AppLog,
122169
_ => return None,
123170
};
124171
Some(res)
@@ -192,7 +239,7 @@ pub fn parse<P: AsRef<Path>>(
192239
/// context.package_info(),
193240
/// &Env::default(),
194241
/// "db/tauri.sqlite",
195-
/// Some(BaseDirectory::App))
242+
/// Some(BaseDirectory::AppData))
196243
/// .expect("failed to resolve path");
197244
/// assert_eq!(path.to_str().unwrap(), "/home/${whoami}/.config/com.tauri.app/db/tauri.sqlite");
198245
///
@@ -242,9 +289,16 @@ pub fn resolve_path<P: AsRef<Path>>(
242289
BaseDirectory::Template => template_dir(),
243290
BaseDirectory::Video => video_dir(),
244291
BaseDirectory::Resource => resource_dir(package_info, env),
245-
BaseDirectory::App => app_dir(config),
246-
BaseDirectory::Log => log_dir(config),
292+
#[allow(deprecated)]
293+
BaseDirectory::App => app_config_dir(config),
294+
#[allow(deprecated)]
295+
BaseDirectory::Log => app_log_dir(config),
247296
BaseDirectory::Temp => Some(temp_dir()),
297+
BaseDirectory::AppConfig => app_config_dir(config),
298+
BaseDirectory::AppData => app_data_dir(config),
299+
BaseDirectory::AppLocalData => app_local_data_dir(config),
300+
BaseDirectory::AppCache => app_cache_dir(config),
301+
BaseDirectory::AppLog => app_log_dir(config),
248302
};
249303
if let Some(mut base_dir_path_value) = base_dir_path {
250304
// use the same path resolution mechanism as the bundler's resource injection algorithm
@@ -459,25 +513,52 @@ pub fn resource_dir(package_info: &PackageInfo, env: &Env) -> Option<PathBuf> {
459513
crate::utils::platform::resource_dir(package_info, env).ok()
460514
}
461515

462-
/// Returns the path to the suggested directory for your app config files.
516+
/// Returns the path to the suggested directory for your app's config files.
463517
///
464-
/// Resolves to `${config_dir}/${bundle_identifier}`.
518+
/// Resolves to [`config_dir`]`/${bundle_identifier}`.
465519
///
466-
/// See [`PathResolver::app_dir`](crate::PathResolver#method.app_dir) for a more convenient helper function.
467-
pub fn app_dir(config: &Config) -> Option<PathBuf> {
520+
/// See [`PathResolver::app_config_dir`](crate::PathResolver#method.app_config_dir) for a more convenient helper function.
521+
pub fn app_config_dir(config: &Config) -> Option<PathBuf> {
468522
dirs_next::config_dir().map(|dir| dir.join(&config.tauri.bundle.identifier))
469523
}
470524

471-
/// Returns the path to the suggested log directory.
525+
/// Returns the path to the suggested directory for your app's data files.
526+
///
527+
/// Resolves to [`data_dir`]`/${bundle_identifier}`.
528+
///
529+
/// See [`PathResolver::app_data_dir`](crate::PathResolver#method.app_data_dir) for a more convenient helper function.
530+
pub fn app_data_dir(config: &Config) -> Option<PathBuf> {
531+
dirs_next::data_dir().map(|dir| dir.join(&config.tauri.bundle.identifier))
532+
}
533+
534+
/// Returns the path to the suggested directory for your app's local data files.
535+
///
536+
/// Resolves to [`local_data_dir`]`/${bundle_identifier}`.
537+
///
538+
/// See [`PathResolver::app_data_dir`](crate::PathResolver#method.app_data_dir) for a more convenient helper function.
539+
pub fn app_local_data_dir(config: &Config) -> Option<PathBuf> {
540+
dirs_next::data_local_dir().map(|dir| dir.join(&config.tauri.bundle.identifier))
541+
}
542+
543+
/// Returns the path to the suggested directory for your app's cache files.
544+
///
545+
/// Resolves to [`cache_dir`]`/${bundle_identifier}`.
546+
///
547+
/// See [`PathResolver::app_cache_dir`](crate::PathResolver#method.app_cache_dir) for a more convenient helper function.
548+
pub fn app_cache_dir(config: &Config) -> Option<PathBuf> {
549+
dirs_next::cache_dir().map(|dir| dir.join(&config.tauri.bundle.identifier))
550+
}
551+
552+
/// Returns the path to the suggested directory for your app's log files.
472553
///
473554
/// ## Platform-specific
474555
///
475-
/// - **Linux:** Resolves to `${config_dir}/${bundle_identifier}/logs`.
476-
/// - **macOS:** Resolves to `${home_dir}//Library/Logs/{bundle_identifier}`
477-
/// - **Windows:** Resolves to `${config_dir}/${bundle_identifier}/logs`.
556+
/// - **Linux:** Resolves to [`config_dir`]`/${bundle_identifier}/logs`.
557+
/// - **macOS:** Resolves to [`home_dir`]`/Library/Logs/${bundle_identifier}`
558+
/// - **Windows:** Resolves to [`config_dir`]`/${bundle_identifier}/logs`.
478559
///
479-
/// See [`PathResolver::log_dir`](crate::PathResolver#method.log_dir) for a more convenient helper function.
480-
pub fn log_dir(config: &Config) -> Option<PathBuf> {
560+
/// See [`PathResolver::app_log_dir`](crate::PathResolver#method.app_log_dir) for a more convenient helper function.
561+
pub fn app_log_dir(config: &Config) -> Option<PathBuf> {
481562
#[cfg(target_os = "macos")]
482563
let path = dirs_next::home_dir().map(|dir| {
483564
dir
@@ -491,3 +572,33 @@ pub fn log_dir(config: &Config) -> Option<PathBuf> {
491572

492573
path
493574
}
575+
576+
/// Returns the path to the suggested directory for your app's config files.
577+
///
578+
/// Resolves to [`config_dir`]`/${bundle_identifier}`.
579+
///
580+
/// See [`PathResolver::app_config_dir`](crate::PathResolver#method.app_config_dir) for a more convenient helper function.
581+
#[deprecated(
582+
since = "1.2.0",
583+
note = "Will be removed in 2.0.0. Use `app_config_dir` or `app_data_dir` instead."
584+
)]
585+
pub fn app_dir(config: &Config) -> Option<PathBuf> {
586+
app_config_dir(config)
587+
}
588+
589+
/// Returns the path to the suggested directory for your app's log files.
590+
///
591+
/// ## Platform-specific
592+
///
593+
/// - **Linux:** Resolves to [`config_dir`]`/${bundle_identifier}`.
594+
/// - **macOS:** Resolves to [`home_dir`]`/Library/Logs/${bundle_identifier}`
595+
/// - **Windows:** Resolves to [`config_dir`]`/${bundle_identifier}`.
596+
///
597+
/// See [`PathResolver::app_log_dir`](crate::PathResolver#method.app_log_dir) for a more convenient helper function.
598+
#[deprecated(
599+
since = "1.2.0",
600+
note = "Will be removed in 2.0.0. Use `app_log_dir` instead."
601+
)]
602+
pub fn log_dir(config: &Config) -> Option<PathBuf> {
603+
app_log_dir(config)
604+
}

core/tauri/src/app.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,47 @@ impl PathResolver {
288288
.map(|dir| dir.join(resource_relpath(path.as_ref())))
289289
}
290290

291-
/// Returns the path to the suggested directory for your app config files.
291+
/// Returns the path to the suggested directory for your app's config files.
292+
pub fn app_config_dir(&self) -> Option<PathBuf> {
293+
crate::api::path::app_config_dir(&self.config)
294+
}
295+
296+
/// Returns the path to the suggested directory for your app's data files.
297+
pub fn app_data_dir(&self) -> Option<PathBuf> {
298+
crate::api::path::app_data_dir(&self.config)
299+
}
300+
301+
/// Returns the path to the suggested directory for your app's local data files.
302+
pub fn app_local_data_dir(&self) -> Option<PathBuf> {
303+
crate::api::path::app_local_data_dir(&self.config)
304+
}
305+
306+
/// Returns the path to the suggested directory for your app's cache files.
307+
pub fn app_cache_dir(&self) -> Option<PathBuf> {
308+
crate::api::path::app_cache_dir(&self.config)
309+
}
310+
311+
/// Returns the path to the suggested directory for your app's log files.
312+
pub fn app_log_dir(&self) -> Option<PathBuf> {
313+
crate::api::path::app_log_dir(&self.config)
314+
}
315+
316+
/// Returns the path to the suggested directory for your app's config files.
317+
#[deprecated(
318+
since = "1.2.0",
319+
note = "Will be removed in 2.0.0. Use `app_config_dir` or `app_data_dir` instead."
320+
)]
292321
pub fn app_dir(&self) -> Option<PathBuf> {
293-
crate::api::path::app_dir(&self.config)
322+
self.app_config_dir()
294323
}
295324

296-
/// Returns the path to the suggested log directory.
325+
/// Returns the path to the suggested directory for your app's log files.
326+
#[deprecated(
327+
since = "1.2.0",
328+
note = "Will be removed in 2.0.0. Use `app_log_dir` instead."
329+
)]
297330
pub fn log_dir(&self) -> Option<PathBuf> {
298-
crate::api::path::log_dir(&self.config)
331+
self.app_log_dir()
299332
}
300333
}
301334

core/tauri/src/endpoints/file_system.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ mod tests {
399399
impl Arbitrary for BaseDirectory {
400400
fn arbitrary(g: &mut Gen) -> Self {
401401
if bool::arbitrary(g) {
402-
BaseDirectory::App
402+
BaseDirectory::AppData
403403
} else {
404404
BaseDirectory::Resource
405405
}

0 commit comments

Comments
 (0)