2019-04-11 15:23:14 -07:00
|
|
|
use crate::common::*;
|
2017-04-21 22:20:13 -07:00
|
|
|
|
|
|
|
pub struct Platform;
|
|
|
|
|
|
|
|
pub trait PlatformInterface {
|
|
|
|
/// Construct a command equivelant to running the script at `path` with the
|
|
|
|
/// shebang line `shebang`
|
2018-12-08 14:29:41 -08:00
|
|
|
fn make_shebang_command(
|
|
|
|
path: &Path,
|
|
|
|
command: &str,
|
|
|
|
argument: Option<&str>,
|
|
|
|
) -> Result<Command, brev::OutputError>;
|
2017-04-21 22:20:13 -07:00
|
|
|
|
|
|
|
/// Set the execute permission on the file pointed to by `path`
|
|
|
|
fn set_execute_permission(path: &Path) -> Result<(), io::Error>;
|
|
|
|
|
|
|
|
/// Extract the signal from a process exit status, if it was terminated by a signal
|
|
|
|
fn signal_from_exit_status(exit_status: process::ExitStatus) -> Option<i32>;
|
2018-06-19 10:04:03 -07:00
|
|
|
|
|
|
|
/// Translate a path from a "native" path to a path the interpreter expects
|
|
|
|
fn to_shell_path(path: &Path) -> Result<String, String>;
|
2017-04-21 22:20:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
impl PlatformInterface for Platform {
|
2018-12-08 14:29:41 -08:00
|
|
|
fn make_shebang_command(
|
|
|
|
path: &Path,
|
|
|
|
_command: &str,
|
|
|
|
_argument: Option<&str>,
|
|
|
|
) -> Result<Command, brev::OutputError> {
|
2017-04-21 22:20:13 -07:00
|
|
|
// shebang scripts can be executed directly on unix
|
2017-11-16 23:30:08 -08:00
|
|
|
Ok(Command::new(path))
|
2017-04-21 22:20:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set_execute_permission(path: &Path) -> Result<(), io::Error> {
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
|
|
|
|
// get current permissions
|
|
|
|
let mut permissions = fs::metadata(&path)?.permissions();
|
|
|
|
|
|
|
|
// set the execute bit
|
|
|
|
let current_mode = permissions.mode();
|
|
|
|
permissions.set_mode(current_mode | 0o100);
|
|
|
|
|
|
|
|
// set the new permissions
|
|
|
|
fs::set_permissions(&path, permissions)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signal_from_exit_status(exit_status: process::ExitStatus) -> Option<i32> {
|
|
|
|
use std::os::unix::process::ExitStatusExt;
|
|
|
|
exit_status.signal()
|
|
|
|
}
|
2018-06-19 10:04:03 -07:00
|
|
|
|
|
|
|
fn to_shell_path(path: &Path) -> Result<String, String> {
|
2018-12-08 14:29:41 -08:00
|
|
|
path
|
|
|
|
.to_str()
|
|
|
|
.map(str::to_string)
|
|
|
|
.ok_or_else(|| String::from("Error getting current directory: unicode decode error"))
|
2018-06-19 10:04:03 -07:00
|
|
|
}
|
2017-04-21 22:20:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
impl PlatformInterface for Platform {
|
2018-12-08 14:29:41 -08:00
|
|
|
fn make_shebang_command(
|
|
|
|
path: &Path,
|
|
|
|
command: &str,
|
|
|
|
argument: Option<&str>,
|
|
|
|
) -> Result<Command, brev::OutputError> {
|
2017-04-23 16:09:34 -07:00
|
|
|
// Translate path to the interpreter from unix style to windows style
|
2017-11-16 23:30:08 -08:00
|
|
|
let mut cygpath = Command::new("cygpath");
|
2017-04-23 16:09:34 -07:00
|
|
|
cygpath.arg("--windows");
|
|
|
|
cygpath.arg(command);
|
|
|
|
|
2017-11-16 23:30:08 -08:00
|
|
|
let mut cmd = Command::new(brev::output(cygpath)?);
|
2017-04-21 22:20:13 -07:00
|
|
|
if let Some(argument) = argument {
|
|
|
|
cmd.arg(argument);
|
|
|
|
}
|
|
|
|
cmd.arg(path);
|
2017-04-23 16:09:34 -07:00
|
|
|
Ok(cmd)
|
2017-04-21 22:20:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set_execute_permission(_path: &Path) -> Result<(), io::Error> {
|
|
|
|
// it is not necessary to set an execute permission on a script on windows,
|
|
|
|
// so this is a nop
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signal_from_exit_status(_exit_status: process::ExitStatus) -> Option<i32> {
|
|
|
|
// The rust standard library does not expose a way to extract a signal
|
2017-04-23 15:37:17 -07:00
|
|
|
// from a windows process exit status, so just return None
|
2017-04-21 22:20:13 -07:00
|
|
|
None
|
|
|
|
}
|
2018-06-19 10:04:03 -07:00
|
|
|
|
|
|
|
fn to_shell_path(path: &Path) -> Result<String, String> {
|
|
|
|
// Translate path from windows style to unix style
|
|
|
|
let mut cygpath = Command::new("cygpath");
|
|
|
|
cygpath.arg("--unix");
|
|
|
|
cygpath.arg(path);
|
|
|
|
brev::output(cygpath).map_err(|e| format!("Error converting shell path: {}", e))
|
|
|
|
}
|
2017-04-21 22:20:13 -07:00
|
|
|
}
|