|
1 | | -use super::*; |
| 1 | +use {super::*, std::path::Path}; |
2 | 2 |
|
3 | 3 | pub(crate) struct Evaluator<'src: 'run, 'run> { |
4 | 4 | pub(crate) assignments: Option<&'run Table<'src, Assignment<'src>>>, |
5 | 5 | pub(crate) context: ExecutionContext<'src, 'run>, |
6 | 6 | pub(crate) is_dependency: bool, |
7 | 7 | pub(crate) scope: Scope<'src, 'run>, |
| 8 | + pub(crate) working_directory: Option<PathBuf>, |
8 | 9 | } |
9 | 10 |
|
10 | 11 | impl<'src, 'run> Evaluator<'src, 'run> { |
| 12 | + pub(crate) fn working_directory(&self) -> Option<&Path> { |
| 13 | + self.working_directory.as_deref() |
| 14 | + } |
| 15 | + |
| 16 | + pub(crate) fn working_directory_or_invocation(&self) -> PathBuf { |
| 17 | + self |
| 18 | + .working_directory |
| 19 | + .clone() |
| 20 | + .unwrap_or_else(|| self.context.config.invocation_directory.clone()) |
| 21 | + } |
| 22 | + |
11 | 23 | pub(crate) fn evaluate_assignments( |
12 | 24 | config: &'run Config, |
13 | 25 | dotenv: &'run BTreeMap<String, String>, |
@@ -55,6 +67,7 @@ impl<'src, 'run> Evaluator<'src, 'run> { |
55 | 67 | assignments: Some(&module.assignments), |
56 | 68 | scope, |
57 | 69 | is_dependency: false, |
| 70 | + working_directory: context.module_default_working_directory(), |
58 | 71 | }; |
59 | 72 |
|
60 | 73 | for assignment in module.assignments.values() { |
@@ -273,10 +286,13 @@ impl<'src, 'run> Evaluator<'src, 'run> { |
273 | 286 | .settings |
274 | 287 | .shell_command(self.context.config); |
275 | 288 |
|
| 289 | + cmd.arg(command).args(args); |
| 290 | + |
| 291 | + if let Some(working_directory) = self.working_directory() { |
| 292 | + cmd.current_dir(working_directory); |
| 293 | + } |
| 294 | + |
276 | 295 | cmd |
277 | | - .arg(command) |
278 | | - .args(args) |
279 | | - .current_dir(self.context.working_directory()) |
280 | 296 | .export( |
281 | 297 | &self.context.module.settings, |
282 | 298 | self.context.dotenv, |
@@ -325,8 +341,9 @@ impl<'src, 'run> Evaluator<'src, 'run> { |
325 | 341 | arguments: &[String], |
326 | 342 | parameters: &[Parameter<'src>], |
327 | 343 | scope: &'run Scope<'src, 'run>, |
| 344 | + working_directory: Option<PathBuf>, |
328 | 345 | ) -> RunResult<'src, (Scope<'src, 'run>, Vec<String>)> { |
329 | | - let mut evaluator = Self::new(context, is_dependency, scope); |
| 346 | + let mut evaluator = Self::new(context, is_dependency, scope, working_directory); |
330 | 347 |
|
331 | 348 | let mut positional = Vec::new(); |
332 | 349 |
|
@@ -374,12 +391,14 @@ impl<'src, 'run> Evaluator<'src, 'run> { |
374 | 391 | context: &ExecutionContext<'src, 'run>, |
375 | 392 | is_dependency: bool, |
376 | 393 | scope: &'run Scope<'src, 'run>, |
| 394 | + working_directory: Option<PathBuf>, |
377 | 395 | ) -> Self { |
378 | 396 | Self { |
379 | 397 | assignments: None, |
380 | 398 | context: *context, |
381 | 399 | is_dependency, |
382 | 400 | scope: scope.child(), |
| 401 | + working_directory: working_directory.or_else(|| context.module_default_working_directory()), |
383 | 402 | } |
384 | 403 | } |
385 | 404 | } |
|
0 commit comments