build.rs/src/main.rs

79 lines
2.7 KiB
Rust

use std::env;
use std::collections::HashMap;
use structopt::StructOpt;
// For UNIX extended metadata
mod log;
mod db;
mod dvcs;
mod cli;
mod task;
fn main() -> Result<(), std::io::Error> {
let cmd = cli::Cli::from_args();
let base_dir: String = cmd.basedir().to_str().unwrap().into();
std::env::set_var("GITBUILDDIR", &base_dir);
let mut tasks = if cmd.tasks.is_empty() {
task::from_dir(&base_dir).expect("Could not load DB")
} else {
task::from_dir_and_list(&base_dir, cmd.tasks).expect("Could not load given tasks")
};
let (config_folder, ignored_tasks) = task::config(&std::path::Path::new(&base_dir));
std::env::set_var("GITBUILDCONF", &config_folder);
// 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 {
if ignored_tasks.contains(&task.name) {
// Skip task which has CONFIG/task.ignore
continue;
}
println!("TASK: {:?}", task.name);
let mut context = HashMap::new();
context.insert("$i18n_task", task.name.as_str());
log::debug("found_task", Some(&context));
// Maybe the task has a source we should clone?
if let Some(repo) = &task.repo {
let source_dir = format!("{}/.{}", base_dir, &task.name);
if task.cloned == false {
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
println!("Downloaded source for {}", task.name);
std::env::set_current_dir(&source_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);
std::env::set_current_dir(&source_dir);
task.checkout();
task.update_and_run(&cmd.force);
//task.run();
}
} else {
// No source, chaneg working dir to basedir
std::env::set_current_dir(&base_dir);
println!("Taks {} doesn't have a source, run it", task.name);
task.run_once();
}
}
Ok(())
}