From 77361a038423b61f84fe73c07ff298772ae8fd5b Mon Sep 17 00:00:00 2001 From: Huw Walters Date: Thu, 26 Sep 2024 22:07:49 +0100 Subject: [PATCH 1/2] Correct Windows conditional compilation. --- src/main.rs | 8 +++----- src/tests/formatter.rs | 7 +++++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 10e6707e..adb5487e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -175,15 +175,13 @@ fn handle_docs(cli: Cli) -> Result<(), Box> { } } -/* -#[cfg(target_os = "windows")] +#[cfg(windows)] fn set_file_permission(_file: &fs::File, _output: String) { // We don't need to set permission on Windows } -*/ -#[cfg(not(target_os = "windows"))] -fn set_file_permission(file: &std::fs::File, path: String) { +#[cfg(not(windows))] +fn set_file_permission(file: &fs::File, path: String) { use std::os::unix::prelude::PermissionsExt; let mut perm = fs::metadata(path).unwrap().permissions(); perm.set_mode(0o755); diff --git a/src/tests/formatter.rs b/src/tests/formatter.rs index a15da212..2298082b 100644 --- a/src/tests/formatter.rs +++ b/src/tests/formatter.rs @@ -1,6 +1,8 @@ +use std::env; +use std::fs; +#[cfg(unix)] use std::{ - env, - fs::{self, Permissions}, + fs::Permissions, os::unix::fs::PermissionsExt, }; @@ -16,6 +18,7 @@ fn create_fake_binary(fmt: BashFormatter) { let name: String = fmt.as_cmd(); fs::write(&name, body).expect("Couldn't write fake script"); + #[cfg(unix)] fs::set_permissions(&name, Permissions::from_mode(0o755)) .expect("Couldn't set perms for fake script"); } From c1fc9cfecb28bc92322e0e25575c1cef9b95ea53 Mon Sep 17 00:00:00 2001 From: Huw Walters Date: Thu, 26 Sep 2024 21:54:28 +0100 Subject: [PATCH 2/2] Launch Git Bash on Windows. --- src/compiler.rs | 54 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index 406faec7..4b99d231 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -12,7 +12,7 @@ use heraclitus_compiler::prelude::*; use std::env; use std::fs; use std::fs::File; -use std::io::Write; +use std::io::{ErrorKind, Write}; use std::path::PathBuf; use std::process::{Command, ExitStatus}; use std::time::Instant; @@ -247,13 +247,13 @@ impl AmberCompiler { } pub fn execute(code: String, flags: &[String]) -> Result { - let code = format!("set -- {};\n\n{}", flags.join(" "), code); - Command::new("/usr/bin/env") - .arg("bash") - .arg("-c") - .arg(code) - .spawn()? - .wait() + if let Some(mut command) = Self::find_bash() { + let code = format!("set -- {};\n{}", flags.join(" "), code); + command.arg("-c").arg(code).spawn()?.wait() + } else { + let error = std::io::Error::new(ErrorKind::NotFound, "Failed to find Bash"); + Err(error) + } } pub fn generate_docs(&self, output: String) -> Result<(), Message> { @@ -262,16 +262,38 @@ impl AmberCompiler { .map(|(block, meta)| self.document(block, meta, output)) } - #[allow(dead_code)] + #[cfg(test)] pub fn test_eval(&mut self) -> Result { self.compile().map_or_else(Err, |(_, code)| { - let child = Command::new("/usr/bin/env") - .arg("bash") - .arg("-c") - .arg::<&str>(code.as_ref()) - .output() - .unwrap(); - Ok(String::from_utf8_lossy(&child.stdout).to_string()) + if let Some(mut command) = Self::find_bash() { + let child = command.arg("-c").arg::<&str>(code.as_ref()).output().unwrap(); + let output = String::from_utf8_lossy(&child.stdout).to_string(); + Ok(output) + } else { + let message = Message::new_err_msg("Failed to find Bash"); + Err(message) + } }) } + + #[cfg(windows)] + fn find_bash() -> Option { + if let Some(paths) = env::var_os("PATH") { + for path in env::split_paths(&paths) { + let path = path.join("bash.exe"); + if path.exists() { + let command = Command::new(path); + return Some(command); + } + } + } + return None; + } + + #[cfg(not(windows))] + fn find_bash() -> Option { + let mut command = Command::new("/usr/bin/env"); + command.arg("bash"); + Some(command) + } }