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
15		"windows" => "Windows_NT",
16
17		"linux" => "Linux",
18
19		_ => std::env::consts::OS,
20	};
21
22	let Release = {
23		#[cfg(target_os = "macos")]
24		{
25			std::process::Command::new("sw_vers")
26				.arg("-productVersion")
27				.output()
28				.ok()
29				.map(|O| String::from_utf8_lossy(&O.stdout).trim().to_string())
30				.unwrap_or_else(|| "14.0".to_string())
31		}
32
33		#[cfg(target_os = "windows")]
34		{
35			std::process::Command::new("cmd")
36				.args(["/c", "ver"])
37				.output()
38				.ok()
39				.map(|O| {
40					let Output = String::from_utf8_lossy(&O.stdout);
41					Output
42						.split('[')
43						.nth(1)
44						.and_then(|S| S.split(']').next())
45						.and_then(|S| S.strip_prefix("Version "))
46						.unwrap_or("10.0.0")
47						.to_string()
48				})
49				.unwrap_or_else(|| "10.0.0".to_string())
50		}
51
52		#[cfg(target_os = "linux")]
53		{
54			std::process::Command::new("uname")
55				.arg("-r")
56				.output()
57				.ok()
58				.map(|O| String::from_utf8_lossy(&O.stdout).trim().to_string())
59				.unwrap_or_else(|| "6.1.0".to_string())
60		}
61
62		#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
63		{
64			"0.0.0".to_string()
65		}
66	};
67
68	let mut Sys = System::new();
69
70	Sys.refresh_cpu_all();
71
72	let Cpus:Vec<Value> = Sys
73		.cpus()
74		.iter()
75		.map(|Cpu| {
76			json!({
77				"model": Cpu.brand(),
78				"speed": Cpu.frequency()
79			})
80		})
81		.collect();
82
83	Ok(json!({
84		"type": OsType,
85		"release": Release,
86		"arch": std::env::consts::ARCH,
87		"platform": std::env::consts::OS,
88		"cpus": Cpus
89	}))
90}