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
use std::{
    collections::HashMap,
    env,
    fs::{self, canonicalize},
    hint, io,
    path::Path,
    process::{self},
    sync::{
        atomic::{AtomicU8, Ordering},
        Mutex,
    },
    thread,
};

use crossbeam_utils::Backoff;
use dircpy::copy_dir;
use thiserror::Error;
use which::which;

use crate::{
    notifier::Notifier, parser::Spanned, Command, EnvCommand, FsCommand, IoCommand, Module, Unit,
};

#[derive(Debug, Error)]
pub enum RuntimeError {
    #[error("A dependency of this unit failed")]
    DependencyError(Spanned<String>),
    #[error("Dependency {1} failed preventing completion of {0}")]
    FailedDependency(String, Spanned<String>),
    #[error("Failed to execute {0:?}: {1}")]
    ExecutionFailure(Vec<Spanned<String>>, io::Error),
    #[error("{0}")]
    FsError(FsError),
    #[error("{0}")]
    JoinPathsError(env::JoinPathsError),
}

#[derive(Debug)]
pub struct Runtime {
    module: Module,
    notifier: Notifier,
    once: HashMap<Spanned<String>, AtomicU8>,
}

const UOS_INCOMPLETE: u8 = 0;
const UOS_IN_PROGRESS: u8 = 1;
const UOS_COMPLETE: u8 = 2;
const UOS_FAILED: u8 = 3;

impl Runtime {
    pub fn new(module: Module, notifier: Notifier) -> Self {
        let mut once = HashMap::with_capacity(module.units.len());
        for name in module.units.keys() {
            once.insert(name.clone(), AtomicU8::new(UOS_INCOMPLETE));
        }

        Self {
            module,
            notifier,
            once,
        }
    }

    pub fn notifier(&self) -> &Notifier {
        &self.notifier
    }

    fn get_unit(&self, name: impl Into<String>) -> (&Spanned<String>, &Unit) {
        self.module
            .units
            .get_key_value(&Spanned::new(name.into()))
            .unwrap()
    }

    fn get_uos(&self, name: impl Into<String>) -> &AtomicU8 {
        self.once.get(&Spanned::new(name.into())).unwrap()
    }

    pub fn run(&self, unit_name: &str) -> Result<(), RuntimeError> {
        let (unit_span, unit) = self.get_unit(unit_name);
        let errors = Mutex::new(Vec::new());

        self.notifier.run(unit_name);

        if !unit.depends_on.is_empty() {
            thread::scope(|s| {
                let mut deps = unit
                    .depends_on
                    .iter()
                    .filter(|d| self.get_uos(d.inner()).load(Ordering::Acquire) == UOS_INCOMPLETE);
                let first = deps.next();

                #[allow(clippy::while_let_on_iterator)]
                while let Some(name) = deps.next() {
                    let uos_state = self.get_uos(name.inner());
                    let uos = uos_state.compare_exchange(
                        UOS_INCOMPLETE,
                        UOS_IN_PROGRESS,
                        Ordering::Acquire,
                        Ordering::Relaxed,
                    );
                    if uos.is_ok() {
                        s.spawn(|| {
                            self.notifier.start_dep(unit_name, name.inner());
                            if let Err(e) = self.run(name.inner()) {
                                uos_state.store(UOS_FAILED, Ordering::Release);
                                errors.lock().unwrap().push(e);
                            } else {
                                uos_state.store(UOS_COMPLETE, Ordering::Release);
                            }
                        });
                    } else if Err(UOS_FAILED) == uos {
                        errors.lock().unwrap().push(RuntimeError::FailedDependency(
                            unit_name.to_string(),
                            name.clone(),
                        ));
                    }
                }

                if let Some(first) = first {
                    let uos_state = self.get_uos(first.inner());
                    let uos = uos_state.compare_exchange(
                        UOS_INCOMPLETE,
                        UOS_IN_PROGRESS,
                        Ordering::Acquire,
                        Ordering::Relaxed,
                    );
                    if uos.is_ok() {
                        self.notifier.start_dep(unit_name, first.inner());
                        if let Err(e) = self.run(first.inner()) {
                            uos_state.store(UOS_FAILED, Ordering::Release);
                            errors.lock().unwrap().push(e);
                        } else {
                            uos_state.store(UOS_COMPLETE, Ordering::Release);
                        }
                    } else if Err(UOS_FAILED) == uos {
                        errors.lock().unwrap().push(RuntimeError::FailedDependency(
                            unit_name.to_string(),
                            first.clone(),
                        ));
                    }
                }
            });

            let mut errors = errors.into_inner().unwrap();
            unit.depends_on
                .iter()
                .filter_map(|d| {
                    let uos = self.get_uos(d.inner());
                    if uos.load(Ordering::Acquire) == UOS_IN_PROGRESS {
                        Some((d, uos))
                    } else {
                        None
                    }
                })
                .for_each(|(d, uos)| {
                    self.notifier.block_on_dep(unit_name, d.inner());
                    let backoff = Backoff::new();
                    while uos.load(Ordering::Acquire) < UOS_COMPLETE {
                        // WARN: Graphic depiction of long running spin loop
                        hint::spin_loop();
                        backoff.snooze();
                    }
                    if uos.load(Ordering::Relaxed) == UOS_FAILED {
                        errors.push(RuntimeError::FailedDependency(
                            unit_name.to_string(),
                            d.clone(),
                        ));
                    }
                });

            if !errors.is_empty() {
                self.notifier.err(&errors);
                return Err(RuntimeError::DependencyError(unit_span.clone()));
            }
        }

        for cmd in &unit.commands {
            cmd.call(self)?;
        }

        self.notifier.complete(unit_name);

        Ok(())
    }
}

impl Command {
    pub fn call(&self, rt: &Runtime) -> Result<(), RuntimeError> {
        use Command::*;

        rt.notifier.call(self);

        match self {
            // no op, shouldn't be Vec<Command>
            DependsOn(_) => Ok(()),
            Meta(_) => Ok(()),

            Do(units) => units.iter().try_for_each(|unit| rt.run(unit.inner())),
            Exec(cmd) => exec(cmd),

            Fs(cmd) => cmd.call(),
            Io(cmd) => cmd.call(),
            Env(cmd) => cmd.call(),
        }
    }
}

fn exec(cmd: &[Spanned<String>]) -> Result<(), RuntimeError> {
    let prog = &cmd[0];
    let args = cmd[1..].iter().map(Spanned::inner);

    let exec = process::Command::new(which(prog.inner()).map_err(|e| {
        RuntimeError::ExecutionFailure(
            cmd.to_vec(),
            io::Error::new(io::ErrorKind::NotFound, e.to_string()),
        )
    })?)
    .args(args)
    .output()
    .map_err(|io| RuntimeError::ExecutionFailure(cmd.to_vec(), io))?;

    if !exec.status.success() {
        return Err(RuntimeError::ExecutionFailure(
            cmd.to_vec(),
            io::Error::new(
                io::ErrorKind::Other,
                format!("Process returned non-successfully with {}.", exec.status),
            ),
        ));
    }

    Ok(())
}

impl FsCommand {
    pub fn call(&self) -> Result<(), RuntimeError> {
        use FsCommand::*;

        match self {
            Create(p) => fs::File::create(p.inner())
                .map(|_| ())
                .map_err(|io| FsError::CreateFileError(p.clone(), io)),
            CreateDir(p) => {
                fs::create_dir_all(p.inner()).map_err(|io| FsError::CreateDirError(p.clone(), io))
            }
            Remove(p) => {
                let path: &Path = p.inner().as_ref();
                if path.is_dir() {
                    fs::remove_dir_all(path)
                } else {
                    fs::remove_file(path)
                }
                .map_err(|io| FsError::RemoveError(p.clone(), io))
            }
            Copy(src, dst) => {
                let src_path: &Path = src.inner().as_ref();
                if src_path.is_dir() {
                    copy_dir(src_path, dst.inner())
                } else {
                    fs::copy(src_path, dst.inner()).map(|_| ())
                }
                .map_err(|io| FsError::CopyError(src.clone(), dst.clone(), io))
            }
            Move(src, dst) => {
                let src_path: &Path = src.inner().as_ref();
                if src_path.is_dir() {
                    copy_dir(src_path, dst.inner())
                } else {
                    fs::copy(src_path, dst.inner()).map(|_| ())
                }
                .map_err(|io| {
                    RuntimeError::FsError(FsError::CopyError(src.clone(), dst.clone(), io))
                })?;

                if src_path.is_dir() {
                    fs::remove_dir_all(src_path)
                } else {
                    fs::remove_file(src_path)
                }
                .map_err(|io| FsError::RemoveError(src.clone(), io))
            }
            PrintFile(p) => {
                let contents = fs::read_to_string(p.inner())
                    .map_err(|io| RuntimeError::FsError(FsError::FileAccessError(p.clone(), io)))?;
                println!("{contents}");
                Ok(())
            }
            EPrintFile(p) => {
                let contents = fs::read_to_string(p.inner())
                    .map_err(|io| RuntimeError::FsError(FsError::FileAccessError(p.clone(), io)))?;
                eprintln!("{contents}");
                Ok(())
            }
        }
        .map_err(RuntimeError::FsError)
    }
}

#[derive(Debug, Error)]
pub enum FsError {
    #[error("Failed to create file {0}")]
    CreateFileError(Spanned<String>, io::Error),
    #[error("Failed to create directory {0}")]
    CreateDirError(Spanned<String>, io::Error),
    #[error("Failed to remove {0}")]
    RemoveError(Spanned<String>, io::Error),
    #[error("Failed to get contents of file {0}")]
    FileAccessError(Spanned<String>, io::Error),
    #[error("Failed to copy {0} to {1}")]
    CopyError(Spanned<String>, Spanned<String>, io::Error),
}

impl IoCommand {
    pub fn call(&self) -> Result<(), RuntimeError> {
        use IoCommand::*;

        match self {
            PrintLn(t) => println!("{t}"),
            Print(t) => print!("{t}"),
            EPrintLn(t) => eprintln!("{t}"),
            EPrint(t) => eprint!("{t}"),
        };

        Ok(())
    }
}

impl EnvCommand {
    pub fn call(&self) -> Result<(), RuntimeError> {
        use EnvCommand::*;

        match self {
            SetVar(var, val) => {
                env::set_var(var.inner(), val.inner());
                Ok(())
            }
            RemoveVar(var) => {
                env::remove_var(var.inner());
                Ok(())
            }
            PathPush(p) => {
                let mut path_var: Vec<_> = env::var_os("PATH")
                    .map(|i| env::split_paths(&i).collect())
                    .unwrap_or_default();
                path_var.push(canonicalize(p.inner()).unwrap());

                let new_path = env::join_paths(path_var).map_err(RuntimeError::JoinPathsError)?;
                env::set_var("PATH", new_path);

                Ok(())
            }
            PathRemove(p) => {
                if let Some(i) = env::var_os("PATH") {
                    let mut path_var = env::split_paths(&i).collect::<Vec<_>>();
                    path_var.retain(|i| i != &canonicalize(p.inner()).unwrap());

                    let new_path =
                        env::join_paths(path_var).map_err(RuntimeError::JoinPathsError)?;
                    env::set_var("PATH", new_path);
                }

                Ok(())
            }
        }
    }
}