1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
extern crate clap; extern crate regex; extern crate atty; extern crate ansi_term; use std::{io, fs, env, process, convert, ffi}; use std::collections::BTreeMap; use self::clap::{App, Arg, ArgGroup, AppSettings}; use super::{Slurp, RunError, RunOptions, compile}; 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)] 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 { "auto" => Some(UseColor::Auto), "always" => Some(UseColor::Always), "never" => Some(UseColor::Never), _ => None, } } fn should_color_stream(self, stream: atty::Stream) -> bool { match self { UseColor::Auto => atty::is(stream), UseColor::Always => true, UseColor::Never => false, } } 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) } else { ansi_term::Style::default() } } } 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), } } pub fn app() { let matches = App::new("just") .version(concat!("v", env!("CARGO_PKG_VERSION"))) .author("Casey Rodarmor <casey@rodarmor.com>") .about("Just a command runner - https://github.com/casey/just") .setting(AppSettings::ColoredHelp) .arg(Arg::with_name("list") .short("l") .long("list") .help("Lists available recipes and their arguments")) .arg(Arg::with_name("dump") .long("dump") .help("Prints entire justfile")) .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("edit") .short("e") .long("edit") .help("Opens justfile with $EDITOR")) .arg(Arg::with_name("quiet") .short("q") .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") .conflicts_with("quiet")) .arg(Arg::with_name("evaluate") .long("evaluate") .help("Prints evaluated variables")) .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("set") .long("set") .takes_value(true) .number_of_values(2) .value_names(&["variable", "value"]) .multiple(true) .help("Sets <variable> to <value>")) .arg(Arg::with_name("working-directory") .long("working-directory") .takes_value(true) .help("Uses <working-directory> as working directory. --justfile must also be set") .requires("justfile")) .arg(Arg::with_name("justfile") .long("justfile") .takes_value(true) .help("Uses <justfile> as justfile. --working-directory must also be set") .requires("working-directory")) .arg(Arg::with_name("arguments") .multiple(true) .help("The recipe(s) to run, defaults to the first recipe in the justfile")) .group(ArgGroup::with_name("early-exit") .args(&["dump", "edit", "list", "show", "summary", "arguments", "evaluate"])) .get_matches(); let use_color_argument = matches.value_of("color").expect("--color had no value"); 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."), }; 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) } } } } match env::current_dir() { Ok(pathbuf) => if pathbuf.as_os_str() == "/" { die!("No justfile found."); }, Err(error) => die!("Error getting current dir: {}", error), } if let Err(error) = env::set_current_dir("..") { die!("Error changing directory: {}", error); } } 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)); } let justfile = compile(&text).unwrap_or_else(|error| if use_color.should_color_stderr() { die!("{:#}", error); } else { die!("{}", error); } ); if matches.is_present("summary") { if justfile.count() == 0 { warn!("Justfile contains no recipes."); } else { println!("{}", justfile.recipes.keys().cloned().collect::<Vec<_>>().join(" ")); } process::exit(0); } if matches.is_present("dump") { println!("{}", justfile); process::exit(0); } if matches.is_present("list") { let blue = use_color.blue(atty::Stream::Stdout); println!("Available recipes:"); for (name, recipe) in &justfile.recipes { print!(" {}", name); for parameter in &recipe.parameters { if use_color.should_color_stdout() { print!(" {:#}", parameter); } else { print!(" {}", parameter); } } if let Some(doc) = recipe.doc { print!(" {} {}", blue.paint("#"), blue.paint(doc)); } println!(""); } process::exit(0); } if let Some(name) = matches.value_of("show") { match justfile.recipes.get(name) { Some(recipe) => { println!("{}", recipe); process::exit(0); } None => { warn!("Justfile does not contain recipe `{}`.", name); if let Some(suggestion) = justfile.suggest(name) { warn!("Did you mean `{}`?", suggestion); } process::exit(-1) } } } 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(); 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() { rest } else if let Some(recipe) = justfile.first() { vec![recipe] } else { die!("Justfile contains no recipes"); }; let options = RunOptions { dry_run: matches.is_present("dry-run"), 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_stderr() { warn!("{:#}", run_error); } else { warn!("{}", run_error); } } match run_error { RunError::Code{code, .. } | RunError::BacktickCode{code, ..} => process::exit(code), _ => process::exit(-1), } } }