Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(os): refactor and improvemnts #419

Merged
merged 3 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changes/os-plugin-refactor.md
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/os/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ log = { workspace = true }
thiserror = { workspace = true }
os_info = "3"
sys-locale = "0.3"
gethostname = "0.4"
87 changes: 63 additions & 24 deletions plugins/os/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ declare global {

type Platform =
| "linux"
| "darwin"
| "macos"
| "ios"
| "freebsd"
| "dragonfly"
| "netbsd"
| "openbsd"
| "solaris"
| "android"
| "win32";
| "windows";

type OsType = "Linux" | "Darwin" | "Windows_NT";
type OsType = "linux" | "windows" | "macss" | "ios" | "android";

type Arch =
| "x86"
Expand Down Expand Up @@ -55,8 +55,9 @@ function isWindows(): boolean {
const EOL = isWindows() ? "\r\n" : "\n";

/**
* Returns a string identifying the operating system platform.
* The value is set at compile time. Possible values are `'linux'`, `'darwin'`, `'ios'`, `'freebsd'`, `'dragonfly'`, `'netbsd'`, `'openbsd'`, `'solaris'`, `'android'`, `'win32'`
* Returns a string describing the specific operating system in use.
* The value is set at compile time. Possible values are `'linux'`, `'macos'`, `'ios'`, `'freebsd'`, `'dragonfly'`, `'netbsd'`, `'openbsd'`, `'solaris'`, `'android'`, `'windows'`
*
* @example
* ```typescript
* import { platform } from '@tauri-apps/plugin-os';
Expand All @@ -71,7 +72,7 @@ async function platform(): Promise<Platform> {
}

/**
* Returns a string identifying the kernel version.
* Returns the current operating system version.
* @example
* ```typescript
* import { version } from '@tauri-apps/plugin-os';
Expand All @@ -84,47 +85,49 @@ async function version(): Promise<string> {
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
* ```typescript
* import { type } from '@tauri-apps/plugin-os';
* const osType = await type();
* import { family } from '@tauri-apps/plugin-os';
* const family = await family();
* ```
*
* @since 2.0.0
*/
async function type(): Promise<OsType> {
return window.__TAURI_INVOKE__("plugin:os|kind");
async function family(): Promise<Family> {
return window.__TAURI_INVOKE__("plugin:os|family");
}

/**
* Returns the operating system CPU architecture for which the tauri app was compiled.
* Possible values are `'x86'`, `'x86_64'`, `'arm'`, `'aarch64'`, `'mips'`, `'mips64'`, `'powerpc'`, `'powerpc64'`, `'riscv64'`, `'s390x'`, `'sparc64'`.
* Returns the current operating system type. Returns `'linux'` on Linux, `'macos'` on macOS, `'windows'` on Windows, `'ios'` on iOS and `'android'` on Android.
* @example
* ```typescript
* import { arch } from '@tauri-apps/plugin-os';
* const archName = await arch();
* import { type } from '@tauri-apps/plugin-os';
* const osType = await type();
* ```
*
* @since 2.0.0
*/
async function arch(): Promise<Arch> {
return window.__TAURI_INVOKE__("plugin:os|arch");
async function type(): Promise<OsType> {
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
* ```typescript
* import { tempdir } from '@tauri-apps/plugin-os';
* const tempdirPath = await tempdir();
* import { arch } from '@tauri-apps/plugin-os';
* const archName = await arch();
* ```
*
* @since 2.0.0
*/
async function tempdir(): Promise<string> {
return window.__TAURI_INVOKE__("plugin:os|tempdir");
async function arch(): Promise<Arch> {
return window.__TAURI_INVOKE__("plugin:os|arch");
}

/**
Expand All @@ -144,5 +147,41 @@ async function locale(): Promise<string | null> {
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 };
2 changes: 1 addition & 1 deletion plugins/os/src/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions plugins/os/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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]
Expand All @@ -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()
}
73 changes: 46 additions & 27 deletions plugins/os/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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 couldn’t be obtained, `None` is returned instead.
/// Returns the current operating system locale with the `BCP-47` language tag. If the locale couldn’t 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()
}
Loading