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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
//! # runner
//!
//! Runs the requested tasks.<br>
//! The flow is as follows:
//!
//! * Load env variables
//! * Create an execution plan based on the requested task and its dependencies
//! * Run all tasks defined in the execution plan
//!

#[cfg(test)]
#[path = "runner_test.rs"]
mod runner_test;

use std::cell::RefCell;
use std::rc::Rc;
use std::thread;
use std::time::SystemTime;

use indexmap::IndexMap;
use regex::Regex;

use crate::command;
use crate::condition;
use crate::environment;
use crate::error::CargoMakeError;
use crate::execution_plan::ExecutionPlanBuilder;
use crate::functions;
use crate::installer;
use crate::logger;
use crate::plugin::runner::run_task as run_task_plugin;
use crate::profile;
use crate::proxy_task::create_proxy_task;
use crate::scriptengine;
use crate::time_summary;
use crate::types::{
    CliArgs, Config, DeprecationInfo, EnvInfo, EnvValue, ExecutionPlan, FlowInfo, FlowState,
    MaybeArray, RunTaskInfo, RunTaskName, RunTaskOptions, RunTaskRoutingInfo, Step, Task,
    TaskWatchOptions,
};

fn do_in_task_working_directory<F>(step: &Step, mut action: F) -> Result<(), CargoMakeError>
where
    F: FnMut() -> Result<bool, CargoMakeError>,
{
    let revert_directory = match step.config.cwd {
        Some(ref cwd) => {
            let expanded_cwd = environment::expand_value(cwd);

            if expanded_cwd.len() > 0 {
                let directory = envmnt::get_or("CARGO_MAKE_WORKING_DIRECTORY", "");

                environment::setup_cwd(Some(&expanded_cwd));

                directory
            } else {
                "".to_string()
            }
        }
        None => "".to_string(),
    };

    action()?;

    // revert to original cwd
    match step.config.cwd {
        Some(_) => {
            environment::setup_cwd(Some(&revert_directory));
        }
        _ => (),
    };
    Ok(())
}

pub(crate) fn validate_condition(
    flow_info: &FlowInfo,
    step: &Step,
) -> Result<bool, CargoMakeError> {
    let mut valid = true;

    let do_validate = || -> Result<bool, CargoMakeError> {
        valid = condition::validate_condition_for_step(&flow_info, &step)?;
        Ok(valid)
    };

    do_in_task_working_directory(&step, do_validate)?;

    Ok(valid)
}

pub(crate) fn get_sub_task_info_for_routing_info(
    flow_info: &FlowInfo,
    routing_info: &Vec<RunTaskRoutingInfo>,
) -> Result<(Option<Vec<String>>, bool, bool, Option<String>), CargoMakeError> {
    let mut task_name = None;

    let mut fork = false;
    let mut parallel = false;
    let mut cleanup_task = None;
    for routing_step in routing_info {
        let invoke = condition::validate_conditions(
            &flow_info,
            &routing_step.condition,
            &routing_step.condition_script,
            None,
            routing_step.condition_script_runner_args.clone(),
        )?;

        if invoke {
            let task_name_values = match routing_step.name.clone() {
                RunTaskName::Single(name) => vec![name],
                RunTaskName::Multiple(names) => names,
            };
            task_name = Some(task_name_values);
            fork = routing_step.fork.unwrap_or(false);
            parallel = routing_step.parallel.unwrap_or(false);
            cleanup_task = routing_step.cleanup_task.clone();
            break;
        }
    }

    Ok((task_name, fork, parallel, cleanup_task))
}

fn create_fork_step(flow_info: &FlowInfo) -> Step {
    let fork_task = create_proxy_task(
        &flow_info.task,
        true,
        true,
        None,
        flow_info.cli_arguments.clone(),
    );

    Step {
        name: "cargo_make_run_fork".to_string(),
        config: fork_task,
    }
}

fn run_cleanup_task(
    flow_info: &FlowInfo,
    flow_state: Rc<RefCell<FlowState>>,
    task: &str,
) -> Result<(), CargoMakeError> {
    match flow_info.config.tasks.get(task) {
        Some(cleanup_task_info) => run_task(
            &flow_info,
            flow_state,
            &Step {
                name: task.to_string(),
                config: cleanup_task_info.clone(),
            },
        ),
        None => Err(CargoMakeError::NotFound(format!(
            "Cleanup task: {} not found.",
            &task
        ))),
    }
}

fn run_forked_task(
    flow_info: &FlowInfo,
    flow_state: Rc<RefCell<FlowState>>,
    cleanup_task: &Option<String>,
) -> Result<(), CargoMakeError> {
    // run task as a sub process
    let step = create_fork_step(&flow_info);

    match cleanup_task {
        Some(cleanup_task_name) => {
            // run the forked task (forked tasks only run a command + args)
            let exit_code =
                command::run_command(&step.config.command.unwrap(), &step.config.args, false)?;

            if exit_code != 0 {
                run_cleanup_task(&flow_info, flow_state, &cleanup_task_name)?;
                command::validate_exit_code(exit_code)
            } else {
                Ok(())
            }
        }
        None => run_task(&flow_info, flow_state, &step),
    }
}

/// runs a sub task and returns true/false based if a sub task was actually invoked
fn run_sub_task_and_report(
    flow_info: &FlowInfo,
    flow_state: Rc<RefCell<FlowState>>,
    sub_task: &RunTaskInfo,
) -> Result<bool, CargoMakeError> {
    let (task_names, fork, parallel, cleanup_task) = match sub_task {
        RunTaskInfo::Name(ref name) => (Some(vec![name.to_string()]), false, false, None),
        RunTaskInfo::Details(ref details) => {
            let task_name_values = match details.name.clone() {
                RunTaskName::Single(name) => vec![name],
                RunTaskName::Multiple(names) => names,
            };
            (
                Some(task_name_values),
                details.fork.unwrap_or(false),
                details.parallel.unwrap_or(false),
                details.cleanup_task.clone(),
            )
        }
        RunTaskInfo::Routing(ref routing_info) => {
            get_sub_task_info_for_routing_info(&flow_info, routing_info)?
        }
    };

    if task_names.is_some() {
        let names = task_names.unwrap();
        let mut threads = vec![];

        // clean up task only supported for forked tasks
        if !fork && cleanup_task.is_some() {
            error!("Invalid task, cannot use cleanup_task without fork.");
        }

        for name in names {
            let task_run_fn = move |flow_info: &FlowInfo,
                                    flow_state: Rc<RefCell<FlowState>>,
                                    fork: bool,
                                    cleanup_task: &Option<String>|
                  -> Result<(), CargoMakeError> {
                let mut sub_flow_info = flow_info.clone();
                sub_flow_info.task = name;

                if fork {
                    run_forked_task(&sub_flow_info, flow_state, cleanup_task)
                } else {
                    run_flow(&sub_flow_info, flow_state, true)
                }
            };

            if parallel {
                let run_flow_info = flow_info.clone();
                // we do not support merging changes back to parent
                let cloned_flow_state = flow_state.borrow().clone();
                let cloned_cleanup_task = cleanup_task.clone();
                threads.push(thread::spawn(move || -> Result<(), CargoMakeError> {
                    task_run_fn(
                        &run_flow_info,
                        Rc::new(RefCell::new(cloned_flow_state)),
                        fork,
                        &cloned_cleanup_task,
                    )
                }));
            } else {
                task_run_fn(&flow_info, flow_state.clone(), fork, &cleanup_task)?;
            }
        }

        if threads.len() > 0 {
            for task_thread in threads {
                task_thread.join().unwrap()?;
            }
        }

        if let Some(cleanup_task_name) = cleanup_task {
            run_cleanup_task(&flow_info, flow_state, &cleanup_task_name)?;
        }

        Ok(true)
    } else {
        Ok(false)
    }
}

fn run_sub_task(
    flow_info: &FlowInfo,
    flow_state: Rc<RefCell<FlowState>>,
    sub_task: &RunTaskInfo,
) -> Result<bool, CargoMakeError> {
    run_sub_task_and_report(&flow_info, flow_state, &sub_task)
}

fn create_watch_task_name(task: &str) -> String {
    let mut watch_task_name = "".to_string();
    watch_task_name.push_str(&task);
    watch_task_name.push_str("-watch");

    watch_task_name
}

fn create_watch_step(task: &str, options: Option<TaskWatchOptions>, flow_info: &FlowInfo) -> Step {
    let watch_task = create_watch_task(&task, options, flow_info);

    let watch_task_name = create_watch_task_name(&task);

    Step {
        name: watch_task_name,
        config: watch_task,
    }
}

fn watch_task(
    flow_info: &FlowInfo,
    flow_state: Rc<RefCell<FlowState>>,
    task: &str,
    options: Option<TaskWatchOptions>,
) -> Result<(), CargoMakeError> {
    let step = create_watch_step(&task, options, flow_info);

    run_task(&flow_info, flow_state, &step)
}

fn is_watch_enabled() -> bool {
    !envmnt::is_or("CARGO_MAKE_DISABLE_WATCH", false)
}

fn should_watch(task: &Task) -> bool {
    match task.watch {
        Some(ref watch_value) => match watch_value {
            TaskWatchOptions::Boolean(watch_bool) => {
                if *watch_bool {
                    is_watch_enabled()
                } else {
                    false
                }
            }
            TaskWatchOptions::Options(_) => is_watch_enabled(),
        },
        None => false,
    }
}

pub(crate) fn run_task(
    flow_info: &FlowInfo,
    flow_state: Rc<RefCell<FlowState>>,
    step: &Step,
) -> Result<(), CargoMakeError> {
    let options = RunTaskOptions {
        plugins_enabled: true,
    };

    run_task_with_options(flow_info, flow_state, step, &options)
}

pub(crate) fn run_task_with_options(
    flow_info: &FlowInfo,
    flow_state: Rc<RefCell<FlowState>>,
    step: &Step,
    options: &RunTaskOptions,
) -> Result<(), CargoMakeError> {
    let start_time = SystemTime::now();

    // if a plugin is handling the task execution flow
    if run_task_plugin(flow_info, flow_state.clone(), step, options) {
        time_summary::add(
            &mut flow_state.borrow_mut().time_summary,
            &step.name,
            start_time,
        );
        return Ok(());
    }

    if step.config.is_actionable() {
        match step.config.env {
            Some(ref env) => environment::set_current_task_meta_info_env(env.clone()),
            None => (),
        };

        if validate_condition(
            &flow_info,
            &environment::expand_condition_script_runner_arguments(&step),
        )? {
            if logger::should_reduce_output(&flow_info) && step.config.script.is_none() {
                debug!("Running Task: {}", &step.name);
            } else {
                info!("Running Task: {}", &step.name);
            }

            if !step.config.is_valid() {
                error!(
                    "Invalid task: {}, contains multiple actions.\n{:#?}",
                    &step.name, &step.config
                );
            }

            let deprecated_info = step.config.deprecated.clone();
            match deprecated_info {
                Some(deprecated) => match deprecated {
                    DeprecationInfo::Boolean(value) => {
                        if value {
                            warn!("Task: {} is deprecated.", &step.name);
                        }

                        ()
                    }
                    DeprecationInfo::Message(ref message) => {
                        warn!("Task: {} is deprecated - {}", &step.name, message);

                        ()
                    }
                },
                None => (),
            };

            //get profile
            let profile_name = profile::get();

            match step.config.env_files {
                Some(ref env_files) => environment::set_env_files(env_files.clone()),
                None => (),
            };
            match step.config.env {
                Some(ref env) => environment::set_env(env.clone()),
                None => (),
            };

            envmnt::set("CARGO_MAKE_CURRENT_TASK_NAME", &step.name);

            //make sure profile env is not overwritten
            profile::set(&profile_name);

            // modify step using env and functions
            let mut updated_step = functions::run(&step)?;
            updated_step = environment::expand_env(&updated_step);

            let watch = should_watch(&step.config);

            if watch {
                watch_task(
                    &flow_info,
                    flow_state,
                    &step.name,
                    step.config.watch.clone(),
                )?;
            } else {
                do_in_task_working_directory(&step, || -> Result<bool, CargoMakeError> {
                    installer::install(&updated_step.config, flow_info, flow_state.clone())?;
                    Ok(true)
                })?;

                match step.config.run_task {
                    Some(ref sub_task) => {
                        time_summary::add(
                            &mut flow_state.borrow_mut().time_summary,
                            &step.name,
                            start_time,
                        );

                        run_sub_task(&flow_info, flow_state, sub_task)?;
                    }
                    None => {
                        do_in_task_working_directory(&step, || -> Result<bool, CargoMakeError> {
                            // run script
                            let script_runner_done = scriptengine::invoke(
                                &updated_step.config,
                                flow_info,
                                flow_state.clone(),
                            )?;

                            // run command
                            if !script_runner_done {
                                command::run(&updated_step)?;
                            };
                            Ok(true)
                        })?;

                        time_summary::add(
                            &mut flow_state.borrow_mut().time_summary,
                            &step.name,
                            start_time,
                        );
                    }
                };
            }
        } else {
            let fail_message = match step.config.condition {
                Some(ref condition) => match condition.fail_message {
                    Some(ref value) => value.to_string(),
                    None => "".to_string(),
                },
                None => "".to_string(),
            };

            if logger::should_reduce_output(&flow_info) && !step.config.is_actionable() {
                debug!("Skipping Task: {} {}", &step.name, &fail_message);
            } else {
                info!("Skipping Task: {} {}", &step.name, &fail_message);
            }
        }
    } else {
        debug!("Ignoring Empty Task: {}", &step.name);
    }

    Ok(())
}

fn run_task_flow(
    flow_info: &FlowInfo,
    flow_state: Rc<RefCell<FlowState>>,
    execution_plan: &ExecutionPlan,
) -> Result<(), CargoMakeError> {
    for step in &execution_plan.steps {
        run_task(&flow_info, flow_state.clone(), &step)?;
    }
    Ok(())
}

fn create_watch_task(task: &str, options: Option<TaskWatchOptions>, flow_info: &FlowInfo) -> Task {
    let mut task_config =
        create_proxy_task(&task, true, true, None, flow_info.cli_arguments.clone());

    let mut env_map = task_config.env.unwrap_or(IndexMap::new());
    env_map.insert(
        "CARGO_MAKE_DISABLE_WATCH".to_string(),
        EnvValue::Value("true".to_string()),
    );
    task_config.env = Some(env_map);

    let make_args = task_config.args.unwrap();
    let mut make_command = String::new();
    for make_arg in make_args {
        if make_arg.contains(" ") {
            make_command.push_str("\"");
            make_command.push_str(&make_arg);
            make_command.push_str("\"");
        } else {
            make_command.push_str(&make_arg);
        }

        make_command.push(' ');
    }
    make_command = make_command.trim().to_string();

    let mut watch_args = vec!["watch".to_string()];

    match options {
        Some(task_watch_options) => match task_watch_options {
            TaskWatchOptions::Options(watch_options) => {
                let watch_version = match watch_options.version {
                    Some(value) => value.to_string(),
                    _ => "8.4.1".to_string(), // current version
                };
                task_config.install_crate_args = Some(vec!["--version".to_string(), watch_version]);

                match watch_options.why {
                    Some(option_value) => {
                        if option_value {
                            watch_args.push("--why".to_string());
                        } else {
                            watch_args.push("-q".to_string());
                        }
                    }
                    None => watch_args.push("-q".to_string()),
                }

                if let Some(option_value) = watch_options.postpone {
                    if option_value {
                        watch_args.push("--postpone".to_string());
                    }
                }

                match watch_options.ignore_pattern {
                    Some(MaybeArray::Single(value)) => {
                        watch_args.extend_from_slice(&["-i".to_string(), value])
                    }
                    Some(MaybeArray::Multiple(values)) => watch_args.extend(
                        values
                            .iter()
                            .flat_map(|value| ["-i".to_string(), value.to_string()])
                            .collect::<Vec<String>>(),
                    ),
                    _ => (),
                };

                if let Some(option_value) = watch_options.no_git_ignore {
                    if option_value {
                        watch_args.push("--no-gitignore".to_string());
                    }
                }

                match watch_options.watch {
                    Some(paths) => {
                        for watch_path in paths {
                            watch_args.extend_from_slice(&["-w".to_string(), watch_path])
                        }
                    }
                    _ => (),
                };
            }
            _ => watch_args.push("-q".to_string()),
        },
        None => watch_args.push("-q".to_string()),
    }

    watch_args.extend_from_slice(&["-x".to_string(), make_command.to_string()]);

    task_config.args = Some(watch_args);

    task_config
}

pub(crate) fn run_flow(
    flow_info: &FlowInfo,
    flow_state: Rc<RefCell<FlowState>>,
    sub_flow: bool,
) -> Result<(), CargoMakeError> {
    let allow_private = sub_flow || flow_info.allow_private;

    let execution_plan = ExecutionPlanBuilder {
        crate_info: Some(&flow_info.env_info.crate_info),
        disable_workspace: flow_info.disable_workspace,
        allow_private,
        sub_flow,
        skip_tasks_pattern: flow_info.skip_tasks_pattern.as_ref(),
        skip_init_end_tasks: flow_info.skip_init_end_tasks,
        ..ExecutionPlanBuilder::new(&flow_info.config, &flow_info.task)
    }
    .build()?;
    debug!("Created execution plan: {:#?}", &execution_plan);

    run_task_flow(&flow_info, flow_state, &execution_plan)?;

    Ok(())
}

fn run_protected_flow(
    flow_info: &FlowInfo,
    flow_state: Rc<RefCell<FlowState>>,
) -> Result<(), CargoMakeError> {
    let proxy_task = create_proxy_task(
        &flow_info.task,
        flow_info.allow_private,
        flow_info.skip_init_end_tasks,
        None,
        flow_info.cli_arguments.clone(),
    );

    let exit_code = command::run_command(&proxy_task.command.unwrap(), &proxy_task.args, false)?;

    if exit_code != 0 {
        match flow_info.config.config.on_error_task {
            Some(ref on_error_task) => {
                let mut error_flow_info = flow_info.clone();
                error_flow_info.disable_on_error = true;
                error_flow_info.task = on_error_task.clone();

                run_flow(&error_flow_info, flow_state, false)?;
            }
            _ => (),
        };

        error!("Task error detected, exit code: {}", &exit_code);
    }
    Ok(())
}

/// Runs the requested tasks.<br>
/// The flow is as follows:
///
/// * Create an execution plan based on the requested task and its dependencies
/// * Run all tasks defined in the execution plan
pub fn run(
    config: Config,
    task: &str,
    env_info: EnvInfo,
    cli_args: &CliArgs,
    start_time: SystemTime,
    time_summary_vec: Vec<(String, u128)>,
) -> Result<(), CargoMakeError> {
    time_summary::init(&config, &cli_args);

    let skip_tasks_pattern = match cli_args.skip_tasks_pattern {
        Some(ref pattern) => match Regex::new(pattern) {
            Ok(reg) => Some(reg),
            Err(_) => {
                warn!("Invalid skip tasks pattern provided: {}", pattern);
                None
            }
        },
        None => None,
    };

    let flow_info = FlowInfo {
        config,
        task: task.to_string(),
        env_info,
        disable_workspace: cli_args.disable_workspace,
        disable_on_error: cli_args.disable_on_error,
        allow_private: cli_args.allow_private,
        skip_init_end_tasks: cli_args.skip_init_end_tasks,
        skip_tasks_pattern,
        cli_arguments: cli_args.arguments.clone(),
    };
    let mut flow_state = FlowState::new();
    flow_state.time_summary = time_summary_vec;

    let flow_state_rc = Rc::new(RefCell::new(flow_state));

    if flow_info.disable_on_error || flow_info.config.config.on_error_task.is_none() {
        run_flow(&flow_info, flow_state_rc.clone(), false)?;
    } else {
        run_protected_flow(&flow_info, flow_state_rc.clone())?;
    }

    let time_string = match start_time.elapsed() {
        Ok(elapsed) => {
            let time_millies = elapsed.as_millis() as f64 / 1000.0;
            format!(" in {:.2} seconds", time_millies)
        }
        _ => "".to_string(),
    };

    time_summary::print(&flow_state_rc.borrow().time_summary);

    info!("Build Done{}.", &time_string);

    Ok(())
}