1#![doc(
8 html_logo_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png",
9 html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
10)]
11
12use std::fmt::Display;
13
14pub use os_info::Version;
15use serialize_to_javascript::{default_template, DefaultTemplate, Template};
16use tauri::{
17 plugin::{Builder, TauriPlugin},
18 Runtime,
19};
20
21mod commands;
22mod error;
23
24pub use error::Error;
25
26pub enum OsType {
27 Linux,
28 Windows,
29 Macos,
30 IOS,
31 Android,
32}
33
34impl Display for OsType {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 match self {
37 Self::Linux => write!(f, "linux"),
38 Self::Windows => write!(f, "windows"),
39 Self::Macos => write!(f, "macos"),
40 Self::IOS => write!(f, "ios"),
41 Self::Android => write!(f, "android"),
42 }
43 }
44}
45
46pub fn platform() -> &'static str {
48 std::env::consts::OS
49}
50
51pub fn version() -> Version {
53 os_info::get().version().clone()
54}
55
56pub fn type_() -> OsType {
58 #[cfg(any(
59 target_os = "linux",
60 target_os = "dragonfly",
61 target_os = "freebsd",
62 target_os = "netbsd",
63 target_os = "openbsd"
64 ))]
65 return OsType::Linux;
66 #[cfg(target_os = "windows")]
67 return OsType::Windows;
68 #[cfg(target_os = "macos")]
69 return OsType::Macos;
70 #[cfg(target_os = "ios")]
71 return OsType::IOS;
72 #[cfg(target_os = "android")]
73 return OsType::Android;
74}
75
76pub fn family() -> &'static str {
78 std::env::consts::FAMILY
79}
80
81pub fn arch() -> &'static str {
83 std::env::consts::ARCH
84}
85
86pub fn exe_extension() -> &'static str {
88 std::env::consts::EXE_EXTENSION
89}
90
91pub fn locale() -> Option<String> {
93 sys_locale::get_locale()
94}
95
96pub fn hostname() -> String {
98 gethostname::gethostname().to_string_lossy().to_string()
99}
100
101#[derive(Template)]
102#[default_template("./init.js")]
103struct InitJavascript<'a> {
104 eol: &'static str,
105 os_type: String,
106 platform: &'a str,
107 family: &'a str,
108 version: String,
109 arch: &'a str,
110 exe_extension: &'a str,
111}
112
113impl InitJavascript<'_> {
114 fn new() -> Self {
115 Self {
116 #[cfg(windows)]
117 eol: "\r\n",
118 #[cfg(not(windows))]
119 eol: "\n",
120 os_type: crate::type_().to_string(),
121 platform: crate::platform(),
122 family: crate::family(),
123 version: crate::version().to_string(),
124 arch: crate::arch(),
125 exe_extension: crate::exe_extension(),
126 }
127 }
128}
129
130pub fn init<R: Runtime>() -> TauriPlugin<R> {
131 let init_js = InitJavascript::new()
132 .render_default(&Default::default())
133 .unwrap();
135
136 Builder::new("os")
137 .js_init_script(init_js.to_string())
138 .invoke_handler(tauri::generate_handler![
139 commands::locale,
140 commands::hostname
141 ])
142 .build()
143}