Skip to content

Commit

Permalink
feat(core): add PathResolver::resolve_resource API (#4116)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 13, 2022
1 parent bad85a1 commit e35aaeb
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 167 deletions.
5 changes: 5 additions & 0 deletions .changes/resolve-resource.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Added `PathResolver::resolve_resource` API.
2 changes: 1 addition & 1 deletion core/tauri/Cargo.toml
Expand Up @@ -57,7 +57,7 @@ thiserror = "1.0"
once_cell = "1.10"
tauri-runtime = { version = "0.5.0", path = "../tauri-runtime" }
tauri-macros = { version = "1.0.0-rc.6", path = "../tauri-macros" }
tauri-utils = { version = "1.0.0-rc.6", path = "../tauri-utils" }
tauri-utils = { version = "1.0.0-rc.6", features = [ "resources" ], path = "../tauri-utils" }
tauri-runtime-wry = { version = "0.5.1", path = "../tauri-runtime-wry", optional = true }
rand = "0.8"
semver = "1.0"
Expand Down
39 changes: 37 additions & 2 deletions core/tauri/src/app.rs
Expand Up @@ -22,7 +22,7 @@ use crate::{
scope::FsScope,
sealed::{ManagerBase, RuntimeOrDispatch},
utils::config::Config,
utils::{assets::Assets, Env},
utils::{assets::Assets, resources::resource_relpath, Env},
Context, EventLoopMessage, Invoke, InvokeError, InvokeResponse, Manager, Runtime, Scopes,
StateManager, Theme, Window,
};
Expand All @@ -39,7 +39,7 @@ use tauri_utils::PackageInfo;

use std::{
collections::HashMap,
path::PathBuf,
path::{Path, PathBuf},
sync::{mpsc::Sender, Arc, Weak},
};

Expand Down Expand Up @@ -255,6 +255,41 @@ impl PathResolver {
crate::api::path::resource_dir(&self.package_info, &self.env)
}

/// Resolves the path of the given resource.
/// Note that the path must be the same as provided in `tauri.conf.json`.
///
/// This function is helpful when your resource path includes a root dir (`/`) or parent component (`..`),
/// because Tauri replaces them with a parent folder, so simply using [`Self::resource_dir`] and joining the path
/// won't work.
///
/// # Examples
///
/// `tauri.conf.json`:
/// ```json
/// {
/// "tauri": {
/// "bundle": {
/// "resources": ["../assets/*"]
/// }
/// }
/// }
/// ```
///
/// ```no_run
/// tauri::Builder::default()
/// .setup(|app| {
/// let resource_path = app.path_resolver()
/// .resolve_resource("../assets/logo.svg")
/// .expect("failed to resolve resource dir");
/// Ok(())
/// });
/// ```
pub fn resolve_resource<P: AsRef<Path>>(&self, path: P) -> Option<PathBuf> {
self
.resource_dir()
.map(|dir| dir.join(resource_relpath(path.as_ref())))
}

/// Returns the path to the suggested directory for your app config files.
pub fn app_dir(&self) -> Option<PathBuf> {
crate::api::path::app_dir(&self.config)
Expand Down

0 comments on commit e35aaeb

Please sign in to comment.