From 84a55da1ceea6559339cfb2d2866f40e6df32213 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sat, 22 Apr 2017 21:39:29 -0700 Subject: [PATCH] Make shebangs work on windows (#185) We use EXEPATH, which points to the root of the MinGW installation and can be used as a base for translating the unix path to the executable in the shebang line. If we're not on MinGW, well, we just throw up our hands and hope for the best. --- src/lib.rs | 2 +- src/platform.rs | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f45c5a5..200a1b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ mod prelude { pub use std::io::prelude::*; pub use libc::{EXIT_FAILURE, EXIT_SUCCESS}; pub use regex::Regex; - pub use std::path::Path; + pub use std::path::{Path, PathBuf}; pub use std::{cmp, env, fs, fmt, io, iter, process}; } diff --git a/src/platform.rs b/src/platform.rs index 007ce12..11f7f0e 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -44,7 +44,22 @@ impl PlatformInterface for Platform { #[cfg(windows)] impl PlatformInterface for Platform { fn make_shebang_command(path: &Path, command: &str, argument: Option<&str>) -> process::Command { - let mut cmd = process::Command::new(command); + let mut cmd = match env::var_os("EXEPATH") { + Some(exepath) => { + // On MinGW, `EXEPATH` is the root of the installation. Use it to + // construct a full windows path to the binary in the shebang line. + let mut translated_command = PathBuf::from(exepath); + for part in command.split("/") { + translated_command.push(part); + } + process::Command::new(translated_command) + } + None => { + // We're not on MinGW >_< The path in the shebang might be a windows + // path, in which case it'll work, so just use it and hope for the best. + process::Command::new(command) + } + }; if let Some(argument) = argument { cmd.arg(argument); }