Skip to content

Commit e35aaeb

Browse files
authored
feat(core): add PathResolver::resolve_resource API (#4116)
1 parent bad85a1 commit e35aaeb

5 files changed

Lines changed: 133 additions & 167 deletions

File tree

.changes/resolve-resource.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Added `PathResolver::resolve_resource` API.

core/tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ thiserror = "1.0"
5757
once_cell = "1.10"
5858
tauri-runtime = { version = "0.5.0", path = "../tauri-runtime" }
5959
tauri-macros = { version = "1.0.0-rc.6", path = "../tauri-macros" }
60-
tauri-utils = { version = "1.0.0-rc.6", path = "../tauri-utils" }
60+
tauri-utils = { version = "1.0.0-rc.6", features = [ "resources" ], path = "../tauri-utils" }
6161
tauri-runtime-wry = { version = "0.5.1", path = "../tauri-runtime-wry", optional = true }
6262
rand = "0.8"
6363
semver = "1.0"

core/tauri/src/app.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
scope::FsScope,
2323
sealed::{ManagerBase, RuntimeOrDispatch},
2424
utils::config::Config,
25-
utils::{assets::Assets, Env},
25+
utils::{assets::Assets, resources::resource_relpath, Env},
2626
Context, EventLoopMessage, Invoke, InvokeError, InvokeResponse, Manager, Runtime, Scopes,
2727
StateManager, Theme, Window,
2828
};
@@ -39,7 +39,7 @@ use tauri_utils::PackageInfo;
3939

4040
use std::{
4141
collections::HashMap,
42-
path::PathBuf,
42+
path::{Path, PathBuf},
4343
sync::{mpsc::Sender, Arc, Weak},
4444
};
4545

@@ -255,6 +255,41 @@ impl PathResolver {
255255
crate::api::path::resource_dir(&self.package_info, &self.env)
256256
}
257257

258+
/// Resolves the path of the given resource.
259+
/// Note that the path must be the same as provided in `tauri.conf.json`.
260+
///
261+
/// This function is helpful when your resource path includes a root dir (`/`) or parent component (`..`),
262+
/// because Tauri replaces them with a parent folder, so simply using [`Self::resource_dir`] and joining the path
263+
/// won't work.
264+
///
265+
/// # Examples
266+
///
267+
/// `tauri.conf.json`:
268+
/// ```json
269+
/// {
270+
/// "tauri": {
271+
/// "bundle": {
272+
/// "resources": ["../assets/*"]
273+
/// }
274+
/// }
275+
/// }
276+
/// ```
277+
///
278+
/// ```no_run
279+
/// tauri::Builder::default()
280+
/// .setup(|app| {
281+
/// let resource_path = app.path_resolver()
282+
/// .resolve_resource("../assets/logo.svg")
283+
/// .expect("failed to resolve resource dir");
284+
/// Ok(())
285+
/// });
286+
/// ```
287+
pub fn resolve_resource<P: AsRef<Path>>(&self, path: P) -> Option<PathBuf> {
288+
self
289+
.resource_dir()
290+
.map(|dir| dir.join(resource_relpath(path.as_ref())))
291+
}
292+
258293
/// Returns the path to the suggested directory for your app config files.
259294
pub fn app_dir(&self) -> Option<PathBuf> {
260295
crate::api::path::app_dir(&self.config)

0 commit comments

Comments
 (0)