just/src/run.rs

336 lines
9.9 KiB
Rust
Raw Normal View History

2017-11-16 23:30:08 -08:00
use common::*;
2016-10-23 16:43:52 -07:00
use std::{convert, ffi};
2017-11-16 23:30:08 -08:00
use clap::{App, Arg, ArgGroup, AppSettings};
use compile;
use misc::maybe_s;
use configuration::DEFAULT_SHELL;
2016-10-23 16:43:52 -07:00
macro_rules! die {
($($arg:tt)*) => {{
extern crate std;
eprintln!($($arg)*);
process::exit(EXIT_FAILURE)
2016-10-23 16:43:52 -07: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"));
2017-11-16 23:30:08 -08:00
let error = Command::new(editor)
.arg(path)
.status();
match error {
Ok(status) => process::exit(status.code().unwrap_or(EXIT_FAILURE)),
Err(error) => die!("Failed to invoke editor: {}", error),
}
}
2017-11-16 23:30:08 -08:00
trait Slurp {
fn slurp(&mut self) -> Result<String, io::Error>;
}
impl Slurp for fs::File {
fn slurp(&mut self) -> io::Result<String> {
let mut destination = String::new();
self.read_to_string(&mut destination)?;
Ok(destination)
}
}
pub fn run() {
let matches = App::new("just")
.version(concat!("v", env!("CARGO_PKG_VERSION")))
.author(env!("CARGO_PKG_AUTHORS"))
.about(concat!(env!("CARGO_PKG_DESCRIPTION"), " - ", env!("CARGO_PKG_HOMEPAGE")))
.setting(AppSettings::ColoredHelp)
.setting(AppSettings::TrailingVarArg)
.arg(Arg::with_name("ARGUMENTS")
.multiple(true)
.help("The recipe(s) to run, defaults to the first recipe in the justfile"))
.arg(Arg::with_name("COLOR")
.long("color")
.takes_value(true)
.possible_values(&["auto", "always", "never"])
.default_value("auto")
.help("Prints colorful output"))
.arg(Arg::with_name("DRY-RUN")
.long("dry-run")
.help("Prints what just would do without doing it")
.conflicts_with("quiet"))
.arg(Arg::with_name("DUMP")
.long("dump")
.help("Prints entire justfile"))
.arg(Arg::with_name("EDIT")
.short("e")
.long("edit")
.help("Opens justfile with $EDITOR"))
.arg(Arg::with_name("EVALUATE")
.long("evaluate")
.help("Prints evaluated variables"))
.arg(Arg::with_name("HIGHLIGHT")
.long("highlight")
.help("Highlight echoed recipe lines in bold"))
.arg(Arg::with_name("JUSTFILE")
.short("f")
.long("justfile")
.takes_value(true)
.help("Uses <JUSTFILE> as justfile. --working-directory must also be set")
.requires("WORKING-DIRECTORY"))
.arg(Arg::with_name("LIST")
.short("l")
.long("list")
.help("Lists available recipes and their arguments"))
.arg(Arg::with_name("QUIET")
.short("q")
.long("quiet")
.help("Suppresses all output")
.conflicts_with("DRY-RUN"))
.arg(Arg::with_name("SET")
2016-10-30 03:08:28 -07:00
.long("set")
.takes_value(true)
.number_of_values(2)
.value_names(&["VARIABLE", "VALUE"])
2016-10-30 03:08:28 -07:00
.multiple(true)
.help("Sets <VARIABLE> to <VALUE>"))
.arg(Arg::with_name("SHELL")
.long("shell")
.takes_value(true)
.default_value(DEFAULT_SHELL)
.help("Invoke <SHELL> to run recipes"))
.arg(Arg::with_name("SHOW")
.short("s")
.long("show")
.takes_value(true)
.value_name("RECIPE")
.help("Shows information about <RECIPE>"))
.arg(Arg::with_name("SUMMARY")
.long("summary")
.help("Lists names of available recipes"))
.arg(Arg::with_name("VERBOSE")
.short("v")
.long("verbose")
.help("Use verbose output"))
.arg(Arg::with_name("WORKING-DIRECTORY")
.short("d")
.long("working-directory")
.takes_value(true)
.help("Uses <WORKING-DIRECTORY> as working directory. --justfile must also be set")
.requires("JUSTFILE"))
.group(ArgGroup::with_name("EARLY-EXIT")
.args(&["DUMP", "EDIT", "LIST", "SHOW", "SUMMARY", "ARGUMENTS", "EVALUATE"]))
2016-10-23 16:43:52 -07:00
.get_matches();
let color = match matches.value_of("COLOR").expect("`--color` had no value") {
"auto" => Color::auto(),
"always" => Color::always(),
"never" => Color::never(),
other => die!("Invalid argument `{}` to --color. This is a bug in just.", other),
};
let set_count = matches.occurrences_of("SET");
2017-11-16 23:30:08 -08:00
let mut overrides = Map::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::new("^([^=]+)=(.*)$").unwrap();
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.get(1).unwrap().as_str(), captures.get(2).unwrap().as_str());
}
let rest = raw_arguments.iter().skip_while(|arg| override_re.is_match(arg))
.enumerate()
.flat_map(|(i, argument)| {
if i == 0 {
if let Some(i) = argument.rfind('/') {
if matches.is_present("WORKING-DIRECTORY") {
die!("--working-directory and a path prefixed recipe may not be used together.");
}
let (dir, recipe) = argument.split_at(i + 1);
if let Err(error) = env::set_current_dir(dir) {
die!("Error changing directory: {}", error);
}
if recipe.is_empty() {
return None;
} else {
return Some(recipe);
}
}
}
Some(*argument)
})
.collect::<Vec<&str>>();
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) {
if matches.is_present("EDIT") {
edit(file);
}
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
}
if matches.is_present("EDIT") {
edit(name);
}
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 = compile(&text).unwrap_or_else(|error|
if color.stderr().active() {
die!("{:#}", error);
} else {
die!("{}", error);
}
);
2016-10-23 16:43:52 -07:00
if matches.is_present("SUMMARY") {
2016-10-23 16:43:52 -07:00
if justfile.count() == 0 {
eprintln!("Justfile contains no recipes.");
2016-10-23 16:43:52 -07:00
} else {
let summary = justfile.recipes.iter()
.filter(|&(_, recipe)| !recipe.private)
.map(|(name, _)| name)
.cloned()
.collect::<Vec<_>>()
.join(" ");
println!("{}", summary);
2016-10-23 16:43:52 -07:00
}
process::exit(EXIT_SUCCESS);
2016-10-23 16:43:52 -07:00
}
if matches.is_present("DUMP") {
println!("{}", justfile);
process::exit(EXIT_SUCCESS);
}
if matches.is_present("LIST") {
let doc_color = color.stdout().doc();
println!("Available recipes:");
for (name, recipe) in &justfile.recipes {
if recipe.private {
continue;
}
print!(" {}", name);
for parameter in &recipe.parameters {
if color.stdout().active() {
print!(" {:#}", parameter);
} else {
print!(" {}", parameter);
}
}
if let Some(doc) = recipe.doc {
print!(" {} {}", doc_color.paint("#"), doc_color.paint(doc));
}
2017-11-17 17:28:06 -08:00
println!();
}
process::exit(EXIT_SUCCESS);
}
if let Some(name) = matches.value_of("SHOW") {
match justfile.recipes.get(name) {
2016-10-23 16:43:52 -07:00
Some(recipe) => {
println!("{}", recipe);
process::exit(EXIT_SUCCESS);
2016-10-23 16:43:52 -07:00
}
None => {
eprintln!("Justfile does not contain recipe `{}`.", name);
if let Some(suggestion) = justfile.suggest(name) {
eprintln!("Did you mean `{}`?", suggestion);
}
process::exit(EXIT_FAILURE)
}
2016-10-23 16:43:52 -07:00
}
}
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() {
let min_arguments = recipe.min_arguments();
if min_arguments > 0 {
die!("Recipe `{}` cannot be used as default recipe since it requires at least {} argument{}.",
recipe.name, min_arguments, maybe_s(min_arguments));
}
vec![recipe.name]
2016-10-23 16:43:52 -07:00
} else {
die!("Justfile contains no recipes.");
2016-10-23 16:43:52 -07:00
};
2017-11-16 23:30:08 -08:00
let options = Configuration {
dry_run: matches.is_present("DRY-RUN"),
evaluate: matches.is_present("EVALUATE"),
highlight: matches.is_present("HIGHLIGHT"),
overrides: overrides,
quiet: matches.is_present("QUIET"),
2017-11-16 23:30:08 -08:00
shell: matches.value_of("SHELL").unwrap(),
color: color,
verbose: matches.is_present("VERBOSE"),
};
2016-10-30 13:14:39 -07:00
if let Err(run_error) = justfile.run(&arguments, &options) {
if !options.quiet {
if color.stderr().active() {
eprintln!("{:#}", run_error);
} else {
eprintln!("{}", run_error);
}
}
2017-11-16 23:30:08 -08:00
process::exit(run_error.code().unwrap_or(EXIT_FAILURE));
2016-10-23 16:43:52 -07:00
}
}