2022-08-17 10:31:53 -04:00
|
|
|
use std::process::Command;
|
2023-08-26 17:35:07 -04:00
|
|
|
use std::time::Duration;
|
2022-08-17 10:31:53 -04:00
|
|
|
|
2019-05-22 07:50:23 -04:00
|
|
|
use crate::exercise::{Exercise, Mode};
|
2019-01-09 16:04:08 -05:00
|
|
|
use crate::verify::test;
|
2019-01-09 14:33:43 -05:00
|
|
|
use indicatif::ProgressBar;
|
|
|
|
|
2020-06-04 10:31:17 -04:00
|
|
|
// Invoke the rust compiler on the path of the given exercise,
|
|
|
|
// and run the ensuing binary.
|
|
|
|
// The verbose argument helps determine whether or not to show
|
|
|
|
// the output from the test harnesses (if the mode of the exercise is test)
|
|
|
|
pub fn run(exercise: &Exercise, verbose: bool) -> Result<(), ()> {
|
2019-04-11 16:41:24 -04:00
|
|
|
match exercise.mode {
|
2020-06-04 10:31:17 -04:00
|
|
|
Mode::Test => test(exercise, verbose)?,
|
2019-04-11 16:41:24 -04:00
|
|
|
Mode::Compile => compile_and_run(exercise)?,
|
2020-02-14 09:25:03 -05:00
|
|
|
Mode::Clippy => compile_and_run(exercise)?,
|
2019-03-06 15:47:00 -05:00
|
|
|
}
|
2019-04-11 16:41:24 -04:00
|
|
|
Ok(())
|
2019-03-06 15:47:00 -05:00
|
|
|
}
|
2019-01-09 14:33:58 -05:00
|
|
|
|
2022-08-17 10:31:53 -04:00
|
|
|
// Resets the exercise by stashing the changes.
|
|
|
|
pub fn reset(exercise: &Exercise) -> Result<(), ()> {
|
|
|
|
let command = Command::new("git")
|
2024-03-26 12:48:06 -04:00
|
|
|
.arg("stash")
|
|
|
|
.arg("--")
|
2022-08-17 10:31:53 -04:00
|
|
|
.arg(&exercise.path)
|
|
|
|
.spawn();
|
|
|
|
|
|
|
|
match command {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_) => Err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 10:31:17 -04:00
|
|
|
// Invoke the rust compiler on the path of the given exercise
|
|
|
|
// and run the ensuing binary.
|
|
|
|
// This is strictly for non-test binaries, so output is displayed
|
2020-02-20 14:11:53 -05:00
|
|
|
fn compile_and_run(exercise: &Exercise) -> Result<(), ()> {
|
2019-03-11 10:09:20 -04:00
|
|
|
let progress_bar = ProgressBar::new_spinner();
|
2022-10-12 16:30:52 -04:00
|
|
|
progress_bar.set_message(format!("Compiling {exercise}..."));
|
2023-08-26 17:35:07 -04:00
|
|
|
progress_bar.enable_steady_tick(Duration::from_millis(100));
|
2019-04-07 12:12:03 -04:00
|
|
|
|
2020-02-20 14:11:53 -05:00
|
|
|
let compilation_result = exercise.compile();
|
|
|
|
let compilation = match compilation_result {
|
|
|
|
Ok(compilation) => compilation,
|
|
|
|
Err(output) => {
|
|
|
|
progress_bar.finish_and_clear();
|
|
|
|
warn!(
|
|
|
|
"Compilation of {} failed!, Compiler error message:\n",
|
|
|
|
exercise
|
|
|
|
);
|
|
|
|
println!("{}", output.stderr);
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-12 16:30:52 -04:00
|
|
|
progress_bar.set_message(format!("Running {exercise}..."));
|
2020-02-20 14:11:53 -05:00
|
|
|
let result = compilation.run();
|
|
|
|
progress_bar.finish_and_clear();
|
2019-03-06 15:47:00 -05:00
|
|
|
|
2020-02-20 14:11:53 -05:00
|
|
|
match result {
|
|
|
|
Ok(output) => {
|
|
|
|
println!("{}", output.stdout);
|
|
|
|
success!("Successfully ran {}", exercise);
|
2019-03-06 15:47:00 -05:00
|
|
|
Ok(())
|
2020-02-20 14:11:53 -05:00
|
|
|
}
|
|
|
|
Err(output) => {
|
|
|
|
println!("{}", output.stdout);
|
|
|
|
println!("{}", output.stderr);
|
2019-03-06 15:47:00 -05:00
|
|
|
|
2020-02-20 14:11:53 -05:00
|
|
|
warn!("Ran {} with errors", exercise);
|
2019-03-06 15:47:00 -05:00
|
|
|
Err(())
|
2019-01-09 14:33:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|