From 071e4387453b7ae736ac459098a112fc7ca9c84c Mon Sep 17 00:00:00 2001 From: wheatfox Date: Fri, 20 Dec 2024 22:43:24 +0800 Subject: [PATCH] Rewrite get_os_distro() --- compiler-cli/src/fs.rs | 37 ++++++++++++++++---------------- compiler-core/src/error.rs | 26 ++++++++++------------ compiler-core/src/error/tests.rs | 4 ++-- 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/compiler-cli/src/fs.rs b/compiler-cli/src/fs.rs index f8836a3cb39..9ac6e75cbce 100644 --- a/compiler-cli/src/fs.rs +++ b/compiler-cli/src/fs.rs @@ -56,29 +56,30 @@ pub fn get_project_root(path: Utf8PathBuf) -> Result { #[inline] pub fn get_os() -> OS { - OS::from(std::env::consts::OS) + gleam_core::error::str2os(std::env::consts::OS) } // Return the distro enum if /etc/os-release exists, otherwise return Other pub fn get_os_distro() -> Distro { - if let OS::Linux = get_os() { - let os_release = std::fs::read_to_string("/etc/os-release"); - match os_release { - Ok(release) => { - let mut distro = Distro::Other; - for line in release.lines() { - if line.starts_with("ID=") { - let distro_id = line.split('=').nth(1).unwrap_or("other"); - distro = Distro::from(distro_id); - break; - } - } - distro - } - Err(_) => Distro::Other, - } + let path = Utf8Path::new("/etc/os-release"); + if get_os() != OS::Linux || !path.exists() { + return Distro::Other; + } + let os_release = read(&path); + if os_release.is_err() { + return Distro::Other; + } + let os_release = os_release.unwrap_or_default(); + let distro = os_release.lines().find(|line| line.starts_with("ID=")); + if let Some(distro) = distro { + let id = distro + .split('=') + .nth(1) + .unwrap_or("other") + .replace("\"", ""); + return gleam_core::error::str2distro(&id); } else { - Distro::Other + return Distro::Other; } } diff --git a/compiler-core/src/error.rs b/compiler-core/src/error.rs index 65d192cf440..ee0fca00abd 100644 --- a/compiler-core/src/error.rs +++ b/compiler-core/src/error.rs @@ -359,14 +359,12 @@ pub enum OS { Other, } -impl From<&str> for OS { - fn from(env_os: &str) -> Self { - match env_os { - "linux" => OS::Linux, - "macos" => OS::MacOS, - "windows" => OS::Windows, - _ => OS::Other, - } +pub fn str2os(env_os: &str) -> OS { + match env_os { + "linux" => OS::Linux, + "macos" => OS::MacOS, + "windows" => OS::Windows, + _ => OS::Other, } } @@ -377,13 +375,11 @@ pub enum Distro { Other, } -impl From<&str> for Distro { - fn from(distro_id: &str) -> Self { - match distro_id { - "ubuntu" => Distro::Ubuntu, - "debian" => Distro::Debian, - _ => Distro::Other, - } +pub fn str2distro(distro_id: &str) -> Distro { + match distro_id { + "ubuntu" => Distro::Ubuntu, + "debian" => Distro::Debian, + _ => Distro::Other, } } diff --git a/compiler-core/src/error/tests.rs b/compiler-core/src/error/tests.rs index 3900c7f1e0a..3c5a1ea2f71 100644 --- a/compiler-core/src/error/tests.rs +++ b/compiler-core/src/error/tests.rs @@ -9,7 +9,7 @@ fn test_shell_program_not_found_error() { for cmd in &cmds { for os in &oses { - let os_enum = OS::from(*os); + let os_enum = str2os(os); match os_enum { OS::MacOS | OS::Windows => { let err = Error::ShellProgramNotFound { @@ -25,7 +25,7 @@ fn test_shell_program_not_found_error() { } OS::Linux => { for distro in &distros { - let distro_enum = Distro::from(*distro); + let distro_enum = str2distro(distro); let err = Error::ShellProgramNotFound { program: cmd.to_string(), os: os_enum,