Skip to content

Commit

Permalink
Rewrite get_os_distro(), get_os() and OS enum struct
Browse files Browse the repository at this point in the history
  • Loading branch information
enkerewpo committed Dec 20, 2024
1 parent 370f819 commit 37a443a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 77 deletions.
5 changes: 2 additions & 3 deletions compiler-cli/src/beam_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use gleam_core::{
paths, Result,
};

use crate::fs::get_os;

use std::{
collections::HashSet,
io::{self, BufRead, BufReader, Write},
Expand All @@ -13,8 +15,6 @@ use std::{
use camino::{Utf8Path, Utf8PathBuf};
use itertools::Itertools;

use crate::fs::{get_os, get_os_distro};

#[derive(Debug)]
struct BeamCompilerInner {
process: Child,
Expand Down Expand Up @@ -114,7 +114,6 @@ impl BeamCompiler {
io::ErrorKind::NotFound => Error::ShellProgramNotFound {
program: "escript".into(),
os: get_os(),
distro: get_os_distro(),
},
other => Error::ShellCommand {
program: "escript".into(),
Expand Down
55 changes: 30 additions & 25 deletions compiler-cli/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use gleam_core::{
build::{NullTelemetry, Target},
error::{Distro, Error, FileIoAction, FileKind, OS},
error::{parse_linux_distribution, Distro, Error, FileIoAction, FileKind, OS},
io::{
BeamCompiler, CommandExecutor, Content, DirEntry, FileSystemReader, FileSystemWriter,
OutputFile, ReadDir, Stdio, WrappedReader,
Expand Down Expand Up @@ -54,32 +54,38 @@ pub fn get_project_root(path: Utf8PathBuf) -> Result<Utf8PathBuf, Error> {
})
}

#[inline]
pub fn get_os() -> OS {
OS::from(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,
match std::env::consts::OS {
"macos" => OS::MacOS,
"windows" => OS::Windows,
"linux" => {
let distro = get_linux_distribution();
OS::Linux(distro)
}
} else {
Distro::Other
_ => OS::Other,
}
}

pub fn get_linux_distribution() -> Distro {
let path = Utf8Path::new("/etc/os-release");
if std::env::consts::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 parse_linux_distribution(&id);
}
Distro::Other
}

/// A `FileWriter` implementation that writes to the file system.
Expand Down Expand Up @@ -212,7 +218,6 @@ impl CommandExecutor for ProjectIO {
io::ErrorKind::NotFound => Error::ShellProgramNotFound {
program: program.to_string(),
os: get_os(),
distro: get_os_distro(),
},

other => Error::ShellCommand {
Expand Down
45 changes: 18 additions & 27 deletions compiler-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,7 @@ pub enum Error {
Gzip(String),

#[error("shell program `{program}` not found")]
ShellProgramNotFound {
program: String,
os: OS,
distro: Distro,
},
ShellProgramNotFound { program: String, os: OS },

#[error("shell program `{program}` failed")]
ShellCommand {
Expand Down Expand Up @@ -350,40 +346,35 @@ impl SmallVersion {
}
}
}

#[derive(Debug, Clone, Eq, PartialEq, Copy)]
pub enum OS {
Linux,
Linux(Distro),
MacOS,
Windows,
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,
}
}
}

#[derive(Debug, Clone, Eq, PartialEq, Copy)]
pub enum Distro {
Ubuntu,
Debian,
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 parse_os(os: &str, distro: &str) -> OS {
match os {
"macos" => OS::MacOS,
"windows" => OS::Windows,
"linux" => OS::Linux(parse_linux_distribution(distro)),
_ => OS::Other,
}
}

pub fn parse_linux_distribution(distro: &str) -> Distro {
match distro {
"ubuntu" => Distro::Ubuntu,
"debian" => Distro::Debian,
_ => Distro::Other,
}
}

Expand Down Expand Up @@ -1060,7 +1051,7 @@ your app.src file \"{app_ver}\"."
}]
}

Error::ShellProgramNotFound { program , os, distro } => {
Error::ShellProgramNotFound { program , os } => {
let mut text = format!("The program `{program}` was not found. Is it installed?\n");

match program.as_str() {
Expand Down Expand Up @@ -1117,7 +1108,7 @@ https://git-scm.com/book/en/v2/Getting-Started-Installing-Git",
_ => (),
}
}
OS::Linux => {
OS::Linux(distro) => {
fn apt_install(program: &str) -> String {
format!("\nYou can install {} via apt: sudo apt install {}", program, program)
}
Expand Down
36 changes: 14 additions & 22 deletions compiler-core/src/error/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,28 @@ fn test_shell_program_not_found_error() {

for cmd in &cmds {
for os in &oses {
let os_enum = OS::from(*os);
match os_enum {
OS::MacOS | OS::Windows => {
if os != &"linux" {
let err = Error::ShellProgramNotFound {
program: cmd.to_string(),
os: parse_os(os, "other"),
}
.to_diagnostics();
assert_snapshot!(
format!("shell_program_not_found_{cmd}_{os}_other"),
err[0].text
);
} else {
for distro in &distros {
let err = Error::ShellProgramNotFound {
program: cmd.to_string(),
os: os_enum,
distro: Distro::Other,
os: parse_os(os, distro),
}
.to_diagnostics();
assert_snapshot!(
format!("shell_program_not_found_{cmd}_{os}_other"),
format!("shell_program_not_found_{cmd}_{os}_{distro}"),
err[0].text
);
}
OS::Linux => {
for distro in &distros {
let distro_enum = Distro::from(*distro);
let err = Error::ShellProgramNotFound {
program: cmd.to_string(),
os: os_enum,
distro: distro_enum,
}
.to_diagnostics();
assert_snapshot!(
format!("shell_program_not_found_{cmd}_{os}_{distro}"),
err[0].text
);
}
}
_ => (),
}
}
}
Expand Down

0 comments on commit 37a443a

Please sign in to comment.