just/src/app.rs

256 lines
7.5 KiB
Rust
Raw Normal View History

2016-10-23 16:43:52 -07:00
extern crate clap;
2016-10-30 03:08:28 -07:00
extern crate regex;
extern crate atty;
2016-10-23 16:43:52 -07:00
use std::{io, fs, env, process};
2016-10-30 03:08:28 -07:00
use std::collections::BTreeMap;
use self::clap::{App, Arg, AppSettings};
2016-10-30 13:17:08 -07:00
use super::{Slurp, RunError};
2016-10-23 16:43:52 -07:00
macro_rules! warn {
($($arg:tt)*) => {{
extern crate std;
use std::io::prelude::*;
let _ = writeln!(&mut std::io::stderr(), $($arg)*);
}};
}
macro_rules! die {
($($arg:tt)*) => {{
extern crate std;
warn!($($arg)*);
process::exit(-1)
}};
}
#[derive(Copy, Clone)]
enum UseColor {
Auto,
Always,
Never,
}
impl UseColor {
fn from_argument(use_color: &str) -> UseColor {
match use_color {
"auto" => UseColor::Auto,
"always" => UseColor::Always,
"never" => UseColor::Never,
_ => panic!("Invalid argument to --color. This is a bug in just."),
}
}
fn should_color_stream(self, stream: atty::Stream) -> bool {
match self {
UseColor::Auto => atty::is(stream),
UseColor::Always => true,
UseColor::Never => false,
}
}
}
2016-10-23 16:43:52 -07:00
pub fn app() {
let matches = App::new("just")
.version(concat!("v", env!("CARGO_PKG_VERSION")))
2016-10-30 13:14:39 -07:00
.author("Casey Rodarmor <casey@rodarmor.com>")
.about("Just a command runner - https://github.com/casey/just")
.setting(AppSettings::ColoredHelp)
2016-10-23 16:43:52 -07:00
.arg(Arg::with_name("list")
.short("l")
.long("list")
.help("List available recipes")
.conflicts_with("dump")
.conflicts_with("show"))
.arg(Arg::with_name("dump")
.long("dump")
.help("Print entire justfile")
.conflicts_with("show")
.conflicts_with("list"))
.arg(Arg::with_name("show")
.short("s")
.long("show")
.takes_value(true)
.value_name("recipe")
.help("Show information about <recipe>")
.conflicts_with("dump")
.conflicts_with("list"))
.arg(Arg::with_name("quiet")
.short("q")
.long("quiet")
.help("Suppress all output"))
2016-10-30 13:14:39 -07:00
.arg(Arg::with_name("dry-run")
.long("dry-run")
.help("Print recipe text without executing"))
.arg(Arg::with_name("evaluate")
.long("evaluate")
.help("Print evaluated variables"))
.arg(Arg::with_name("color")
.long("color")
.takes_value(true)
.possible_values(&["auto", "always", "never"])
.default_value("auto")
.help("Print colorful output"))
2016-10-30 03:08:28 -07:00
.arg(Arg::with_name("set")
.long("set")
.takes_value(true)
.number_of_values(2)
.value_names(&["variable", "value"])
.multiple(true)
.help("Set <variable> to <value>"))
.arg(Arg::with_name("working-directory")
.long("working-directory")
.takes_value(true)
.help("Use <working-directory> as working directory. --justfile must also be set"))
.arg(Arg::with_name("justfile")
.long("justfile")
.takes_value(true)
.help("Use <justfile> as justfile. --working-directory must also be set"))
2016-10-29 00:14:41 -07:00
.arg(Arg::with_name("arguments")
2016-10-23 16:43:52 -07:00
.multiple(true)
.help("The recipe(s) to run, defaults to the first recipe in the justfile"))
2016-10-23 16:43:52 -07:00
.get_matches();
// it is not obvious to me what we should do if only one of --justfile and
// --working-directory are passed. refuse to run in that case to avoid
// suprises.
if matches.is_present("justfile") ^ matches.is_present("working-directory") {
die!("--justfile and --working-directory may only be used together");
}
// --dry-run and --quiet don't make sense together
if matches.is_present("dry-run") && matches.is_present("quiet") {
die!("--dry-run and --quiet may not be used together");
}
let use_color_argument = matches.value_of("color").expect("--color had no value");
let use_color = UseColor::from_argument(use_color_argument);
let justfile_option = matches.value_of("justfile");
let working_directory_option = matches.value_of("working-directory");
let text;
if let (Some(file), Some(directory)) = (justfile_option, working_directory_option) {
text = fs::File::open(file)
.unwrap_or_else(|error| die!("Error opening justfile: {}", error))
.slurp()
.unwrap_or_else(|error| die!("Error reading justfile: {}", error));
if let Err(error) = env::set_current_dir(directory) {
die!("Error changing directory to {}: {}", directory, error);
}
} else {
let name;
'outer: loop {
for candidate in &["justfile", "Justfile"] {
match fs::metadata(candidate) {
Ok(metadata) => if metadata.is_file() {
name = *candidate;
break 'outer;
},
Err(error) => {
if error.kind() != io::ErrorKind::NotFound {
die!("Error fetching justfile metadata: {}", error)
}
}
2016-10-23 16:43:52 -07:00
}
}
match env::current_dir() {
Ok(pathbuf) => if pathbuf.as_os_str() == "/" { die!("No justfile found."); },
Err(error) => die!("Error getting current dir: {}", error),
}
2016-10-23 16:43:52 -07:00
if let Err(error) = env::set_current_dir("..") {
die!("Error changing directory: {}", error);
}
2016-10-23 16:43:52 -07:00
}
text = fs::File::open(name)
.unwrap_or_else(|error| die!("Error opening justfile: {}", error))
.slurp()
.unwrap_or_else(|error| die!("Error reading justfile: {}", error));
}
2016-10-23 16:43:52 -07:00
let justfile = super::parse(&text).unwrap_or_else(|error|
if use_color.should_color_stream(atty::Stream::Stderr) {
die!("{:#}", error);
} else {
die!("{}", error);
}
);
2016-10-23 16:43:52 -07:00
if matches.is_present("list") {
if justfile.count() == 0 {
warn!("Justfile contains no recipes");
} else {
2016-10-28 15:59:50 -07:00
println!("{}", justfile.recipes().join(" "));
2016-10-23 16:43:52 -07:00
}
process::exit(0);
}
if matches.is_present("dump") {
println!("{}", justfile);
process::exit(0);
}
2016-10-23 16:43:52 -07:00
if let Some(name) = matches.value_of("show") {
match justfile.get(name) {
Some(recipe) => {
println!("{}", recipe);
2016-10-23 16:43:52 -07:00
process::exit(0);
}
None => die!("justfile contains no recipe \"{}\"", name)
}
}
2016-10-30 03:08:28 -07:00
let set_count = matches.occurrences_of("set");
let mut overrides = BTreeMap::new();
if set_count > 0 {
let mut values = matches.values_of("set").unwrap();
for _ in 0..set_count {
overrides.insert(values.next().unwrap(), values.next().unwrap());
}
}
let override_re = regex::Regex::new("^([^=]+)=(.*)$").unwrap();
2016-10-29 00:14:41 -07:00
let arguments = if let Some(arguments) = matches.values_of("arguments") {
2016-10-30 03:08:28 -07:00
let mut done = false;
let mut rest = vec![];
for argument in arguments {
if !done && override_re.is_match(argument) {
let captures = override_re.captures(argument).unwrap();
overrides.insert(captures.at(1).unwrap(), captures.at(2).unwrap());
} else {
rest.push(argument);
done = true;
}
}
rest
2016-10-29 00:14:41 -07:00
} else if let Some(recipe) = justfile.first() {
vec![recipe]
2016-10-23 16:43:52 -07:00
} else {
die!("Justfile contains no recipes");
};
let options = super::RunOptions {
dry_run: matches.is_present("dry-run"),
evaluate: matches.is_present("evaluate"),
overrides: overrides,
quiet: matches.is_present("quiet"),
};
2016-10-30 13:14:39 -07:00
if let Err(run_error) = justfile.run(&arguments, &options) {
if !options.quiet {
if use_color.should_color_stream(atty::Stream::Stderr) {
warn!("{:#}", run_error);
} else {
warn!("{}", run_error);
}
}
2016-10-30 01:27:05 -07:00
match run_error {
2016-10-30 13:17:08 -07:00
RunError::Code{code, .. } | RunError::BacktickCode{code, ..} => process::exit(code),
2016-10-30 01:27:05 -07:00
_ => process::exit(-1),
}
2016-10-23 16:43:52 -07:00
}
}