build.rs/src/main.rs

112 lines
3.8 KiB
Rust
Raw Normal View History

2020-11-25 22:59:39 +00:00
use std::env::{set_current_dir as cd, set_var};
2022-01-12 18:20:10 +00:00
use std::process::exit;
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;
mod backend;
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-11-28 15:48:23 +00:00
use log::Context;
2022-01-12 18:20:10 +00:00
use task::Select;
2020-11-28 15:48:23 +00:00
2020-06-01 14:56:24 +00:00
fn main() -> Result<(), std::io::Error> {
let mut context = Context::new();
2022-01-06 17:08:51 +00:00
let (cmd, basedir) = cli::Cli::build();
2020-11-28 15:48:23 +00:00
context.insert("$i18n_basedir".to_string(), basedir.to_str().unwrap().to_string());
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
2022-01-12 18:20:10 +00:00
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() {
2020-11-28 15:48:23 +00:00
log::info("no_task", &context);
2022-01-12 18:20:10 +00:00
Select::All(basedir.clone())
2020-05-01 11:19:33 +00:00
} else {
2022-01-12 18:20:10 +00:00
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);
2020-11-28 10:14:20 +00:00
}
2020-05-01 11:19:33 +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());
2020-11-28 11:16:18 +00:00
for t in &tasks {
2020-11-28 15:48:23 +00:00
t.debug("found_task");
2020-11-28 11:16:18 +00:00
}
2020-11-28 10:14:20 +00:00
let (config_folder, ignored_tasks) = task::config(&basedir);
set_var("FORGEBUILDCONF", &config_folder);
2020-11-28 15:48:23 +00:00
context.insert("$i18n_config".to_string(), config_folder.to_str().unwrap().to_string());
log::info("config", &context);
2020-11-25 22:59:39 +00:00
2020-11-25 22:26:52 +00:00
for task in &tasks {
2020-11-28 15:48:23 +00:00
task.debug("start_proc");
2020-11-28 10:50:56 +00:00
2020-11-25 22:26:52 +00:00
if ignored_tasks.contains(&task.name) {
// Skip task which has CONFIG/task.ignore
continue;
}
2020-11-28 15:48:23 +00:00
task.info("process");
2020-11-28 10:50:56 +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-28 10:14:20 +00:00
let source_dir = format!("{}/.{}", basedir_str, &task.name);
if task.cloned == false {
2020-11-28 15:48:23 +00:00
task.info("clone");
if !repo.download() {
2020-11-28 15:48:23 +00:00
task.error("clone_failed");
2020-04-30 23:45:48 +00:00
// Skip further processing
2020-11-25 22:59:39 +00:00
continue;
2020-04-30 23:45:48 +00:00
}
// New repo just cloned. Check for submodule updates
2020-11-25 22:55:33 +00:00
cd(&source_dir).expect("Failed to change working dir");
if task.subupdates {
let _had_subupdates = repo.subupdate();
}
2020-11-25 22:26:52 +00:00
// Checkout specific branch?
// TODO: To avoid submodule inconsistencies between branches, we should directly clone a 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
}
} else {
// 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);
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
}