Skip to main content

Mountain/IPC/WindServiceHandlers/NativeHost/
OSProperties.rs

1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3//! Wire method: `nativeHost:getOSProperties` (cross-platform).
4//! Returns Electron-shaped `{ type, release, arch, platform, cpus }` tuple
5//! so VS Code's `os` polyfill can continue using the same surface.
6
7use serde_json::{Value, json};
8
9pub async fn NativeOSProperties() -> Result<Value, String> {
10	use sysinfo::System;
11
12	let OsType = match std::env::consts::OS {
13		"macos" => "Darwin",
14		"windows" => "Windows_NT",
15		"linux" => "Linux",
16		_ => std::env::consts::OS,
17	};
18
19	let Release = {
20		#[cfg(target_os = "macos")]
21		{
22			std::process::Command::new("sw_vers")
23				.arg("-productVersion")
24				.output()
25				.ok()
26				.map(|O| String::from_utf8_lossy(&O.stdout).trim().to_string())
27				.unwrap_or_else(|| "14.0".to_string())
28		}
29		#[cfg(target_os = "windows")]
30		{
31			std::process::Command::new("cmd")
32				.args(["/c", "ver"])
33				.output()
34				.ok()
35				.map(|O| {
36					let Output = String::from_utf8_lossy(&O.stdout);
37					Output
38						.split('[')
39						.nth(1)
40						.and_then(|S| S.split(']').next())
41						.and_then(|S| S.strip_prefix("Version "))
42						.unwrap_or("10.0.0")
43						.to_string()
44				})
45				.unwrap_or_else(|| "10.0.0".to_string())
46		}
47		#[cfg(target_os = "linux")]
48		{
49			std::process::Command::new("uname")
50				.arg("-r")
51				.output()
52				.ok()
53				.map(|O| String::from_utf8_lossy(&O.stdout).trim().to_string())
54				.unwrap_or_else(|| "6.1.0".to_string())
55		}
56		#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
57		{
58			"0.0.0".to_string()
59		}
60	};
61
62	let mut Sys = System::new();
63	Sys.refresh_cpu_all();
64	let Cpus:Vec<Value> = Sys
65		.cpus()
66		.iter()
67		.map(|Cpu| {
68			json!({
69				"model": Cpu.brand(),
70				"speed": Cpu.frequency()
71			})
72		})
73		.collect();
74
75	Ok(json!({
76		"type": OsType,
77		"release": Release,
78		"arch": std::env::consts::ARCH,
79		"platform": std::env::consts::OS,
80		"cpus": Cpus
81	}))
82}