2024-03-31 15:55:33 +01:00
|
|
|
use anyhow::{bail, Result};
|
|
|
|
use std::io::{self, stdout, Write};
|
2023-08-26 22:35:07 +01:00
|
|
|
use std::time::Duration;
|
2022-08-17 15:31:53 +01:00
|
|
|
|
2024-03-28 21:11:16 +00:00
|
|
|
use crate::embedded::{WriteStrategy, EMBEDDED_FILES};
|
2019-05-22 12:50:23 +01:00
|
|
|
use crate::exercise::{Exercise, Mode};
|
2019-01-09 21:04:08 +00:00
|
|
|
use crate::verify::test;
|
2019-01-09 19:33:43 +00:00
|
|
|
use indicatif::ProgressBar;
|
|
|
|
|
2020-06-04 15:31:17 +01: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)
|
2024-03-31 15:55:33 +01:00
|
|
|
pub fn run(exercise: &Exercise, verbose: bool) -> Result<()> {
|
2019-04-11 21:41:24 +01:00
|
|
|
match exercise.mode {
|
2024-03-31 15:55:33 +01:00
|
|
|
Mode::Test => test(exercise, verbose),
|
|
|
|
Mode::Compile | Mode::Clippy => compile_and_run(exercise),
|
2019-03-06 20:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-09 19:33:58 +00:00
|
|
|
|
2022-08-17 15:31:53 +01:00
|
|
|
// Resets the exercise by stashing the changes.
|
2024-03-28 21:11:16 +00:00
|
|
|
pub fn reset(exercise: &Exercise) -> io::Result<()> {
|
|
|
|
EMBEDDED_FILES.write_exercise_to_disk(&exercise.path, WriteStrategy::Overwrite)
|
2022-08-17 15:31:53 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 15:31:17 +01: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
|
2024-03-31 15:55:33 +01:00
|
|
|
fn compile_and_run(exercise: &Exercise) -> Result<()> {
|
2019-03-11 14:09:20 +00:00
|
|
|
let progress_bar = ProgressBar::new_spinner();
|
2024-03-31 15:55:33 +01:00
|
|
|
progress_bar.set_message(format!("Running {exercise}..."));
|
2023-08-26 22:35:07 +01:00
|
|
|
progress_bar.enable_steady_tick(Duration::from_millis(100));
|
2019-04-07 17:12:03 +01:00
|
|
|
|
2024-03-31 15:55:33 +01:00
|
|
|
let output = exercise.run()?;
|
2020-02-20 19:11:53 +00:00
|
|
|
progress_bar.finish_and_clear();
|
2019-03-06 20:47:00 +00:00
|
|
|
|
2024-03-31 15:55:33 +01:00
|
|
|
stdout().write_all(&output.stdout)?;
|
|
|
|
if !output.status.success() {
|
|
|
|
stdout().write_all(&output.stderr)?;
|
|
|
|
warn!("Ran {} with errors", exercise);
|
|
|
|
bail!("TODO");
|
2019-01-09 19:33:43 +00:00
|
|
|
}
|
2024-03-31 15:55:33 +01:00
|
|
|
|
|
|
|
success!("Successfully ran {}", exercise);
|
|
|
|
Ok(())
|
2019-01-09 19:33:43 +00:00
|
|
|
}
|