2020-04-30 16:53:49 +00:00
|
|
|
use std::collections::HashMap;
|
2020-11-25 22:59:39 +00:00
|
|
|
use std::env::{set_current_dir as cd, set_var};
|
2020-05-01 10:48:22 +00:00
|
|
|
use structopt::StructOpt;
|
2020-06-01 14:56:24 +00:00
|
|
|
// For UNIX extended metadata
|
2020-04-30 16:53:49 +00:00
|
|
|
|
2020-11-25 22:59:39 +00:00
|
|
|
mod cli;
|
2020-04-30 20:11:06 +00:00
|
|
|
mod db;
|
2020-04-30 23:45:48 +00:00
|
|
|
mod dvcs;
|
2020-11-25 22:59:39 +00:00
|
|
|
mod log;
|
2020-06-01 14:56:24 +00:00
|
|
|
mod task;
|
2020-04-30 16:53:49 +00:00
|
|
|
|
2020-06-01 14:56:24 +00:00
|
|
|
fn main() -> Result<(), std::io::Error> {
|
2020-11-28 10:50:56 +00:00
|
|
|
// TODO: use StructOpt::from_iter_safe so we can hook on Cli error
|
|
|
|
// to display additional unknown_arg error message.
|
2020-05-01 10:48:22 +00:00
|
|
|
let cmd = cli::Cli::from_args();
|
2020-11-27 18:37:46 +00:00
|
|
|
|
|
|
|
let mut context = HashMap::new();
|
|
|
|
|
2020-11-28 10:14:20 +00:00
|
|
|
let basedir = cmd.basedir();
|
|
|
|
let basedir = match basedir.canonicalize() {
|
|
|
|
Ok(p) => p,
|
2020-11-27 18:37:46 +00:00
|
|
|
Err(_) => {
|
2020-11-28 10:50:56 +00:00
|
|
|
context.insert("$i18n_basedir", basedir.to_str().unwrap());
|
2020-11-27 18:37:46 +00:00
|
|
|
log::error("missing_basedir", Some(&context));
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
2020-05-01 10:48:22 +00:00
|
|
|
|
2020-11-28 10:14:20 +00:00
|
|
|
let basedir_str = basedir.to_str().unwrap().to_string();
|
|
|
|
|
|
|
|
set_var("GITBUILDDIR", &basedir);
|
2020-11-25 17:49:02 +00:00
|
|
|
|
2020-11-25 22:26:52 +00:00
|
|
|
let mut tasks = if cmd.tasks.is_empty() {
|
2020-11-28 10:50:56 +00:00
|
|
|
log::info("no_task", Some(&context));
|
2020-11-28 10:14:20 +00:00
|
|
|
task::from_dir(&basedir).expect("Could not load DB")
|
2020-05-01 11:19:33 +00:00
|
|
|
} else {
|
2020-11-28 10:14:20 +00:00
|
|
|
match task::from_dir_and_list(&basedir, cmd.tasks) {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(task::MissingTask(t)) => {
|
2020-11-28 10:50:56 +00:00
|
|
|
context.insert("$i18n_task", &t);
|
2020-11-28 10:14:20 +00:00
|
|
|
log::error("unknown_arg", Some(&context));
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-05-01 11:19:33 +00:00
|
|
|
};
|
2020-11-25 16:10:14 +00:00
|
|
|
|
2020-11-28 11:16:18 +00:00
|
|
|
for t in &tasks {
|
|
|
|
let mut context = context.clone();
|
|
|
|
context.insert("$i18n_task", t.name.as_str());
|
|
|
|
log::debug("found_task", Some(&context));
|
|
|
|
}
|
|
|
|
|
2020-11-28 10:14:20 +00:00
|
|
|
let (config_folder, ignored_tasks) = task::config(&basedir);
|
2020-11-25 22:55:33 +00:00
|
|
|
set_var("GITBUILDCONF", &config_folder);
|
2020-11-28 10:50:56 +00:00
|
|
|
context.insert("$i18n_config", config_folder.to_str().unwrap());
|
|
|
|
log::info("config", Some(&context));
|
2020-11-25 22:59:39 +00:00
|
|
|
|
2020-11-25 22:26:52 +00:00
|
|
|
// Reorder tasks alphanumerically
|
|
|
|
tasks.sort_unstable_by_key(|t| t.name.clone());
|
|
|
|
// Remove duplicates, in case a task was called along
|
|
|
|
// the corresponding source URL (so we'd be tempted to call the task twice)
|
|
|
|
tasks.dedup_by_key(|t| t.name.clone());
|
|
|
|
for task in &tasks {
|
2020-11-28 10:50:56 +00:00
|
|
|
let mut context = context.clone();
|
|
|
|
context.insert("$i18n_task", task.name.as_str());
|
2020-11-28 11:16:18 +00:00
|
|
|
log::debug("start_proc", Some(&context));
|
2020-11-28 10:50:56 +00:00
|
|
|
|
2020-11-25 22:26:52 +00:00
|
|
|
if ignored_tasks.contains(&task.name) {
|
2020-11-25 16:10:14 +00:00
|
|
|
// Skip task which has CONFIG/task.ignore
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-25 11:07:08 +00:00
|
|
|
|
2020-11-28 10:50:56 +00:00
|
|
|
log::info("process", Some(&context));
|
|
|
|
|
2020-11-25 11:07:08 +00:00
|
|
|
// Maybe the task has a source we should clone?
|
2020-11-25 22:26:52 +00:00
|
|
|
if let Some(repo) = &task.repo {
|
2020-11-25 23:21:02 +00:00
|
|
|
context.insert("$source", &repo.source);
|
2020-11-28 10:14:20 +00:00
|
|
|
let source_dir = format!("{}/.{}", basedir_str, &task.name);
|
2020-11-25 11:07:08 +00:00
|
|
|
if task.cloned == false {
|
2020-11-25 22:55:33 +00:00
|
|
|
log::info("clone", Some(&context));
|
2020-11-25 22:26:52 +00:00
|
|
|
if !repo.clone() {
|
|
|
|
context.insert("$i18n_source", &repo.source);
|
2020-04-30 23:45:48 +00:00
|
|
|
log::error("clone_failed", Some(&context));
|
|
|
|
// Skip further processing
|
2020-11-25 22:59:39 +00:00
|
|
|
continue;
|
2020-04-30 23:45:48 +00:00
|
|
|
}
|
2020-07-01 15:38:27 +00:00
|
|
|
// New repo just cloned
|
2020-11-25 11:07:08 +00:00
|
|
|
// TODO: submodule and submodule updates
|
2020-11-25 22:55:33 +00:00
|
|
|
cd(&source_dir).expect("Failed to change working dir");
|
2020-11-25 22:26:52 +00:00
|
|
|
|
|
|
|
// Checkout specific branch?
|
2020-11-25 22:59:39 +00:00
|
|
|
task.checkout();
|
|
|
|
task.run();
|
2020-11-25 22:26:52 +00:00
|
|
|
} else {
|
|
|
|
// So the cloned repo is already here maybe update?
|
|
|
|
// Let's say there was an update and run
|
2020-11-28 10:50:56 +00:00
|
|
|
//println!("Task {} already exists, run i t only if updates", task.name);
|
2020-11-25 22:55:33 +00:00
|
|
|
cd(&source_dir).expect("Failed to change working dir");
|
2020-11-25 22:26:52 +00:00
|
|
|
task.checkout();
|
|
|
|
task.update_and_run(&cmd.force);
|
2020-11-25 22:59:39 +00:00
|
|
|
//task.run();
|
2020-04-30 23:45:48 +00:00
|
|
|
}
|
2020-11-25 11:07:08 +00:00
|
|
|
} else {
|
2020-11-25 17:44:54 +00:00
|
|
|
// No source, chaneg working dir to basedir
|
2020-11-28 10:14:20 +00:00
|
|
|
cd(&basedir).expect("Failed to change working dir");
|
2020-11-28 10:50:56 +00:00
|
|
|
//println!("Taks {} doesn't have a source, run it", task.name);
|
2020-11-25 11:07:08 +00:00
|
|
|
task.run_once();
|
2020-04-30 23:45:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-01 14:56:24 +00:00
|
|
|
Ok(())
|
2020-04-30 16:53:49 +00:00
|
|
|
}
|