2018-10-09 20:21:06 +02:00
|
|
|
use std::env;
|
2018-10-15 18:44:54 +02:00
|
|
|
use std::io;
|
2019-07-12 15:38:18 +02:00
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
use std::fs;
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
use std::fs::Permissions;
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
2018-10-15 18:44:54 +02:00
|
|
|
use std::path::Path;
|
2018-10-09 20:21:06 +02:00
|
|
|
use std::process::Command;
|
|
|
|
use std::process::Stdio;
|
|
|
|
|
2018-10-15 02:25:19 +02:00
|
|
|
pub fn execute(target: &Path) -> io::Result<i32> {
|
|
|
|
trace!("target={:?}", target);
|
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);
|
|
|
|
|
2018-10-15 18:44:54 +02:00
|
|
|
do_execute(target, &args)
|
2018-10-15 17:38:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 15:38:18 +02:00
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
fn ensure_executable(target: &Path) {
|
|
|
|
let perms = Permissions::from_mode(0o770);
|
|
|
|
fs::set_permissions(target, perms).unwrap();
|
|
|
|
}
|
|
|
|
|
2018-10-15 17:38:17 +02:00
|
|
|
#[cfg(target_family = "unix")]
|
2018-10-15 18:44:54 +02:00
|
|
|
fn do_execute(target: &Path, args: &[String]) -> io::Result<i32> {
|
2019-07-12 15:38:18 +02:00
|
|
|
ensure_executable(target);
|
|
|
|
|
2018-10-15 17:38:17 +02:00
|
|
|
Ok(Command::new(target)
|
2018-10-09 20:21:06 +02:00
|
|
|
.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
|
|
|
}
|
2018-10-15 17:38:17 +02:00
|
|
|
|
|
|
|
#[cfg(target_family = "windows")]
|
2018-10-15 18:44:54 +02:00
|
|
|
fn is_script(target: &Path) -> bool {
|
|
|
|
const SCRIPT_EXTENSIONS: &[&str] = &["bat", "cmd"];
|
|
|
|
SCRIPT_EXTENSIONS.contains(
|
|
|
|
&target.extension()
|
|
|
|
.unwrap_or_default()
|
|
|
|
.to_string_lossy()
|
|
|
|
.to_lowercase().as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_family = "windows")]
|
|
|
|
fn do_execute(target: &Path, args: &[String]) -> io::Result<i32> {
|
|
|
|
let target_str = target.as_os_str().to_str().unwrap();
|
|
|
|
|
|
|
|
if is_script(target) {
|
|
|
|
let mut cmd_args = Vec::with_capacity(args.len() + 2);
|
|
|
|
cmd_args.push("/c".to_string());
|
|
|
|
cmd_args.push(target_str.to_string());
|
|
|
|
cmd_args.extend_from_slice(&args);
|
|
|
|
|
|
|
|
Ok(Command::new("cmd")
|
|
|
|
.args(cmd_args)
|
|
|
|
.stdin(Stdio::inherit())
|
|
|
|
.stdout(Stdio::inherit())
|
|
|
|
.stderr(Stdio::inherit())
|
|
|
|
.spawn()?
|
|
|
|
.wait()?
|
|
|
|
.code().unwrap_or(1))
|
|
|
|
} else {
|
|
|
|
Ok(Command::new(target)
|
|
|
|
.args(args)
|
|
|
|
.stdin(Stdio::inherit())
|
|
|
|
.stdout(Stdio::inherit())
|
|
|
|
.stderr(Stdio::inherit())
|
|
|
|
.spawn()?
|
|
|
|
.wait()?
|
|
|
|
.code().unwrap_or(1))
|
|
|
|
}
|
2018-10-15 17:38:17 +02:00
|
|
|
}
|