Skip to content

Commit

Permalink
Support Git Bash on Windows (#501)
Browse files Browse the repository at this point in the history
* Correct Windows conditional compilation.

* Launch Git Bash on Windows.

---------

Co-authored-by: Huw Walters <[email protected]>
  • Loading branch information
hdwalters and Huw Walters authored Oct 3, 2024
1 parent 49c8941 commit 27569c4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
54 changes: 38 additions & 16 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -247,13 +247,13 @@ impl AmberCompiler {
}

pub fn execute(code: String, flags: &[String]) -> Result<ExitStatus, std::io::Error> {
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> {
Expand All @@ -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<String, Message> {
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<Command> {
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<Command> {
let mut command = Command::new("/usr/bin/env");
command.arg("bash");
Some(command)
}
}
8 changes: 3 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,13 @@ fn handle_docs(cli: Cli) -> Result<(), Box<dyn Error>> {
}
}

/*
#[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);
Expand Down
7 changes: 5 additions & 2 deletions src/tests/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::env;
use std::fs;
#[cfg(unix)]
use std::{
env,
fs::{self, Permissions},
fs::Permissions,
os::unix::fs::PermissionsExt,
};

Expand All @@ -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");
}
Expand Down

0 comments on commit 27569c4

Please sign in to comment.