refactor(os)!: make `platform`, `arch`, `type`, `family`, `version` and `exe_extension` functions sync (#1353)

closes #1351
pull/1480/head^2
Amr Bashir 12 months ago committed by GitHub
parent f30a3b0501
commit 0959fe3757
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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,39 +135,39 @@ 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 a String with a `BCP-47` language tag inside. If the locale couldnt 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<string | null> {
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 couldnt 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<string | null> {
return await invoke("plugin:os|exe_extension");
async function locale(): Promise<string | null> {
return await invoke("plugin:os|locale");
}
/**

@ -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__,
},
});

@ -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<R: Runtime>() -> TauriPlugin<R> {
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<R: Runtime>() -> TauriPlugin<R> {
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
])

Loading…
Cancel
Save