build.rs/src/main.rs

115 lines
3.7 KiB
Rust

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;
use log::Context;
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 = Context::new();
let basedir = cmd.basedir();
context.insert("$i18n_basedir".to_string(), basedir.to_str().unwrap().to_string());
let basedir = match basedir.canonicalize() {
Ok(p) => {
context.insert("$i18n_basedir".to_string(), p.to_str().unwrap().to_string());
p
},
Err(_) => {
log::error("missing_basedir", &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", &context);
task::from_dir(&basedir, &context).expect("Could not load DB")
} else {
match task::from_dir_and_list(&basedir, cmd.tasks, &context) {
Ok(t) => t,
Err(task::MissingTask(t)) => {
// Temporarily override the global context
let mut context = context.clone();
context.insert("$i18n_task".to_string(), t);
log::error("unknown_arg", &context);
std::process::exit(1);
}
}
};
// 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 t in &tasks {
t.debug("found_task");
}
let (config_folder, ignored_tasks) = task::config(&basedir);
set_var("FORGEBUILDCONF", &config_folder);
context.insert("$i18n_config".to_string(), config_folder.to_str().unwrap().to_string());
log::info("config", &context);
for task in &tasks {
task.debug("start_proc");
if ignored_tasks.contains(&task.name) {
// Skip task which has CONFIG/task.ignore
continue;
}
task.info("process");
// Maybe the task has a source we should clone?
if let Some(repo) = &task.repo {
let source_dir = format!("{}/.{}", basedir_str, &task.name);
if task.cloned == false {
task.info("clone");
if !repo.clone() {
task.error("clone_failed");
// 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(())
}