-
Notifications
You must be signed in to change notification settings - Fork 476
/
settings.rs
271 lines (239 loc) · 7.46 KB
/
settings.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use super::*;
pub(crate) const DEFAULT_SHELL: &str = "sh";
pub(crate) const DEFAULT_SHELL_ARGS: &[&str] = &["-cu"];
pub(crate) const WINDOWS_POWERSHELL_SHELL: &str = "powershell.exe";
pub(crate) const WINDOWS_POWERSHELL_ARGS: &[&str] = &["-NoLogo", "-Command"];
#[derive(Debug, PartialEq, Serialize, Default)]
pub(crate) struct Settings<'src> {
pub(crate) allow_duplicate_recipes: bool,
pub(crate) allow_duplicate_variables: bool,
pub(crate) dotenv_filename: Option<String>,
pub(crate) dotenv_load: bool,
pub(crate) dotenv_path: Option<PathBuf>,
pub(crate) dotenv_required: bool,
pub(crate) export: bool,
pub(crate) fallback: bool,
pub(crate) ignore_comments: bool,
pub(crate) positional_arguments: bool,
pub(crate) quiet: bool,
#[serde(skip)]
pub(crate) script_interpreter: Option<Interpreter<'src>>,
pub(crate) shell: Option<Interpreter<'src>>,
pub(crate) tempdir: Option<String>,
pub(crate) unstable: bool,
pub(crate) windows_powershell: bool,
pub(crate) windows_shell: Option<Interpreter<'src>>,
pub(crate) working_directory: Option<PathBuf>,
}
impl<'src> Settings<'src> {
pub(crate) fn from_table(sets: Table<'src, Set<'src>>) -> Self {
let mut settings = Self::default();
for (_name, set) in sets {
match set.value {
Setting::AllowDuplicateRecipes(allow_duplicate_recipes) => {
settings.allow_duplicate_recipes = allow_duplicate_recipes;
}
Setting::AllowDuplicateVariables(allow_duplicate_variables) => {
settings.allow_duplicate_variables = allow_duplicate_variables;
}
Setting::DotenvFilename(filename) => {
settings.dotenv_filename = Some(filename.cooked);
}
Setting::DotenvLoad(dotenv_load) => {
settings.dotenv_load = dotenv_load;
}
Setting::DotenvPath(path) => {
settings.dotenv_path = Some(PathBuf::from(path.cooked));
}
Setting::DotenvRequired(dotenv_required) => {
settings.dotenv_required = dotenv_required;
}
Setting::Export(export) => {
settings.export = export;
}
Setting::Fallback(fallback) => {
settings.fallback = fallback;
}
Setting::IgnoreComments(ignore_comments) => {
settings.ignore_comments = ignore_comments;
}
Setting::PositionalArguments(positional_arguments) => {
settings.positional_arguments = positional_arguments;
}
Setting::Quiet(quiet) => {
settings.quiet = quiet;
}
Setting::ScriptInterpreter(script_interpreter) => {
settings.script_interpreter = Some(script_interpreter);
}
Setting::Shell(shell) => {
settings.shell = Some(shell);
}
Setting::Unstable(unstable) => {
settings.unstable = unstable;
}
Setting::WindowsPowerShell(windows_powershell) => {
settings.windows_powershell = windows_powershell;
}
Setting::WindowsShell(windows_shell) => {
settings.windows_shell = Some(windows_shell);
}
Setting::Tempdir(tempdir) => {
settings.tempdir = Some(tempdir.cooked);
}
Setting::WorkingDirectory(working_directory) => {
settings.working_directory = Some(working_directory.cooked.into());
}
}
}
settings
}
pub(crate) fn shell_command(&self, config: &Config) -> Command {
let (command, args) = self.shell(config);
let mut cmd = Command::new(command);
cmd.args(args);
cmd
}
pub(crate) fn shell<'a>(&'a self, config: &'a Config) -> (&'a str, Vec<&'a str>) {
match (&config.shell, &config.shell_args) {
(Some(shell), Some(shell_args)) => (shell, shell_args.iter().map(String::as_ref).collect()),
(Some(shell), None) => (shell, DEFAULT_SHELL_ARGS.to_vec()),
(None, Some(shell_args)) => (
DEFAULT_SHELL,
shell_args.iter().map(String::as_ref).collect(),
),
(None, None) => {
if let (true, Some(shell)) = (cfg!(windows), &self.windows_shell) {
(
shell.command.cooked.as_ref(),
shell
.arguments
.iter()
.map(|argument| argument.cooked.as_ref())
.collect(),
)
} else if cfg!(windows) && self.windows_powershell {
(WINDOWS_POWERSHELL_SHELL, WINDOWS_POWERSHELL_ARGS.to_vec())
} else if let Some(shell) = &self.shell {
(
shell.command.cooked.as_ref(),
shell
.arguments
.iter()
.map(|argument| argument.cooked.as_ref())
.collect(),
)
} else {
(DEFAULT_SHELL, DEFAULT_SHELL_ARGS.to_vec())
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_shell() {
let settings = Settings::default();
let config = Config {
shell_command: false,
..testing::config(&[])
};
assert_eq!(settings.shell(&config), ("sh", vec!["-cu"]));
}
#[test]
fn default_shell_powershell() {
let settings = Settings {
windows_powershell: true,
..Default::default()
};
let config = Config {
shell_command: false,
..testing::config(&[])
};
if cfg!(windows) {
assert_eq!(
settings.shell(&config),
("powershell.exe", vec!["-NoLogo", "-Command"])
);
} else {
assert_eq!(settings.shell(&config), ("sh", vec!["-cu"]));
}
}
#[test]
fn overwrite_shell() {
let settings = Settings::default();
let config = Config {
shell_command: true,
shell: Some("lol".to_string()),
shell_args: Some(vec!["-nice".to_string()]),
..testing::config(&[])
};
assert_eq!(settings.shell(&config), ("lol", vec!["-nice"]));
}
#[test]
fn overwrite_shell_powershell() {
let settings = Settings {
windows_powershell: true,
..Default::default()
};
let config = Config {
shell_command: true,
shell: Some("lol".to_string()),
shell_args: Some(vec!["-nice".to_string()]),
..testing::config(&[])
};
assert_eq!(settings.shell(&config), ("lol", vec!["-nice"]));
}
#[test]
fn shell_cooked() {
let settings = Settings {
shell: Some(Interpreter {
command: StringLiteral {
kind: StringKind::from_token_start("\"").unwrap(),
raw: "asdf.exe",
cooked: "asdf.exe".to_string(),
expand: false,
},
arguments: vec![StringLiteral {
kind: StringKind::from_token_start("\"").unwrap(),
raw: "-nope",
cooked: "-nope".to_string(),
expand: false,
}],
}),
..Default::default()
};
let config = Config {
shell_command: false,
..testing::config(&[])
};
assert_eq!(settings.shell(&config), ("asdf.exe", vec!["-nope"]));
}
#[test]
fn shell_present_but_not_shell_args() {
let settings = Settings {
windows_powershell: true,
..Default::default()
};
let config = Config {
shell: Some("lol".to_string()),
..testing::config(&[])
};
assert_eq!(settings.shell(&config).0, "lol");
}
#[test]
fn shell_args_present_but_not_shell() {
let settings = Settings {
windows_powershell: true,
..Default::default()
};
let config = Config {
shell_command: false,
shell_args: Some(vec!["-nice".to_string()]),
..testing::config(&[])
};
assert_eq!(settings.shell(&config), ("sh", vec!["-nice"]));
}
}