diff --git a/src/db.rs b/src/db.rs index 81f3af9..8fa8320 100644 --- a/src/db.rs +++ b/src/db.rs @@ -5,6 +5,7 @@ use std::ffi::OsString; use std::collections::HashMap; //use crate::log; +use crate::dvcs; #[derive(Debug)] pub struct Entry { @@ -17,7 +18,7 @@ pub struct Task { pub name: OsString, pub bin: PathBuf, pub source: Option, - pub dvcs: Option, + pub dvcs: dvcs::Backend, pub config: HashMap, pub branch: Option, pub host: Option, @@ -28,7 +29,7 @@ impl Task { pub fn new(base_dir: PathBuf, name: OsString) -> Task { let t = Entry::new(base_dir, name.clone()); let source = t.source(); - let cloned = source.clone().map_or(false, |src| { + let cloned = source.clone().map_or(false, |_| { let mut path = t.base_dir.clone(); path.push(format!(".{}", t.name.to_str().expect("WTF"))); path.is_dir() @@ -37,7 +38,7 @@ impl Task { name: name.clone(), bin: PathBuf::from(name.clone()), source, - dvcs: t.dvcs(), + dvcs: dvcs::from_setting(t.dvcs()), config: HashMap::new(), branch: t.branch(), host: t.host(), @@ -77,12 +78,6 @@ impl Entry { pub fn host(&self) -> Option { self.read_setting("branch") } - - pub fn has_dir(&self, name: &str) -> bool { - let mut path = self.base_dir.clone(); - path.push(name); - return path.is_dir(); - } } pub struct Database { @@ -109,7 +104,6 @@ impl Database { Task::new(self.base_dir.clone(), file.file_name().expect("WTF").to_os_string()) ); } - println!("{:?}", file); } return res; diff --git a/src/dvcs.rs b/src/dvcs.rs new file mode 100644 index 0000000..ad556fa --- /dev/null +++ b/src/dvcs.rs @@ -0,0 +1,39 @@ +use std::process::Command; + +pub fn from_setting(setting: Option) -> Backend { + // Git is the default setting until further notice + setting.map_or(Backend::Git, |name| match name.as_ref() { + "git" => Backend::Git, + "mercurial" => Backend::Mercurial, + _ => Backend::Unknown(name.to_string()) + }) +} + +#[derive(Debug)] +pub enum Backend { + Git, + Mercurial, + Unknown(String) +} + +impl Backend { + pub fn clone(&self, source: &str) -> bool { + match self { + Backend::Git => { + let status = Command::new("git") + .arg("clone") + .arg("--recursive") + .arg(source) + .status().expect("PROCESS ERROR!"); + status.success() + }, + Backend::Mercurial => { + unreachable!("Unimplemented"); + }, + Backend::Unknown(name) => { + eprintln!("Unknown DVCS: {}", name); + false + } + } + } +} diff --git a/src/log.rs b/src/log.rs index 9a2257c..dfb197c 100644 --- a/src/log.rs +++ b/src/log.rs @@ -11,6 +11,8 @@ lazy_static!{ static ref TRANSLATIONS: HashMap = load_translations(); } +pub type Context<'a> = &'a HashMap<&'a str, &'a str>; + fn load_translations() -> HashMap { let folder = env::var("I18N").unwrap_or("./i18n/".to_string()); let mut path = PathBuf::from(folder); @@ -86,37 +88,41 @@ impl LogLevel { } } -fn expand(msg: &str, vars: Option<&HashMap>) -> String { - let mut trans_string = trans(msg); + +fn expand(msg: &str, vars: Option) -> String { + //let mut s = msg; if vars.is_some() { - for (key, val) in vars.unwrap() { - trans_string = trans_string.replace(key, val); - } + return vars.unwrap().iter().fold(msg.to_string(), |prev, (key, val)| { + prev.replace(key, val) + }) } - trans_string + return msg.to_string(); } -pub fn info(msg: &str, vars: Option<&HashMap>) { +pub fn info(msg: &str, vars: Option) { if LOGLEVEL.info { - let trans_string = expand(msg, vars); - println!("[git-build] {}", trans_string); + let t_msg = expand(&trans(msg), vars); + println!("[git-build] {}", t_msg); } } -pub fn error(msg: &str) { +pub fn error(msg: &str, vars: Option) { if LOGLEVEL.error { - eprintln!("ERROR: {}", msg); + let t_msg = expand(&trans(msg), vars); + eprintln!("{}{}", trans("error"), t_msg); } } -pub fn warn(msg: &str) { +pub fn warn(msg: &str, vars: Option) { if LOGLEVEL.error { - eprintln!("WARNING: {}", msg); + let t_msg = expand(&trans(msg), vars); + eprintln!("{}{}", trans("warning"), t_msg); } } -pub fn debug(msg: &str) { +pub fn debug(msg: &str, vars: Option) { if LOGLEVEL.debug { - println!("DEBUG: {}", msg); + let t_msg = expand(&trans(msg), vars); + println!("{}{}", trans("debug"), t_msg); } } diff --git a/src/main.rs b/src/main.rs index daacb14..395c6bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,23 +3,34 @@ use std::collections::HashMap; mod log; mod db; +mod dvcs; fn main() { let home_dir = env::var("HOME").expect("$HOME not defined. WTF?"); let base_dir = format!("{}/.git-build", home_dir); - let mut test = HashMap::new(); - test.insert("$i18n_task".to_string(), "test"); - log::info("process", Some(&test)); - let db = db::Database::from_folder(&base_dir); let truc = db.tasks(); - println!("{:?}", truc); for task in truc { - println!("{:#?}", task); - } + let mut context = HashMap::new(); + context.insert("$i18n_task", task.name.to_str().expect("WTF")); + 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 { - //let log_level = env::var("LOG").unwrap_or + if !task.dvcs.clone(source) { + context.insert("$i18n_source", &source); + log::error("clone_failed", Some(&context)); + continue + // Skip further processing + } + } + // Otherwise, it's a sourceless task + continue + } + // So the cloned repo is here maybe update? + } }