feat(os): refactor and improvemnts (#419)

pull/429/head
Amr Bashir 2 years ago committed by GitHub
parent 91ffc01a91
commit 1091d6d6ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,11 @@
---
"os": minor
"os-js": minor
---
The os plugin is recieving a few changes to improve consistency and add new features:
- Renamed `Kind` enum to `OsType` and `kind()` function to `os_type()`.
- Added `family()`,`exe_extension()`, and `hostname()` functions and their equivalents for JS.
- Removed `tempdir()` function and its equivalent on JS, use `std::env::temp_dir` instead of `temp_dir` from `tauri::path::PathResolver::temp_dir` and `path.tempDir` on JS.
- Modified `platform()` implementation to return `windows` instead of `win32` and `macos` instead of `darwin` to align with Rust's `std::env::consts::OS`

17
Cargo.lock generated

@ -206,7 +206,7 @@ checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4"
[[package]] [[package]]
name = "api" name = "api"
version = "2.0.0-alpha.0" version = "2.0.0-alpha.1"
dependencies = [ dependencies = [
"log", "log",
"serde", "serde",
@ -1854,6 +1854,16 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "gethostname"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0176e0459c2e4a1fe232f984bca6890e681076abb9934f6cea7c326f3fc47818"
dependencies = [
"libc",
"windows-targets 0.48.0",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.1.16" version = "0.1.16"
@ -5263,7 +5273,7 @@ dependencies = [
[[package]] [[package]]
name = "tauri-plugin-notification" name = "tauri-plugin-notification"
version = "2.0.0-alpha.0" version = "2.0.0-alpha.1"
dependencies = [ dependencies = [
"log", "log",
"notify-rust", "notify-rust",
@ -5283,6 +5293,7 @@ dependencies = [
name = "tauri-plugin-os" name = "tauri-plugin-os"
version = "2.0.0-alpha.0" version = "2.0.0-alpha.0"
dependencies = [ dependencies = [
"gethostname 0.4.3",
"log", "log",
"os_info", "os_info",
"serde", "serde",
@ -6886,7 +6897,7 @@ version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "592b4883219f345e712b3209c62654ebda0bb50887f330cbd018d0f654bfd507" checksum = "592b4883219f345e712b3209c62654ebda0bb50887f330cbd018d0f654bfd507"
dependencies = [ dependencies = [
"gethostname", "gethostname 0.2.3",
"nix 0.24.3", "nix 0.24.3",
"winapi", "winapi",
"winapi-wsapoll", "winapi-wsapoll",

@ -14,3 +14,4 @@ log = { workspace = true }
thiserror = { workspace = true } thiserror = { workspace = true }
os_info = "3" os_info = "3"
sys-locale = "0.3" sys-locale = "0.3"
gethostname = "0.4"

@ -16,7 +16,7 @@ declare global {
type Platform = type Platform =
| "linux" | "linux"
| "darwin" | "macos"
| "ios" | "ios"
| "freebsd" | "freebsd"
| "dragonfly" | "dragonfly"
@ -24,9 +24,9 @@ type Platform =
| "openbsd" | "openbsd"
| "solaris" | "solaris"
| "android" | "android"
| "win32"; | "windows";
type OsType = "Linux" | "Darwin" | "Windows_NT"; type OsType = "linux" | "windows" | "macss" | "ios" | "android";
type Arch = type Arch =
| "x86" | "x86"
@ -55,8 +55,9 @@ function isWindows(): boolean {
const EOL = isWindows() ? "\r\n" : "\n"; const EOL = isWindows() ? "\r\n" : "\n";
/** /**
* Returns a string identifying the operating system platform. * Returns a string describing the specific operating system in use.
* The value is set at compile time. Possible values are `'linux'`, `'darwin'`, `'ios'`, `'freebsd'`, `'dragonfly'`, `'netbsd'`, `'openbsd'`, `'solaris'`, `'android'`, `'win32'` * The value is set at compile time. Possible values are `'linux'`, `'macos'`, `'ios'`, `'freebsd'`, `'dragonfly'`, `'netbsd'`, `'openbsd'`, `'solaris'`, `'android'`, `'windows'`
*
* @example * @example
* ```typescript * ```typescript
* import { platform } from '@tauri-apps/plugin-os'; * import { platform } from '@tauri-apps/plugin-os';
@ -71,7 +72,7 @@ async function platform(): Promise<Platform> {
} }
/** /**
* Returns a string identifying the kernel version. * Returns the current operating system version.
* @example * @example
* ```typescript * ```typescript
* import { version } from '@tauri-apps/plugin-os'; * import { version } from '@tauri-apps/plugin-os';
@ -84,47 +85,49 @@ async function version(): Promise<string> {
return window.__TAURI_INVOKE__("plugin:os|version"); return window.__TAURI_INVOKE__("plugin:os|version");
} }
type Family = "unix" | "windows";
/** /**
* Returns `'Linux'` on Linux, `'Darwin'` on macOS, and `'Windows_NT'` on Windows. * Returns the current operating system family. Possible values are `'unix'`, `'windows'`.
* @example * @example
* ```typescript * ```typescript
* import { type } from '@tauri-apps/plugin-os'; * import { family } from '@tauri-apps/plugin-os';
* const osType = await type(); * const family = await family();
* ``` * ```
* *
* @since 2.0.0 * @since 2.0.0
*/ */
async function type(): Promise<OsType> { async function family(): Promise<Family> {
return window.__TAURI_INVOKE__("plugin:os|kind"); return window.__TAURI_INVOKE__("plugin:os|family");
} }
/** /**
* Returns the operating system CPU architecture for which the tauri app was compiled. * Returns the current operating system type. Returns `'linux'` on Linux, `'macos'` on macOS, `'windows'` on Windows, `'ios'` on iOS and `'android'` on Android.
* Possible values are `'x86'`, `'x86_64'`, `'arm'`, `'aarch64'`, `'mips'`, `'mips64'`, `'powerpc'`, `'powerpc64'`, `'riscv64'`, `'s390x'`, `'sparc64'`.
* @example * @example
* ```typescript * ```typescript
* import { arch } from '@tauri-apps/plugin-os'; * import { type } from '@tauri-apps/plugin-os';
* const archName = await arch(); * const osType = await type();
* ``` * ```
* *
* @since 2.0.0 * @since 2.0.0
*/ */
async function arch(): Promise<Arch> { async function type(): Promise<OsType> {
return window.__TAURI_INVOKE__("plugin:os|arch"); return window.__TAURI_INVOKE__("plugin:os|os_type");
} }
/** /**
* Returns the operating system's default directory for temporary files as a string. * Returns the current operating system architecture.
* Possible values are `'x86'`, `'x86_64'`, `'arm'`, `'aarch64'`, `'mips'`, `'mips64'`, `'powerpc'`, `'powerpc64'`, `'riscv64'`, `'s390x'`, `'sparc64'`.
* @example * @example
* ```typescript * ```typescript
* import { tempdir } from '@tauri-apps/plugin-os'; * import { arch } from '@tauri-apps/plugin-os';
* const tempdirPath = await tempdir(); * const archName = await arch();
* ``` * ```
* *
* @since 2.0.0 * @since 2.0.0
*/ */
async function tempdir(): Promise<string> { async function arch(): Promise<Arch> {
return window.__TAURI_INVOKE__("plugin:os|tempdir"); return window.__TAURI_INVOKE__("plugin:os|arch");
} }
/** /**
@ -144,5 +147,41 @@ async function locale(): Promise<string | null> {
return window.__TAURI_INVOKE__("plugin:os|locale"); return window.__TAURI_INVOKE__("plugin:os|locale");
} }
export { EOL, platform, version, type, arch, tempdir, locale }; /**
export type { Platform, OsType, Arch }; * Returns the file extension, if any, used for executable binaries on this platform. Possible values are `'exe'` and `''` (empty string).
* @example
* ```typescript
* import { exeExtension } from '@tauri-apps/plugin-os';
* const exeExt = await exeExtension();
* ```
*
* @since 2.0.0
*/
async function exeExtension(): Promise<string | null> {
return window.__TAURI_INVOKE__("plugin:os|exe_extension");
}
/**
* Returns the host name of the operating system.
* @example
* ```typescript
* import { hostname } from '@tauri-apps/api/os';
* const hostname = await hostname();
* ```
*/
async function hostname(): Promise<string | null> {
return window.__TAURI_INVOKE__("plugin:os|hostname");
}
export {
EOL,
platform,
family,
version,
type,
arch,
locale,
exeExtension,
hostname,
};
export type { Platform, OsType, Arch, Family };

@ -1 +1 @@
if("__TAURI__"in window){var __TAURI_OS__=function(n){"use strict";const _=navigator.appVersion.includes("Win")?"\r\n":"\n";return n.EOL=_,n.arch=async function(){return window.__TAURI_INVOKE__("plugin:os|arch")},n.locale=async function(){return window.__TAURI_INVOKE__("plugin:os|locale")},n.platform=async function(){return window.__TAURI_INVOKE__("plugin:os|platform")},n.tempdir=async function(){return window.__TAURI_INVOKE__("plugin:os|tempdir")},n.type=async function(){return window.__TAURI_INVOKE__("plugin:os|kind")},n.version=async function(){return window.__TAURI_INVOKE__("plugin:os|version")},n}({});Object.defineProperty(window.__TAURI__,"os",{value:__TAURI_OS__})} if("__TAURI__"in window){var __TAURI_OS__=function(n){"use strict";const _=navigator.appVersion.includes("Win")?"\r\n":"\n";return n.EOL=_,n.arch=async function(){return window.__TAURI_INVOKE__("plugin:os|arch")},n.exeExtension=async function(){return window.__TAURI_INVOKE__("plugin:os|exe_extension")},n.family=async function(){return window.__TAURI_INVOKE__("plugin:os|family")},n.hostname=async function(){return window.__TAURI_INVOKE__("plugin:os|hostname")},n.locale=async function(){return window.__TAURI_INVOKE__("plugin:os|locale")},n.platform=async function(){return window.__TAURI_INVOKE__("plugin:os|platform")},n.type=async function(){return window.__TAURI_INVOKE__("plugin:os|os_type")},n.version=async function(){return window.__TAURI_INVOKE__("plugin:os|version")},n}({});Object.defineProperty(window.__TAURI__,"os",{value:__TAURI_OS__})}

@ -2,8 +2,6 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
use std::path::PathBuf;
#[tauri::command] #[tauri::command]
pub fn platform() -> &'static str { pub fn platform() -> &'static str {
crate::platform() crate::platform()
@ -15,8 +13,13 @@ pub fn version() -> String {
} }
#[tauri::command] #[tauri::command]
pub fn kind() -> String { pub fn os_type() -> String {
crate::kind().to_string() crate::type_().to_string()
}
#[tauri::command]
pub fn family() -> &'static str {
crate::family()
} }
#[tauri::command] #[tauri::command]
@ -25,11 +28,16 @@ pub fn arch() -> &'static str {
} }
#[tauri::command] #[tauri::command]
pub fn tempdir() -> PathBuf { pub fn exe_extension() -> &'static str {
crate::tempdir() crate::exe_extension()
} }
#[tauri::command] #[tauri::command]
pub fn locale() -> Option<String> { pub fn locale() -> Option<String> {
crate::locale() crate::locale()
} }
#[tauri::command]
pub fn hostname() -> String {
crate::hostname()
}

@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
use std::{fmt::Display, path::PathBuf}; use std::fmt::Display;
pub use os_info::Version; pub use os_info::Version;
use tauri::{ use tauri::{
@ -15,74 +15,93 @@ mod error;
pub use error::Error; pub use error::Error;
pub enum Kind { pub enum OsType {
Linux, Linux,
Windows, Windows,
Darwin, Macos,
IOS, IOS,
Android, Android,
} }
impl Display for Kind { impl Display for OsType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
Self::Linux => write!(f, "Linux"), Self::Linux => write!(f, "linux"),
Self::Windows => write!(f, "Linux_NT"), Self::Windows => write!(f, "windows"),
Self::Darwin => write!(f, "Darwin"), Self::Macos => write!(f, "macos"),
Self::IOS => write!(f, "iOS"), Self::IOS => write!(f, "ios"),
Self::Android => write!(f, "Android"), Self::Android => write!(f, "android"),
} }
} }
} }
/// Returns a string describing the specific operating system in use, see [std::env::consts::OS].
pub fn platform() -> &'static str { pub fn platform() -> &'static str {
match std::env::consts::OS { std::env::consts::OS
"windows" => "win32",
"macos" => "darwin",
_ => std::env::consts::OS,
}
} }
/// Returns the current operating system version.
pub fn version() -> Version { pub fn version() -> Version {
os_info::get().version().clone() os_info::get().version().clone()
} }
pub fn kind() -> Kind { /// Returns the current operating system type.
#[cfg(target_os = "linux")] pub fn type_() -> OsType {
return Kind::Linux; #[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
return OsType::Linux;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
return Kind::Windows; return OsType::Windows;
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
return Kind::Darwin; return OsType::Macos;
#[cfg(target_os = "ios")] #[cfg(target_os = "ios")]
return Kind::IOS; return OsType::IOS;
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
return Kind::Android; return OsType::Android;
}
/// Returns the current operating system family, see [std::env::consts::FAMILY].
pub fn family() -> &'static str {
std::env::consts::FAMILY
} }
/// Returns the current operating system architecture, see [std::env::consts::ARCH].
pub fn arch() -> &'static str { pub fn arch() -> &'static str {
std::env::consts::ARCH std::env::consts::ARCH
} }
pub fn tempdir() -> PathBuf { /// Returns the file extension, if any, used for executable binaries on this platform. Example value is `exe`, see [std::env::consts::EXE_EXTENSION].
std::env::temp_dir() pub fn exe_extension() -> &'static str {
std::env::consts::EXE_EXTENSION
} }
/// Returns the locale with the `BCP-47` language tag. If the locale couldnt be obtained, `None` is returned instead. /// Returns the current operating system locale with the `BCP-47` language tag. If the locale couldnt be obtained, `None` is returned instead.
pub fn locale() -> Option<String> { pub fn locale() -> Option<String> {
sys_locale::get_locale() sys_locale::get_locale()
} }
/// Returns the current operating system hostname.
pub fn hostname() -> String {
gethostname::gethostname().to_string_lossy().to_string()
}
pub fn init<R: Runtime>() -> TauriPlugin<R> { pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("os") Builder::new("os")
.js_init_script(include_str!("api-iife.js").to_string()) .js_init_script(include_str!("api-iife.js").to_string())
.invoke_handler(tauri::generate_handler![ .invoke_handler(tauri::generate_handler![
commands::platform, commands::platform,
commands::version, commands::version,
commands::kind, commands::os_type,
commands::family,
commands::arch, commands::arch,
commands::tempdir, commands::exe_extension,
commands::locale commands::locale,
commands::hostname
]) ])
.build() .build()
} }

@ -436,10 +436,12 @@ export class Stronghold {
* @returns * @returns
*/ */
static async load(path: string, password: string): Promise<Stronghold> { static async load(path: string, password: string): Promise<Stronghold> {
return await window.__TAURI_INVOKE__("plugin:stronghold|initialize", { return await window
snapshotPath: path, .__TAURI_INVOKE__("plugin:stronghold|initialize", {
password, snapshotPath: path,
}).then(() => new Stronghold(path)); password,
})
.then(() => new Stronghold(path));
} }
/** /**

Loading…
Cancel
Save