2019-04-11 15:23:14 -07:00
|
|
|
use crate::common::*;
|
2016-10-23 16:43:52 -07:00
|
|
|
|
2019-10-09 00:18:53 -07:00
|
|
|
pub fn run() -> Result<(), i32> {
|
2018-05-06 19:02:17 -07:00
|
|
|
#[cfg(windows)]
|
2019-10-09 00:18:53 -07:00
|
|
|
ansi_term::enable_ansi_support().ok();
|
2018-05-06 19:02:17 -07:00
|
|
|
|
2018-08-27 16:03:52 -07:00
|
|
|
env_logger::Builder::from_env(
|
2018-12-08 14:29:41 -08:00
|
|
|
env_logger::Env::new()
|
|
|
|
.filter("JUST_LOG")
|
|
|
|
.write_style("JUST_LOG_STYLE"),
|
|
|
|
)
|
|
|
|
.init();
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2019-10-07 02:06:45 -07:00
|
|
|
let app = Config::app();
|
2019-07-18 21:58:06 -07:00
|
|
|
|
|
|
|
let matches = app.get_matches();
|
2016-10-23 16:43:52 -07:00
|
|
|
|
2019-10-09 00:18:53 -07:00
|
|
|
let config = match Config::from_matches(&matches) {
|
|
|
|
Ok(config) => config,
|
|
|
|
Err(error) => {
|
|
|
|
eprintln!("error: {}", error);
|
|
|
|
return Err(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
};
|
2016-12-30 00:09:35 -08:00
|
|
|
|
2019-10-09 00:18:53 -07:00
|
|
|
let justfile = config.justfile;
|
2019-10-07 02:06:45 -07:00
|
|
|
|
2019-11-07 13:29:17 -08:00
|
|
|
if let Some(directory) = config.search_directory {
|
|
|
|
if let Err(error) = env::set_current_dir(&directory) {
|
|
|
|
die!(
|
|
|
|
"Error changing directory to {}: {}",
|
|
|
|
directory.display(),
|
|
|
|
error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 00:18:53 -07:00
|
|
|
let mut working_directory = config.working_directory.map(PathBuf::from);
|
2019-04-08 00:54:05 -07:00
|
|
|
|
|
|
|
if let (Some(justfile), None) = (justfile, working_directory.as_ref()) {
|
|
|
|
let mut justfile = justfile.to_path_buf();
|
|
|
|
|
|
|
|
if !justfile.is_absolute() {
|
|
|
|
match justfile.canonicalize() {
|
|
|
|
Ok(canonical) => justfile = canonical,
|
2019-10-09 00:18:53 -07:00
|
|
|
Err(err) => {
|
|
|
|
eprintln!(
|
|
|
|
"Could not canonicalize justfile path `{}`: {}",
|
|
|
|
justfile.display(),
|
|
|
|
err
|
|
|
|
);
|
|
|
|
return Err(EXIT_FAILURE);
|
|
|
|
}
|
2019-04-08 00:54:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
justfile.pop();
|
|
|
|
|
|
|
|
working_directory = Some(justfile);
|
|
|
|
}
|
2016-10-23 19:56:30 -07:00
|
|
|
|
|
|
|
let text;
|
2019-04-08 00:54:05 -07:00
|
|
|
if let (Some(justfile), Some(directory)) = (justfile, working_directory) {
|
2019-10-07 04:04:39 -07:00
|
|
|
if config.subcommand == Subcommand::Edit {
|
2019-11-07 13:52:22 -08:00
|
|
|
return Subcommand::edit(justfile);
|
2016-11-12 14:02:15 -08:00
|
|
|
}
|
|
|
|
|
2019-04-16 22:06:28 -07:00
|
|
|
text = fs::read_to_string(justfile)
|
2016-10-23 19:56:30 -07:00
|
|
|
.unwrap_or_else(|error| die!("Error reading justfile: {}", error));
|
|
|
|
|
2019-04-08 00:54:05 -07:00
|
|
|
if let Err(error) = env::set_current_dir(&directory) {
|
|
|
|
die!(
|
|
|
|
"Error changing directory to {}: {}",
|
|
|
|
directory.display(),
|
|
|
|
error
|
|
|
|
);
|
2016-10-23 19:56:30 -07:00
|
|
|
}
|
|
|
|
} else {
|
2019-06-01 22:38:03 -07:00
|
|
|
let current_dir = match env::current_dir() {
|
|
|
|
Ok(current_dir) => current_dir,
|
|
|
|
Err(io_error) => die!("Error getting current dir: {}", io_error),
|
|
|
|
};
|
|
|
|
match search::justfile(¤t_dir) {
|
|
|
|
Ok(name) => {
|
2019-10-07 04:04:39 -07:00
|
|
|
if config.subcommand == Subcommand::Edit {
|
2019-11-07 13:52:22 -08:00
|
|
|
return Subcommand::edit(&name);
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
2019-10-09 00:18:53 -07:00
|
|
|
text = match fs::read_to_string(&name) {
|
|
|
|
Err(error) => {
|
|
|
|
eprintln!("Error reading justfile: {}", error);
|
|
|
|
return Err(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
Ok(text) => text,
|
|
|
|
};
|
2016-10-23 16:43:52 -07:00
|
|
|
|
2019-06-01 22:38:03 -07:00
|
|
|
let parent = name.parent().unwrap();
|
2016-10-23 16:43:52 -07:00
|
|
|
|
2019-06-01 22:38:03 -07:00
|
|
|
if let Err(error) = env::set_current_dir(&parent) {
|
2019-10-09 00:18:53 -07:00
|
|
|
eprintln!(
|
2019-06-01 22:38:03 -07:00
|
|
|
"Error changing directory to {}: {}",
|
|
|
|
parent.display(),
|
|
|
|
error
|
|
|
|
);
|
2019-10-09 00:18:53 -07:00
|
|
|
return Err(EXIT_FAILURE);
|
2019-06-01 22:38:03 -07:00
|
|
|
}
|
2016-10-23 19:56:30 -07:00
|
|
|
}
|
2019-10-09 00:18:53 -07:00
|
|
|
Err(search_error) => {
|
|
|
|
eprintln!("{}", search_error);
|
|
|
|
return Err(EXIT_FAILURE);
|
|
|
|
}
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|
2016-10-23 19:56:30 -07:00
|
|
|
}
|
2016-10-23 16:43:52 -07:00
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
let justfile = match Compiler::compile(&text) {
|
2019-10-09 00:18:53 -07:00
|
|
|
Err(error) => {
|
|
|
|
if config.color.stderr().active() {
|
|
|
|
eprintln!("{:#}", error);
|
|
|
|
} else {
|
|
|
|
eprintln!("{}", error);
|
|
|
|
}
|
|
|
|
return Err(EXIT_FAILURE);
|
2016-11-07 21:01:27 -08:00
|
|
|
}
|
2019-10-09 00:18:53 -07:00
|
|
|
Ok(justfile) => justfile,
|
|
|
|
};
|
2016-10-23 16:43:52 -07:00
|
|
|
|
2019-09-21 18:53:30 -07:00
|
|
|
for warning in &justfile.warnings {
|
2019-10-07 02:06:45 -07:00
|
|
|
if config.color.stderr().active() {
|
2019-09-21 18:53:30 -07:00
|
|
|
eprintln!("{:#}", warning);
|
|
|
|
} else {
|
|
|
|
eprintln!("{}", warning);
|
|
|
|
}
|
2019-04-18 11:48:02 -07:00
|
|
|
}
|
|
|
|
|
2019-11-07 13:52:22 -08:00
|
|
|
config.subcommand.run(&config, justfile)
|
2016-10-23 16:43:52 -07:00
|
|
|
}
|