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
use self::output::RuntimeError;
use crate::{runtime::output::Notification, Command, CommandDefinition, InternalCommand, Task};
use run_script::run_script;
use std::{collections::HashMap, path::PathBuf};

pub mod cli;
pub mod output;

pub struct ExecutionPool {
    pub tasks: HashMap<String, Task>,
    pub cmd_defs: HashMap<Command, CommandDefinition>,
    pub active_tasks: Vec<String>,
}

impl ExecutionPool {
    pub fn new(
        tasks: HashMap<String, Task>,
        cmd_defs: HashMap<Command, CommandDefinition>,
    ) -> Self {
        Self {
            tasks,
            cmd_defs,
            active_tasks: Vec::new(),
        }
    }

    pub fn run(&mut self, main_task: impl Into<String>) {
        let main_task = main_task.into();
        Notification::Start {
            build_goal: main_task.clone(),
        }
        .print();
        match self.deploy_task(main_task) {
            Ok(_) => {
                Notification::Completion.print();
            }
            Err(e) => {
                Notification::Fail { error: e }.print();
                std::process::exit(1);
            }
        }
    }

    pub fn purge_task_as_dependency(&mut self, task_name: impl Into<String>) {
        let task_name = task_name.into();

        self.tasks.iter_mut().for_each(|(_, v)| {
            v.dependencies.remove(&task_name);
        });
    }

    pub fn deploy_task(&mut self, task_name: impl Into<String>) -> Result<(), RuntimeError> {
        let task_name = task_name.into();
        let task = self.tasks.get(&task_name).expect("Bad developer").clone();
        self.active_tasks.push(task_name.to_owned());

        Notification::TaskRun {
            task_name: task_name.clone(),
            dep_names: task.dependencies.clone(),
        }
        .print();

        if !task.dependencies.is_empty() {
            for dep in &task.dependencies {
                if self.active_tasks.contains(dep) {
                    continue;
                }

                Notification::DependencyRun {
                    dep_name: dep.clone(),
                }
                .print();
                self.deploy_task(dep)?;
            }
        }

        for command in &task.commands {
            self.run_command(command)?;
        }

        self.purge_task_as_dependency(task_name);
        Ok(())
    }

    pub fn run_command(&self, command: &Command) -> Result<(), RuntimeError> {
        match command {
            Command::Internal(ic) => {
                Notification::CommandRun {
                    command: command.to_owned(),
                }
                .print();
                match ic {
                    InternalCommand::Exec(e) => {
                        let (code, _, _) = run_script!(e)?;
                        if code != 0 {
                            return Err(RuntimeError::ProcessFailure(e.to_string(), code));
                        }
                    }
                    InternalCommand::MakeFile(f) => {
                        std::fs::write(PathBuf::from(f), "")?;
                    }
                    InternalCommand::MakeDirectory(d) => {
                        std::fs::create_dir_all(PathBuf::from(d))?;
                    }
                    InternalCommand::SetEnvironmentVar(v, c) => {
                        std::env::set_var(v, c);
                    }
                    InternalCommand::PrintString(t) => {
                        println!("{t}");
                    }
                    InternalCommand::PrintFile(f) => {
                        let file = std::fs::read_to_string(f)?;
                        println!("{file}");
                    }
                    InternalCommand::RemoveDirectory(d) => {
                        std::fs::remove_dir_all(d)?;
                    }
                    InternalCommand::RemoveFile(f) => {
                        std::fs::remove_file(f)?;
                    }
                    InternalCommand::CopyFile(s, d) => {
                        std::fs::copy(s, d)?;
                    }
                    InternalCommand::MoveFile(s, d) => {
                        std::fs::copy(s, d)?;
                        std::fs::remove_file(s)?;
                    }
                }
            }
            _ => {
                Notification::CommandDefinition {
                    command: command.to_owned(),
                }
                .print();

                let def = match self.cmd_defs.get(command) {
                    Some(d) => d,
                    None => {
                        return Err(RuntimeError::NonExistentCommand(command.to_owned()));
                    }
                };
                for cmd in &def.commands {
                    self.run_command(cmd)?;
                }
            }
        }
        Ok(())
    }
}