Start implementing DVCS backend
This commit is contained in:
parent
ed17533c02
commit
2c4fa7e7b5
14
src/db.rs
14
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<String>,
|
||||
pub dvcs: Option<String>,
|
||||
pub dvcs: dvcs::Backend,
|
||||
pub config: HashMap<String, String>,
|
||||
pub branch: Option<String>,
|
||||
pub host: Option<String>,
|
||||
|
@ -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<String> {
|
||||
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;
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
use std::process::Command;
|
||||
|
||||
pub fn from_setting(setting: Option<String>) -> 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
36
src/log.rs
36
src/log.rs
|
@ -11,6 +11,8 @@ lazy_static!{
|
|||
static ref TRANSLATIONS: HashMap<String, String> = load_translations();
|
||||
}
|
||||
|
||||
pub type Context<'a> = &'a HashMap<&'a str, &'a str>;
|
||||
|
||||
fn load_translations() -> HashMap<String, String> {
|
||||
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,&str>>) -> String {
|
||||
let mut trans_string = trans(msg);
|
||||
|
||||
fn expand(msg: &str, vars: Option<Context>) -> 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<String,&str>>) {
|
||||
pub fn info(msg: &str, vars: Option<Context>) {
|
||||
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<Context>) {
|
||||
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<Context>) {
|
||||
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<Context>) {
|
||||
if LOGLEVEL.debug {
|
||||
println!("DEBUG: {}", msg);
|
||||
let t_msg = expand(&trans(msg), vars);
|
||||
println!("{}{}", trans("debug"), t_msg);
|
||||
}
|
||||
}
|
||||
|
|
27
src/main.rs
27
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?
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue