build.rs/src/main.rs

112 lines
3.8 KiB
Rust

use std::env::{set_current_dir as cd, set_var};
use std::process::exit;
mod cli;
mod db;
mod backend;
mod log;
mod task;
use log::Context;
use task::Select;
fn main() -> Result<(), std::io::Error> {
let mut context = Context::new();
let (cmd, basedir) = cli::Cli::build();
context.insert("$i18n_basedir".to_string(), basedir.to_str().unwrap().to_string());
let basedir_str = basedir.to_str().unwrap().to_string();
set_var("GITBUILDDIR", &basedir);
if (cmd.force && cmd.inbox) || (cmd.inbox && cmd.inboxdir.is_some()) || (cmd.force && cmd.inboxdir.is_some()) {
println!("CONFLICTING COMMANDS: You can only run --inbox, --inbox-dir or --forge. These options cannot be combined");
exit(2);
}
// Setup a filter for the tasks to load/run
let select = if cmd.inbox {
Select::Inbox(basedir.clone())
} else if let Some(inboxdir) = cmd.inboxdir {
Select::InboxDir(basedir.clone(), inboxdir)
} else if cmd.tasks.is_empty() {
log::info("no_task", &context);
Select::All(basedir.clone())
} else {
Select::List(basedir.clone(), cmd.tasks.clone())
};
// Load requested tasks
let mut tasks = match select.apply(&context) {
Ok(t) => t,
Err(task::MissingTask(t)) => {
context.insert("$i18n_arg".to_string(), t);
log::error("unknown_arg", &context);
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.download() {
task.error("clone_failed");
// Skip further processing
continue;
}
// New repo just cloned. Check for submodule updates
cd(&source_dir).expect("Failed to change working dir");
if task.subupdates {
let _had_subupdates = repo.subupdate();
}
// Checkout specific branch?
// TODO: To avoid submodule inconsistencies between branches, we should directly clone a 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(())
}