Skip to content

Commit 428d50a

Browse files
authored
refactor(core): use bundle identifier on app dir, closes #1693 (#1703)
* refactor(core): use bundle identifier on app dir, closes #1693 * fix: tests * clippy
1 parent 935db26 commit 428d50a

File tree

9 files changed

+132
-93
lines changed

9 files changed

+132
-93
lines changed

.changes/app-dir-refactor.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
**Breaking:** `api::path::resolve_path()` and `api::path::app_dir()` now takes the config as first argument.
6+
**Breaking:** `api::path::app_dir()` now resolves to `${configDir}/${config.tauri.bundle.identifier}`.

core/tauri-utils/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ pub struct PackageConfig {
397397
}
398398

399399
/// The tauri.conf.json mapper.
400-
#[derive(Debug, PartialEq, Deserialize)]
400+
#[derive(Debug, Default, PartialEq, Deserialize)]
401401
#[serde(rename_all = "camelCase")]
402402
pub struct Config {
403403
/// Package settings.

core/tauri/src/api/path.rs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use std::{
77
path::{Path, PathBuf},
88
};
99

10+
use crate::Config;
11+
1012
use serde_repr::{Deserialize_repr, Serialize_repr};
1113

1214
/// A Base Directory to use.
@@ -52,7 +54,7 @@ pub enum BaseDirectory {
5254
/// The Resource directory.
5355
Resource,
5456
/// The default App config directory.
55-
/// Resolves to ${CONFIG_DIR}/${APP_NAME}
57+
/// Resolves to ${BaseDirectory::Config}/${config.tauri.bundle.identifier}
5658
App,
5759
/// The current working directory.
5860
Current,
@@ -63,11 +65,13 @@ pub enum BaseDirectory {
6365
/// # Example
6466
/// ```
6567
/// use tauri::api::path::{resolve_path, BaseDirectory};
66-
/// let path = resolve_path("path/to/something", Some(BaseDirectory::Config))
68+
/// // we use the default config, but in an actual app you should get the Config created from tauri.conf.json
69+
/// let path = resolve_path(&Default::default(), "path/to/something", Some(BaseDirectory::Config))
6770
/// .expect("failed to resolve path");
6871
/// // path is equal to "/home/${whoami}/.config/path/to/something" on Linux
6972
/// ```
7073
pub fn resolve_path<P: AsRef<Path>>(
74+
config: &Config,
7175
path: P,
7276
dir: Option<BaseDirectory>,
7377
) -> crate::api::Result<PathBuf> {
@@ -90,7 +94,7 @@ pub fn resolve_path<P: AsRef<Path>>(
9094
BaseDirectory::Template => template_dir(),
9195
BaseDirectory::Video => video_dir(),
9296
BaseDirectory::Resource => resource_dir(),
93-
BaseDirectory::App => app_dir(),
97+
BaseDirectory::App => app_dir(config),
9498
BaseDirectory::Current => Some(env::current_dir()?),
9599
};
96100
if let Some(mut base_dir_path_value) = base_dir_path {
@@ -193,24 +197,7 @@ pub fn resource_dir() -> Option<PathBuf> {
193197
crate::api::platform::resource_dir().ok()
194198
}
195199

196-
fn app_name() -> crate::api::Result<String> {
197-
let exe = std::env::current_exe()?;
198-
let app_name = exe
199-
.file_stem()
200-
.expect("failed to get exe filename")
201-
.to_string_lossy();
202-
203-
Ok(app_name.to_string())
204-
}
205-
206200
/// Returns the path to the suggested directory for your app config files.
207-
pub fn app_dir() -> Option<PathBuf> {
208-
dirs_next::config_dir().and_then(|mut dir| {
209-
if let Ok(app_name) = app_name() {
210-
dir.push(app_name);
211-
Some(dir)
212-
} else {
213-
None
214-
}
215-
})
201+
pub fn app_dir(config: &Config) -> Option<PathBuf> {
202+
dirs_next::config_dir().map(|dir| dir.join(&config.tauri.bundle.identifier))
216203
}

core/tauri/src/endpoints.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ use crate::{
99
};
1010
use serde::{Deserialize, Serialize};
1111
use serde_json::Value as JsonValue;
12+
13+
use std::sync::Arc;
14+
1215
mod app;
1316
mod cli;
1417
mod dialog;
@@ -58,7 +61,7 @@ impl Module {
5861
self,
5962
window: Window<M>,
6063
resolver: InvokeResolver<M>,
61-
config: &Config,
64+
config: Arc<Config>,
6265
package_info: PackageInfo,
6366
) {
6467
match self {
@@ -70,8 +73,12 @@ impl Module {
7073
}),
7174
Self::Process(cmd) => resolver
7275
.respond_async(async move { cmd.run().and_then(|r| r.json).map_err(InvokeError::from) }),
73-
Self::Fs(cmd) => resolver
74-
.respond_async(async move { cmd.run().and_then(|r| r.json).map_err(InvokeError::from) }),
76+
Self::Fs(cmd) => resolver.respond_async(async move {
77+
cmd
78+
.run(config)
79+
.and_then(|r| r.json)
80+
.map_err(InvokeError::from)
81+
}),
7582
Self::Window(cmd) => resolver.respond_async(async move {
7683
cmd
7784
.run(window)
@@ -113,15 +120,12 @@ impl Module {
113120
})
114121
}
115122
}
116-
Self::Notification(cmd) => {
117-
let identifier = config.tauri.bundle.identifier.clone();
118-
resolver.respond_async(async move {
119-
cmd
120-
.run(identifier)
121-
.and_then(|r| r.json)
122-
.map_err(InvokeError::from)
123-
})
124-
}
123+
Self::Notification(cmd) => resolver.respond_closure(move || {
124+
cmd
125+
.run(config)
126+
.and_then(|r| r.json)
127+
.map_err(InvokeError::from)
128+
}),
125129
Self::Http(cmd) => resolver.respond_async(async move {
126130
cmd
127131
.run()
@@ -142,7 +146,7 @@ impl Module {
142146
pub(crate) fn handle<P: Params>(
143147
module: String,
144148
invoke: Invoke<P>,
145-
config: &Config,
149+
config: Arc<Config>,
146150
package_info: &PackageInfo,
147151
) {
148152
let Invoke { message, resolver } = invoke;

0 commit comments

Comments
 (0)