2016-10-23 16:43:52 -07:00
|
|
|
extern crate clap;
|
2016-10-30 03:08:28 -07:00
|
|
|
extern crate regex;
|
2016-11-07 21:01:27 -08:00
|
|
|
extern crate atty;
|
2016-11-12 23:31:19 -08:00
|
|
|
extern crate ansi_term;
|
2016-10-23 16:43:52 -07:00
|
|
|
|
2016-11-12 14:02:15 -08:00
|
|
|
use std::{io, fs, env, process, convert, ffi};
|
2016-10-30 03:08:28 -07:00
|
|
|
use std::collections::BTreeMap;
|
2016-11-12 13:31:54 -08:00
|
|
|
use self::clap::{App, Arg, ArgGroup, AppSettings};
|
2016-11-16 20:17:24 -08:00
|
|
|
use super::{Slurp, RunError, RunOptions, compile};
|
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)
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2016-11-07 21:01:27 -08:00
|
|
|
#[derive(Copy, Clone)]
|
2016-11-16 21:06:51 -08:00
|
|
|
pub enum UseColor {
|
2016-11-07 21:01:27 -08:00
|
|
|
Auto,
|
|
|
|
Always,
|
|
|
|
Never,
|
|
|
|
}
|
|
|
|
|
2016-11-16 21:06:51 -08:00
|
|
|
impl Default for UseColor {
|
|
|
|
fn default() -> UseColor {
|
|
|
|
UseColor::Never
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 21:01:27 -08:00
|
|
|
impl UseColor {
|
2016-11-15 20:39:56 -08:00
|
|
|
fn from_argument(use_color: &str) -> Option<UseColor> {
|
2016-11-07 21:01:27 -08:00
|
|
|
match use_color {
|
2016-11-15 20:39:56 -08:00
|
|
|
"auto" => Some(UseColor::Auto),
|
|
|
|
"always" => Some(UseColor::Always),
|
|
|
|
"never" => Some(UseColor::Never),
|
|
|
|
_ => None,
|
2016-11-07 21:01:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn should_color_stream(self, stream: atty::Stream) -> bool {
|
|
|
|
match self {
|
|
|
|
UseColor::Auto => atty::is(stream),
|
|
|
|
UseColor::Always => true,
|
2016-11-11 18:46:04 -08:00
|
|
|
UseColor::Never => false,
|
2016-11-07 21:01:27 -08:00
|
|
|
}
|
|
|
|
}
|
2016-11-12 23:31:19 -08:00
|
|
|
|
2016-11-16 21:06:51 -08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-11-12 23:31:19 -08:00
|
|
|
fn blue(self, stream: atty::Stream) -> ansi_term::Style {
|
|
|
|
if self.should_color_stream(stream) {
|
|
|
|
ansi_term::Style::new().fg(ansi_term::Color::Blue)
|
|
|
|
} else {
|
|
|
|
ansi_term::Style::default()
|
|
|
|
}
|
|
|
|
}
|
2016-11-07 21:01:27 -08:00
|
|
|
}
|
|
|
|
|
2016-11-12 14:02:15 -08:00
|
|
|
fn edit<P: convert::AsRef<ffi::OsStr>>(path: P) -> ! {
|
|
|
|
let editor = env::var_os("EDITOR")
|
|
|
|
.unwrap_or_else(|| die!("Error getting EDITOR environment variable"));
|
|
|
|
|
|
|
|
let error = process::Command::new(editor)
|
|
|
|
.arg(path)
|
|
|
|
.status();
|
|
|
|
|
|
|
|
match error {
|
|
|
|
Ok(status) => process::exit(status.code().unwrap_or(-1)),
|
|
|
|
Err(error) => die!("Failed to invoke editor: {}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-23 16:43:52 -07:00
|
|
|
pub fn app() {
|
2016-10-30 18:12:59 -07:00
|
|
|
let matches = App::new("just")
|
2016-11-10 23:09:02 -08:00
|
|
|
.version(concat!("v", env!("CARGO_PKG_VERSION")))
|
2016-10-30 13:14:39 -07:00
|
|
|
.author("Casey Rodarmor <casey@rodarmor.com>")
|
2016-10-30 18:12:59 -07:00
|
|
|
.about("Just a command runner - https://github.com/casey/just")
|
2016-11-07 21:05:07 -08:00
|
|
|
.setting(AppSettings::ColoredHelp)
|
2016-10-23 16:43:52 -07:00
|
|
|
.arg(Arg::with_name("list")
|
|
|
|
.short("l")
|
|
|
|
.long("list")
|
2016-11-12 13:31:54 -08:00
|
|
|
.help("Lists available recipes and their arguments"))
|
2016-11-11 20:25:37 -08:00
|
|
|
.arg(Arg::with_name("dump")
|
|
|
|
.long("dump")
|
2016-11-12 13:31:54 -08:00
|
|
|
.help("Prints entire justfile"))
|
2016-11-11 20:25:37 -08:00
|
|
|
.arg(Arg::with_name("show")
|
|
|
|
.short("s")
|
|
|
|
.long("show")
|
|
|
|
.takes_value(true)
|
|
|
|
.value_name("recipe")
|
2016-11-12 13:31:54 -08:00
|
|
|
.help("Shows information about <recipe>"))
|
2016-11-12 11:40:52 -08:00
|
|
|
.arg(Arg::with_name("summary")
|
|
|
|
.long("summary")
|
2016-11-12 13:31:54 -08:00
|
|
|
.help("Lists names of available recipes"))
|
2016-11-12 14:02:15 -08:00
|
|
|
.arg(Arg::with_name("edit")
|
|
|
|
.short("e")
|
|
|
|
.long("edit")
|
|
|
|
.help("Opens justfile with $EDITOR"))
|
2016-11-05 01:01:43 -07:00
|
|
|
.arg(Arg::with_name("quiet")
|
|
|
|
.short("q")
|
|
|
|
.long("quiet")
|
2016-11-12 09:52:52 -08:00
|
|
|
.help("Suppresses all output")
|
2016-11-11 20:47:17 -08:00
|
|
|
.conflicts_with("dry-run"))
|
2016-11-16 21:06:51 -08:00
|
|
|
.arg(Arg::with_name("verbose")
|
|
|
|
.short("v")
|
|
|
|
.long("verbose")
|
|
|
|
.help("Use verbose output"))
|
2016-10-30 13:14:39 -07:00
|
|
|
.arg(Arg::with_name("dry-run")
|
|
|
|
.long("dry-run")
|
2016-11-12 09:52:52 -08:00
|
|
|
.help("Prints what just would do without doing it")
|
2016-11-11 20:47:17 -08:00
|
|
|
.conflicts_with("quiet"))
|
2016-10-30 13:14:39 -07:00
|
|
|
.arg(Arg::with_name("evaluate")
|
|
|
|
.long("evaluate")
|
2016-11-12 09:52:52 -08:00
|
|
|
.help("Prints evaluated variables"))
|
2016-11-07 21:01:27 -08:00
|
|
|
.arg(Arg::with_name("color")
|
|
|
|
.long("color")
|
|
|
|
.takes_value(true)
|
|
|
|
.possible_values(&["auto", "always", "never"])
|
|
|
|
.default_value("auto")
|
2016-11-12 09:52:52 -08:00
|
|
|
.help("Prints 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)
|
2016-11-12 09:52:52 -08:00
|
|
|
.help("Sets <variable> to <value>"))
|
2016-10-23 19:56:30 -07:00
|
|
|
.arg(Arg::with_name("working-directory")
|
|
|
|
.long("working-directory")
|
|
|
|
.takes_value(true)
|
2016-11-12 09:52:52 -08:00
|
|
|
.help("Uses <working-directory> as working directory. --justfile must also be set")
|
2016-11-11 20:47:17 -08:00
|
|
|
.requires("justfile"))
|
2016-10-23 19:56:30 -07:00
|
|
|
.arg(Arg::with_name("justfile")
|
|
|
|
.long("justfile")
|
|
|
|
.takes_value(true)
|
2016-11-12 09:52:52 -08:00
|
|
|
.help("Uses <justfile> as justfile. --working-directory must also be set")
|
2016-11-11 20:47:17 -08:00
|
|
|
.requires("working-directory"))
|
2016-10-29 00:14:41 -07:00
|
|
|
.arg(Arg::with_name("arguments")
|
2016-10-23 16:43:52 -07:00
|
|
|
.multiple(true)
|
2016-11-11 20:25:37 -08:00
|
|
|
.help("The recipe(s) to run, defaults to the first recipe in the justfile"))
|
2016-11-12 13:31:54 -08:00
|
|
|
.group(ArgGroup::with_name("early-exit")
|
2016-11-12 14:04:46 -08:00
|
|
|
.args(&["dump", "edit", "list", "show", "summary", "arguments", "evaluate"]))
|
2016-10-23 16:43:52 -07:00
|
|
|
.get_matches();
|
|
|
|
|
2016-11-07 21:01:27 -08:00
|
|
|
let use_color_argument = matches.value_of("color").expect("--color had no value");
|
2016-11-15 20:39:56 -08:00
|
|
|
let use_color = match UseColor::from_argument(use_color_argument) {
|
|
|
|
Some(use_color) => use_color,
|
|
|
|
None => die!("Invalid argument to --color. This is a bug in just."),
|
|
|
|
};
|
2016-11-07 21:01:27 -08:00
|
|
|
|
2016-10-23 19:56:30 -07:00
|
|
|
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) {
|
2016-11-12 14:02:15 -08:00
|
|
|
if matches.is_present("edit") {
|
|
|
|
edit(file);
|
|
|
|
}
|
|
|
|
|
2016-10-23 19:56:30 -07:00
|
|
|
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 {
|
2016-11-05 01:25:36 -07:00
|
|
|
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 19:56:30 -07:00
|
|
|
}
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-23 19:56:30 -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
|
|
|
|
2016-10-23 19:56:30 -07:00
|
|
|
if let Err(error) = env::set_current_dir("..") {
|
|
|
|
die!("Error changing directory: {}", error);
|
|
|
|
}
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
|
|
|
|
2016-11-12 14:02:15 -08:00
|
|
|
if matches.is_present("edit") {
|
|
|
|
edit(name);
|
|
|
|
}
|
|
|
|
|
2016-11-05 01:25:36 -07:00
|
|
|
text = fs::File::open(name)
|
2016-10-23 19:56:30 -07:00
|
|
|
.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
|
|
|
|
2016-11-16 20:17:24 -08:00
|
|
|
let justfile = compile(&text).unwrap_or_else(|error|
|
2016-11-16 21:06:51 -08:00
|
|
|
if use_color.should_color_stderr() {
|
2016-11-07 21:01:27 -08:00
|
|
|
die!("{:#}", error);
|
|
|
|
} else {
|
|
|
|
die!("{}", error);
|
|
|
|
}
|
|
|
|
);
|
2016-10-23 16:43:52 -07:00
|
|
|
|
2016-11-12 11:40:52 -08:00
|
|
|
if matches.is_present("summary") {
|
2016-10-23 16:43:52 -07:00
|
|
|
if justfile.count() == 0 {
|
2016-11-12 11:40:52 -08:00
|
|
|
warn!("Justfile contains no recipes.");
|
2016-10-23 16:43:52 -07:00
|
|
|
} else {
|
2016-11-12 14:43:47 -08:00
|
|
|
println!("{}", justfile.recipes.keys().cloned().collect::<Vec<_>>().join(" "));
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
|
|
|
process::exit(0);
|
|
|
|
}
|
|
|
|
|
2016-11-11 20:25:37 -08:00
|
|
|
if matches.is_present("dump") {
|
|
|
|
println!("{}", justfile);
|
|
|
|
process::exit(0);
|
|
|
|
}
|
|
|
|
|
2016-11-12 11:40:52 -08:00
|
|
|
if matches.is_present("list") {
|
2016-11-12 23:31:19 -08:00
|
|
|
let blue = use_color.blue(atty::Stream::Stdout);
|
2016-11-12 11:40:52 -08:00
|
|
|
println!("Available recipes:");
|
|
|
|
for (name, recipe) in &justfile.recipes {
|
|
|
|
print!(" {}", name);
|
|
|
|
for parameter in &recipe.parameters {
|
2016-11-16 21:06:51 -08:00
|
|
|
if use_color.should_color_stdout() {
|
2016-11-12 23:31:19 -08:00
|
|
|
print!(" {:#}", parameter);
|
|
|
|
} else {
|
|
|
|
print!(" {}", parameter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(doc) = recipe.doc {
|
|
|
|
print!(" {} {}", blue.paint("#"), blue.paint(doc));
|
2016-11-12 11:40:52 -08:00
|
|
|
}
|
|
|
|
println!("");
|
|
|
|
}
|
|
|
|
process::exit(0);
|
|
|
|
}
|
|
|
|
|
2016-10-23 16:43:52 -07:00
|
|
|
if let Some(name) = matches.value_of("show") {
|
2016-11-12 11:40:52 -08:00
|
|
|
match justfile.recipes.get(name) {
|
2016-10-23 16:43:52 -07:00
|
|
|
Some(recipe) => {
|
2016-10-28 16:32:13 -07:00
|
|
|
println!("{}", recipe);
|
2016-10-23 16:43:52 -07:00
|
|
|
process::exit(0);
|
|
|
|
}
|
2016-11-12 12:36:12 -08:00
|
|
|
None => {
|
|
|
|
warn!("Justfile does not contain recipe `{}`.", name);
|
|
|
|
if let Some(suggestion) = justfile.suggest(name) {
|
|
|
|
warn!("Did you mean `{}`?", suggestion);
|
|
|
|
}
|
|
|
|
process::exit(-1)
|
|
|
|
}
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-13 21:26:28 -08:00
|
|
|
let raw_arguments = matches.values_of("arguments").map(|values| values.collect::<Vec<_>>())
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
for argument in raw_arguments.iter().take_while(|arg| override_re.is_match(arg)) {
|
|
|
|
let captures = override_re.captures(argument).unwrap();
|
|
|
|
overrides.insert(captures.at(1).unwrap(), captures.at(2).unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
let rest = raw_arguments.iter().skip_while(|arg| override_re.is_match(arg))
|
|
|
|
.cloned().collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let arguments = if !rest.is_empty() {
|
2016-10-30 03:08:28 -07:00
|
|
|
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");
|
|
|
|
};
|
|
|
|
|
2016-11-16 20:17:24 -08:00
|
|
|
let options = RunOptions {
|
2016-11-05 00:31:38 -07:00
|
|
|
dry_run: matches.is_present("dry-run"),
|
|
|
|
evaluate: matches.is_present("evaluate"),
|
|
|
|
overrides: overrides,
|
2016-11-05 01:01:43 -07:00
|
|
|
quiet: matches.is_present("quiet"),
|
2016-11-16 21:06:51 -08:00
|
|
|
use_color: use_color,
|
|
|
|
verbose: matches.is_present("verbose"),
|
2016-11-05 00:31:38 -07:00
|
|
|
};
|
2016-10-30 13:14:39 -07:00
|
|
|
|
2016-11-05 00:31:38 -07:00
|
|
|
if let Err(run_error) = justfile.run(&arguments, &options) {
|
2016-11-05 01:01:43 -07:00
|
|
|
if !options.quiet {
|
2016-11-16 21:06:51 -08:00
|
|
|
if use_color.should_color_stderr() {
|
2016-11-07 21:01:27 -08:00
|
|
|
warn!("{:#}", run_error);
|
|
|
|
} else {
|
|
|
|
warn!("{}", run_error);
|
|
|
|
}
|
2016-11-05 01:01:43 -07:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|