mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-06-04 13:48:01 +02:00
refactor(os)!: make platform, arch, type, family, version and exe_extension functions sync (#1353)
closes #1351
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"os": "patch"
|
||||
"os-js": "patch"
|
||||
---
|
||||
|
||||
**Breaking** Changed `platform`, `arch`, `type`, `family`, `version` and `exe_extension` functions to be sync.
|
||||
@@ -1 +1 @@
|
||||
if("__TAURI__"in window){var __TAURI_PLUGIN_OS__=function(n){"use strict";async function t(n,t={},i){return window.__TAURI_INTERNALS__.invoke(n,t,i)}return"function"==typeof SuppressedError&&SuppressedError,n.arch=async function(){return await t("plugin:os|arch")},n.eol=function(){return window.__TAURI_OS_PLUGIN_INTERNALS__.eol},n.exeExtension=async function(){return await t("plugin:os|exe_extension")},n.family=async function(){return await t("plugin:os|family")},n.hostname=async function(){return await t("plugin:os|hostname")},n.locale=async function(){return await t("plugin:os|locale")},n.platform=async function(){return await t("plugin:os|platform")},n.type=async function(){return await t("plugin:os|os_type")},n.version=async function(){return await t("plugin:os|version")},n}({});Object.defineProperty(window.__TAURI__,"os",{value:__TAURI_PLUGIN_OS__})}
|
||||
if("__TAURI__"in window){var __TAURI_PLUGIN_OS__=function(_){"use strict";async function n(_,n={},o){return window.__TAURI_INTERNALS__.invoke(_,n,o)}return"function"==typeof SuppressedError&&SuppressedError,_.arch=function(){return window.__TAURI_OS_PLUGIN_INTERNALS__.arch},_.eol=function(){return window.__TAURI_OS_PLUGIN_INTERNALS__.eol},_.exeExtension=function(){return window.__TAURI_OS_PLUGIN_INTERNALS__.exe_extension},_.family=function(){return window.__TAURI_OS_PLUGIN_INTERNALS__.family},_.hostname=async function(){return await n("plugin:os|hostname")},_.locale=async function(){return await n("plugin:os|locale")},_.platform=function(){return window.__TAURI_OS_PLUGIN_INTERNALS__.platform},_.type=function(){return window.__TAURI_OS_PLUGIN_INTERNALS__.os_type},_.version=function(){return window.__TAURI_OS_PLUGIN_INTERNALS__.version},_}({});Object.defineProperty(window.__TAURI__,"os",{value:__TAURI_PLUGIN_OS__})}
|
||||
|
||||
@@ -15,6 +15,12 @@ declare global {
|
||||
interface Window {
|
||||
__TAURI_OS_PLUGIN_INTERNALS__: {
|
||||
eol: string;
|
||||
os_type: OsType;
|
||||
platform: Platform;
|
||||
family: Family;
|
||||
version: string;
|
||||
arch: Arch;
|
||||
exe_extension: string;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -70,8 +76,8 @@ function eol(): string {
|
||||
* @since 2.0.0
|
||||
*
|
||||
*/
|
||||
async function platform(): Promise<Platform> {
|
||||
return await invoke("plugin:os|platform");
|
||||
function platform(): Platform {
|
||||
return window.__TAURI_OS_PLUGIN_INTERNALS__.platform;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,8 +90,8 @@ async function platform(): Promise<Platform> {
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
async function version(): Promise<string> {
|
||||
return await invoke("plugin:os|version");
|
||||
function version(): string {
|
||||
return window.__TAURI_OS_PLUGIN_INTERNALS__.version;
|
||||
}
|
||||
|
||||
type Family = "unix" | "windows";
|
||||
@@ -100,8 +106,8 @@ type Family = "unix" | "windows";
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
async function family(): Promise<Family> {
|
||||
return await invoke("plugin:os|family");
|
||||
function family(): Family {
|
||||
return window.__TAURI_OS_PLUGIN_INTERNALS__.family;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,8 +120,8 @@ async function family(): Promise<Family> {
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
async function type(): Promise<OsType> {
|
||||
return await invoke("plugin:os|os_type");
|
||||
function type(): OsType {
|
||||
return window.__TAURI_OS_PLUGIN_INTERNALS__.os_type;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,8 +135,22 @@ async function type(): Promise<OsType> {
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
async function arch(): Promise<Arch> {
|
||||
return await invoke("plugin:os|arch");
|
||||
function arch(): Arch {
|
||||
return window.__TAURI_OS_PLUGIN_INTERNALS__.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
|
||||
*/
|
||||
function exeExtension(): string {
|
||||
return window.__TAURI_OS_PLUGIN_INTERNALS__.exe_extension;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -150,20 +170,6 @@ async function locale(): Promise<string | null> {
|
||||
return await invoke("plugin:os|locale");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 await invoke("plugin:os|exe_extension");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the host name of the operating system.
|
||||
* @example
|
||||
|
||||
@@ -2,36 +2,6 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#[tauri::command]
|
||||
pub fn platform() -> &'static str {
|
||||
crate::platform()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn version() -> String {
|
||||
crate::version().to_string()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn os_type() -> String {
|
||||
crate::type_().to_string()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn family() -> &'static str {
|
||||
crate::family()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn arch() -> &'static str {
|
||||
crate::arch()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn exe_extension() -> &'static str {
|
||||
crate::exe_extension()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn locale() -> Option<String> {
|
||||
crate::locale()
|
||||
|
||||
@@ -6,5 +6,11 @@
|
||||
Object.defineProperty(window, "__TAURI_OS_PLUGIN_INTERNALS__", {
|
||||
value: {
|
||||
eol: __TEMPLATE_eol__,
|
||||
os_type: __TEMPLATE_os_type__,
|
||||
platform: __TEMPLATE_platform__,
|
||||
family: __TEMPLATE_family__,
|
||||
version: __TEMPLATE_version__,
|
||||
arch: __TEMPLATE_arch__,
|
||||
exe_extension: __TEMPLATE_exe_extension__,
|
||||
},
|
||||
});
|
||||
|
||||
+28
-16
@@ -102,30 +102,42 @@ pub fn hostname() -> String {
|
||||
|
||||
#[derive(Template)]
|
||||
#[default_template("./init.js")]
|
||||
struct InitJavascript {
|
||||
struct InitJavascript<'a> {
|
||||
eol: &'static str,
|
||||
os_type: String,
|
||||
platform: &'a str,
|
||||
family: &'a str,
|
||||
version: String,
|
||||
arch: &'a str,
|
||||
exe_extension: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> InitJavascript<'a> {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
#[cfg(windows)]
|
||||
eol: "\r\n",
|
||||
#[cfg(not(windows))]
|
||||
eol: "\n",
|
||||
os_type: crate::type_().to_string(),
|
||||
platform: crate::platform(),
|
||||
family: crate::family(),
|
||||
version: crate::version().to_string(),
|
||||
arch: crate::arch(),
|
||||
exe_extension: crate::exe_extension(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
let init_js = InitJavascript {
|
||||
#[cfg(windows)]
|
||||
eol: "\r\n",
|
||||
#[cfg(not(windows))]
|
||||
eol: "\n",
|
||||
}
|
||||
.render_default(&Default::default())
|
||||
// this will never fail with the above global_os_api eol values
|
||||
.unwrap();
|
||||
let init_js = InitJavascript::new()
|
||||
.render_default(&Default::default())
|
||||
// this will never fail with the above global_os_api values
|
||||
.unwrap();
|
||||
|
||||
Builder::new("os")
|
||||
.js_init_script(init_js.to_string())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
commands::platform,
|
||||
commands::version,
|
||||
commands::os_type,
|
||||
commands::family,
|
||||
commands::arch,
|
||||
commands::exe_extension,
|
||||
commands::locale,
|
||||
commands::hostname
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user