1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use crate::common::*;
pub struct Platform;
pub trait PlatformInterface {
fn make_shebang_command(
path: &Path,
command: &str,
argument: Option<&str>,
) -> Result<Command, brev::OutputError>;
fn set_execute_permission(path: &Path) -> Result<(), io::Error>;
fn signal_from_exit_status(exit_status: process::ExitStatus) -> Option<i32>;
fn to_shell_path(path: &Path) -> Result<String, String>;
}
#[cfg(unix)]
impl PlatformInterface for Platform {
fn make_shebang_command(
path: &Path,
_command: &str,
_argument: Option<&str>,
) -> Result<Command, brev::OutputError> {
Ok(Command::new(path))
}
fn set_execute_permission(path: &Path) -> Result<(), io::Error> {
use std::os::unix::fs::PermissionsExt;
let mut permissions = fs::metadata(&path)?.permissions();
let current_mode = permissions.mode();
permissions.set_mode(current_mode | 0o100);
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()
}
fn to_shell_path(path: &Path) -> Result<String, String> {
path
.to_str()
.map(str::to_string)
.ok_or_else(|| String::from("Error getting current directory: unicode decode error"))
}
}
#[cfg(windows)]
impl PlatformInterface for Platform {
fn make_shebang_command(
path: &Path,
command: &str,
argument: Option<&str>,
) -> Result<Command, brev::OutputError> {
let mut cygpath = Command::new("cygpath");
cygpath.arg("--windows");
cygpath.arg(command);
let mut cmd = Command::new(brev::output(cygpath)?);
if let Some(argument) = argument {
cmd.arg(argument);
}
cmd.arg(path);
Ok(cmd)
}
fn set_execute_permission(_path: &Path) -> Result<(), io::Error> {
Ok(())
}
fn signal_from_exit_status(_exit_status: process::ExitStatus) -> Option<i32> {
None
}
fn to_shell_path(path: &Path) -> Result<String, String> {
let mut cygpath = Command::new("cygpath");
cygpath.arg("--unix");
cygpath.arg(path);
brev::output(cygpath).map_err(|e| format!("Error converting shell path: {}", e))
}
}