mom-task 1.3.0

Task runner for teams and individuals.
Documentation
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
#[cfg(test)]
#[path = "tasks_test.rs"]
mod tasks_test;

use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap};
use std::env::temp_dir;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::{fmt, fs, mem};

use crate::args::ArgsContext;
use crate::builtin_commands::get_builtin_command;
use crate::defaults::default_false;
use crate::errors::{AwareTaskError, TaskError};
use crate::inherit_option_value;
use crate::mom_files::MomFile;
use crate::print_utils::{MomOutput, INFO_COLOR};
use crate::serde_common::CommonFields;
use crate::tera::get_tera_instance;
use colored::Colorize;
use serde::{de, Deserialize, Serialize};

use crate::types::DynErrResult;
use crate::utils::{
    expand_arg, expand_args, get_working_directory, join_commands, split_command,
    TMP_FOLDER_NAMESPACE,
};
use md5::{Digest, Md5};

pub const DRY_RUN_MESSAGE: &str = "Dry run mode, nothing executed.";

cfg_if::cfg_if! {
    if #[cfg(target_os = "windows")] {
        // Will run the actual script in CMD, but we don't need to specify /C option
        const DEFAULT_SCRIPT_RUNNER: &str = "powershell {{ script_path }}";
        const DEFAULT_SCRIPT_EXTENSION: &str = "cmd";
    } else if #[cfg(target_os = "linux")] {
        const DEFAULT_SCRIPT_RUNNER: &str = "bash {{ script_path }}";
        const DEFAULT_SCRIPT_EXTENSION: &str = "sh";
    } else if #[cfg(target_os = "macos")] {
        const DEFAULT_SCRIPT_RUNNER: &str = "bash {{ script_path }}";
        const DEFAULT_SCRIPT_EXTENSION: &str = "sh";
    }else {
        compile_error!("Unsupported platform.");
    }
}

cfg_if::cfg_if! {
    if #[cfg(target_os = "windows")] {
        fn create_script_file<P: AsRef<Path>>(path: P) -> DynErrResult<File> {
            Ok(File::create(&path)?)
        }
    } else {
        use std::os::unix::fs::OpenOptionsExt;
        use std::fs::OpenOptions;
        fn create_script_file<P: AsRef<Path>>(path: P) -> DynErrResult<File> {
            Ok(OpenOptions::new()
            .create(true)
            .write(true)
            .mode(0o770)  // Create with appropriate permission
            .open(path)?)
        }
    }
}

/// Creates a temporal script returns the path to it.
/// The OS should take care of cleaning the file.
///
/// # Arguments
///
/// * `content` - Content of the script file
fn get_temp_script(
    content: &str,
    extension: &str,
    task_name: &str,
    mom_file_path: &Path,
) -> DynErrResult<PathBuf> {
    let mut path = temp_dir();
    path.push(TMP_FOLDER_NAMESPACE);
    fs::create_dir_all(&path)?;

    let extension = if extension.is_empty() {
        String::new()
    } else if extension.starts_with('.') {
        String::from(extension)
    } else {
        format!(".{}", extension)
    };

    // get md5 hash of the task_name, mom_file_path and content
    let mut hasher = Md5::new();
    hasher.update(task_name.as_bytes());
    hasher.update(mom_file_path.to_str().unwrap().as_bytes());
    hasher.update(content.as_bytes());
    let hash = hasher.finalize();

    let file_name = format!("{:X}{}", hash, extension);
    path.push(file_name);

    // Uses the temp file as a cache, so it doesn't have to create it every time
    // we run the same script.
    if path.exists() {
        return Ok(path);
    }
    let mut file = create_script_file(&path)?;
    file.write_all(content.as_bytes())?;
    Ok(path)
}

#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub(crate) struct TaskNameOption {
    task: String,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub(crate) struct CmdOption {
    #[serde(flatten)]
    command: String,
}

#[derive(Debug, Serialize, Clone)]
#[serde(untagged)]
pub(crate) enum Cmd {
    #[serde(rename = "task_name")]
    TaskName(String),
    #[serde(rename = "task")]
    Task(Box<Task>),
    #[serde(rename = "cmd")]
    Cmd(String),
}

#[derive(Debug, Deserialize, Clone)]
#[serde(untagged)]
pub(crate) enum StringOrTask {
    String(String),
    Task(Box<Task>),
}

impl<'de> de::Deserialize<'de> for Cmd {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        struct CmdVisitor;

        impl<'de> de::Visitor<'de> for CmdVisitor {
            type Value = Cmd;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("cmd, task name or task")
            }

            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                Ok(Cmd::Cmd(value.to_string()))
            }

            fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
            where
                V: de::MapAccess<'de>,
            {
                match map.next_key::<String>()? {
                    Some(key) => match key.as_str() {
                        "task" => {
                            let string_or_task: StringOrTask = map.next_value()?;
                            match string_or_task {
                                StringOrTask::String(s) => Ok(Cmd::TaskName(s)),
                                StringOrTask::Task(t) => Ok(Cmd::Task(t)),
                            }
                        }
                        "cmd" => {
                            let cmd: String = map.next_value()?;
                            Ok(Cmd::Cmd(cmd))
                        }
                        _ => Err(de::Error::unknown_field(key.as_str(), &["task", "cmd"])),
                    },
                    None => Err(de::Error::missing_field("task_name or task")),
                }

                // Deserialize::deserialize(de::value::MapAccessDeserializer::new(map))
            }
        }

        deserializer.deserialize_any(CmdVisitor {})
    }
}

#[derive(Debug, Deserialize, Serialize, Clone)]
struct TaskCondition(String);

impl TaskCondition {
    pub(crate) fn holds(
        &self,
        task_name: &str,
        tera: &mut tera::Tera,
        context: &tera::Context,
        env: &HashMap<String, String>,
    ) -> Result<bool, AwareTaskError> {
        let template_name = format!("{}.condition", task_name);
        tera.add_raw_template(&template_name, &self.0)
            .map_err(|e| {
                AwareTaskError::new(
                    task_name,
                    TaskError::ConfigError(format!("Invalid condition: {}", e)),
                )
            })?;
        let result = tera.render(&template_name, context).map_err(|e| {
            AwareTaskError::new(
                task_name,
                TaskError::ConfigError(format!("Invalid condition: {}", e)),
            )
        })?;
        let result = expand_arg(&result, env);
        let result = result.trim().to_lowercase();
        Ok(result == "true")
    }
}

/// Represents a Task
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(deny_unknown_fields)]
pub(crate) struct Task {
    /// Name of the task
    #[serde(skip_deserializing)]
    pub(crate) name: String,

    #[serde(flatten)]
    pub(crate) common: CommonFields,

    /// Condition to run the task
    condition: Option<TaskCondition>,

    /// Help of the task
    help: Option<String>,

    /// Script to run
    script: Option<String>,

    /// Interpreter program to use
    script_runner: Option<String>,

    /// Script extension
    #[serde(alias = "script_ext")]
    script_extension: Option<String>,

    /// A program to run
    program: Option<String>,

    /// Args to pass to a command
    args: Option<String>,

    /// Run commands
    cmds: Option<Vec<Cmd>>,

    /// Extends args from bases
    #[serde(alias = "args+")]
    args_extend: Option<String>,

    /// Task to run instead if the OS is linux
    pub(crate) linux: Option<Box<Task>>,

    /// Task to run instead if the OS is windows
    pub(crate) windows: Option<Box<Task>>,

    /// Task to run instead if the OS is macos
    pub(crate) macos: Option<Box<Task>>,

    /// If private, it cannot be called
    #[serde(default = "default_false")]
    private: bool,
}

impl Task {
    /// Returns the dependencies of the task.
    pub(crate) fn get_dependencies(&self) -> Vec<&str> {
        let mut dependencies: Vec<&str> = self.common.extend.iter().collect();

        if let Some(cmds) = &self.cmds {
            for cmd in cmds {
                match cmd {
                    Cmd::TaskName(task_name) => {
                        dependencies.push(task_name);
                    }
                    Cmd::Task(task) => {
                        dependencies.append(&mut task.get_dependencies());
                    }
                    Cmd::Cmd(_) => {}
                }
            }
        }

        dependencies
    }

    /// Does extra setup on the task and does some validation.
    ///
    /// # Arguments
    ///
    /// * `name`: name of the task
    /// * `base_path`: path to use as a reference to resolve relative paths
    ///
    /// returns: Result<(), Box<dyn Error, Global>>
    ///
    pub(crate) fn setup(&mut self, name: &str, base_path: &Path) -> Result<(), AwareTaskError> {
        self.name = String::from(name);
        match self.common.setup(base_path) {
            Ok(_) => {}
            Err(e) => {
                return Err(AwareTaskError::new(
                    name,
                    TaskError::ConfigError(format!("{}", e)),
                ))
            }
        }
        match self.validate() {
            Ok(_) => Ok(()),
            Err(e) => Err(AwareTaskError::new(name, e)),
        }
    }

    #[cfg(test)]
    pub(crate) fn script(&self) -> Option<&str> {
        self.script.as_deref()
    }

    /// Helper function for running a task. Accepts the environment variables as a HashMap.
    /// So that we can reuse the environment variables for multiple tasks.
    pub(crate) fn run(
        &self,
        args: &ArgsContext,
        mom_file: &MomFile,
        dry_run: bool,
    ) -> Result<(), AwareTaskError> {
        let env = self.get_env(&mom_file.common.env);
        let vars = self.get_vars(&mom_file.common.vars);

        let mut tera_instance = self
            .get_tera_instance(mom_file, env.clone())
            .map_err(|e| AwareTaskError::new(&self.name, e))?;
        let mut tera_context = self.get_tera_context(args, mom_file, &env, &vars);

        if let Some(condition) = &self.condition {
            if !condition.holds(&self.name, &mut tera_instance, &tera_context, &env)? {
                println!("{}", format!("{} skipped", &self.name).mom_info());
                return Ok(());
            }
        }

        let result = if self.script.is_some() {
            self.run_script(
                mom_file,
                &env,
                &mut tera_instance,
                &mut tera_context,
                dry_run,
            )
        } else if self.program.is_some() {
            self.run_program(
                mom_file,
                &env,
                &mut tera_instance,
                &mut tera_context,
                dry_run,
            )
        } else if self.cmds.is_some() {
            self.run_cmds(
                args,
                mom_file,
                &env,
                &mut tera_instance,
                &mut tera_context,
                dry_run,
            )
        } else {
            Err(TaskError::ConfigError(String::from("Nothing to run.")))
        };

        match result {
            Ok(_) => Ok(()),
            Err(e) => Err(AwareTaskError::new(&self.name, e)),
        }
    }

    /// Extends from the given task.
    ///
    /// # Arguments
    ///
    /// * `base_task`: task to extend from
    ///
    /// returns: ()
    ///
    pub(crate) fn extend(&mut self, base_task: &Task) {
        inherit_option_value!(self.help, base_task.help);
        inherit_option_value!(self.script, base_task.script);
        inherit_option_value!(self.script_runner, base_task.script_runner);
        inherit_option_value!(self.script_extension, base_task.script_extension);
        inherit_option_value!(self.program, base_task.program);
        inherit_option_value!(self.args, base_task.args);
        inherit_option_value!(self.cmds, base_task.cmds);
        inherit_option_value!(self.condition, base_task.condition);
        self.common.extend(&base_task.common);

        if self.args_extend.is_some() {
            let new_args = mem::take(&mut self.args_extend).unwrap();
            if self.args.is_none() {
                self.args = mem::replace(&mut self.args, Some(String::new()));
            }
            if let Some(args) = &mut self.args {
                args.push(' ');
                args.push_str(&new_args);
            } else {
                self.args = Some(new_args);
            }
        }
    }

    /// Returns the name of the task
    pub(crate) fn get_name(&self) -> &str {
        &self.name
    }

    /// Returns weather the task is private or not
    pub(crate) fn is_private(&self) -> bool {
        self.private
    }

    /// Returns the help for the task
    pub(crate) fn get_help(&self) -> &str {
        match self.help {
            Some(ref help) => help.trim(),
            None => "",
        }
    }

    /// Returns the environment variables by merging the ones from the mom file with
    /// the ones from the task, where the task takes precedence.
    ///
    /// # Arguments
    ///
    /// * `mom_file`: mom file to load extra environment variables from
    ///
    /// returns: HashMap<String, String, RandomState>
    fn get_env(&self, env: &HashMap<String, String>) -> HashMap<String, String> {
        let mut new_env = self.common.env.clone();
        for (key, val) in env {
            new_env.entry(key.clone()).or_insert_with(|| val.clone());
        }
        new_env
    }

    /// Returns the environment variables by merging the ones from the mom file with
    /// the ones from the task, where the task takes precedence.
    ///
    /// # Arguments
    ///
    /// * `mom_file`: mom file to load extra environment variables from
    ///
    /// returns: HashMap<String, String, RandomState>
    fn get_vars(
        &self,
        env: &HashMap<String, serde_yaml::Value>,
    ) -> HashMap<String, serde_yaml::Value> {
        let mut new_vars: HashMap<String, serde_yaml::Value> = self.common.vars.clone();
        for (key, val) in env {
            new_vars.entry(key.clone()).or_insert_with(|| val.clone());
        }
        new_vars
    }

    /// Same as `get_env` but for the tera templates
    fn get_templates(&self, tera_templates: &BTreeMap<String, String>) -> BTreeMap<String, String> {
        let mut new_templates: BTreeMap<String, String> = self.common.incl.clone();
        for (key, val) in tera_templates {
            new_templates
                .entry(key.clone())
                .or_insert_with(|| val.clone());
        }
        new_templates
    }

    /// Validates the task configuration.
    ///
    /// # Arguments
    ///
    /// * `name` - Name of the task
    fn validate(&self) -> Result<(), TaskError> {
        if self.script.is_some() && self.program.is_some() {
            return Err(TaskError::ConfigError(String::from(
                "Cannot set both `script` and `program`.",
            )));
        }

        if self.script.is_some() && self.cmds.is_some() {
            return Err(TaskError::ConfigError(String::from(
                "Cannot set both `cmds` and `script`.",
            )));
        }

        if self.program.is_some() && self.cmds.is_some() {
            return Err(TaskError::ConfigError(String::from(
                "Cannot set both `cmds` and `program`.",
            )));
        }

        Ok(())
    }

    // Returns the Tera instance for the Tera template engine.
    fn get_tera_instance(
        &self,
        mom_file: &MomFile,
        env: HashMap<String, String>,
    ) -> Result<tera::Tera, TaskError> {
        let mut tera = get_tera_instance(env);
        for (name, template) in mom_file.common.incl.iter() {
            tera.add_raw_template(&format!("incl.{name}"), template)?;
        }
        for (name, template) in self.common.incl.iter() {
            tera.add_raw_template(&format!("incl.{name}"), template)?;
        }
        Ok(tera)
    }

    /// Returns the context for the Tera template engine.
    fn get_tera_context(
        &self,
        args: &ArgsContext,
        mom_file: &MomFile,
        env: &HashMap<String, String>,
        vars: &HashMap<String, serde_yaml::Value>,
    ) -> tera::Context {
        let mut context = tera::Context::new();

        context.insert("args", &args.args);
        context.insert("kwargs", &args.kwargs);
        context.insert("pkwargs", &args.pkwargs);
        context.insert("vars", &vars);
        context.insert("env", &env);
        context.insert("TASK", self);
        context.insert("FILE", mom_file);

        context
    }

    /// Sets common parameters for commands, like stdout, stderr, stdin, working directory and
    /// environment variables.
    ///
    /// # Arguments
    ///
    /// * `command` - Command to set the parameters for
    /// * `mom_file` - Configuration file
    fn set_command_basics(
        &self,
        command: &mut Command,
        mom_file: &MomFile,
        env: &HashMap<String, String>,
    ) -> Result<(), TaskError> {
        command.envs(env);
        command.stdout(Stdio::inherit());
        command.stderr(Stdio::inherit());
        command.stdin(Stdio::inherit());

        let wd = match &self.common.wd {
            None => mom_file.common.wd.as_ref(),
            Some(wd) => Some(wd),
        };

        if let Some(wd) = wd {
            let wd = expand_arg(wd, env);
            let wd = Path::new(wd.as_ref());
            let mom_file_folder = &mom_file.directory;
            // wd may be absolute or relative to the mom file folder
            let wd = get_working_directory(mom_file_folder, wd);
            command.current_dir(wd);
        }

        Ok(())
    }

    /// Spawns a command and waits for its execution.
    ///
    /// # Arguments
    ///
    /// * `command` - Command to spawn
    fn spawn_command(&self, command: &mut Command, dry_run: bool) -> Result<(), TaskError> {
        if dry_run {
            println!("{}", DRY_RUN_MESSAGE.mom_info());
            return Ok(());
        }
        let mut child = match command.spawn() {
            Ok(child) => child,
            Err(e) => {
                return Err(TaskError::RuntimeError(format!("{}", e)));
            }
        };

        // let child handle ctrl-c to prevent dropping the parent and leaving the child running
        ctrlc::set_handler(move || {}).unwrap_or(());

        let result = child.wait()?;
        match result.success() {
            true => Ok(()),
            false => match result.code() {
                None => Err(TaskError::RuntimeError(String::from(
                    "Process did not terminate correctly",
                ))),
                Some(code) => Err(TaskError::RuntimeError(format!(
                    "Process terminated with exit code {}",
                    code
                ))),
            },
        }
    }

    /// Runs a program
    fn run_program(
        &self,
        mom_file: &MomFile,
        env: &HashMap<String, String>,
        tera_instance: &mut tera::Tera,
        tera_context: &mut tera::Context,
        dry_mode: bool,
    ) -> Result<(), TaskError> {
        let program = self.program.as_ref().unwrap();

        // In case the program is specified with ~ or $HOME, or something like that
        let program = expand_arg(program, env);

        let mut command = Command::new(program.as_ref());
        self.set_command_basics(&mut command, mom_file, env)?;

        let args_list = match &self.args {
            None => vec![],
            Some(args) => {
                let task_name = &self.name;
                let template_name = format!("tasks.{task_name}.args");
                tera_instance.add_raw_template(&template_name, args)?;
                let rendered_args = tera_instance.render(&template_name, tera_context)?;
                split_command(&rendered_args)
            }
        };
        if args_list.is_empty() {
            println!("{}", format!("{}: {}", self.name, program).mom_info());
        } else {
            let display_args = join_commands(&args_list);
            let args = expand_args(&args_list, env);
            let args = args.iter().map(|s| s.as_ref());
            command.args(args);

            println!(
                "{}",
                format!("{}: {} {}", self.name, program, display_args).mom_info()
            );
        }

        self.spawn_command(&mut command, dry_mode)
    }

    #[allow(clippy::too_many_arguments)]
    fn run_cmds_cmd(
        &self,
        cmd: &str,
        cmd_index: usize,
        mom_file: &MomFile,
        env: &HashMap<String, String>,
        tera_instance: &mut tera::Tera,
        tera_context: &mut tera::Context,
        dry_run: bool,
    ) -> Result<(), TaskError> {
        let task_name = &self.name;
        let task_name = &format!("{task_name}.cmds.{cmd_index}");
        let template_name = &format!("tasks.{task_name}");
        tera_instance.add_raw_template(template_name, cmd)?;

        let cmd = tera_instance.render(template_name, tera_context);
        let cmd = cmd?;
        let cmd_args = split_command(&cmd);
        let cmd_args: Vec<Cow<str>> = expand_args(&cmd_args, env);
        let cmd_args: Vec<&str> = cmd_args.iter().map(|s| s.as_ref()).collect();
        let program = match cmd_args.first() {
            Some(program) => program,
            None => {
                return Err(TaskError::RuntimeError(format!(
                    "Error running task: {}",
                    "No program specified"
                )))
            }
        };
        // We print the clean commands, not the rendered ones. For a nicer output.
        let command_info = format!("{task_name}: {}", join_commands(&cmd_args)).mom_info();

        // We need to do some refactoring, dry_run should not be dependent on the Command instance.
        if !dry_run {
            if let Some(builtin_command) = get_builtin_command(program) {
                println!("{}", command_info);
                return builtin_command(&cmd_args[1..])
                    .map_err(|e| TaskError::RuntimeError(format!("Error running task: {}", e)));
            }
        }
        let program_args = &cmd_args[1..];
        let mut command: Command = Command::new(program);
        self.set_command_basics(&mut command, mom_file, env)?;
        command.args(program_args);

        println!("{}", command_info);
        self.spawn_command(&mut command, dry_run)
    }

    fn run_cmds_task_name(
        &self,
        task_name: &str,
        cmd_index: usize,
        args: &ArgsContext,
        mom_file: &MomFile,
        dry_run: bool,
    ) -> Result<(), TaskError> {
        let display_task_name = format!("{}.cmds.{}.{}", self.name, cmd_index, task_name);
        if let Some(mut task) = mom_file.clone_task(task_name) {
            // The env and vars of the parent take precedence in this case.
            task.common.env = self.get_env(&task.common.env);
            task.common.vars = self.get_vars(&task.common.vars);
            task.common.incl = self.get_templates(&task.common.incl);

            // Should setup first, to load the env_file.
            task.setup(&display_task_name, &mom_file.directory)?;

            if let Err(e) = task.run(args, mom_file, dry_run) {
                Err(TaskError::RuntimeError(format!(
                    "Error running task: {}",
                    e
                )))
            } else {
                Ok(())
            }
        } else {
            Err(TaskError::NotFound(task_name.to_string()))
        }
    }

    fn run_cmds_task(
        &self,
        task: &Task,
        cmd_index: usize,
        args: &ArgsContext,
        mom_file: &MomFile,
        dry_run: bool,
    ) -> Result<(), TaskError> {
        let mut task = task.clone();
        let task_name = format!("{}.cmds.{}", self.name, cmd_index);

        task.setup(&task_name, &mom_file.directory)?;

        // Should setup first, to load the env_file. This way the child task inherits from the parent,
        // but can override specific values

        let extend = &task.common.extend.clone();

        for base_name in extend.iter() {
            // Because the bases have been loaded already, there cannot be any circular dependencies
            // Todo, get reference to base task instead of cloning it
            let base_task = mom_file.clone_task(base_name);
            match base_task {
                Some(base_task) => task.extend(&base_task),
                None => {
                    return Err(TaskError::NotFound(base_name.to_string()));
                }
            }
        }

        // Done after setup and bases, so that the env and vars specified directly in the child take precedence
        task.common.env = task.get_env(&self.common.env);
        task.common.vars = task.get_vars(&self.common.vars);
        task.common.incl = task.get_templates(&self.common.incl);

        // This should load the mom file env and vars
        task.run(args, mom_file, dry_run).map_err(|e| e.into())
    }

    /// Runs the commands specified with the cmds option.
    fn run_cmds(
        &self,
        args: &ArgsContext,
        mom_file: &MomFile,
        env: &HashMap<String, String>,
        tera_instance: &mut tera::Tera,
        tera_context: &mut tera::Context,
        dry_run: bool,
    ) -> Result<(), TaskError> {
        for (i, cmd) in self.cmds.as_ref().unwrap().iter().enumerate() {
            match cmd {
                Cmd::Cmd(cmd) => {
                    self.run_cmds_cmd(cmd, i, mom_file, env, tera_instance, tera_context, dry_run)?;
                }
                Cmd::TaskName(task_name) => {
                    self.run_cmds_task_name(task_name, i, args, mom_file, dry_run)?;
                }
                Cmd::Task(task) => {
                    self.run_cmds_task(task, i, args, mom_file, dry_run)?;
                }
            }
        }
        Ok(())
    }

    /// Runs a script
    fn run_script(
        &self,
        mom_file: &MomFile,
        env: &HashMap<String, String>,
        tera_instance: &mut tera::Tera,
        tera_context: &mut tera::Context,
        dry_run: bool,
    ) -> Result<(), TaskError> {
        let script = self.script.as_ref().unwrap();

        let task_name = &self.name;
        let template_name = format!("tasks.{task_name}.script");
        tera_instance.add_raw_template(&template_name, script)?;
        let script = tera_instance.render(&template_name, tera_context)?;
        let default_script_extension = String::from(DEFAULT_SCRIPT_EXTENSION);
        let script_extension = self
            .script_extension
            .as_ref()
            .unwrap_or(&default_script_extension);

        let script_path = get_temp_script(
            &script,
            script_extension,
            &self.name,
            mom_file.filepath.as_path(),
        );

        let script_path = match script_path {
            Ok(path) => path,
            Err(e) => {
                return Err(TaskError::RuntimeError(format!(
                    "Error creating script file: {}",
                    e
                )))
            }
        };

        cfg_if::cfg_if! {
            if #[cfg(target_os = "windows")]
            {
                let script_path = script_path.to_str().unwrap();
                let script_path = script_path.replace('\\', "\\\\");
                tera_context.insert("script_path", &script_path);
            } else {
                tera_context.insert("script_path", &script_path);
            }
        }

        // Interpreter is a list, because sometimes there is need to pass extra arguments to the
        // interpreter, such as the /C option in the batch case
        let script_runner = if let Some(script_runner) = &self.script_runner {
            script_runner
        } else {
            DEFAULT_SCRIPT_RUNNER
        };

        let script_runner_template_name = format!("tasks.{task_name}.script_runner");
        tera_instance.add_raw_template(&script_runner_template_name, script_runner)?;

        let script_runner = tera_instance.render(&script_runner_template_name, tera_context)?;
        let script_runner_values = split_command(&script_runner);
        let script_runner_values = expand_args(&script_runner_values, env);
        let script_runner_values: Vec<&str> =
            script_runner_values.iter().map(|s| s.as_ref()).collect();
        let program = script_runner_values[0];
        let args = &script_runner_values[1..];

        let mut command = Command::new(program);

        // The script runner might not contain the actual script path, but we just leave it as a feature ;)
        command.args(args);

        self.set_command_basics(&mut command, mom_file, env)?;

        println!("{}", format!("{task_name}: {script_runner}").mom_info());
        println!("{}", "Script Begin:".mom_info());
        println!("{}", script.color(INFO_COLOR));
        println!("{}", "Script End.".mom_info());

        self.spawn_command(&mut command, dry_run)
    }
}