Move all run options into a struct (#16)

In preparation for adding a --quiet flag, since the number of parameters
to run is getting a bit silly.
This commit is contained in:
Casey Rodarmor 2016-11-05 00:31:38 -07:00 committed by GitHub
parent 416f3b3d59
commit 599cc80f86
3 changed files with 34 additions and 24 deletions

View File

@ -160,10 +160,13 @@ pub fn app() {
die!("Justfile contains no recipes"); die!("Justfile contains no recipes");
}; };
let dry_run = matches.is_present("dry-run"); let options = super::RunOptions {
let evaluate = matches.is_present("evaluate"); dry_run: matches.is_present("dry-run"),
evaluate: matches.is_present("evaluate"),
overrides: overrides,
};
if let Err(run_error) = justfile.run(&overrides, &arguments, dry_run, evaluate) { if let Err(run_error) = justfile.run(&arguments, &options) {
warn!("{}", run_error); warn!("{}", run_error);
match run_error { match run_error {
RunError::Code{code, .. } | RunError::BacktickCode{code, ..} => process::exit(code), RunError::Code{code, .. } | RunError::BacktickCode{code, ..} => process::exit(code),

View File

@ -832,6 +832,13 @@ struct Justfile<'a> {
exports: Set<&'a str>, exports: Set<&'a str>,
} }
#[derive(Default)]
struct RunOptions<'a> {
dry_run: bool,
evaluate: bool,
overrides: Map<&'a str, &'a str>,
}
impl<'a, 'b> Justfile<'a> where 'a: 'b { impl<'a, 'b> Justfile<'a> where 'a: 'b {
fn first(&self) -> Option<&'a str> { fn first(&self) -> Option<&'a str> {
let mut first: Option<&Recipe<'a>> = None; let mut first: Option<&Recipe<'a>> = None;
@ -857,12 +864,10 @@ impl<'a, 'b> Justfile<'a> where 'a: 'b {
fn run( fn run(
&'a self, &'a self,
overrides: &Map<&'a str, &'a str>,
arguments: &[&'a str], arguments: &[&'a str],
dry_run: bool, options: &RunOptions<'a>,
evaluate: bool,
) -> Result<(), RunError<'a>> { ) -> Result<(), RunError<'a>> {
let unknown_overrides = overrides.keys().cloned() let unknown_overrides = options.overrides.keys().cloned()
.filter(|name| !self.assignments.contains_key(name)) .filter(|name| !self.assignments.contains_key(name))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -870,8 +875,8 @@ impl<'a, 'b> Justfile<'a> where 'a: 'b {
return Err(RunError::UnknownOverrides{overrides: unknown_overrides}); return Err(RunError::UnknownOverrides{overrides: unknown_overrides});
} }
let scope = try!(evaluate_assignments(&self.assignments, overrides)); let scope = try!(evaluate_assignments(&self.assignments, &options.overrides));
if evaluate { if options.evaluate {
for (name, value) in scope { for (name, value) in scope {
println!("{} = \"{}\"", name, value); println!("{} = \"{}\"", name, value);
} }
@ -894,7 +899,7 @@ impl<'a, 'b> Justfile<'a> where 'a: 'b {
expected: recipe.parameters.len(), expected: recipe.parameters.len(),
}); });
} }
try!(self.run_recipe(recipe, rest, &scope, &mut ran, dry_run)); try!(self.run_recipe(recipe, rest, &scope, &mut ran, options.dry_run));
return Ok(()); return Ok(());
} }
} else { } else {
@ -912,7 +917,7 @@ impl<'a, 'b> Justfile<'a> where 'a: 'b {
return Err(RunError::UnknownRecipes{recipes: missing}); return Err(RunError::UnknownRecipes{recipes: missing});
} }
for recipe in arguments.iter().map(|name| &self.recipes[name]) { for recipe in arguments.iter().map(|name| &self.recipes[name]) {
try!(self.run_recipe(recipe, &[], &scope, &mut ran, dry_run)); try!(self.run_recipe(recipe, &[], &scope, &mut ran, options.dry_run));
} }
Ok(()) Ok(())
} }

View File

@ -1,9 +1,8 @@
extern crate tempdir; extern crate tempdir;
extern crate brev; extern crate brev;
use super::{Token, Error, ErrorKind, Justfile, RunError}; use super::{Token, Error, ErrorKind, Justfile, RunError, RunOptions};
use super::TokenKind::*; use super::TokenKind::*;
use std::collections::BTreeMap as Map;
fn tokenize_success(text: &str, expected_summary: &str) { fn tokenize_success(text: &str, expected_summary: &str) {
let tokens = super::tokenize(text).unwrap(); let tokens = super::tokenize(text).unwrap();
@ -617,7 +616,7 @@ fn conjoin_and() {
#[test] #[test]
fn unknown_recipes() { fn unknown_recipes() {
match parse_success("a:\nb:\nc:").run(&Map::new(), &["a", "x", "y", "z"], false, false).unwrap_err() { match parse_success("a:\nb:\nc:").run(&["a", "x", "y", "z"], &Default::default()).unwrap_err() {
RunError::UnknownRecipes{recipes} => assert_eq!(recipes, &["x", "y", "z"]), RunError::UnknownRecipes{recipes} => assert_eq!(recipes, &["x", "y", "z"]),
other => panic!("expected an unknown recipe error, but got: {}", other), other => panic!("expected an unknown recipe error, but got: {}", other),
} }
@ -742,7 +741,7 @@ a:
x x
"; ";
match parse_success(text).run(&Map::new(), &["a"], false, false).unwrap_err() { match parse_success(text).run(&["a"], &Default::default()).unwrap_err() {
RunError::Code{recipe, code} => { RunError::Code{recipe, code} => {
assert_eq!(recipe, "a"); assert_eq!(recipe, "a");
assert_eq!(code, 200); assert_eq!(code, 200);
@ -753,7 +752,8 @@ a:
#[test] #[test]
fn code_error() { fn code_error() {
match parse_success("fail:\n @function x { return 100; }; x").run(&Map::new(), &["fail"], false, false).unwrap_err() { match parse_success("fail:\n @function x { return 100; }; x")
.run(&["fail"], &Default::default()).unwrap_err() {
RunError::Code{recipe, code} => { RunError::Code{recipe, code} => {
assert_eq!(recipe, "fail"); assert_eq!(recipe, "fail");
assert_eq!(code, 100); assert_eq!(code, 100);
@ -768,7 +768,7 @@ fn run_args() {
a return code: a return code:
@function x { {{return}} {{code + "0"}}; }; x"#; @function x { {{return}} {{code + "0"}}; }; x"#;
match parse_success(text).run(&Map::new(), &["a", "return", "15"], false, false).unwrap_err() { match parse_success(text).run(&["a", "return", "15"], &Default::default()).unwrap_err() {
RunError::Code{recipe, code} => { RunError::Code{recipe, code} => {
assert_eq!(recipe, "a"); assert_eq!(recipe, "a");
assert_eq!(code, 150); assert_eq!(code, 150);
@ -779,7 +779,7 @@ a return code:
#[test] #[test]
fn missing_args() { fn missing_args() {
match parse_success("a b c d:").run(&Map::new(), &["a", "b", "c"], false, false).unwrap_err() { match parse_success("a b c d:").run(&["a", "b", "c"], &Default::default()).unwrap_err() {
RunError::ArgumentCountMismatch{recipe, found, expected} => { RunError::ArgumentCountMismatch{recipe, found, expected} => {
assert_eq!(recipe, "a"); assert_eq!(recipe, "a");
assert_eq!(found, 2); assert_eq!(found, 2);
@ -791,7 +791,8 @@ fn missing_args() {
#[test] #[test]
fn missing_default() { fn missing_default() {
match parse_success("a b c d:\n echo {{b}}{{c}}{{d}}").run(&Map::new(), &["a"], false, false).unwrap_err() { match parse_success("a b c d:\n echo {{b}}{{c}}{{d}}")
.run(&["a"], &Default::default()).unwrap_err() {
RunError::ArgumentCountMismatch{recipe, found, expected} => { RunError::ArgumentCountMismatch{recipe, found, expected} => {
assert_eq!(recipe, "a"); assert_eq!(recipe, "a");
assert_eq!(found, 0); assert_eq!(found, 0);
@ -803,7 +804,8 @@ fn missing_default() {
#[test] #[test]
fn backtick_code() { fn backtick_code() {
match parse_success("a:\n echo {{`function f { return 100; }; f`}}").run(&Map::new(), &["a"], false, false).unwrap_err() { match parse_success("a:\n echo {{`function f { return 100; }; f`}}")
.run(&["a"], &Default::default()).unwrap_err() {
RunError::BacktickCode{code, token} => { RunError::BacktickCode{code, token} => {
assert_eq!(code, 100); assert_eq!(code, 100);
assert_eq!(token.lexeme, "`function f { return 100; }; f`"); assert_eq!(token.lexeme, "`function f { return 100; }; f`");
@ -814,11 +816,11 @@ fn backtick_code() {
#[test] #[test]
fn unknown_overrides() { fn unknown_overrides() {
let mut overrides = Map::new(); let mut options: RunOptions = Default::default();
overrides.insert("foo", "bar"); options.overrides.insert("foo", "bar");
overrides.insert("baz", "bob"); options.overrides.insert("baz", "bob");
match parse_success("a:\n echo {{`function f { return 100; }; f`}}") match parse_success("a:\n echo {{`function f { return 100; }; f`}}")
.run(&overrides, &["a"], false, false).unwrap_err() { .run(&["a"], &options).unwrap_err() {
RunError::UnknownOverrides{overrides} => { RunError::UnknownOverrides{overrides} => {
assert_eq!(overrides, &["baz", "foo"]); assert_eq!(overrides, &["baz", "foo"]);
}, },