summaryrefslogtreecommitdiff
path: root/src/run.rs
blob: 809b79daf451254af188c69b87a1233489575aae (plain)
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
use crate::util::clean;
use crate::verify::test;
use console::{style, Emoji};
use indicatif::ProgressBar;
use std::process::Command;

pub fn run(matches: clap::ArgMatches) {
    if let Some(filename) = matches.value_of("file") {
        if matches.is_present("test") {
            match test(filename) {
                Ok(_) => (),
                Err(_) => (),
            }
            std::process::exit(0);
        }
        let bar = ProgressBar::new_spinner();
        bar.set_message(format!("Compiling {}...", filename).as_str());
        bar.enable_steady_tick(100);
        let compilecmd = Command::new("rustc")
            .args(&[filename, "-o", "temp"])
            .output()
            .expect("fail");
        bar.set_message(format!("Running {}...", filename).as_str());
        if compilecmd.status.success() {
            let runcmd = Command::new("./temp").output().expect("fail");
            bar.finish_and_clear();

            if runcmd.status.success() {
                println!("{}", String::from_utf8_lossy(&runcmd.stdout));
                let formatstr = format!("{} Successfully ran {}", Emoji("✅", "✓"), filename);
                println!("{}", style(formatstr).green());
                clean();
            } else {
                println!("{}", String::from_utf8_lossy(&runcmd.stdout));
                println!("{}", String::from_utf8_lossy(&runcmd.stderr));

                let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename);
                println!("{}", style(formatstr).red());
                clean();
            }
        } else {
            bar.finish_and_clear();
            let formatstr = format!(
                "{} Compilation of {} failed! Compiler error message:\n",
                Emoji("⚠️ ", "!"),
                filename
            );
            println!("{}", style(formatstr).red());
            println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
            clean();
        }
    } else {
        panic!("Please supply a filename!");
    }
}