-
Notifications
You must be signed in to change notification settings - Fork 499
/
executor.rs
161 lines (140 loc) · 4.16 KB
/
executor.rs
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use super::*;
pub(crate) enum Executor<'a> {
Command(&'a Interpreter<'a>),
Shebang(Shebang<'a>),
}
impl Executor<'_> {
pub(crate) fn command<'src>(
&self,
path: &Path,
recipe: &'src str,
working_directory: Option<&Path>,
) -> RunResult<'src, Command> {
match self {
Self::Command(interpreter) => {
let mut command = Command::new(&interpreter.command.cooked);
if let Some(working_directory) = working_directory {
command.current_dir(working_directory);
}
for arg in &interpreter.arguments {
command.arg(&arg.cooked);
}
command.arg(path);
Ok(command)
}
Self::Shebang(shebang) => {
// make script executable
Platform::set_execute_permission(path).map_err(|error| Error::TempdirIo {
recipe,
io_error: error,
})?;
// create command to run script
Platform::make_shebang_command(path, working_directory, *shebang).map_err(|output_error| {
Error::Cygpath {
recipe,
output_error,
}
})
}
}
}
pub(crate) fn script_filename(&self, recipe: &str, extension: Option<&str>) -> String {
let extension = extension.unwrap_or_else(|| {
let interpreter = match self {
Self::Command(interpreter) => &interpreter.command.cooked,
Self::Shebang(shebang) => shebang.interpreter_filename(),
};
match interpreter {
"cmd" | "cmd.exe" => ".bat",
"powershell" | "powershell.exe" | "pwsh" | "pwsh.exe" => ".ps1",
_ => "",
}
});
format!("{recipe}{extension}")
}
pub(crate) fn error<'src>(&self, io_error: io::Error, recipe: &'src str) -> Error<'src> {
match self {
Self::Command(Interpreter { command, arguments }) => {
let mut command = command.cooked.clone();
for arg in arguments {
command.push(' ');
command.push_str(&arg.cooked);
}
Error::Script {
command,
io_error,
recipe,
}
}
Self::Shebang(shebang) => Error::Shebang {
argument: shebang.argument.map(String::from),
command: shebang.interpreter.to_owned(),
io_error,
recipe,
},
}
}
// Script text for `recipe` given evaluated `lines` including blanks so line
// numbers in errors from generated script match justfile source lines.
pub(crate) fn script<D>(&self, recipe: &Recipe<D>, lines: &[String]) -> String {
let mut script = String::new();
let mut n = 0;
let shebangs = recipe
.body
.iter()
.take_while(|line| line.is_shebang())
.count();
if let Self::Shebang(shebang) = self {
for shebang_line in &lines[..shebangs] {
if shebang.include_shebang_line() {
script.push_str(shebang_line);
}
script.push('\n');
n += 1;
}
}
for (line, text) in recipe.body.iter().zip(lines).skip(n) {
while n < line.number {
script.push('\n');
n += 1;
}
script.push_str(text);
script.push('\n');
n += 1;
}
script
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn shebang_script_filename() {
#[track_caller]
fn case(interpreter: &str, recipe: &str, extension: Option<&str>, expected: &str) {
assert_eq!(
Executor::Shebang(Shebang::new(&format!("#!{interpreter}")).unwrap())
.script_filename(recipe, extension),
expected
);
assert_eq!(
Executor::Command(&Interpreter {
command: StringLiteral::from_raw(interpreter),
arguments: Vec::new()
})
.script_filename(recipe, extension),
expected
);
}
case("bar", "foo", Some(".sh"), "foo.sh");
case("pwsh.exe", "foo", Some(".sh"), "foo.sh");
case("cmd.exe", "foo", Some(".sh"), "foo.sh");
case("powershell", "foo", None, "foo.ps1");
case("pwsh", "foo", None, "foo.ps1");
case("powershell.exe", "foo", None, "foo.ps1");
case("pwsh.exe", "foo", None, "foo.ps1");
case("cmd", "foo", None, "foo.bat");
case("cmd.exe", "foo", None, "foo.bat");
case("bar", "foo", None, "foo");
}
}