fix init script

pull/427/head
Lucas Nogueira 2 years ago
parent 263b74b1b7
commit 919fe51570
No known key found for this signature in database
GPG Key ID: FFEA6C72E73482F1

1
Cargo.lock generated

@ -5298,6 +5298,7 @@ dependencies = [
"os_info",
"serde",
"serde_json",
"serialize-to-javascript",
"sys-locale",
"tauri",
"thiserror",

@ -15,3 +15,4 @@ thiserror = { workspace = true }
os_info = "3"
sys-locale = "0.3"
gethostname = "0.4"
serialize-to-javascript = "=0.1.1"

@ -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__;

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

Loading…
Cancel
Save