build.rs/src/main.rs

113 lines
3.8 KiB
Rust

use std::collections::HashMap;
use std::env::{set_current_dir as cd, set_var};
use structopt::StructOpt;
// For UNIX extended metadata
mod cli;
mod db;
mod dvcs;
mod log;
mod task;
fn main() -> Result<(), std::io::Error> {
// TODO: use StructOpt::from_iter_safe so we can hook on Cli error
// to display additional unknown_arg error message.
let cmd = cli::Cli::from_args();
let mut context = HashMap::new();
let basedir = cmd.basedir();
let basedir = match basedir.canonicalize() {
Ok(p) => p,
Err(_) => {
context.insert("$i18n_basedir", basedir.to_str().unwrap());
log::error("missing_basedir", Some(&context));
std::process::exit(1);
}
};
let basedir_str = basedir.to_str().unwrap().to_string();
set_var("GITBUILDDIR", &basedir);
let mut tasks = if cmd.tasks.is_empty() {
log::info("no_task", Some(&context));
task::from_dir(&basedir).expect("Could not load DB")
} else {
match task::from_dir_and_list(&basedir, cmd.tasks) {
Ok(t) => t,
Err(task::MissingTask(t)) => {
context.insert("$i18n_task", &t);
log::error("unknown_arg", Some(&context));
std::process::exit(1);
}
}
};
for t in &tasks {
let mut context = context.clone();
context.insert("$i18n_task", t.name.as_str());
log::debug("found_task", Some(&context));
}
let (config_folder, ignored_tasks) = task::config(&basedir);
set_var("GITBUILDCONF", &config_folder);
context.insert("$i18n_config", config_folder.to_str().unwrap());
log::info("config", Some(&context));
// 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 {
let mut context = context.clone();
context.insert("$i18n_task", task.name.as_str());
log::debug("start_proc", Some(&context));
if ignored_tasks.contains(&task.name) {
// Skip task which has CONFIG/task.ignore
continue;
}
log::info("process", Some(&context));
// Maybe the task has a source we should clone?
if let Some(repo) = &task.repo {
context.insert("$source", &repo.source);
let source_dir = format!("{}/.{}", basedir_str, &task.name);
if task.cloned == false {
log::info("clone", Some(&context));
if !repo.clone() {
context.insert("$i18n_source", &repo.source);
log::error("clone_failed", Some(&context));
// Skip further processing
continue;
}
// New repo just cloned
// TODO: submodule and submodule updates
cd(&source_dir).expect("Failed to change working dir");
// Checkout specific branch?
task.checkout();
task.run();
} else {
// So the cloned repo is already here maybe update?
// Let's say there was an update and run
//println!("Task {} already exists, run i t only if updates", task.name);
cd(&source_dir).expect("Failed to change working dir");
task.checkout();
task.update_and_run(&cmd.force);
//task.run();
}
} else {
// No source, chaneg working dir to basedir
cd(&basedir).expect("Failed to change working dir");
//println!("Taks {} doesn't have a source, run it", task.name);
task.run_once();
}
}
Ok(())
}