From 724fa2b09b10e47d2061ea5d518ffd367af604f4 Mon Sep 17 00:00:00 2001 From: Tyler Horth Date: Sun, 13 Dec 2020 17:00:25 -0800 Subject: [PATCH] add build only command --- cargo-aoc/src/app.rs | 20 ++++++++++++++++++-- cargo-aoc/src/main.rs | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/cargo-aoc/src/app.rs b/cargo-aoc/src/app.rs index ee23579..d62e0eb 100644 --- a/cargo-aoc/src/app.rs +++ b/cargo-aoc/src/app.rs @@ -135,7 +135,7 @@ impl AOCApp { Ok(()) } - pub fn execute_default(&self, args: &ArgMatches) -> Result<(), Box> { + fn generate_subproject(&self, args: &ArgMatches) -> Result<(), Box> { let day: Option = args .value_of("day") .map(|d| d.parse().expect("Failed to parse day")); @@ -221,8 +221,12 @@ impl AOCApp { fs::write("target/aoc/aoc-autobuild/src/main.rs", &main_content) .expect("failed to write src/main.rs"); + Ok(()) + } + + fn cargo_exec(args: &[&str]) { let status = process::Command::new("cargo") - .args(&["run", "--release"]) + .args(args) .current_dir("target/aoc/aoc-autobuild") .spawn() .expect("Failed to run cargo") @@ -232,6 +236,18 @@ impl AOCApp { if !status.success() { process::exit(status.code().unwrap_or(-1)); } + } + + pub fn execute_build(&self, args: &ArgMatches) -> Result<(), Box> { + self.generate_subproject(args)?; + Self::cargo_exec(&["build"]); + + Ok(()) + } + + pub fn execute_default(&self, args: &ArgMatches) -> Result<(), Box> { + self.generate_subproject(args)?; + Self::cargo_exec(&["run", "--release"]); Ok(()) } diff --git a/cargo-aoc/src/main.rs b/cargo-aoc/src/main.rs index ac9779a..d93b45d 100644 --- a/cargo-aoc/src/main.rs +++ b/cargo-aoc/src/main.rs @@ -112,6 +112,28 @@ fn main() { .takes_value(true), ), ) + .subcommand( + SubCommand::with_name("build") + .about("Builds the subproject for debugging purposes") + .arg( + Arg::with_name("day") + .short("d") + .help("Specifies the day. Defaults to today's date.") + .takes_value(true), + ) + .arg( + Arg::with_name("year") + .short("y") + .help("Specifies the year. Defaults to the current year.") + .takes_value(true), + ) + .arg( + Arg::with_name("input") + .short("i") + .help("Use an alternate input file.") + .takes_value(true), + ) + ) .get_matches(); // Creates an AOCApp that we'll use to launch actions (commands) @@ -122,14 +144,20 @@ fn main() { ("input", Some(m)) => app.execute_input(&m), ("bench", Some(m)) => { if let Err(e) = app.execute_bench(&m) { - eprintln!("An error occurs : {}", e.description()); + eprintln!("An error occurs : {}", e); + std::process::exit(-1); + } + } + ("build", Some(m)) => { + if let Err(e) = app.execute_build(&m) { + eprintln!("An error occurs : {}", e); std::process::exit(-1); } } (c, Some(_)) => panic!("Unknown command `{}`", c), _ => { if let Err(e) = app.execute_default(&matches) { - eprintln!("An error occurs : {}", e.description()); + eprintln!("An error occurs : {}", e); std::process::exit(-1); } }