Add --verbose flag (#123)

Causes all recipe lines to be printed, regardless of --quiet or `@`.
Prints the name of each recipe before running it. Hopefully useful for
diagnosing problems.
This commit is contained in:
Casey Rodarmor 2016-11-16 21:06:51 -08:00 committed by GitHub
parent 133b4a2ada
commit 2f4bcc57bc
4 changed files with 60 additions and 24 deletions

View File

@ -113,10 +113,6 @@ create:
mkdir -p tmp
@echo '{{quine-text}}' > tmp/gen0.c
# clean up
clean:
rm -r tmp
# run all polyglot recipes
polyglot: python js perl sh ruby

View File

@ -24,12 +24,18 @@ macro_rules! die {
}
#[derive(Copy, Clone)]
enum UseColor {
pub enum UseColor {
Auto,
Always,
Never,
}
impl Default for UseColor {
fn default() -> UseColor {
UseColor::Never
}
}
impl UseColor {
fn from_argument(use_color: &str) -> Option<UseColor> {
match use_color {
@ -48,6 +54,14 @@ impl UseColor {
}
}
pub fn should_color_stdout(self) -> bool {
self.should_color_stream(atty::Stream::Stdout)
}
pub fn should_color_stderr(self) -> bool {
self.should_color_stream(atty::Stream::Stderr)
}
fn blue(self, stream: atty::Stream) -> ansi_term::Style {
if self.should_color_stream(stream) {
ansi_term::Style::new().fg(ansi_term::Color::Blue)
@ -102,6 +116,10 @@ pub fn app() {
.long("quiet")
.help("Suppresses all output")
.conflicts_with("dry-run"))
.arg(Arg::with_name("verbose")
.short("v")
.long("verbose")
.help("Use verbose output"))
.arg(Arg::with_name("dry-run")
.long("dry-run")
.help("Prints what just would do without doing it")
@ -200,7 +218,7 @@ pub fn app() {
}
let justfile = compile(&text).unwrap_or_else(|error|
if use_color.should_color_stream(atty::Stream::Stderr) {
if use_color.should_color_stderr() {
die!("{:#}", error);
} else {
die!("{}", error);
@ -227,7 +245,7 @@ pub fn app() {
for (name, recipe) in &justfile.recipes {
print!(" {}", name);
for parameter in &recipe.parameters {
if use_color.should_color_stream(atty::Stream::Stdout) {
if use_color.should_color_stdout() {
print!(" {:#}", parameter);
} else {
print!(" {}", parameter);
@ -292,11 +310,13 @@ pub fn app() {
evaluate: matches.is_present("evaluate"),
overrides: overrides,
quiet: matches.is_present("quiet"),
use_color: use_color,
verbose: matches.is_present("verbose"),
};
if let Err(run_error) = justfile.run(&arguments, &options) {
if !options.quiet {
if use_color.should_color_stream(atty::Stream::Stderr) {
if use_color.should_color_stderr() {
warn!("{:#}", run_error);
} else {
warn!("{}", run_error);

View File

@ -164,6 +164,18 @@ fn quiet() {
)
}
#[test]
fn verbose() {
integration_test(
&["--verbose"],
"default:\n @echo hello",
0,
"hello\n",
"===> Running recipe `default`...\necho hello\n",
)
}
#[test]
fn order() {
let text = "

View File

@ -1,3 +1,12 @@
#[macro_use]
extern crate lazy_static;
extern crate regex;
extern crate tempdir;
extern crate itertools;
extern crate ansi_term;
extern crate unicode_width;
extern crate edit_distance;
#[cfg(test)]
mod unit;
@ -8,24 +17,14 @@ mod app;
pub use app::app;
#[macro_use]
extern crate lazy_static;
extern crate regex;
extern crate tempdir;
extern crate itertools;
extern crate ansi_term;
extern crate unicode_width;
extern crate edit_distance;
use std::io::prelude::*;
use std::{fs, fmt, process, io, iter, cmp};
use std::ops::Range;
use std::fmt::Display;
use app::UseColor;
use regex::Regex;
use std::collections::{BTreeMap as Map, BTreeSet as Set};
use std::fmt::Display;
use std::io::prelude::*;
use std::ops::Range;
use std::os::unix::fs::PermissionsExt;
use std::{fs, fmt, process, io, iter, cmp};
macro_rules! warn {
($($arg:tt)*) => {{
@ -278,6 +277,11 @@ impl<'a> Recipe<'a> {
exports: &Set<&'a str>,
options: &RunOptions,
) -> Result<(), RunError<'a>> {
if options.verbose {
let cyan = maybe_cyan(options.use_color.should_color_stderr());
warn!("{}===> Running recipe `{}`...{}", cyan.prefix(), self.name, cyan.suffix());
}
let argument_map = self.parameters.iter().enumerate()
.map(|(i, parameter)| if i < arguments.len() {
Ok((parameter.name, arguments[i]))
@ -390,7 +394,9 @@ impl<'a> Recipe<'a> {
if quiet_command {
command = &command[1..];
}
if options.dry_run || !((quiet_command ^ self.quiet) || options.quiet) {
if options.dry_run
|| options.verbose
|| !((quiet_command ^ self.quiet) || options.quiet) {
warn!("{}", command);
}
if options.dry_run {
@ -1099,6 +1105,8 @@ struct RunOptions<'a> {
evaluate: bool,
overrides: Map<&'a str, &'a str>,
quiet: bool,
use_color: UseColor,
verbose: bool,
}
impl<'a, 'b> Justfile<'a> where 'a: 'b {