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::{DefaultTemplate, Template, default_template};
16use tauri::{
17 Runtime,
18 plugin::{Builder, TauriPlugin},
19};
20
21mod commands;
22mod error;
23
24pub use error::Error;
25
26pub enum OsType {
28 Linux,
30 Windows,
32 Macos,
34 IOS,
36 Android,
38}
39
40impl Display for OsType {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 match self {
43 Self::Linux => write!(f, "linux"),
44 Self::Windows => write!(f, "windows"),
45 Self::Macos => write!(f, "macos"),
46 Self::IOS => write!(f, "ios"),
47 Self::Android => write!(f, "android"),
48 }
49 }
50}
51
52pub fn platform() -> &'static str {
54 std::env::consts::OS
55}
56
57pub fn version() -> Version {
59 os_info::get().version().clone()
60}
61
62pub fn type_() -> OsType {
64 #[cfg(any(
65 target_os = "linux",
66 target_os = "dragonfly",
67 target_os = "freebsd",
68 target_os = "netbsd",
69 target_os = "openbsd"
70 ))]
71 return OsType::Linux;
72 #[cfg(target_os = "windows")]
73 return OsType::Windows;
74 #[cfg(target_os = "macos")]
75 return OsType::Macos;
76 #[cfg(target_os = "ios")]
77 return OsType::IOS;
78 #[cfg(target_os = "android")]
79 return OsType::Android;
80}
81
82pub fn family() -> &'static str {
84 std::env::consts::FAMILY
85}
86
87pub fn arch() -> &'static str {
89 std::env::consts::ARCH
90}
91
92pub fn exe_extension() -> &'static str {
94 std::env::consts::EXE_EXTENSION
95}
96
97pub fn locale() -> Option<String> {
99 sys_locale::get_locale()
100}
101
102pub fn hostname() -> String {
104 gethostname::gethostname().to_string_lossy().to_string()
105}
106
107#[derive(Template)]
108#[default_template("./init.js")]
109struct InitJavascript<'a> {
110 eol: &'static str,
111 os_type: String,
112 platform: &'a str,
113 family: &'a str,
114 version: String,
115 arch: &'a str,
116 exe_extension: &'a str,
117}
118
119impl InitJavascript<'_> {
120 fn new() -> Self {
121 Self {
122 #[cfg(windows)]
123 eol: "\r\n",
124 #[cfg(not(windows))]
125 eol: "\n",
126 os_type: crate::type_().to_string(),
127 platform: crate::platform(),
128 family: crate::family(),
129 version: crate::version().to_string(),
130 arch: crate::arch(),
131 exe_extension: crate::exe_extension(),
132 }
133 }
134}
135
136pub fn init<R: Runtime>() -> TauriPlugin<R> {
138 let init_js = InitJavascript::new()
139 .render_default(&Default::default())
140 .unwrap();
142
143 Builder::new("os")
144 .initialization_script(init_js.to_string())
145 .invoke_handler(tauri::generate_handler![
146 commands::locale,
147 commands::hostname
148 ])
149 .build()
150}