Skip to main content

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::{DefaultTemplate, Template, default_template};
16use tauri::{
17    Runtime,
18    plugin::{Builder, TauriPlugin},
19};
20
21mod commands;
22mod error;
23
24pub use error::Error;
25
26/// The type of the current operating system, as returned by [`type_`].
27pub enum OsType {
28    /// Linux and Linux-based systems such as FreeBSD, DragonFly BSD, NetBSD and OpenBSD.
29    Linux,
30    /// Windows.
31    Windows,
32    /// macOS.
33    Macos,
34    /// iOS.
35    IOS,
36    /// Android.
37    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
52/// Returns a string describing the specific operating system in use, see [std::env::consts::OS].
53pub fn platform() -> &'static str {
54    std::env::consts::OS
55}
56
57/// Returns the current operating system version.
58pub fn version() -> Version {
59    os_info::get().version().clone()
60}
61
62/// Returns the current operating system type.
63pub 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
82/// Returns the current operating system family, see [std::env::consts::FAMILY].
83pub fn family() -> &'static str {
84    std::env::consts::FAMILY
85}
86
87/// Returns the current operating system architecture, see [std::env::consts::ARCH].
88pub fn arch() -> &'static str {
89    std::env::consts::ARCH
90}
91
92/// Returns the file extension, if any, used for executable binaries on this platform. Example value is `exe`, see [std::env::consts::EXE_EXTENSION].
93pub fn exe_extension() -> &'static str {
94    std::env::consts::EXE_EXTENSION
95}
96
97/// Returns the current operating system locale with the `BCP-47` language tag. If the locale couldn't be obtained, `None` is returned instead.
98pub fn locale() -> Option<String> {
99    sys_locale::get_locale()
100}
101
102/// Returns the current operating system hostname.
103pub 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
136/// Initializes the plugin.
137pub fn init<R: Runtime>() -> TauriPlugin<R> {
138    let init_js = InitJavascript::new()
139        .render_default(&Default::default())
140        // this will never fail with the above global_os_api values
141        .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}