From e0ee5f1d7fd706705f169074356280ea8b245d69 Mon Sep 17 00:00:00 2001 From: olivia Date: Fri, 23 Nov 2018 15:18:43 +0100 Subject: [PATCH] implement run command --- src/main.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8e7172ce..1a085ef0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ extern crate clap; extern crate console; extern crate indicatif; -use clap::{App, SubCommand}; +use clap::{App, Arg, SubCommand}; use console::{style, Emoji}; use indicatif::ProgressBar; use std::fs::remove_file; @@ -15,8 +15,11 @@ fn main() { .author("Olivia Hugger") .about("Test") .subcommand(SubCommand::with_name("verify").alias("v")) - .subcommand(SubCommand::with_name("run").alias("r")) - .get_matches(); + .subcommand( + SubCommand::with_name("run") + .alias("r") + .arg(Arg::with_name("file").required(true).index(1)), + ).get_matches(); println!(r#" _ _ _ "#); println!(r#" _ __ _ _ ___| |_| (_)_ __ __ _ ___ "#); @@ -26,6 +29,51 @@ fn main() { println!(r#" |___/ "#); println!(""); + if let Some(matches) = matches.subcommand_matches("run") { + if let Some(filename) = matches.value_of("file") { + 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().unwrap(); + } 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().unwrap(); + } + } 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().unwrap(); + } + } else { + panic!("Please supply a filename!"); + } + } + if let Some(_) = matches.subcommand_matches("verify") { compile_only("exercises/ex1.rs"); compile_only("exercises/ex2.rs");