git-build.rs/src/main.rs

113 lines
3.8 KiB
Rust

use std::env;
use std::collections::HashMap;
use structopt::StructOpt;
use std::fs::{DirBuilder};
use std::path::{Path,PathBuf};
// For UNIX extended metadata
mod log;
mod db;
mod dvcs;
mod cli;
mod task;
fn main() -> Result<(), std::io::Error> {
let default_folder;
// Check if we defined a given folder in which to find the tasks
let base_dir = match env::var_os("GITBUILDDIR") {
Some(val) => {
default_folder = false;
PathBuf::from(val)
},
None => {
default_folder = true;
let home_dir = env::var("HOME").expect("$HOME not defined. WTF?");
PathBuf::from(&format!("{}/.git-build", home_dir).clone())
}
};
// If we're not using a GITBUILDDIR folder,
// We're gonna check if they already is a .git-build folder, and create one otherwise.
if !base_dir.exists() {
if default_folder == false {
let mut context = HashMap::new();
context.insert("$i18n_folder", base_dir.to_str().unwrap());
log::error("no_folder", Some(&context));
return Err(std::io::Error::new(std::io::ErrorKind::NotFound, "Folder not found."));
} else {
log::info("create_git_build_folder", Some(&HashMap::new()));
DirBuilder::new()
.recursive(true)
.create(&base_dir).unwrap();
}
}
let cmd = cli::Cli::from_args();
let 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")
};
// Change directory to the base_dir
// assert!(env::set_current_dir(&base_dir).is_ok());
// println!("Successfully changed working directory to {}!", base_dir.display());
for (task_name, task) in tasks.iter() {
let mut context = HashMap::new();
//context.insert("$i18n_task", task_name.to_str().expect("WTF"));
context.insert("$i18n_task", task_name.as_str());
log::debug("found_task", Some(&context));
if task.cloned == false {
// Maybe the task has a source we should clone?
if let Some(source) = &task.source {
// If we're not using the default folder, change it
let curr_dirr = env::current_dir().unwrap(); // TODO: Might fail because of a lack of permission to read current dir.
env::set_current_dir(&base_dir).unwrap();
let mut context = HashMap::new();
context.insert("$i18n_folder", base_dir.to_str().unwrap());
log::info("change_folder", Some(&context));
if !task.dvcs.clone(source) {
context.insert("$i18n_source", &source);
log::error("clone_failed", Some(&context));
// Change back to the parent folder.
// TODO: refactor this.
env::set_current_dir(curr_dirr).unwrap();
let mut context = HashMap::new();
context.insert("$i18n_folder", curr_dirr.to_str().unwrap().clone());
log::info("change_folder", Some(&context));
// Skip further processing
continue
}
// Change back to the parent folder.
// TODO: refactor this.
env::set_current_dir(curr_dirr).unwrap();
let mut context = HashMap::new();
context.insert("$i18n_folder", curr_dirr.clone().to_str().unwrap());
log::info("change_folder", Some(&context));
// TODO: in case the clone fails
}
// Otherwise, it's a sourceless task
continue
}
// The repo has been cloned already, we update it.
task.dvcs.update();
}
Ok(())
}