-
Notifications
You must be signed in to change notification settings - Fork 776
Expand file tree
/
Copy pathcommand_ext.rs
More file actions
137 lines (110 loc) · 3.52 KB
/
command_ext.rs
File metadata and controls
137 lines (110 loc) · 3.52 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use super::*;
pub(crate) trait CommandExt {
fn export(
&mut self,
settings: &Settings,
dotenv: &BTreeMap<String, String>,
scope: &Scope,
unexports: &HashSet<String>,
) -> &mut Command;
fn export_scope(&mut self, settings: &Settings, scope: &Scope, unexports: &HashSet<String>);
fn output_guard(self) -> (io::Result<process::Output>, Option<Signal>);
fn output_guard_stdout(self) -> Result<String, OutputError>;
fn resolve(program: impl AsRef<OsStr>) -> Command;
fn status_guard(self) -> (io::Result<ExitStatus>, Option<Signal>);
}
impl CommandExt for Command {
fn export(
&mut self,
settings: &Settings,
dotenv: &BTreeMap<String, String>,
scope: &Scope,
unexports: &HashSet<String>,
) -> &mut Command {
for (name, value) in dotenv {
self.env(name, value);
}
if let Some(parent) = scope.parent() {
self.export_scope(settings, parent, unexports);
}
self
}
fn export_scope(&mut self, settings: &Settings, scope: &Scope, unexports: &HashSet<String>) {
if let Some(parent) = scope.parent() {
self.export_scope(settings, parent, unexports);
}
for unexport in unexports {
self.env_remove(unexport);
}
for binding in scope.bindings() {
if binding.export || (settings.export && !binding.prelude) {
self.env(binding.name.lexeme(), &binding.value);
}
}
}
fn output_guard(self) -> (io::Result<process::Output>, Option<Signal>) {
SignalHandler::spawn(self, process::Child::wait_with_output)
}
fn output_guard_stdout(self) -> Result<String, OutputError> {
let (result, caught) = self.output_guard();
let output = result.map_err(OutputError::Io)?;
OutputError::result_from_exit_status(output.status)?;
let output = str::from_utf8(&output.stdout).map_err(OutputError::Utf8)?;
if let Some(signal) = caught {
return Err(OutputError::Interrupted(signal));
}
Ok(
output
.strip_suffix("\r\n")
.or_else(|| output.strip_suffix("\n"))
.unwrap_or(output)
.into(),
)
}
fn resolve(program: impl AsRef<OsStr>) -> Self {
let program = Path::new(program.as_ref());
if !cfg!(windows) {
return Self::new(program);
}
let mut candidates = vec![program.into()];
let mut components = program.components();
if matches!(components.next(), Some(Component::Normal(_))) && components.next().is_none() {
if let Some(path) = env::var_os("PATH") {
for path in env::split_paths(&path) {
candidates.push(path.join(program));
}
}
}
let extensions = if program.extension().is_none() {
let pathext = env::var_os("PATHEXT")
.unwrap_or(".COM;.EXE;.BAT;.CMD".into())
.to_string_lossy()
.into_owned();
let mut extensions = Vec::new();
for extension in pathext.split(';') {
if let Some(extension) = extension.strip_prefix('.') {
extensions.push(extension.to_owned());
}
}
Some(extensions)
} else {
None
};
for candidate in candidates {
if let Some(extensions) = &extensions {
for extension in extensions {
let path = candidate.with_extension(extension);
if path.is_file() {
return Self::new(path);
}
}
} else if candidate.is_file() {
return Self::new(candidate);
}
}
Self::new(program)
}
fn status_guard(self) -> (io::Result<ExitStatus>, Option<Signal>) {
SignalHandler::spawn(self, |mut child| child.wait())
}
}