From c70451b28ea0eab77600757db2861d67f967e4c6 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 7 Jun 2023 21:07:08 +0300 Subject: [PATCH 1/9] refactor(os): refactor EOL const --- .changes/os-plugin-refactor.md | 1 + plugins/os/guest-js/index.ts | 15 ++++++++------- plugins/os/src/lib.rs | 10 +++++++++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/.changes/os-plugin-refactor.md b/.changes/os-plugin-refactor.md index 63cd5bddd..ff0f73840 100644 --- a/.changes/os-plugin-refactor.md +++ b/.changes/os-plugin-refactor.md @@ -9,3 +9,4 @@ The os plugin is recieving a few changes to improve consistency and add new feat - 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` +- `EOL` const in JS has been modified into a function `eol()` fix import issues in frameworks like `next.js` diff --git a/plugins/os/guest-js/index.ts b/plugins/os/guest-js/index.ts index 2b94895df..7104c03ad 100644 --- a/plugins/os/guest-js/index.ts +++ b/plugins/os/guest-js/index.ts @@ -11,6 +11,9 @@ declare global { interface Window { __TAURI_INVOKE__: (cmd: string, args?: unknown) => Promise; + __TAURI_OS__: { + EOL: string; + }; } } @@ -41,18 +44,16 @@ type Arch = | "s390x" | "sparc64"; -function isWindows(): boolean { - return navigator.appVersion.includes("Win"); -} - /** - * The operating system-specific end-of-line marker. + * Returns the operating system-specific end-of-line marker. * - `\n` on POSIX * - `\r\n` on Windows * * @since 2.0.0 * */ -const EOL = isWindows() ? "\r\n" : "\n"; +function eol() { + return window.__TAURI_OS__.EOL; +} /** * Returns a string describing the specific operating system in use. @@ -174,7 +175,7 @@ async function hostname(): Promise { } export { - EOL, + eol, platform, family, version, diff --git a/plugins/os/src/lib.rs b/plugins/os/src/lib.rs index ccbbdfef4..efebb832e 100644 --- a/plugins/os/src/lib.rs +++ b/plugins/os/src/lib.rs @@ -91,8 +91,16 @@ pub fn hostname() -> String { } pub fn init() -> TauriPlugin { + let mut init_script = String::new(); + init_script.push_str(include_str!("api-iife.js")); + #[cfg(windows)] + let eol = "\r\n"; + #[cfg(not(windows))] + let eol = "\n"; + init_script.push_str(&format!("window.__TAURI_OS__.EOL = {eol}")); + Builder::new("os") - .js_init_script(include_str!("api-iife.js").to_string()) + .js_init_script(init_script) .invoke_handler(tauri::generate_handler![ commands::platform, commands::version, From 16ac336965a8c71ea7c2f8f318f1c321858fc0a6 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 7 Jun 2023 21:12:21 +0300 Subject: [PATCH 2/9] generated files --- plugins/os/src/api-iife.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/os/src/api-iife.js b/plugins/os/src/api-iife.js index 382e6d153..e47a5aaad 100644 --- a/plugins/os/src/api-iife.js +++ b/plugins/os/src/api-iife.js @@ -1 +1 @@ -if("__TAURI__"in window){var __TAURI_OS__=function(n){"use strict";const _=navigator.appVersion.includes("Win")?"\r\n":"\n";return n.EOL=_,n.arch=async function(){return window.__TAURI_INVOKE__("plugin:os|arch")},n.exeExtension=async function(){return window.__TAURI_INVOKE__("plugin:os|exe_extension")},n.family=async function(){return window.__TAURI_INVOKE__("plugin:os|family")},n.hostname=async function(){return window.__TAURI_INVOKE__("plugin:os|hostname")},n.locale=async function(){return window.__TAURI_INVOKE__("plugin:os|locale")},n.platform=async function(){return window.__TAURI_INVOKE__("plugin:os|platform")},n.type=async function(){return window.__TAURI_INVOKE__("plugin:os|os_type")},n.version=async function(){return window.__TAURI_INVOKE__("plugin:os|version")},n}({});Object.defineProperty(window.__TAURI__,"os",{value:__TAURI_OS__})} +if("__TAURI__"in window){var __TAURI_OS__=function(n){"use strict";return n.arch=async function(){return window.__TAURI_INVOKE__("plugin:os|arch")},n.eol=function(){return window.__TAURI_OS__.EOL},n.exeExtension=async function(){return window.__TAURI_INVOKE__("plugin:os|exe_extension")},n.family=async function(){return window.__TAURI_INVOKE__("plugin:os|family")},n.hostname=async function(){return window.__TAURI_INVOKE__("plugin:os|hostname")},n.locale=async function(){return window.__TAURI_INVOKE__("plugin:os|locale")},n.platform=async function(){return window.__TAURI_INVOKE__("plugin:os|platform")},n.type=async function(){return window.__TAURI_INVOKE__("plugin:os|os_type")},n.version=async function(){return window.__TAURI_INVOKE__("plugin:os|version")},n}({});Object.defineProperty(window.__TAURI__,"os",{value:__TAURI_OS__})} From 0903cc867124fe0d4cc27dbc55c64206aba4aaa5 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 7 Jun 2023 21:23:02 +0300 Subject: [PATCH 3/9] qoutes --- plugins/os/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/os/src/lib.rs b/plugins/os/src/lib.rs index efebb832e..721432e88 100644 --- a/plugins/os/src/lib.rs +++ b/plugins/os/src/lib.rs @@ -97,7 +97,7 @@ pub fn init() -> TauriPlugin { let eol = "\r\n"; #[cfg(not(windows))] let eol = "\n"; - init_script.push_str(&format!("window.__TAURI_OS__.EOL = {eol}")); + init_script.push_str(&format!("window.__TAURI_OS__.EOL = '{eol}'")); Builder::new("os") .js_init_script(init_script) From e2295c7db5fee33e74e6533c635f85907eaa08ee Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Wed, 7 Jun 2023 21:42:00 +0300 Subject: [PATCH 4/9] Update lib.rs --- plugins/os/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/os/src/lib.rs b/plugins/os/src/lib.rs index 721432e88..e1c30b810 100644 --- a/plugins/os/src/lib.rs +++ b/plugins/os/src/lib.rs @@ -97,7 +97,7 @@ pub fn init() -> TauriPlugin { let eol = "\r\n"; #[cfg(not(windows))] let eol = "\n"; - init_script.push_str(&format!("window.__TAURI_OS__.EOL = '{eol}'")); + init_script.push_str(&format!("window.__TAURI_OS__.EOL = '{eol}';")); Builder::new("os") .js_init_script(init_script) From ddf982ae051389542d029d37eeb0a2b686a712ac Mon Sep 17 00:00:00 2001 From: amrbashir Date: Thu, 8 Jun 2023 03:43:52 +0300 Subject: [PATCH 5/9] fix init script --- plugins/os/guest-js/index.ts | 6 +++--- plugins/os/src/api-iife.js | 2 +- plugins/os/src/init.js | 9 +++++++++ plugins/os/src/lib.rs | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 plugins/os/src/init.js diff --git a/plugins/os/guest-js/index.ts b/plugins/os/guest-js/index.ts index 7104c03ad..5c85b579a 100644 --- a/plugins/os/guest-js/index.ts +++ b/plugins/os/guest-js/index.ts @@ -11,8 +11,8 @@ declare global { interface Window { __TAURI_INVOKE__: (cmd: string, args?: unknown) => Promise; - __TAURI_OS__: { - EOL: string; + __TAURI__: { + os: { __eol: string }; }; } } @@ -52,7 +52,7 @@ type Arch = * @since 2.0.0 * */ function eol() { - return window.__TAURI_OS__.EOL; + return window.__TAURI__.os.__eol; } /** diff --git a/plugins/os/src/api-iife.js b/plugins/os/src/api-iife.js index e47a5aaad..98ed20a75 100644 --- a/plugins/os/src/api-iife.js +++ b/plugins/os/src/api-iife.js @@ -1 +1 @@ -if("__TAURI__"in window){var __TAURI_OS__=function(n){"use strict";return n.arch=async function(){return window.__TAURI_INVOKE__("plugin:os|arch")},n.eol=function(){return window.__TAURI_OS__.EOL},n.exeExtension=async function(){return window.__TAURI_INVOKE__("plugin:os|exe_extension")},n.family=async function(){return window.__TAURI_INVOKE__("plugin:os|family")},n.hostname=async function(){return window.__TAURI_INVOKE__("plugin:os|hostname")},n.locale=async function(){return window.__TAURI_INVOKE__("plugin:os|locale")},n.platform=async function(){return window.__TAURI_INVOKE__("plugin:os|platform")},n.type=async function(){return window.__TAURI_INVOKE__("plugin:os|os_type")},n.version=async function(){return window.__TAURI_INVOKE__("plugin:os|version")},n}({});Object.defineProperty(window.__TAURI__,"os",{value:__TAURI_OS__})} +if("__TAURI__"in window){var __TAURI_OS__=function(n){"use strict";return n.arch=async function(){return window.__TAURI_INVOKE__("plugin:os|arch")},n.eol=function(){return window.__TAURI__.os.__eol},n.exeExtension=async function(){return window.__TAURI_INVOKE__("plugin:os|exe_extension")},n.family=async function(){return window.__TAURI_INVOKE__("plugin:os|family")},n.hostname=async function(){return window.__TAURI_INVOKE__("plugin:os|hostname")},n.locale=async function(){return window.__TAURI_INVOKE__("plugin:os|locale")},n.platform=async function(){return window.__TAURI_INVOKE__("plugin:os|platform")},n.type=async function(){return window.__TAURI_INVOKE__("plugin:os|os_type")},n.version=async function(){return window.__TAURI_INVOKE__("plugin:os|version")},n}({});Object.defineProperty(window.__TAURI__,"os",{value:__TAURI_OS__})} diff --git a/plugins/os/src/init.js b/plugins/os/src/init.js new file mode 100644 index 000000000..9a22992f2 --- /dev/null +++ b/plugins/os/src/init.js @@ -0,0 +1,9 @@ +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +if (!("os" in window.__TAURI__)) { + window.__TAURI__.os = {}; +} + +window.__TAURI__.os.__eol = __TEMPLATE_eol__; diff --git a/plugins/os/src/lib.rs b/plugins/os/src/lib.rs index e1c30b810..33c8fa937 100644 --- a/plugins/os/src/lib.rs +++ b/plugins/os/src/lib.rs @@ -97,7 +97,7 @@ pub fn init() -> TauriPlugin { let eol = "\r\n"; #[cfg(not(windows))] let eol = "\n"; - init_script.push_str(&format!("window.__TAURI_OS__.EOL = '{eol}';")); + init_script.push_str(&include_str!("init.js").replace("__TEMPLATE_eol__", eol)); Builder::new("os") .js_init_script(init_script) From 62aa234e6343b262ef6c547225a30aed65bbf539 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Thu, 8 Jun 2023 03:53:25 +0300 Subject: [PATCH 6/9] lint --- plugins/os/src/init.js | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/os/src/init.js b/plugins/os/src/init.js index 9a22992f2..802113692 100644 --- a/plugins/os/src/init.js +++ b/plugins/os/src/init.js @@ -6,4 +6,5 @@ if (!("os" in window.__TAURI__)) { window.__TAURI__.os = {}; } +// eslint-disable-next-line window.__TAURI__.os.__eol = __TEMPLATE_eol__; From 919fe51570010584feac431c257d5c97142ead03 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sat, 17 Jun 2023 19:08:16 -0300 Subject: [PATCH 7/9] fix init script --- Cargo.lock | 1 + plugins/os/Cargo.toml | 1 + plugins/os/src/init.js | 4 +--- plugins/os/src/lib.rs | 28 ++++++++++++++++++++-------- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d5c3a5404..4664da113 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5298,6 +5298,7 @@ dependencies = [ "os_info", "serde", "serde_json", + "serialize-to-javascript", "sys-locale", "tauri", "thiserror", diff --git a/plugins/os/Cargo.toml b/plugins/os/Cargo.toml index ef2f8a6d7..e22804105 100644 --- a/plugins/os/Cargo.toml +++ b/plugins/os/Cargo.toml @@ -15,3 +15,4 @@ thiserror = { workspace = true } os_info = "3" sys-locale = "0.3" gethostname = "0.4" +serialize-to-javascript = "=0.1.1" diff --git a/plugins/os/src/init.js b/plugins/os/src/init.js index 802113692..596974a5a 100644 --- a/plugins/os/src/init.js +++ b/plugins/os/src/init.js @@ -2,9 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT -if (!("os" in window.__TAURI__)) { - window.__TAURI__.os = {}; -} +__RAW_global_os_api__ // eslint-disable-next-line window.__TAURI__.os.__eol = __TEMPLATE_eol__; diff --git a/plugins/os/src/lib.rs b/plugins/os/src/lib.rs index 33c8fa937..c66580d12 100644 --- a/plugins/os/src/lib.rs +++ b/plugins/os/src/lib.rs @@ -5,6 +5,7 @@ use std::fmt::Display; pub use os_info::Version; +use serialize_to_javascript::{default_template, DefaultTemplate, Template}; use tauri::{ plugin::{Builder, TauriPlugin}, Runtime, @@ -90,17 +91,28 @@ pub fn hostname() -> String { gethostname::gethostname().to_string_lossy().to_string() } +#[derive(Template)] +#[default_template("./init.js")] +struct InitJavascript { + #[raw] + global_os_api: &'static str, + eol: &'static str, +} + pub fn init() -> TauriPlugin { - let mut init_script = String::new(); - init_script.push_str(include_str!("api-iife.js")); - #[cfg(windows)] - let eol = "\r\n"; - #[cfg(not(windows))] - let eol = "\n"; - init_script.push_str(&include_str!("init.js").replace("__TEMPLATE_eol__", eol)); + let init_js = InitJavascript { + global_os_api: include_str!("api-iife.js"), + #[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(); Builder::new("os") - .js_init_script(init_script) + .js_init_script(init_js.to_string()) .invoke_handler(tauri::generate_handler![ commands::platform, commands::version, From fceb1f8533be9701f73f997912fc161ed0832c58 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sun, 18 Jun 2023 07:20:00 -0300 Subject: [PATCH 8/9] eslint ignore init.js --- .eslintignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.eslintignore b/.eslintignore index efef8f77c..329feacba 100644 --- a/.eslintignore +++ b/.eslintignore @@ -2,4 +2,5 @@ target node_modules dist dist-js -api-iife.js \ No newline at end of file +api-iife.js +init.js \ No newline at end of file From b00bbd735022e310987ebdd6e6c2da80cbacfdf5 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sun, 18 Jun 2023 07:20:06 -0300 Subject: [PATCH 9/9] fmt --- plugins/os/src/init.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/os/src/init.js b/plugins/os/src/init.js index 596974a5a..33e42748c 100644 --- a/plugins/os/src/init.js +++ b/plugins/os/src/init.js @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT -__RAW_global_os_api__ +__RAW_global_os_api__; // eslint-disable-next-line window.__TAURI__.os.__eol = __TEMPLATE_eol__;