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.
This commit is contained in:
Casey Rodarmor 2017-04-22 21:39:29 -07:00 committed by GitHub
parent 6a0e3abb32
commit 84a55da1ce
2 changed files with 17 additions and 2 deletions

View File

@ -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};
}

View File

@ -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);
}