2019-04-07 16:12:03 +00:00
|
|
|
use crate::util;
|
2019-01-09 21:04:08 +00:00
|
|
|
use crate::verify::test;
|
2019-01-09 19:33:43 +00:00
|
|
|
use console::{style, Emoji};
|
|
|
|
use indicatif::ProgressBar;
|
2019-03-06 20:47:00 +00:00
|
|
|
use std::fs;
|
|
|
|
use toml::Value;
|
2019-01-09 19:33:43 +00:00
|
|
|
|
2019-03-06 20:47:00 +00:00
|
|
|
pub fn run(matches: clap::ArgMatches) -> Result<(), ()> {
|
2019-01-09 19:33:43 +00:00
|
|
|
if let Some(filename) = matches.value_of("file") {
|
2019-03-06 20:47:00 +00:00
|
|
|
let toml: Value = fs::read_to_string("info.toml").unwrap().parse().unwrap();
|
|
|
|
let tomlvec: &Vec<Value> = toml.get("exercises").unwrap().as_array().unwrap();
|
|
|
|
let mut exercises = tomlvec.clone();
|
|
|
|
exercises.retain(|i| i.get("path").unwrap().as_str().unwrap() == filename);
|
|
|
|
if exercises.is_empty() {
|
|
|
|
println!("No exercise found for your filename!");
|
|
|
|
std::process::exit(1);
|
2019-01-09 21:04:08 +00:00
|
|
|
}
|
2019-01-09 19:33:58 +00:00
|
|
|
|
2019-03-06 20:47:00 +00:00
|
|
|
let exercise: &Value = &exercises[0];
|
|
|
|
match exercise.get("mode").unwrap().as_str().unwrap() {
|
|
|
|
"test" => test(exercise.get("path").unwrap().as_str().unwrap())?,
|
|
|
|
"compile" => compile_and_run(exercise.get("path").unwrap().as_str().unwrap())?,
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
panic!("Please supply a filename!");
|
|
|
|
}
|
|
|
|
}
|
2019-01-09 19:33:58 +00:00
|
|
|
|
2019-03-06 20:47:00 +00:00
|
|
|
pub fn compile_and_run(filename: &str) -> Result<(), ()> {
|
2019-03-11 14:09:20 +00:00
|
|
|
let progress_bar = ProgressBar::new_spinner();
|
|
|
|
progress_bar.set_message(format!("Compiling {}...", filename).as_str());
|
|
|
|
progress_bar.enable_steady_tick(100);
|
2019-04-07 16:12:03 +00:00
|
|
|
|
|
|
|
let compilecmd = util::compile_cmd(filename);
|
2019-03-11 14:09:20 +00:00
|
|
|
progress_bar.set_message(format!("Running {}...", filename).as_str());
|
2019-03-06 20:47:00 +00:00
|
|
|
if compilecmd.status.success() {
|
2019-04-07 16:12:03 +00:00
|
|
|
let runcmd = util::run_cmd();
|
2019-03-11 14:09:20 +00:00
|
|
|
progress_bar.finish_and_clear();
|
2019-03-06 20:47:00 +00:00
|
|
|
|
|
|
|
if runcmd.status.success() {
|
|
|
|
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
|
|
|
|
let formatstr = format!("{} Successfully ran {}", Emoji("✅", "✓"), filename);
|
|
|
|
println!("{}", style(formatstr).green());
|
2019-04-07 16:12:03 +00:00
|
|
|
util::clean();
|
2019-03-06 20:47:00 +00:00
|
|
|
Ok(())
|
2019-01-09 19:33:43 +00:00
|
|
|
} else {
|
2019-03-06 20:47:00 +00:00
|
|
|
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
|
|
|
|
println!("{}", String::from_utf8_lossy(&runcmd.stderr));
|
|
|
|
|
|
|
|
let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename);
|
2019-01-09 19:33:43 +00:00
|
|
|
println!("{}", style(formatstr).red());
|
2019-04-07 16:12:03 +00:00
|
|
|
util::clean();
|
2019-03-06 20:47:00 +00:00
|
|
|
Err(())
|
2019-01-09 19:33:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-03-11 14:09:20 +00:00
|
|
|
progress_bar.finish_and_clear();
|
2019-03-06 20:47:00 +00:00
|
|
|
let formatstr = format!(
|
|
|
|
"{} Compilation of {} failed! Compiler error message:\n",
|
|
|
|
Emoji("⚠️ ", "!"),
|
|
|
|
filename
|
|
|
|
);
|
|
|
|
println!("{}", style(formatstr).red());
|
|
|
|
println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
|
2019-04-07 16:12:03 +00:00
|
|
|
util::clean();
|
2019-03-06 20:47:00 +00:00
|
|
|
Err(())
|
2019-01-09 19:33:43 +00:00
|
|
|
}
|
|
|
|
}
|