Extract command builders into util

This commit is contained in:
Chris Pearce 2019-04-07 17:12:03 +01:00
parent fbd0ccbd5b
commit 4fa79ee02f
3 changed files with 41 additions and 26 deletions

View file

@ -1,9 +1,8 @@
use crate::util::clean; use crate::util;
use crate::verify::test; use crate::verify::test;
use console::{style, Emoji}; use console::{style, Emoji};
use indicatif::ProgressBar; use indicatif::ProgressBar;
use std::fs; use std::fs;
use std::process::Command;
use toml::Value; use toml::Value;
pub fn run(matches: clap::ArgMatches) -> Result<(), ()> { pub fn run(matches: clap::ArgMatches) -> Result<(), ()> {
@ -33,20 +32,18 @@ pub fn compile_and_run(filename: &str) -> Result<(), ()> {
let progress_bar = ProgressBar::new_spinner(); let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Compiling {}...", filename).as_str()); progress_bar.set_message(format!("Compiling {}...", filename).as_str());
progress_bar.enable_steady_tick(100); progress_bar.enable_steady_tick(100);
let compilecmd = Command::new("rustc")
.args(&[filename, "-o", "temp", "--color", "always"]) let compilecmd = util::compile_cmd(filename);
.output()
.expect("fail");
progress_bar.set_message(format!("Running {}...", filename).as_str()); progress_bar.set_message(format!("Running {}...", filename).as_str());
if compilecmd.status.success() { if compilecmd.status.success() {
let runcmd = Command::new("./temp").output().expect("fail"); let runcmd = util::run_cmd();
progress_bar.finish_and_clear(); progress_bar.finish_and_clear();
if runcmd.status.success() { if runcmd.status.success() {
println!("{}", String::from_utf8_lossy(&runcmd.stdout)); println!("{}", String::from_utf8_lossy(&runcmd.stdout));
let formatstr = format!("{} Successfully ran {}", Emoji("", ""), filename); let formatstr = format!("{} Successfully ran {}", Emoji("", ""), filename);
println!("{}", style(formatstr).green()); println!("{}", style(formatstr).green());
clean(); util::clean();
Ok(()) Ok(())
} else { } else {
println!("{}", String::from_utf8_lossy(&runcmd.stdout)); println!("{}", String::from_utf8_lossy(&runcmd.stdout));
@ -54,7 +51,7 @@ pub fn compile_and_run(filename: &str) -> Result<(), ()> {
let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename); let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename);
println!("{}", style(formatstr).red()); println!("{}", style(formatstr).red());
clean(); util::clean();
Err(()) Err(())
} }
} else { } else {
@ -66,7 +63,7 @@ pub fn compile_and_run(filename: &str) -> Result<(), ()> {
); );
println!("{}", style(formatstr).red()); println!("{}", style(formatstr).red());
println!("{}", String::from_utf8_lossy(&compilecmd.stderr)); println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
clean(); util::clean();
Err(()) Err(())
} }
} }

View file

@ -1,4 +1,29 @@
use std::fs::remove_file; use std::fs::remove_file;
use std::process::{Command, Output};
const RUSTC_COLOR_ARGS: &[&str] = &["--color", "always"];
pub fn compile_test_cmd(filename: &str) -> Output {
Command::new("rustc")
.args(&["--test", filename, "-o", "temp"])
.args(RUSTC_COLOR_ARGS)
.output()
.expect("failed to compile exercise")
}
pub fn compile_cmd(filename: &str) -> Output {
Command::new("rustc")
.args(&[filename, "-o", "temp"])
.args(RUSTC_COLOR_ARGS)
.output()
.expect("failed to compile exercise")
}
pub fn run_cmd() -> Output {
Command::new("./temp")
.output()
.expect("failed to run exercise")
}
pub fn clean() { pub fn clean() {
let _ignored = remove_file("temp"); let _ignored = remove_file("temp");

View file

@ -1,8 +1,7 @@
use crate::util::clean; use crate::util;
use console::{style, Emoji}; use console::{style, Emoji};
use indicatif::ProgressBar; use indicatif::ProgressBar;
use std::fs; use std::fs;
use std::process::Command;
use toml::Value; use toml::Value;
pub fn verify(start_at: Option<&str>) -> Result<(), ()> { pub fn verify(start_at: Option<&str>) -> Result<(), ()> {
@ -34,10 +33,7 @@ fn compile_only(filename: &str) -> Result<(), ()> {
let progress_bar = ProgressBar::new_spinner(); let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Compiling {}...", filename).as_str()); progress_bar.set_message(format!("Compiling {}...", filename).as_str());
progress_bar.enable_steady_tick(100); progress_bar.enable_steady_tick(100);
let compilecmd = Command::new("rustc") let compilecmd = util::compile_cmd(filename);
.args(&[filename, "-o", "temp", "--color", "always"])
.output()
.expect("fail");
progress_bar.finish_and_clear(); progress_bar.finish_and_clear();
if compilecmd.status.success() { if compilecmd.status.success() {
let formatstr = format!( let formatstr = format!(
@ -46,7 +42,7 @@ fn compile_only(filename: &str) -> Result<(), ()> {
filename filename
); );
println!("{}", style(formatstr).green()); println!("{}", style(formatstr).green());
clean(); util::clean();
Ok(()) Ok(())
} else { } else {
let formatstr = format!( let formatstr = format!(
@ -56,7 +52,7 @@ fn compile_only(filename: &str) -> Result<(), ()> {
); );
println!("{}", style(formatstr).red()); println!("{}", style(formatstr).red());
println!("{}", String::from_utf8_lossy(&compilecmd.stderr)); println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
clean(); util::clean();
Err(()) Err(())
} }
} }
@ -65,19 +61,16 @@ pub fn test(filename: &str) -> Result<(), ()> {
let progress_bar = ProgressBar::new_spinner(); let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Testing {}...", filename).as_str()); progress_bar.set_message(format!("Testing {}...", filename).as_str());
progress_bar.enable_steady_tick(100); progress_bar.enable_steady_tick(100);
let testcmd = Command::new("rustc") let testcmd = util::compile_test_cmd(filename);
.args(&["--test", filename, "-o", "temp", "--color", "always"])
.output()
.expect("fail");
if testcmd.status.success() { if testcmd.status.success() {
progress_bar.set_message(format!("Running {}...", filename).as_str()); progress_bar.set_message(format!("Running {}...", filename).as_str());
let runcmd = Command::new("./temp").output().expect("fail"); let runcmd = util::run_cmd();
progress_bar.finish_and_clear(); progress_bar.finish_and_clear();
if runcmd.status.success() { if runcmd.status.success() {
let formatstr = format!("{} Successfully tested {}!", Emoji("", ""), filename); let formatstr = format!("{} Successfully tested {}!", Emoji("", ""), filename);
println!("{}", style(formatstr).green()); println!("{}", style(formatstr).green());
clean(); util::clean();
Ok(()) Ok(())
} else { } else {
let formatstr = format!( let formatstr = format!(
@ -87,7 +80,7 @@ pub fn test(filename: &str) -> Result<(), ()> {
); );
println!("{}", style(formatstr).red()); println!("{}", style(formatstr).red());
println!("{}", String::from_utf8_lossy(&runcmd.stdout)); println!("{}", String::from_utf8_lossy(&runcmd.stdout));
clean(); util::clean();
Err(()) Err(())
} }
} else { } else {
@ -99,7 +92,7 @@ pub fn test(filename: &str) -> Result<(), ()> {
); );
println!("{}", style(formatstr).red()); println!("{}", style(formatstr).red());
println!("{}", String::from_utf8_lossy(&testcmd.stderr)); println!("{}", String::from_utf8_lossy(&testcmd.stderr));
clean(); util::clean();
Err(()) Err(())
} }
} }