2019-01-09 19:09:49 +00:00
|
|
|
use clap::{App, Arg, SubCommand, crate_version};
|
2018-11-09 19:31:14 +00:00
|
|
|
use console::{style, Emoji};
|
2018-11-14 19:12:20 +00:00
|
|
|
use indicatif::ProgressBar;
|
2018-11-26 10:10:38 +00:00
|
|
|
use syntect::easy::HighlightFile;
|
|
|
|
use syntect::parsing::SyntaxSet;
|
|
|
|
use syntect::highlighting::{ThemeSet, Style};
|
2019-01-09 19:09:49 +00:00
|
|
|
use syntect::util::{as_24_bit_terminal_escaped};
|
2018-11-09 21:31:54 +00:00
|
|
|
use std::fs::remove_file;
|
2018-11-26 10:10:38 +00:00
|
|
|
use std::io::BufRead;
|
2018-11-14 19:12:20 +00:00
|
|
|
use std::process::Command;
|
2018-11-26 11:41:39 +00:00
|
|
|
use std::sync::mpsc::channel;
|
|
|
|
use std::time::Duration;
|
|
|
|
use notify::DebouncedEvent;
|
|
|
|
use notify::{RecommendedWatcher, Watcher, RecursiveMode};
|
2018-05-14 17:41:58 +01:00
|
|
|
|
2018-11-09 19:31:14 +00:00
|
|
|
fn main() {
|
2018-11-26 10:10:38 +00:00
|
|
|
let matches = App::new("rustlings")
|
2018-11-14 19:12:20 +00:00
|
|
|
.version(crate_version!())
|
|
|
|
.author("Olivia Hugger")
|
|
|
|
.about("Test")
|
|
|
|
.subcommand(SubCommand::with_name("verify").alias("v"))
|
2018-11-26 11:41:39 +00:00
|
|
|
.subcommand(SubCommand::with_name("watch").alias("w"))
|
2018-11-23 14:18:43 +00:00
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("run")
|
|
|
|
.alias("r")
|
|
|
|
.arg(Arg::with_name("file").required(true).index(1)),
|
|
|
|
).get_matches();
|
2018-11-14 19:12:20 +00:00
|
|
|
|
2018-11-26 10:10:38 +00:00
|
|
|
let ss = SyntaxSet::load_defaults_newlines();
|
|
|
|
let ts = ThemeSet::load_defaults();
|
|
|
|
|
2018-11-14 19:12:20 +00:00
|
|
|
println!(r#" _ _ _ "#);
|
|
|
|
println!(r#" _ __ _ _ ___| |_| (_)_ __ __ _ ___ "#);
|
|
|
|
println!(r#" | '__| | | / __| __| | | '_ \ / _` / __| "#);
|
|
|
|
println!(r#" | | | |_| \__ \ |_| | | | | | (_| \__ \ "#);
|
|
|
|
println!(r#" |_| \__,_|___/\__|_|_|_| |_|\__, |___/ "#);
|
|
|
|
println!(r#" |___/ "#);
|
|
|
|
println!("");
|
|
|
|
|
2018-11-23 14:18:43 +00:00
|
|
|
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());
|
2018-11-26 11:41:39 +00:00
|
|
|
clean();
|
2018-11-23 14:18:43 +00:00
|
|
|
} 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());
|
2018-11-26 11:41:39 +00:00
|
|
|
clean();
|
2018-11-23 14:18:43 +00:00
|
|
|
}
|
|
|
|
} 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));
|
2018-11-26 11:41:39 +00:00
|
|
|
clean();
|
2018-11-23 14:18:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic!("Please supply a filename!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 19:12:20 +00:00
|
|
|
if let Some(_) = matches.subcommand_matches("verify") {
|
2018-11-26 11:41:39 +00:00
|
|
|
match verify() {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(_) => std::process::exit(1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(_) = matches.subcommand_matches("watch") {
|
|
|
|
watch().unwrap();
|
2018-11-14 19:12:20 +00:00
|
|
|
}
|
2018-11-26 10:10:38 +00:00
|
|
|
|
|
|
|
if let None = matches.subcommand_name() {
|
2018-11-26 10:29:39 +00:00
|
|
|
let mut highlighter = HighlightFile::new("default_out.md", &ss, &ts.themes["base16-eighties.dark"]).unwrap();
|
2018-11-26 10:10:38 +00:00
|
|
|
for maybe_line in highlighter.reader.lines() {
|
|
|
|
let line = maybe_line.unwrap();
|
|
|
|
let regions: Vec<(Style, &str)> = highlighter.highlight_lines.highlight(&line, &ss);
|
|
|
|
println!("{}", as_24_bit_terminal_escaped(®ions[..], true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("\x1b[0m");
|
2018-05-06 17:59:50 +01:00
|
|
|
}
|
|
|
|
|
2018-11-26 11:41:39 +00:00
|
|
|
fn watch() -> notify::Result<()> {
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
|
|
|
|
let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(2))?;
|
|
|
|
watcher.watch("./exercises", RecursiveMode::Recursive)?;
|
|
|
|
|
|
|
|
let _ignored = verify();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match rx.recv() {
|
|
|
|
Ok(event) => {
|
|
|
|
match event {
|
|
|
|
DebouncedEvent::Chmod(_)
|
|
|
|
| DebouncedEvent::Write(_) => {
|
|
|
|
let _ignored = verify();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => println!("watch error: {:?}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verify() -> Result<(), ()> {
|
|
|
|
compile_only("exercises/ex1.rs")?;
|
|
|
|
compile_only("exercises/ex2.rs")?;
|
|
|
|
compile_only("exercises/ex3.rs")?;
|
|
|
|
compile_only("exercises/ex4.rs")?;
|
|
|
|
compile_only("exercises/ex5.rs")?;
|
|
|
|
compile_only("exercises/variables/variables1.rs")?;
|
|
|
|
compile_only("exercises/variables/variables2.rs")?;
|
|
|
|
compile_only("exercises/variables/variables3.rs")?;
|
|
|
|
compile_only("exercises/variables/variables4.rs")?;
|
|
|
|
compile_only("exercises/functions/functions1.rs")?;
|
|
|
|
compile_only("exercises/functions/functions2.rs")?;
|
|
|
|
compile_only("exercises/functions/functions3.rs")?;
|
|
|
|
compile_only("exercises/functions/functions4.rs")?;
|
|
|
|
compile_only("exercises/functions/functions5.rs")?;
|
|
|
|
compile_only("exercises/primitive_types/primitive_types1.rs")?;
|
|
|
|
compile_only("exercises/primitive_types/primitive_types2.rs")?;
|
|
|
|
compile_only("exercises/primitive_types/primitive_types3.rs")?;
|
|
|
|
compile_only("exercises/primitive_types/primitive_types4.rs")?;
|
|
|
|
compile_only("exercises/primitive_types/primitive_types5.rs")?;
|
|
|
|
compile_only("exercises/primitive_types/primitive_types6.rs")?;
|
|
|
|
test("exercises/tests/tests1.rs")?;
|
|
|
|
test("exercises/tests/tests2.rs")?;
|
|
|
|
test("exercises/tests/tests3.rs")?;
|
|
|
|
test("exercises/tests/tests4.rs")?;
|
|
|
|
test("exercises/if/if1.rs")?;
|
|
|
|
compile_only("exercises/strings/strings1.rs")?;
|
|
|
|
compile_only("exercises/strings/strings2.rs")?;
|
|
|
|
compile_only("exercises/strings/strings3.rs")?;
|
|
|
|
compile_only("exercises/move_semantics/move_semantics1.rs")?;
|
|
|
|
compile_only("exercises/move_semantics/move_semantics2.rs")?;
|
|
|
|
compile_only("exercises/move_semantics/move_semantics3.rs")?;
|
|
|
|
compile_only("exercises/move_semantics/move_semantics4.rs")?;
|
|
|
|
compile_only("exercises/modules/modules1.rs")?;
|
|
|
|
compile_only("exercises/modules/modules2.rs")?;
|
|
|
|
compile_only("exercises/macros/macros1.rs")?;
|
|
|
|
compile_only("exercises/macros/macros2.rs")?;
|
|
|
|
compile_only("exercises/macros/macros3.rs")?;
|
|
|
|
compile_only("exercises/macros/macros4.rs")?;
|
|
|
|
test("exercises/error_handling/errors1.rs")?;
|
|
|
|
test("exercises/error_handling/errors2.rs")?;
|
|
|
|
test("exercises/error_handling/errors3.rs")?;
|
|
|
|
test("exercises/error_handling/errorsn.rs")?;
|
|
|
|
compile_only("exercises/error_handling/option1.rs")?;
|
|
|
|
test("exercises/error_handling/result1.rs")?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compile_only(filename: &str) -> Result<(), ()> {
|
2018-11-14 19:12:20 +00:00
|
|
|
let bar = ProgressBar::new_spinner();
|
|
|
|
bar.set_message(format!("Compiling {}...", filename).as_str());
|
|
|
|
bar.enable_steady_tick(100);
|
|
|
|
let compilecmd = Command::new("rustc")
|
2018-11-26 09:54:08 +00:00
|
|
|
.args(&[filename, "-o", "temp", "--color", "always"])
|
2018-11-14 19:12:20 +00:00
|
|
|
.output()
|
|
|
|
.expect("fail");
|
|
|
|
bar.finish_and_clear();
|
|
|
|
if compilecmd.status.success() {
|
|
|
|
let formatstr = format!(
|
|
|
|
"{} Successfully compiled {}!",
|
|
|
|
Emoji("✅", "✓"),
|
|
|
|
filename
|
|
|
|
);
|
|
|
|
println!("{}", style(formatstr).green());
|
2018-11-26 11:41:39 +00:00
|
|
|
clean();
|
|
|
|
Ok(())
|
2018-11-14 19:12:20 +00:00
|
|
|
} else {
|
|
|
|
let formatstr = format!(
|
|
|
|
"{} Compilation of {} failed! Compiler error message:\n",
|
|
|
|
Emoji("⚠️ ", "!"),
|
|
|
|
filename
|
|
|
|
);
|
|
|
|
println!("{}", style(formatstr).red());
|
|
|
|
println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
|
2018-11-26 11:41:39 +00:00
|
|
|
clean();
|
|
|
|
Err(())
|
2018-11-14 19:12:20 +00:00
|
|
|
}
|
2018-11-09 19:31:14 +00:00
|
|
|
}
|
2018-11-09 21:31:54 +00:00
|
|
|
|
2018-11-26 11:41:39 +00:00
|
|
|
fn test(filename: &str) -> Result<(), ()> {
|
2018-11-14 19:12:20 +00:00
|
|
|
let bar = ProgressBar::new_spinner();
|
|
|
|
bar.set_message(format!("Testing {}...", filename).as_str());
|
|
|
|
bar.enable_steady_tick(100);
|
|
|
|
let testcmd = Command::new("rustc")
|
|
|
|
.args(&["--test", filename, "-o", "temp"])
|
|
|
|
.output()
|
|
|
|
.expect("fail");
|
|
|
|
bar.finish_and_clear();
|
|
|
|
if testcmd.status.success() {
|
|
|
|
let formatstr = format!("{} Successfully tested {}!", Emoji("✅", "✓"), filename);
|
|
|
|
println!("{}", style(formatstr).green());
|
2018-11-26 11:41:39 +00:00
|
|
|
clean();
|
|
|
|
Ok(())
|
2018-11-14 19:12:20 +00:00
|
|
|
} else {
|
|
|
|
let formatstr = format!(
|
|
|
|
"{} Testing of {} failed! Please try again.",
|
|
|
|
Emoji("⚠️ ", "!"),
|
|
|
|
filename
|
|
|
|
);
|
|
|
|
println!("{}", style(formatstr).red());
|
2018-11-26 11:41:39 +00:00
|
|
|
clean();
|
|
|
|
Err(())
|
2018-11-14 19:12:20 +00:00
|
|
|
}
|
2018-11-09 21:31:54 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 11:41:39 +00:00
|
|
|
fn clean() {
|
|
|
|
let _ignored = remove_file("temp");
|
2018-11-09 21:31:54 +00:00
|
|
|
}
|