From 0959fe3757250c6dea6247edb20e6ab468f20511 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Mon, 17 Jun 2024 18:03:04 +0300 Subject: [PATCH] refactor(os)!: make `platform`, `arch`, `type`, `family`, `version` and `exe_extension` functions sync (#1353) closes #1351 --- .changes/os-sync-functions.md | 6 ++++ plugins/os/api-iife.js | 2 +- plugins/os/guest-js/index.ts | 52 +++++++++++++++++++---------------- plugins/os/src/commands.rs | 30 -------------------- plugins/os/src/init.js | 6 ++++ plugins/os/src/lib.rs | 44 ++++++++++++++++++----------- 6 files changed, 70 insertions(+), 70 deletions(-) create mode 100644 .changes/os-sync-functions.md diff --git a/.changes/os-sync-functions.md b/.changes/os-sync-functions.md new file mode 100644 index 00000000..f6baf7b5 --- /dev/null +++ b/.changes/os-sync-functions.md @@ -0,0 +1,6 @@ +--- +"os": "patch" +"os-js": "patch" +--- + +**Breaking** Changed `platform`, `arch`, `type`, `family`, `version` and `exe_extension` functions to be sync. diff --git a/plugins/os/api-iife.js b/plugins/os/api-iife.js index 80fb7d62..2b7924de 100644 --- a/plugins/os/api-iife.js +++ b/plugins/os/api-iife.js @@ -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__})} diff --git a/plugins/os/guest-js/index.ts b/plugins/os/guest-js/index.ts index f7913e13..404e6676 100644 --- a/plugins/os/guest-js/index.ts +++ b/plugins/os/guest-js/index.ts @@ -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 { - return await invoke("plugin:os|platform"); +function platform(): Platform { + return window.__TAURI_OS_PLUGIN_INTERNALS__.platform; } /** @@ -84,8 +90,8 @@ async function platform(): Promise { * * @since 2.0.0 */ -async function version(): Promise { - 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 { - return await invoke("plugin:os|family"); +function family(): Family { + return window.__TAURI_OS_PLUGIN_INTERNALS__.family; } /** @@ -114,8 +120,8 @@ async function family(): Promise { * * @since 2.0.0 */ -async function type(): Promise { - return await invoke("plugin:os|os_type"); +function type(): OsType { + return window.__TAURI_OS_PLUGIN_INTERNALS__.os_type; } /** @@ -129,39 +135,39 @@ async function type(): Promise { * * @since 2.0.0 */ -async function arch(): Promise { - return await invoke("plugin:os|arch"); +function arch(): Arch { + return window.__TAURI_OS_PLUGIN_INTERNALS__.arch; } /** - * Returns a String with a `BCP-47` language tag inside. If the locale couldn’t be obtained, `null` is returned instead. + * Returns the file extension, if any, used for executable binaries on this platform. Possible values are `'exe'` and `''` (empty string). * @example * ```typescript - * import { locale } from '@tauri-apps/plugin-os'; - * const locale = await locale(); - * if (locale) { - * // use the locale string here - * } + * import { exeExtension } from '@tauri-apps/plugin-os'; + * const exeExt = await exeExtension(); * ``` * * @since 2.0.0 */ -async function locale(): Promise { - return await invoke("plugin:os|locale"); +function exeExtension(): string { + return window.__TAURI_OS_PLUGIN_INTERNALS__.exe_extension; } /** - * Returns the file extension, if any, used for executable binaries on this platform. Possible values are `'exe'` and `''` (empty string). + * Returns a String with a `BCP-47` language tag inside. If the locale couldn’t be obtained, `null` is returned instead. * @example * ```typescript - * import { exeExtension } from '@tauri-apps/plugin-os'; - * const exeExt = await exeExtension(); + * import { locale } from '@tauri-apps/plugin-os'; + * const locale = await locale(); + * if (locale) { + * // use the locale string here + * } * ``` * * @since 2.0.0 */ -async function exeExtension(): Promise { - return await invoke("plugin:os|exe_extension"); +async function locale(): Promise { + return await invoke("plugin:os|locale"); } /** diff --git a/plugins/os/src/commands.rs b/plugins/os/src/commands.rs index fdfa09a0..b10c7f5d 100644 --- a/plugins/os/src/commands.rs +++ b/plugins/os/src/commands.rs @@ -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 { crate::locale() diff --git a/plugins/os/src/init.js b/plugins/os/src/init.js index 5d2f3d1c..f3e2d4aa 100644 --- a/plugins/os/src/init.js +++ b/plugins/os/src/init.js @@ -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__, }, }); diff --git a/plugins/os/src/lib.rs b/plugins/os/src/lib.rs index 3297f4fc..98813578 100644 --- a/plugins/os/src/lib.rs +++ b/plugins/os/src/lib.rs @@ -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, } -pub fn init() -> TauriPlugin { - let init_js = InitJavascript { - #[cfg(windows)] - eol: "\r\n", - #[cfg(not(windows))] - eol: "\n", +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(), + } } - .render_default(&Default::default()) - // this will never fail with the above global_os_api eol values - .unwrap(); +} + +pub fn init() -> TauriPlugin { + 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 ])