Skip to content

Commit 0959fe3

Browse files
authored
refactor(os)!: make platform, arch, type, family, version and exe_extension functions sync (#1353)
closes #1351
1 parent f30a3b0 commit 0959fe3

File tree

6 files changed

+70
-70
lines changed

6 files changed

+70
-70
lines changed

.changes/os-sync-functions.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"os": "patch"
3+
"os-js": "patch"
4+
---
5+
6+
**Breaking** Changed `platform`, `arch`, `type`, `family`, `version` and `exe_extension` functions to be sync.

plugins/os/api-iife.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/os/guest-js/index.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ declare global {
1515
interface Window {
1616
__TAURI_OS_PLUGIN_INTERNALS__: {
1717
eol: string;
18+
os_type: OsType;
19+
platform: Platform;
20+
family: Family;
21+
version: string;
22+
arch: Arch;
23+
exe_extension: string;
1824
};
1925
}
2026
}
@@ -70,8 +76,8 @@ function eol(): string {
7076
* @since 2.0.0
7177
*
7278
*/
73-
async function platform(): Promise<Platform> {
74-
return await invoke("plugin:os|platform");
79+
function platform(): Platform {
80+
return window.__TAURI_OS_PLUGIN_INTERNALS__.platform;
7581
}
7682

7783
/**
@@ -84,8 +90,8 @@ async function platform(): Promise<Platform> {
8490
*
8591
* @since 2.0.0
8692
*/
87-
async function version(): Promise<string> {
88-
return await invoke("plugin:os|version");
93+
function version(): string {
94+
return window.__TAURI_OS_PLUGIN_INTERNALS__.version;
8995
}
9096

9197
type Family = "unix" | "windows";
@@ -100,8 +106,8 @@ type Family = "unix" | "windows";
100106
*
101107
* @since 2.0.0
102108
*/
103-
async function family(): Promise<Family> {
104-
return await invoke("plugin:os|family");
109+
function family(): Family {
110+
return window.__TAURI_OS_PLUGIN_INTERNALS__.family;
105111
}
106112

107113
/**
@@ -114,8 +120,8 @@ async function family(): Promise<Family> {
114120
*
115121
* @since 2.0.0
116122
*/
117-
async function type(): Promise<OsType> {
118-
return await invoke("plugin:os|os_type");
123+
function type(): OsType {
124+
return window.__TAURI_OS_PLUGIN_INTERNALS__.os_type;
119125
}
120126

121127
/**
@@ -129,39 +135,39 @@ async function type(): Promise<OsType> {
129135
*
130136
* @since 2.0.0
131137
*/
132-
async function arch(): Promise<Arch> {
133-
return await invoke("plugin:os|arch");
138+
function arch(): Arch {
139+
return window.__TAURI_OS_PLUGIN_INTERNALS__.arch;
134140
}
135141

136142
/**
137-
* Returns a String with a `BCP-47` language tag inside. If the locale couldn’t be obtained, `null` is returned instead.
143+
* Returns the file extension, if any, used for executable binaries on this platform. Possible values are `'exe'` and `''` (empty string).
138144
* @example
139145
* ```typescript
140-
* import { locale } from '@tauri-apps/plugin-os';
141-
* const locale = await locale();
142-
* if (locale) {
143-
* // use the locale string here
144-
* }
146+
* import { exeExtension } from '@tauri-apps/plugin-os';
147+
* const exeExt = await exeExtension();
145148
* ```
146149
*
147150
* @since 2.0.0
148151
*/
149-
async function locale(): Promise<string | null> {
150-
return await invoke("plugin:os|locale");
152+
function exeExtension(): string {
153+
return window.__TAURI_OS_PLUGIN_INTERNALS__.exe_extension;
151154
}
152155

153156
/**
154-
* Returns the file extension, if any, used for executable binaries on this platform. Possible values are `'exe'` and `''` (empty string).
157+
* Returns a String with a `BCP-47` language tag inside. If the locale couldn’t be obtained, `null` is returned instead.
155158
* @example
156159
* ```typescript
157-
* import { exeExtension } from '@tauri-apps/plugin-os';
158-
* const exeExt = await exeExtension();
160+
* import { locale } from '@tauri-apps/plugin-os';
161+
* const locale = await locale();
162+
* if (locale) {
163+
* // use the locale string here
164+
* }
159165
* ```
160166
*
161167
* @since 2.0.0
162168
*/
163-
async function exeExtension(): Promise<string | null> {
164-
return await invoke("plugin:os|exe_extension");
169+
async function locale(): Promise<string | null> {
170+
return await invoke("plugin:os|locale");
165171
}
166172

167173
/**

plugins/os/src/commands.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
#[tauri::command]
6-
pub fn platform() -> &'static str {
7-
crate::platform()
8-
}
9-
10-
#[tauri::command]
11-
pub fn version() -> String {
12-
crate::version().to_string()
13-
}
14-
15-
#[tauri::command]
16-
pub fn os_type() -> String {
17-
crate::type_().to_string()
18-
}
19-
20-
#[tauri::command]
21-
pub fn family() -> &'static str {
22-
crate::family()
23-
}
24-
25-
#[tauri::command]
26-
pub fn arch() -> &'static str {
27-
crate::arch()
28-
}
29-
30-
#[tauri::command]
31-
pub fn exe_extension() -> &'static str {
32-
crate::exe_extension()
33-
}
34-
355
#[tauri::command]
366
pub fn locale() -> Option<String> {
377
crate::locale()

plugins/os/src/init.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@
66
Object.defineProperty(window, "__TAURI_OS_PLUGIN_INTERNALS__", {
77
value: {
88
eol: __TEMPLATE_eol__,
9+
os_type: __TEMPLATE_os_type__,
10+
platform: __TEMPLATE_platform__,
11+
family: __TEMPLATE_family__,
12+
version: __TEMPLATE_version__,
13+
arch: __TEMPLATE_arch__,
14+
exe_extension: __TEMPLATE_exe_extension__,
915
},
1016
});

plugins/os/src/lib.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,30 +102,42 @@ pub fn hostname() -> String {
102102

103103
#[derive(Template)]
104104
#[default_template("./init.js")]
105-
struct InitJavascript {
105+
struct InitJavascript<'a> {
106106
eol: &'static str,
107+
os_type: String,
108+
platform: &'a str,
109+
family: &'a str,
110+
version: String,
111+
arch: &'a str,
112+
exe_extension: &'a str,
107113
}
108114

109-
pub fn init<R: Runtime>() -> TauriPlugin<R> {
110-
let init_js = InitJavascript {
111-
#[cfg(windows)]
112-
eol: "\r\n",
113-
#[cfg(not(windows))]
114-
eol: "\n",
115+
impl<'a> InitJavascript<'a> {
116+
fn new() -> Self {
117+
Self {
118+
#[cfg(windows)]
119+
eol: "\r\n",
120+
#[cfg(not(windows))]
121+
eol: "\n",
122+
os_type: crate::type_().to_string(),
123+
platform: crate::platform(),
124+
family: crate::family(),
125+
version: crate::version().to_string(),
126+
arch: crate::arch(),
127+
exe_extension: crate::exe_extension(),
128+
}
115129
}
116-
.render_default(&Default::default())
117-
// this will never fail with the above global_os_api eol values
118-
.unwrap();
130+
}
131+
132+
pub fn init<R: Runtime>() -> TauriPlugin<R> {
133+
let init_js = InitJavascript::new()
134+
.render_default(&Default::default())
135+
// this will never fail with the above global_os_api values
136+
.unwrap();
119137

120138
Builder::new("os")
121139
.js_init_script(init_js.to_string())
122140
.invoke_handler(tauri::generate_handler![
123-
commands::platform,
124-
commands::version,
125-
commands::os_type,
126-
commands::family,
127-
commands::arch,
128-
commands::exe_extension,
129141
commands::locale,
130142
commands::hostname
131143
])

0 commit comments

Comments
 (0)