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

This commit is contained in:
Amr Bashir
2023-06-07 14:13:31 +03:00
committed by GitHub
parent 91ffc01a91
commit 1091d6d6ac
8 changed files with 157 additions and 66 deletions
+1 -1
View File
@@ -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__})}
+14 -6
View File
@@ -2,8 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use std::path::PathBuf;
#[tauri::command]
pub fn platform() -> &'static str {
crate::platform()
@@ -15,8 +13,13 @@ pub fn version() -> String {
}
#[tauri::command]
pub fn kind() -> String {
crate::kind().to_string()
pub fn os_type() -> String {
crate::type_().to_string()
}
#[tauri::command]
pub fn family() -> &'static str {
crate::family()
}
#[tauri::command]
@@ -25,11 +28,16 @@ pub fn arch() -> &'static str {
}
#[tauri::command]
pub fn tempdir() -> PathBuf {
crate::tempdir()
pub fn exe_extension() -> &'static str {
crate::exe_extension()
}
#[tauri::command]
pub fn locale() -> Option<String> {
crate::locale()
}
#[tauri::command]
pub fn hostname() -> String {
crate::hostname()
}
+46 -27
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use std::{fmt::Display, path::PathBuf};
use std::fmt::Display;
pub use os_info::Version;
use tauri::{
@@ -15,74 +15,93 @@ mod error;
pub use error::Error;
pub enum Kind {
pub enum OsType {
Linux,
Windows,
Darwin,
Macos,
IOS,
Android,
}
impl Display for Kind {
impl Display for OsType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Linux => write!(f, "Linux"),
Self::Windows => write!(f, "Linux_NT"),
Self::Darwin => write!(f, "Darwin"),
Self::IOS => write!(f, "iOS"),
Self::Android => write!(f, "Android"),
Self::Linux => write!(f, "linux"),
Self::Windows => write!(f, "windows"),
Self::Macos => write!(f, "macos"),
Self::IOS => write!(f, "ios"),
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 {
match std::env::consts::OS {
"windows" => "win32",
"macos" => "darwin",
_ => std::env::consts::OS,
}
std::env::consts::OS
}
/// Returns the current operating system version.
pub fn version() -> Version {
os_info::get().version().clone()
}
pub fn kind() -> Kind {
#[cfg(target_os = "linux")]
return Kind::Linux;
/// Returns the current operating system type.
pub fn type_() -> OsType {
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
return OsType::Linux;
#[cfg(target_os = "windows")]
return Kind::Windows;
return OsType::Windows;
#[cfg(target_os = "macos")]
return Kind::Darwin;
return OsType::Macos;
#[cfg(target_os = "ios")]
return Kind::IOS;
return OsType::IOS;
#[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 {
std::env::consts::ARCH
}
pub fn tempdir() -> PathBuf {
std::env::temp_dir()
/// Returns the file extension, if any, used for executable binaries on this platform. Example value is `exe`, see [std::env::consts::EXE_EXTENSION].
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> {
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> {
Builder::new("os")
.js_init_script(include_str!("api-iife.js").to_string())
.invoke_handler(tauri::generate_handler![
commands::platform,
commands::version,
commands::kind,
commands::os_type,
commands::family,
commands::arch,
commands::tempdir,
commands::locale
commands::exe_extension,
commands::locale,
commands::hostname
])
.build()
}