mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-12-29 22:13:34 +03:00
refactor(core): resolve resource_dir using the package info (#1762)
This commit is contained in:
parent
b6b4128885
commit
7bb7dda752
@ -2,5 +2,5 @@
|
||||
"tauri": patch
|
||||
---
|
||||
|
||||
**Breaking:** `api::path::resolve_path()` and `api::path::app_dir()` now takes the config as first argument.
|
||||
**Breaking:** `api::path::resolve_path()` and `api::path::app_dir()` now takes the config as first argument and the `PackageInfo` as second argument.
|
||||
**Breaking:** `api::path::app_dir()` now resolves to `${configDir}/${config.tauri.bundle.identifier}`.
|
||||
|
5
.changes/resources-dir-refactor.md
Normal file
5
.changes/resources-dir-refactor.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"tauri-utils": patch
|
||||
---
|
||||
|
||||
The `platform::resource_dir` API now takes the `PackageInfo`.
|
@ -62,6 +62,23 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
|
||||
quote!(None)
|
||||
};
|
||||
|
||||
let package_name = if let Some(product_name) = &config.package.product_name {
|
||||
quote!(#product_name.to_string())
|
||||
} else {
|
||||
quote!(env!("CARGO_PKG_NAME").to_string())
|
||||
};
|
||||
let package_version = if let Some(version) = &config.package.version {
|
||||
quote!(#version.to_string())
|
||||
} else {
|
||||
quote!(env!("CARGO_PKG_VERSION").to_string())
|
||||
};
|
||||
let package_info = quote!(
|
||||
#root::api::PackageInfo {
|
||||
name: #package_name,
|
||||
version: #package_version,
|
||||
}
|
||||
);
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
let system_tray_icon = if let Some(tray) = &config.tauri.system_tray {
|
||||
let mut system_tray_icon_path = tray.icon_path.clone();
|
||||
@ -73,7 +90,7 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
quote!(Some(
|
||||
::tauri::platform::resource_dir()
|
||||
::tauri::platform::resource_dir(&#package_info)
|
||||
.expect("failed to read resource dir")
|
||||
.join(
|
||||
#system_tray_icon_file_name
|
||||
@ -103,26 +120,12 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
|
||||
quote!(None)
|
||||
};
|
||||
|
||||
let package_name = if let Some(product_name) = &config.package.product_name {
|
||||
quote!(#product_name.to_string())
|
||||
} else {
|
||||
quote!(env!("CARGO_PKG_NAME").to_string())
|
||||
};
|
||||
let package_version = if let Some(version) = &config.package.version {
|
||||
quote!(#version.to_string())
|
||||
} else {
|
||||
quote!(env!("CARGO_PKG_VERSION").to_string())
|
||||
};
|
||||
|
||||
// double braces are purposeful to force the code into a block expression
|
||||
Ok(quote!(#root::Context {
|
||||
config: #config,
|
||||
assets: ::std::sync::Arc::new(#assets),
|
||||
default_window_icon: #default_window_icon,
|
||||
system_tray_icon: #system_tray_icon,
|
||||
package_info: #root::api::PackageInfo {
|
||||
name: #package_name,
|
||||
version: #package_version,
|
||||
},
|
||||
package_info: #package_info,
|
||||
}))
|
||||
}
|
||||
|
@ -20,5 +20,8 @@ html5ever = "0.25"
|
||||
proc-macro2 = { version = "1.0", optional = true }
|
||||
quote = { version = "1.0", optional = true }
|
||||
|
||||
[target."cfg(target_os = \"linux\")".dependencies]
|
||||
heck = "0.3"
|
||||
|
||||
[features]
|
||||
build = [ "proc-macro2", "quote" ]
|
||||
|
@ -14,6 +14,29 @@ pub mod html;
|
||||
/// Platform helpers
|
||||
pub mod platform;
|
||||
|
||||
/// `App` package information.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PackageInfo {
|
||||
/// App name.
|
||||
pub name: String,
|
||||
/// App version.
|
||||
pub version: String,
|
||||
}
|
||||
|
||||
impl PackageInfo {
|
||||
/// Returns the application package name.
|
||||
/// On macOS and Windows it's the `name` field, and on Linux it's the `name` in `kebab-case`.
|
||||
pub fn package_name(&self) -> String {
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
use heck::KebabCase;
|
||||
self.name.to_kebab_case()
|
||||
}
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
return self.name.clone();
|
||||
}
|
||||
}
|
||||
|
||||
/// Result type alias using the crate's error type.
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
|
@ -7,6 +7,8 @@ use std::{
|
||||
path::{PathBuf, MAIN_SEPARATOR},
|
||||
};
|
||||
|
||||
use crate::PackageInfo;
|
||||
|
||||
/// Try to determine the current target triple.
|
||||
///
|
||||
/// Returns a target triple (e.g. `x86_64-unknown-linux-gnu` or `i686-pc-windows-msvc`) or an
|
||||
@ -70,13 +72,9 @@ pub fn target_triple() -> crate::Result<String> {
|
||||
/// `${exe_dir}/../lib/${exe_name}`.
|
||||
///
|
||||
/// On MacOS, it's `${exe_dir}../Resources` (inside .app).
|
||||
pub fn resource_dir() -> crate::Result<PathBuf> {
|
||||
pub fn resource_dir(package_info: &PackageInfo) -> crate::Result<PathBuf> {
|
||||
let exe = std::env::current_exe()?;
|
||||
let exe_dir = exe.parent().expect("failed to get exe directory");
|
||||
let app_name = exe
|
||||
.file_name()
|
||||
.expect("failed to get exe filename")
|
||||
.to_string_lossy();
|
||||
let curr_dir = exe_dir.display().to_string();
|
||||
|
||||
if curr_dir.ends_with(format!("{S}target{S}debug", S = MAIN_SEPARATOR).as_str())
|
||||
@ -90,12 +88,19 @@ pub fn resource_dir() -> crate::Result<PathBuf> {
|
||||
if cfg!(target_os = "linux") {
|
||||
if curr_dir.ends_with("/data/usr/bin") {
|
||||
// running from the deb bundle dir
|
||||
Ok(exe_dir.join(format!("../lib/{}", app_name)))
|
||||
Ok(exe_dir.join(format!("../lib/{}", package_info.package_name())))
|
||||
} else if let Ok(appdir) = env::var("APPDIR") {
|
||||
Ok(PathBuf::from(format!("{}/usr/lib/{}", appdir, app_name)))
|
||||
Ok(PathBuf::from(format!(
|
||||
"{}/usr/lib/{}",
|
||||
appdir,
|
||||
package_info.package_name()
|
||||
)))
|
||||
} else {
|
||||
// running bundle
|
||||
Ok(PathBuf::from(format!("/usr/lib/{}", app_name)))
|
||||
Ok(PathBuf::from(format!(
|
||||
"/usr/lib/{}",
|
||||
package_info.package_name()
|
||||
)))
|
||||
}
|
||||
} else if cfg!(target_os = "macos") {
|
||||
Ok(exe_dir.join("../Resources"))
|
||||
|
@ -52,15 +52,6 @@ pub use error::Error;
|
||||
/// Tauri API result type.
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// `App` package information.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PackageInfo {
|
||||
/// App name.
|
||||
pub name: String,
|
||||
/// App version.
|
||||
pub version: String,
|
||||
}
|
||||
|
||||
// Not public API
|
||||
#[doc(hidden)]
|
||||
pub mod private {
|
||||
|
@ -7,7 +7,7 @@ use std::{
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use crate::Config;
|
||||
use crate::{Config, PackageInfo};
|
||||
|
||||
use serde_repr::{Deserialize_repr, Serialize_repr};
|
||||
|
||||
@ -65,14 +65,22 @@ pub enum BaseDirectory {
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// use tauri::api::path::{resolve_path, BaseDirectory};
|
||||
/// // we use the default config, but in an actual app you should get the Config created from tauri.conf.json
|
||||
/// let path = resolve_path(&Default::default(), "path/to/something", Some(BaseDirectory::Config))
|
||||
/// .expect("failed to resolve path");
|
||||
/// use tauri::api::{path::{resolve_path, BaseDirectory}, PackageInfo};
|
||||
/// // we use the default config and a mock PackageInfo, but in an actual app you should get the Config created from tauri.conf.json and the app's PackageInfo instance
|
||||
/// let path = resolve_path(
|
||||
/// &Default::default(),
|
||||
/// &PackageInfo {
|
||||
/// name: "app".into(),
|
||||
/// version: "1.0.0".into(),
|
||||
/// },
|
||||
/// "path/to/something",
|
||||
/// Some(BaseDirectory::Config)
|
||||
/// ).expect("failed to resolve path");
|
||||
/// // path is equal to "/home/${whoami}/.config/path/to/something" on Linux
|
||||
/// ```
|
||||
pub fn resolve_path<P: AsRef<Path>>(
|
||||
config: &Config,
|
||||
package_info: &PackageInfo,
|
||||
path: P,
|
||||
dir: Option<BaseDirectory>,
|
||||
) -> crate::api::Result<PathBuf> {
|
||||
@ -94,7 +102,7 @@ pub fn resolve_path<P: AsRef<Path>>(
|
||||
BaseDirectory::Runtime => runtime_dir(),
|
||||
BaseDirectory::Template => template_dir(),
|
||||
BaseDirectory::Video => video_dir(),
|
||||
BaseDirectory::Resource => resource_dir(),
|
||||
BaseDirectory::Resource => resource_dir(package_info),
|
||||
BaseDirectory::App => app_dir(config),
|
||||
BaseDirectory::Current => Some(env::current_dir()?),
|
||||
};
|
||||
@ -194,8 +202,8 @@ pub fn video_dir() -> Option<PathBuf> {
|
||||
}
|
||||
|
||||
/// Returns the path to the resource directory of this app.
|
||||
pub fn resource_dir() -> Option<PathBuf> {
|
||||
crate::api::platform::resource_dir().ok()
|
||||
pub fn resource_dir(package_info: &PackageInfo) -> Option<PathBuf> {
|
||||
crate::api::platform::resource_dir(package_info).ok()
|
||||
}
|
||||
|
||||
/// Returns the path to the suggested directory for your app config files.
|
||||
|
@ -75,7 +75,7 @@ impl Module {
|
||||
.respond_async(async move { cmd.run().and_then(|r| r.json).map_err(InvokeError::from) }),
|
||||
Self::Fs(cmd) => resolver.respond_async(async move {
|
||||
cmd
|
||||
.run(config)
|
||||
.run(config, &package_info)
|
||||
.and_then(|r| r.json)
|
||||
.map_err(InvokeError::from)
|
||||
}),
|
||||
@ -122,7 +122,7 @@ impl Module {
|
||||
}
|
||||
Self::Notification(cmd) => resolver.respond_closure(move || {
|
||||
cmd
|
||||
.run(config)
|
||||
.run(config, &package_info)
|
||||
.and_then(|r| r.json)
|
||||
.map_err(InvokeError::from)
|
||||
}),
|
||||
|
@ -8,7 +8,7 @@ use crate::{
|
||||
dir, file,
|
||||
path::{resolve_path, BaseDirectory},
|
||||
},
|
||||
Config,
|
||||
Config, PackageInfo,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -102,11 +102,15 @@ pub enum Cmd {
|
||||
|
||||
impl Cmd {
|
||||
#[allow(unused_variables)]
|
||||
pub fn run(self, config: Arc<Config>) -> crate::Result<InvokeResponse> {
|
||||
pub fn run(
|
||||
self,
|
||||
config: Arc<Config>,
|
||||
package_info: &PackageInfo,
|
||||
) -> crate::Result<InvokeResponse> {
|
||||
match self {
|
||||
#[cfg(fs_read_text_file)]
|
||||
Self::ReadTextFile { path, options } => {
|
||||
read_text_file(&config, path, options).map(Into::into)
|
||||
read_text_file(&config, package_info, path, options).map(Into::into)
|
||||
}
|
||||
#[cfg(not(fs_read_text_file))]
|
||||
Self::ReadTextFile { .. } => Err(crate::Error::ApiNotAllowlisted(
|
||||
@ -115,7 +119,7 @@ impl Cmd {
|
||||
|
||||
#[cfg(fs_read_binary_file)]
|
||||
Self::ReadBinaryFile { path, options } => {
|
||||
read_binary_file(&config, path, options).map(Into::into)
|
||||
read_binary_file(&config, package_info, path, options).map(Into::into)
|
||||
}
|
||||
#[cfg(not(fs_read_binary_file))]
|
||||
Self::ReadBinaryFile { .. } => Err(crate::Error::ApiNotAllowlisted(
|
||||
@ -127,7 +131,7 @@ impl Cmd {
|
||||
path,
|
||||
contents,
|
||||
options,
|
||||
} => write_file(&config, path, contents, options).map(Into::into),
|
||||
} => write_file(&config, package_info, path, contents, options).map(Into::into),
|
||||
#[cfg(not(fs_write_file))]
|
||||
Self::WriteFile { .. } => Err(crate::Error::ApiNotAllowlisted(
|
||||
"fs > writeFile".to_string(),
|
||||
@ -138,14 +142,16 @@ impl Cmd {
|
||||
path,
|
||||
contents,
|
||||
options,
|
||||
} => write_binary_file(&config, path, contents, options).map(Into::into),
|
||||
} => write_binary_file(&config, package_info, path, contents, options).map(Into::into),
|
||||
#[cfg(not(fs_write_binary_file))]
|
||||
Self::WriteBinaryFile { .. } => Err(crate::Error::ApiNotAllowlisted(
|
||||
"writeBinaryFile".to_string(),
|
||||
)),
|
||||
|
||||
#[cfg(fs_read_dir)]
|
||||
Self::ReadDir { path, options } => read_dir(&config, path, options).map(Into::into),
|
||||
Self::ReadDir { path, options } => {
|
||||
read_dir(&config, package_info, path, options).map(Into::into)
|
||||
}
|
||||
#[cfg(not(fs_read_dir))]
|
||||
Self::ReadDir { .. } => Err(crate::Error::ApiNotAllowlisted("fs > readDir".to_string())),
|
||||
|
||||
@ -154,26 +160,32 @@ impl Cmd {
|
||||
source,
|
||||
destination,
|
||||
options,
|
||||
} => copy_file(&config, source, destination, options).map(Into::into),
|
||||
} => copy_file(&config, package_info, source, destination, options).map(Into::into),
|
||||
#[cfg(not(fs_copy_file))]
|
||||
Self::CopyFile { .. } => Err(crate::Error::ApiNotAllowlisted("fs > copyFile".to_string())),
|
||||
|
||||
#[cfg(fs_create_dir)]
|
||||
Self::CreateDir { path, options } => create_dir(&config, path, options).map(Into::into),
|
||||
Self::CreateDir { path, options } => {
|
||||
create_dir(&config, package_info, path, options).map(Into::into)
|
||||
}
|
||||
#[cfg(not(fs_create_dir))]
|
||||
Self::CreateDir { .. } => Err(crate::Error::ApiNotAllowlisted(
|
||||
"fs > createDir".to_string(),
|
||||
)),
|
||||
|
||||
#[cfg(fs_remove_dir)]
|
||||
Self::RemoveDir { path, options } => remove_dir(&config, path, options).map(Into::into),
|
||||
Self::RemoveDir { path, options } => {
|
||||
remove_dir(&config, package_info, path, options).map(Into::into)
|
||||
}
|
||||
#[cfg(not(fs_remove_dir))]
|
||||
Self::RemoveDir { .. } => Err(crate::Error::ApiNotAllowlisted(
|
||||
"fs > removeDir".to_string(),
|
||||
)),
|
||||
|
||||
#[cfg(fs_remove_file)]
|
||||
Self::RemoveFile { path, options } => remove_file(&config, path, options).map(Into::into),
|
||||
Self::RemoveFile { path, options } => {
|
||||
remove_file(&config, package_info, path, options).map(Into::into)
|
||||
}
|
||||
#[cfg(not(fs_remove_file))]
|
||||
Self::RemoveFile { .. } => Err(crate::Error::ApiNotAllowlisted(
|
||||
"fs > removeFile".to_string(),
|
||||
@ -184,7 +196,7 @@ impl Cmd {
|
||||
old_path,
|
||||
new_path,
|
||||
options,
|
||||
} => rename_file(&config, old_path, new_path, options).map(Into::into),
|
||||
} => rename_file(&config, package_info, old_path, new_path, options).map(Into::into),
|
||||
#[cfg(not(fs_rename_file))]
|
||||
Self::RenameFile { .. } => Err(crate::Error::ApiNotAllowlisted(
|
||||
"fs > renameFile".to_string(),
|
||||
@ -192,7 +204,7 @@ impl Cmd {
|
||||
|
||||
#[cfg(fs_path)]
|
||||
Self::ResolvePath { path, directory } => {
|
||||
resolve_path_handler(&config, path, directory).map(Into::into)
|
||||
resolve_path_handler(&config, package_info, path, directory).map(Into::into)
|
||||
}
|
||||
#[cfg(not(fs_path))]
|
||||
Self::ResolvePath { .. } => Err(crate::Error::ApiNotAllowlisted("fs > pathApi".to_string())),
|
||||
@ -204,6 +216,7 @@ impl Cmd {
|
||||
#[cfg(fs_read_dir)]
|
||||
pub fn read_dir(
|
||||
config: &Config,
|
||||
package_info: &PackageInfo,
|
||||
path: PathBuf,
|
||||
options: Option<DirOperationOptions>,
|
||||
) -> crate::Result<Vec<dir::DiskEntry>> {
|
||||
@ -212,7 +225,7 @@ pub fn read_dir(
|
||||
} else {
|
||||
(false, None)
|
||||
};
|
||||
dir::read_dir(resolve_path(config, path, dir)?, recursive)
|
||||
dir::read_dir(resolve_path(config, package_info, path, dir)?, recursive)
|
||||
.map_err(crate::Error::FailedToExecuteApi)
|
||||
}
|
||||
|
||||
@ -220,14 +233,15 @@ pub fn read_dir(
|
||||
#[cfg(fs_copy_file)]
|
||||
pub fn copy_file(
|
||||
config: &Config,
|
||||
package_info: &PackageInfo,
|
||||
source: PathBuf,
|
||||
destination: PathBuf,
|
||||
options: Option<FileOperationOptions>,
|
||||
) -> crate::Result<()> {
|
||||
let (src, dest) = match options.and_then(|o| o.dir) {
|
||||
Some(dir) => (
|
||||
resolve_path(config, source, Some(dir.clone()))?,
|
||||
resolve_path(config, destination, Some(dir))?,
|
||||
resolve_path(config, package_info, source, Some(dir.clone()))?,
|
||||
resolve_path(config, package_info, destination, Some(dir))?,
|
||||
),
|
||||
None => (source, destination),
|
||||
};
|
||||
@ -239,6 +253,7 @@ pub fn copy_file(
|
||||
#[cfg(fs_create_dir)]
|
||||
pub fn create_dir(
|
||||
config: &Config,
|
||||
package_info: &PackageInfo,
|
||||
path: PathBuf,
|
||||
options: Option<DirOperationOptions>,
|
||||
) -> crate::Result<()> {
|
||||
@ -247,7 +262,7 @@ pub fn create_dir(
|
||||
} else {
|
||||
(false, None)
|
||||
};
|
||||
let resolved_path = resolve_path(config, path, dir)?;
|
||||
let resolved_path = resolve_path(config, package_info, path, dir)?;
|
||||
if recursive {
|
||||
fs::create_dir_all(resolved_path)?;
|
||||
} else {
|
||||
@ -261,6 +276,7 @@ pub fn create_dir(
|
||||
#[cfg(fs_remove_dir)]
|
||||
pub fn remove_dir(
|
||||
config: &Config,
|
||||
package_info: &PackageInfo,
|
||||
path: PathBuf,
|
||||
options: Option<DirOperationOptions>,
|
||||
) -> crate::Result<()> {
|
||||
@ -269,7 +285,7 @@ pub fn remove_dir(
|
||||
} else {
|
||||
(false, None)
|
||||
};
|
||||
let resolved_path = resolve_path(config, path, dir)?;
|
||||
let resolved_path = resolve_path(config, package_info, path, dir)?;
|
||||
if recursive {
|
||||
fs::remove_dir_all(resolved_path)?;
|
||||
} else {
|
||||
@ -283,10 +299,11 @@ pub fn remove_dir(
|
||||
#[cfg(fs_remove_file)]
|
||||
pub fn remove_file(
|
||||
config: &Config,
|
||||
package_info: &PackageInfo,
|
||||
path: PathBuf,
|
||||
options: Option<FileOperationOptions>,
|
||||
) -> crate::Result<()> {
|
||||
let resolved_path = resolve_path(config, path, options.and_then(|o| o.dir))?;
|
||||
let resolved_path = resolve_path(config, package_info, path, options.and_then(|o| o.dir))?;
|
||||
fs::remove_file(resolved_path)?;
|
||||
Ok(())
|
||||
}
|
||||
@ -295,14 +312,15 @@ pub fn remove_file(
|
||||
#[cfg(fs_rename_file)]
|
||||
pub fn rename_file(
|
||||
config: &Config,
|
||||
package_info: &PackageInfo,
|
||||
old_path: PathBuf,
|
||||
new_path: PathBuf,
|
||||
options: Option<FileOperationOptions>,
|
||||
) -> crate::Result<()> {
|
||||
let (old, new) = match options.and_then(|o| o.dir) {
|
||||
Some(dir) => (
|
||||
resolve_path(config, old_path, Some(dir.clone()))?,
|
||||
resolve_path(config, new_path, Some(dir))?,
|
||||
resolve_path(config, package_info, old_path, Some(dir.clone()))?,
|
||||
resolve_path(config, package_info, new_path, Some(dir))?,
|
||||
),
|
||||
None => (old_path, new_path),
|
||||
};
|
||||
@ -313,13 +331,19 @@ pub fn rename_file(
|
||||
#[cfg(fs_write_file)]
|
||||
pub fn write_file(
|
||||
config: &Config,
|
||||
package_info: &PackageInfo,
|
||||
path: PathBuf,
|
||||
contents: String,
|
||||
options: Option<FileOperationOptions>,
|
||||
) -> crate::Result<()> {
|
||||
File::create(resolve_path(config, path, options.and_then(|o| o.dir))?)
|
||||
.map_err(crate::Error::Io)
|
||||
.and_then(|mut f| f.write_all(contents.as_bytes()).map_err(|err| err.into()))?;
|
||||
File::create(resolve_path(
|
||||
config,
|
||||
package_info,
|
||||
path,
|
||||
options.and_then(|o| o.dir),
|
||||
)?)
|
||||
.map_err(crate::Error::Io)
|
||||
.and_then(|mut f| f.write_all(contents.as_bytes()).map_err(|err| err.into()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -327,6 +351,7 @@ pub fn write_file(
|
||||
#[cfg(fs_write_binary_file)]
|
||||
pub fn write_binary_file(
|
||||
config: &Config,
|
||||
package_info: &PackageInfo,
|
||||
path: PathBuf,
|
||||
contents: String,
|
||||
options: Option<FileOperationOptions>,
|
||||
@ -334,9 +359,14 @@ pub fn write_binary_file(
|
||||
base64::decode(contents)
|
||||
.map_err(crate::Error::Base64Decode)
|
||||
.and_then(|c| {
|
||||
File::create(resolve_path(config, path, options.and_then(|o| o.dir))?)
|
||||
.map_err(Into::into)
|
||||
.and_then(|mut f| f.write_all(&c).map_err(|err| err.into()))
|
||||
File::create(resolve_path(
|
||||
config,
|
||||
package_info,
|
||||
path,
|
||||
options.and_then(|o| o.dir),
|
||||
)?)
|
||||
.map_err(Into::into)
|
||||
.and_then(|mut f| f.write_all(&c).map_err(|err| err.into()))
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
@ -345,31 +375,44 @@ pub fn write_binary_file(
|
||||
#[cfg(fs_read_text_file)]
|
||||
pub fn read_text_file(
|
||||
config: &Config,
|
||||
package_info: &PackageInfo,
|
||||
path: PathBuf,
|
||||
options: Option<FileOperationOptions>,
|
||||
) -> crate::Result<String> {
|
||||
file::read_string(resolve_path(config, path, options.and_then(|o| o.dir))?)
|
||||
.map_err(crate::Error::FailedToExecuteApi)
|
||||
file::read_string(resolve_path(
|
||||
config,
|
||||
package_info,
|
||||
path,
|
||||
options.and_then(|o| o.dir),
|
||||
)?)
|
||||
.map_err(crate::Error::FailedToExecuteApi)
|
||||
}
|
||||
|
||||
/// Reads a binary file.
|
||||
#[cfg(fs_read_binary_file)]
|
||||
pub fn read_binary_file(
|
||||
config: &Config,
|
||||
package_info: &PackageInfo,
|
||||
path: PathBuf,
|
||||
options: Option<FileOperationOptions>,
|
||||
) -> crate::Result<Vec<u8>> {
|
||||
file::read_binary(resolve_path(config, path, options.and_then(|o| o.dir))?)
|
||||
.map_err(crate::Error::FailedToExecuteApi)
|
||||
file::read_binary(resolve_path(
|
||||
config,
|
||||
package_info,
|
||||
path,
|
||||
options.and_then(|o| o.dir),
|
||||
)?)
|
||||
.map_err(crate::Error::FailedToExecuteApi)
|
||||
}
|
||||
|
||||
#[cfg(fs_path)]
|
||||
pub fn resolve_path_handler(
|
||||
config: &Config,
|
||||
package_info: &PackageInfo,
|
||||
path: String,
|
||||
directory: Option<BaseDirectory>,
|
||||
) -> crate::Result<PathBuf> {
|
||||
resolve_path(config, path, directory).map_err(Into::into)
|
||||
resolve_path(config, package_info, path, directory).map_err(Into::into)
|
||||
}
|
||||
|
||||
// test webview functionality.
|
||||
|
@ -7,7 +7,7 @@ use serde::Deserialize;
|
||||
|
||||
#[cfg(notification_all)]
|
||||
use crate::api::notification::Notification;
|
||||
use crate::Config;
|
||||
use crate::{Config, PackageInfo};
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
@ -42,7 +42,11 @@ pub enum Cmd {
|
||||
|
||||
impl Cmd {
|
||||
#[allow(unused_variables)]
|
||||
pub fn run(self, config: Arc<Config>) -> crate::Result<InvokeResponse> {
|
||||
pub fn run(
|
||||
self,
|
||||
config: Arc<Config>,
|
||||
package_info: &PackageInfo,
|
||||
) -> crate::Result<InvokeResponse> {
|
||||
match self {
|
||||
#[cfg(notification_all)]
|
||||
Self::Notification { options } => send(options, &config).map(Into::into),
|
||||
@ -50,13 +54,13 @@ impl Cmd {
|
||||
Self::Notification { .. } => Err(crate::Error::ApiNotAllowlisted("notification".to_string())),
|
||||
Self::IsNotificationPermissionGranted => {
|
||||
#[cfg(notification_all)]
|
||||
return is_permission_granted(&config).map(Into::into);
|
||||
return is_permission_granted(&config, package_info).map(Into::into);
|
||||
#[cfg(not(notification_all))]
|
||||
Ok(false.into())
|
||||
}
|
||||
Self::RequestNotificationPermission => {
|
||||
#[cfg(notification_all)]
|
||||
return request_permission(&config).map(Into::into);
|
||||
return request_permission(&config, package_info).map(Into::into);
|
||||
#[cfg(not(notification_all))]
|
||||
Ok(PERMISSION_DENIED.into())
|
||||
}
|
||||
@ -79,8 +83,11 @@ pub fn send(options: NotificationOptions, config: &Config) -> crate::Result<Invo
|
||||
}
|
||||
|
||||
#[cfg(notification_all)]
|
||||
pub fn is_permission_granted(config: &Config) -> crate::Result<InvokeResponse> {
|
||||
let settings = crate::settings::read_settings(config);
|
||||
pub fn is_permission_granted(
|
||||
config: &Config,
|
||||
package_info: &PackageInfo,
|
||||
) -> crate::Result<InvokeResponse> {
|
||||
let settings = crate::settings::read_settings(config, package_info);
|
||||
if let Some(allow_notification) = settings.allow_notification {
|
||||
Ok(allow_notification.into())
|
||||
} else {
|
||||
@ -89,8 +96,8 @@ pub fn is_permission_granted(config: &Config) -> crate::Result<InvokeResponse> {
|
||||
}
|
||||
|
||||
#[cfg(notification_all)]
|
||||
pub fn request_permission(config: &Config) -> crate::Result<String> {
|
||||
let mut settings = crate::settings::read_settings(config);
|
||||
pub fn request_permission(config: &Config, package_info: &PackageInfo) -> crate::Result<String> {
|
||||
let mut settings = crate::settings::read_settings(config, package_info);
|
||||
if let Some(allow_notification) = settings.allow_notification {
|
||||
return Ok(if allow_notification {
|
||||
PERMISSION_GRANTED.to_string()
|
||||
@ -105,12 +112,12 @@ pub fn request_permission(config: &Config) -> crate::Result<String> {
|
||||
match answer {
|
||||
crate::api::dialog::AskResponse::Yes => {
|
||||
settings.allow_notification = Some(true);
|
||||
crate::settings::write_settings(config, settings)?;
|
||||
crate::settings::write_settings(config, package_info, settings)?;
|
||||
Ok(PERMISSION_GRANTED.to_string())
|
||||
}
|
||||
crate::api::dialog::AskResponse::No => {
|
||||
settings.allow_notification = Some(false);
|
||||
crate::settings::write_settings(config, settings)?;
|
||||
crate::settings::write_settings(config, package_info, settings)?;
|
||||
Ok(PERMISSION_DENIED.to_string())
|
||||
}
|
||||
}
|
||||
|
@ -55,7 +55,10 @@ use std::{borrow::Borrow, collections::HashMap, path::PathBuf, sync::Arc};
|
||||
// Export types likely to be used by the application.
|
||||
pub use {
|
||||
self::api::assets::Assets,
|
||||
self::api::config::{Config, WindowUrl},
|
||||
self::api::{
|
||||
config::{Config, WindowUrl},
|
||||
PackageInfo,
|
||||
},
|
||||
self::app::{App, Builder, GlobalWindowEvent, SystemTrayEvent, WindowMenuEvent},
|
||||
self::hooks::{
|
||||
Invoke, InvokeError, InvokeHandler, InvokeMessage, InvokeResolver, InvokeResponse, OnPageLoad,
|
||||
|
@ -278,6 +278,7 @@ impl<P: Params> WindowManager<P> {
|
||||
|
||||
let local_app_data = resolve_path(
|
||||
&self.inner.config,
|
||||
&self.inner.package_info,
|
||||
&self.inner.config.tauri.bundle.identifier,
|
||||
Some(BaseDirectory::LocalData),
|
||||
);
|
||||
|
@ -6,6 +6,7 @@ use crate::{
|
||||
api::{
|
||||
file::read_binary,
|
||||
path::{resolve_path, BaseDirectory},
|
||||
PackageInfo,
|
||||
},
|
||||
Config,
|
||||
};
|
||||
@ -26,14 +27,23 @@ pub struct Settings {
|
||||
}
|
||||
|
||||
/// Gets the path to the settings file
|
||||
fn get_settings_path(config: &Config) -> crate::api::Result<PathBuf> {
|
||||
resolve_path(config, ".tauri-settings", Some(BaseDirectory::App))
|
||||
fn get_settings_path(config: &Config, package_info: &PackageInfo) -> crate::api::Result<PathBuf> {
|
||||
resolve_path(
|
||||
config,
|
||||
package_info,
|
||||
".tauri-settings",
|
||||
Some(BaseDirectory::App),
|
||||
)
|
||||
}
|
||||
|
||||
/// Write the settings to the file system.
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn write_settings(config: &Config, settings: Settings) -> crate::Result<()> {
|
||||
let settings_path = get_settings_path(config)?;
|
||||
pub(crate) fn write_settings(
|
||||
config: &Config,
|
||||
package_info: &PackageInfo,
|
||||
settings: Settings,
|
||||
) -> crate::Result<()> {
|
||||
let settings_path = get_settings_path(config, package_info)?;
|
||||
let settings_folder = Path::new(&settings_path).parent().unwrap();
|
||||
if !settings_folder.exists() {
|
||||
std::fs::create_dir(settings_folder)?;
|
||||
@ -47,8 +57,8 @@ pub(crate) fn write_settings(config: &Config, settings: Settings) -> crate::Resu
|
||||
}
|
||||
|
||||
/// Reads the settings from the file system.
|
||||
pub fn read_settings(config: &Config) -> Settings {
|
||||
if let Ok(settings_path) = get_settings_path(config) {
|
||||
pub fn read_settings(config: &Config, package_info: &PackageInfo) -> Settings {
|
||||
if let Ok(settings_path) = get_settings_path(config, package_info) {
|
||||
if settings_path.exists() {
|
||||
read_binary(settings_path)
|
||||
.and_then(|settings| bincode::deserialize(&settings).map_err(Into::into))
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user