tauri_plugin_os/
lib.rs

1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5//! Read information about the operating system.
6
7#![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
46/// Returns a string describing the specific operating system in use, see [std::env::consts::OS].
47pub fn platform() -> &'static str {
48    std::env::consts::OS
49}
50
51/// Returns the current operating system version.
52pub fn version() -> Version {
53    os_info::get().version().clone()
54}
55
56/// Returns the current operating system type.
57pub 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
76/// Returns the current operating system family, see [std::env::consts::FAMILY].
77pub fn family() -> &'static str {
78    std::env::consts::FAMILY
79}
80
81/// Returns the current operating system architecture, see [std::env::consts::ARCH].
82pub fn arch() -> &'static str {
83    std::env::consts::ARCH
84}
85
86/// Returns the file extension, if any, used for executable binaries on this platform. Example value is `exe`, see [std::env::consts::EXE_EXTENSION].
87pub fn exe_extension() -> &'static str {
88    std::env::consts::EXE_EXTENSION
89}
90
91/// Returns the current operating system locale with the `BCP-47` language tag. If the locale couldn't be obtained, `None` is returned instead.
92pub fn locale() -> Option<String> {
93    sys_locale::get_locale()
94}
95
96/// Returns the current operating system hostname.
97pub 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        // this will never fail with the above global_os_api values
134        .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}