build.rs/src/main.rs

115 lines
3.7 KiB
Rust
Raw Normal View History

2020-11-25 22:59:39 +00:00
use std::env::{set_current_dir as cd, set_var};
2020-05-01 10:48:22 +00:00
use structopt::StructOpt;
2020-06-01 14:56:24 +00:00
// For UNIX extended metadata
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;
2020-04-30 23:45:48 +00:00
mod dvcs;
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;
2020-06-01 14:56:24 +00:00
fn main() -> Result<(), std::io::Error> {
2020-11-28 10:50:56 +00:00
// TODO: use StructOpt::from_iter_safe so we can hook on Cli error
// to display additional unknown_arg error message.
2020-05-01 10:48:22 +00:00
let cmd = cli::Cli::from_args();
2020-11-28 15:48:23 +00:00
let mut context = Context::new();
2020-11-28 10:14:20 +00:00
let basedir = cmd.basedir();
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 = match basedir.canonicalize() {
2020-11-28 15:48:23 +00:00
Ok(p) => {
context.insert("$i18n_basedir".to_string(), p.to_str().unwrap().to_string());
p
},
Err(_) => {
2020-11-28 15:48:23 +00:00
log::error("missing_basedir", &context);
std::process::exit(1);
}
};
2020-05-01 10:48:22 +00:00
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
2020-11-25 22:26:52 +00:00
let mut tasks = if cmd.tasks.is_empty() {
2020-11-28 15:48:23 +00:00
log::info("no_task", &context);
task::from_dir(&basedir, &context).expect("Could not load DB")
2020-05-01 11:19:33 +00:00
} else {
2020-11-28 15:48:23 +00:00
match task::from_dir_and_list(&basedir, cmd.tasks, &context) {
2020-11-28 10:14:20 +00:00
Ok(t) => t,
Err(task::MissingTask(t)) => {
2020-11-28 15:48:23 +00:00
// Temporarily override the global context
let mut context = context.clone();
context.insert("$i18n_task".to_string(), t);
log::error("unknown_arg", &context);
2020-11-28 10:14:20 +00:00
std::process::exit(1);
}
}
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");
2020-11-25 22:26:52 +00:00
if !repo.clone() {
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
}
2020-07-01 15:38:27 +00:00
// New repo just cloned
// TODO: submodule and submodule updates
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
// Checkout 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
}