From 919fe51570010584feac431c257d5c97142ead03 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sat, 17 Jun 2023 19:08:16 -0300 Subject: [PATCH] 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 d5c3a540..4664da11 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 ef2f8a6d..e2280410 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 80211369..596974a5 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 33c8fa93..c66580d1 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,