2018-10-09 20:21:06 +02:00
|
|
|
use std::env;
|
|
|
|
use std::process::Command;
|
|
|
|
use std::process::Stdio;
|
|
|
|
use std::path::Path;
|
2018-10-15 02:25:19 +02:00
|
|
|
use std::io;
|
2018-10-09 20:21:06 +02:00
|
|
|
|
|
|
|
#[cfg(target_family = "windows")]
|
|
|
|
const PATH_SEPARATOR: char = ';';
|
|
|
|
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
const PATH_SEPARATOR: char = ':';
|
|
|
|
|
2018-10-15 02:25:19 +02:00
|
|
|
pub fn execute(target: &Path) -> io::Result<i32> {
|
|
|
|
let target_dir = target.parent()
|
|
|
|
.and_then(|dir| dir.to_str())
|
|
|
|
.expect("Unable to construct target directory");
|
|
|
|
let target_file_name = target.file_name()
|
|
|
|
.and_then(|file_name| file_name.to_str())
|
|
|
|
.expect("Unable to identify target file name");
|
|
|
|
trace!("target={:?}", target);
|
|
|
|
trace!("target_dir={:?}", target_dir);
|
|
|
|
trace!("target_file_name={:?}", target_file_name);
|
|
|
|
|
|
|
|
let current_path_env = env::var("PATH").unwrap_or(String::new());
|
|
|
|
let path_env = format!("{}{}{}", target_dir, PATH_SEPARATOR, current_path_env);
|
|
|
|
trace!("path_env={:?}", path_env);
|
2018-10-09 20:21:06 +02:00
|
|
|
|
2018-10-12 04:17:31 +02:00
|
|
|
let args: Vec<String> = env::args().skip(1).collect();
|
2018-10-15 02:25:19 +02:00
|
|
|
trace!("args={:?}", args);
|
|
|
|
|
|
|
|
Ok(Command::new(target_file_name)
|
2018-10-09 20:21:06 +02:00
|
|
|
.env("PATH", path_env)
|
|
|
|
.args(args)
|
|
|
|
.stdin(Stdio::inherit())
|
|
|
|
.stdout(Stdio::inherit())
|
|
|
|
.stderr(Stdio::inherit())
|
2018-10-15 02:25:19 +02:00
|
|
|
.spawn()?
|
|
|
|
.wait()?
|
|
|
|
.code().unwrap_or(1))
|
2018-10-09 20:21:06 +02:00
|
|
|
}
|