2021-04-11 01:09:09 +03:00
|
|
|
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2020-06-28 16:34:43 +03:00
|
|
|
//! Tauri utility helpers
|
|
|
|
#![warn(missing_docs, rust_2018_idioms)]
|
|
|
|
|
2021-02-09 21:22:04 +03:00
|
|
|
pub mod assets;
|
|
|
|
pub mod config;
|
2021-05-05 05:31:05 +03:00
|
|
|
pub mod html;
|
2020-01-18 03:33:17 +03:00
|
|
|
pub mod platform;
|
|
|
|
|
2021-08-15 18:06:46 +03:00
|
|
|
/// `tauri::App` package information.
|
2021-05-10 06:01:12 +03:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct PackageInfo {
|
2021-08-15 18:06:46 +03:00
|
|
|
/// App name
|
2021-05-10 06:01:12 +03:00
|
|
|
pub name: String,
|
2021-08-15 18:06:46 +03:00
|
|
|
/// App version
|
2021-05-10 06:01:12 +03:00
|
|
|
pub version: String,
|
2021-12-09 05:47:43 +03:00
|
|
|
/// The crate authors.
|
|
|
|
pub authors: &'static str,
|
|
|
|
/// The crate description.
|
|
|
|
pub description: &'static str,
|
2021-05-10 06:01:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-08-23 02:41:26 +03:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
{
|
2021-12-15 22:02:48 +03:00
|
|
|
use heck::ToKebabCase;
|
2021-08-23 02:41:26 +03:00
|
|
|
self.name.clone().to_kebab_case()
|
|
|
|
}
|
|
|
|
#[cfg(not(target_os = "linux"))]
|
2021-08-10 22:02:46 +03:00
|
|
|
self.name.clone()
|
2021-05-10 06:01:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-09 16:57:29 +03:00
|
|
|
/// Information about environment variables.
|
|
|
|
#[derive(Debug, Clone)]
|
2021-10-24 15:24:18 +03:00
|
|
|
#[non_exhaustive]
|
2022-01-09 16:57:29 +03:00
|
|
|
pub struct Env {
|
|
|
|
/// The APPIMAGE environment variable.
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
pub appimage: Option<std::ffi::OsString>,
|
|
|
|
/// The APPDIR environment variable.
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
pub appdir: Option<std::ffi::OsString>,
|
|
|
|
}
|
|
|
|
|
2022-01-09 20:26:58 +03:00
|
|
|
#[allow(clippy::derivable_impls)]
|
2022-01-09 16:57:29 +03:00
|
|
|
impl Default for Env {
|
|
|
|
fn default() -> Self {
|
2021-10-24 15:24:18 +03:00
|
|
|
let env = Self {
|
2022-01-09 16:57:29 +03:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
appimage: std::env::var_os("APPIMAGE"),
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
appdir: std::env::var_os("APPDIR"),
|
2021-10-24 15:24:18 +03:00
|
|
|
};
|
2021-10-25 20:01:16 +03:00
|
|
|
#[cfg(target_os = "linux")]
|
2021-10-24 15:24:18 +03:00
|
|
|
if env.appimage.is_some() || env.appdir.is_some() {
|
|
|
|
// validate that we're actually running on an AppImage
|
|
|
|
// an AppImage is mounted to `/tmp/.mount_${appPrefix}${hash}`
|
|
|
|
// see https://github.com/AppImage/AppImageKit/blob/1681fd84dbe09c7d9b22e13cdb16ea601aa0ec47/src/runtime.c#L501
|
|
|
|
if !std::env::current_exe()
|
|
|
|
.map(|p| p.to_string_lossy().into_owned().starts_with("/tmp/.mount_"))
|
|
|
|
.unwrap_or(true)
|
|
|
|
{
|
|
|
|
panic!("`APPDIR` or `APPIMAGE` environment variable found but this application was not detected as an AppImage; this might be a security issue.");
|
|
|
|
}
|
2022-01-09 16:57:29 +03:00
|
|
|
}
|
2021-10-24 15:24:18 +03:00
|
|
|
env
|
2022-01-09 16:57:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 18:06:46 +03:00
|
|
|
/// The result type of `tauri-utils`.
|
2021-02-11 01:51:15 +03:00
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
2020-01-18 03:33:17 +03:00
|
|
|
|
2021-08-15 18:06:46 +03:00
|
|
|
/// The error type of `tauri-utils`.
|
2021-02-11 01:51:15 +03:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
2021-05-06 04:22:45 +03:00
|
|
|
#[non_exhaustive]
|
2020-05-30 02:22:04 +03:00
|
|
|
pub enum Error {
|
2020-06-28 16:34:43 +03:00
|
|
|
/// Target triple architecture error
|
2020-06-18 00:11:11 +03:00
|
|
|
#[error("Unable to determine target-architecture")]
|
|
|
|
Architecture,
|
2020-06-28 16:34:43 +03:00
|
|
|
/// Target triple OS error
|
2020-06-18 00:11:11 +03:00
|
|
|
#[error("Unable to determine target-os")]
|
2021-04-12 16:44:44 +03:00
|
|
|
Os,
|
2020-06-28 16:34:43 +03:00
|
|
|
/// Target triple environment error
|
2020-06-18 00:11:11 +03:00
|
|
|
#[error("Unable to determine target-environment")]
|
|
|
|
Environment,
|
2021-08-15 18:06:46 +03:00
|
|
|
/// Tried to get resource on an unsupported platform
|
2021-02-11 01:51:15 +03:00
|
|
|
#[error("Unsupported platform for reading resources")]
|
|
|
|
UnsupportedPlatform,
|
2020-06-28 16:34:43 +03:00
|
|
|
/// Get parent process error
|
2020-06-18 00:11:11 +03:00
|
|
|
#[error("Could not get parent process")]
|
|
|
|
ParentProcess,
|
2020-06-28 16:34:43 +03:00
|
|
|
/// Get parent process PID error
|
2020-06-18 00:11:11 +03:00
|
|
|
#[error("Could not get parent PID")]
|
2021-04-12 16:44:44 +03:00
|
|
|
ParentPid,
|
2020-06-28 16:34:43 +03:00
|
|
|
/// Get child process error
|
2020-06-18 00:11:11 +03:00
|
|
|
#[error("Could not get child process")]
|
|
|
|
ChildProcess,
|
2021-08-15 18:06:46 +03:00
|
|
|
/// IO error
|
2021-02-11 01:51:15 +03:00
|
|
|
#[error("{0}")]
|
|
|
|
Io(#[from] std::io::Error),
|
2020-06-18 00:11:11 +03:00
|
|
|
}
|