| |
| |
| |
|
|
| |
|
|
| #![doc( |
| html_logo_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png", |
| html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png" |
| )] |
|
|
| use std::fmt::Display; |
|
|
| pub use os_info::Version; |
| use serialize_to_javascript::{default_template, DefaultTemplate, Template}; |
| use tauri::{ |
| plugin::{Builder, TauriPlugin}, |
| Runtime, |
| }; |
|
|
| mod commands; |
| mod error; |
|
|
| pub use error::Error; |
|
|
| pub enum OsType { |
| Linux, |
| Windows, |
| Macos, |
| IOS, |
| Android, |
| } |
|
|
| impl Display for OsType { |
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| match self { |
| Self::Linux => write!(f, "linux"), |
| Self::Windows => write!(f, "windows"), |
| Self::Macos => write!(f, "macos"), |
| Self::IOS => write!(f, "ios"), |
| Self::Android => write!(f, "android"), |
| } |
| } |
| } |
|
|
| |
| pub fn platform() -> &'static str { |
| std::env::consts::OS |
| } |
|
|
| |
| pub fn version() -> Version { |
| os_info::get().version().clone() |
| } |
|
|
| |
| pub fn type_() -> OsType { |
| #[cfg(any( |
| target_os = "linux", |
| target_os = "dragonfly", |
| target_os = "freebsd", |
| target_os = "netbsd", |
| target_os = "openbsd" |
| ))] |
| return OsType::Linux; |
| #[cfg(target_os = "windows")] |
| return OsType::Windows; |
| #[cfg(target_os = "macos")] |
| return OsType::Macos; |
| #[cfg(target_os = "ios")] |
| return OsType::IOS; |
| #[cfg(target_os = "android")] |
| return OsType::Android; |
| } |
|
|
| |
| pub fn family() -> &'static str { |
| std::env::consts::FAMILY |
| } |
|
|
| |
| pub fn arch() -> &'static str { |
| std::env::consts::ARCH |
| } |
|
|
| |
| pub fn exe_extension() -> &'static str { |
| std::env::consts::EXE_EXTENSION |
| } |
|
|
| |
| pub fn locale() -> Option<String> { |
| sys_locale::get_locale() |
| } |
|
|
| |
| pub fn hostname() -> String { |
| gethostname::gethostname().to_string_lossy().to_string() |
| } |
|
|
| #[derive(Template)] |
| #[default_template("./init.js")] |
| struct InitJavascript<'a> { |
| eol: &'static str, |
| os_type: String, |
| platform: &'a str, |
| family: &'a str, |
| version: String, |
| arch: &'a str, |
| exe_extension: &'a str, |
| } |
|
|
| impl InitJavascript<'_> { |
| 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(), |
| } |
| } |
| } |
|
|
| pub fn init<R: Runtime>() -> TauriPlugin<R> { |
| let init_js = InitJavascript::new() |
| .render_default(&Default::default()) |
| |
| .unwrap(); |
|
|
| Builder::new("os") |
| .js_init_script(init_js.to_string()) |
| .invoke_handler(tauri::generate_handler![ |
| commands::locale, |
| commands::hostname |
| ]) |
| .build() |
| } |
|
|