diff --git a/core/Cargo.toml b/core/Cargo.toml index e3c4959..1accb95 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -17,3 +17,4 @@ lazy_static = "1.0" futures = "0.1" pretty_env_logger = "0.2" toml = "0.4" +rg_lib = { path = "../lib", version = "*" } diff --git a/core/src/creator.rs b/core/src/creator.rs index ac40ffe..b314cda 100644 --- a/core/src/creator.rs +++ b/core/src/creator.rs @@ -1,3 +1,4 @@ +use rg_lib::* ; use model::* ; use res::* ; use def::* ; diff --git a/core/src/def.rs b/core/src/def.rs index 5b0a63c..46c20c6 100644 --- a/core/src/def.rs +++ b/core/src/def.rs @@ -2,8 +2,7 @@ use std ; use err ; pub type Result = std::result::Result; pub type BoolR = std::result::Result<(), err::Error>; -use std::collections::HashMap ; -pub type StrMap = HashMap ; +use rg_lib::StrMap ; pub trait Mustable { @@ -18,6 +17,7 @@ impl Mustable for StrMap { } +/* #[macro_export] macro_rules! map { // Empty object. @@ -41,3 +41,4 @@ macro_rules! map { }) } +*/ diff --git a/core/src/inner/mod.rs b/core/src/inner/mod.rs index e4bc30c..96ff121 100644 --- a/core/src/inner/mod.rs +++ b/core/src/inner/mod.rs @@ -10,6 +10,8 @@ use parser::* ; #[macro_export] macro_rules! inner_use{ () => { + #[allow(unused_imports)] + use rg_lib::* ; #[allow(unused_imports)] use model::* ; #[allow(unused_imports)] diff --git a/core/src/inner/proxy.rs b/core/src/inner/proxy.rs index 8b3d1d6..1693e38 100644 --- a/core/src/inner/proxy.rs +++ b/core/src/inner/proxy.rs @@ -1,7 +1,4 @@ - -use model::* ; -use def::* ; -use res::* ; +inner_use!() ; #[derive(Debug)] pub struct ResProxy { diff --git a/core/src/inner/var.rs b/core/src/inner/var.rs index 8740083..2695da0 100644 --- a/core/src/inner/var.rs +++ b/core/src/inner/var.rs @@ -1,7 +1,4 @@ - -use model::* ; -use def::* ; -use res::* ; +inner_use!() ; #[derive(Debug)] pub struct Vars { @@ -38,46 +35,15 @@ impl ResDesp for Vars impl InvokeStart for Vars { - fn res_start(&self,_context : &mut Context) ->BoolR - { - Ok(()) - - } - fn res_conf(&self,_context : &mut Context) ->BoolR - { - Ok(()) - - } - fn res_check(&self,_context : &mut Context) ->BoolR - { - Ok(()) - - } -} -impl InvokeStop for Vars -{ - fn res_stop(&self,_context : &mut Context) ->BoolR - { - Ok(()) - - } - fn res_clean(&self,_context : &mut Context) ->BoolR - { - Ok(()) - - } - -} -impl InvokeHook for Vars -{ - fn res_before(&self,_context : &mut Context) ->BoolR - { - Ok(()) - } - - fn res_after(&self,_context : &mut Context) ->BoolR + fn res_conf(&self,context : &mut Context) ->BoolR { + for (k,v) in &self.kvmap + { + context.sset(k.clone(),v.clone()); + } Ok(()) } } +impl InvokeStop for Vars { } +impl InvokeHook for Vars { } diff --git a/core/src/lib.rs b/core/src/lib.rs index b0ab094..b40c95f 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -6,8 +6,8 @@ extern crate pretty_env_logger; extern crate serde_derive; extern crate serde; extern crate serde_json; -#[macro_use] -extern crate lazy_static; +#[macro_use] extern crate lazy_static; +#[macro_use] extern crate rg_lib ; extern crate toml; diff --git a/core/src/model.rs b/core/src/model.rs index 0dd6e8c..0be7d1c 100644 --- a/core/src/model.rs +++ b/core/src/model.rs @@ -1,17 +1,70 @@ -use err ; -use def::* ; +use err ; use def::* ; use std ; +use std::convert::{From ,Into} ; use std::collections::HashMap ; #[derive(Debug,Clone)] -pub struct Context +pub enum CtxValue { + Str(String), + f64(f64), +} +impl From for CtxValue{ + fn from(val: String) -> Self { + CtxValue::Str(val) + } +} +impl Into for CtxValue { + fn into(self) -> String { + match self + { + CtxValue::Str(val) => val , + _ => panic!("convert CtxValue to String Fail!"), + } + } +} + +#[derive(Debug,Clone)] +pub struct Context +{ + maps : HashMap , } impl Context { pub fn new() -> Context { - Context{} + Context{ maps: HashMap::new() } + } + pub fn sset(&mut self,key : String , val : T) + where CtxValue: std::convert::From + { + let obj = CtxValue::from(val); + self.maps.insert(key,obj ) ; + } + pub fn set(&mut self,key : &str, val : T) + where CtxValue: std::convert::From + { + let obj = CtxValue::from(val); + self.maps.insert(String::from(key),obj ) ; + + } + pub fn must_get(& self, key :&str)->T + where CtxValue: std::convert::Into + { + self.must_sget(&String::from(key)) + } + pub fn must_sget(& self, key :&String)->T + where CtxValue: std::convert::Into + { + let fund = self.maps.get(key) ; + if let Some(val) = fund + { + return val.clone().into(); + + } + panic!("bad") ; + } + pub fn keep(&self) { @@ -33,15 +86,7 @@ pub trait Res fn start(&self , context : &mut Context ) ->BoolR; fn stop(&self , context : &mut Context ) ->BoolR; fn check(&self , context : &mut Context ) ->BoolR; - fn clean(&self , context : &mut Context ) ->BoolR; - fn info(&self) ->String ; - fn name(&self) ->String ; -} -pub type ResBox = Box ; -pub type ResVec = Vec> ; - -impl std::fmt::Debug for Res -{ + fn clean(&self , context : &mut Context ) ->BoolR; fn info(&self) ->String ; fn name(&self) ->String ; } pub type ResBox = Box ; pub type ResVec = Vec> ; impl std::fmt::Debug for Res { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "info :{}", self.info()) } @@ -49,3 +94,17 @@ impl std::fmt::Debug for Res } +#[cfg(test)] +mod tests +{ + use super::* ; + + #[test] + fn context_use() + { + + let mut ctx = Context::new(); + ctx.set("src",format!("hello")); + let src : String = ctx.must_get("src") ; + } +} diff --git a/core/src/parser.rs b/core/src/parser.rs index 9a08794..0246bef 100644 --- a/core/src/parser.rs +++ b/core/src/parser.rs @@ -1,3 +1,4 @@ +use rg_lib::* ; use def::* ; #[derive(Debug,Clone)] pub enum RgvType{ diff --git a/core/src/res.rs b/core/src/res.rs index d5b7da7..d23d042 100644 --- a/core/src/res.rs +++ b/core/src/res.rs @@ -1,4 +1,4 @@ - +use rg_lib::* ; use err ; use def::* ; use model::* ; diff --git a/extends/src/lib.rs b/extends/src/lib.rs index 46473a8..750a05a 100644 --- a/extends/src/lib.rs +++ b/extends/src/lib.rs @@ -1,6 +1,7 @@ #[macro_use] extern crate log; extern crate pretty_env_logger; +#[macro_use] extern crate rg_lib ; #[macro_use] extern crate rg_core ; #[macro_use] extern crate shells ; diff --git a/extends/src/unix/link.rs b/extends/src/unix/link.rs index 04c51ed..2cb8474 100644 --- a/extends/src/unix/link.rs +++ b/extends/src/unix/link.rs @@ -1,3 +1,4 @@ +use rg_lib::* ; use rg_core::err::* ; use rg_core::def::* ; use rg_core::model::* ; @@ -42,38 +43,56 @@ impl ResDesp for Link } } +impl InvokeHook for Link{ + fn res_before(&self,context : &mut Context) ->BoolR + { + + let ee = EExpress::from_env(); + let src = ee.parse(&self.src) ; + let dst = ee.parse(&self.dst) ; + context.set("src",src); + context.set("dst",dst); + Ok(()) + } -impl InvokeHook for Link{} +} impl InvokeStop for Link{ } - +impl Link +{ + fn clear_link(&self,dst_path: &Path) -> BoolR + { + if dst_path.read_link().is_ok() + { + let (code,stdout,stderr )= sh!("unlink {}",self.dst); + if code != 0 { + ERR!("{:?} {:?} ",stdout,stderr); + } + Ok(()) + } + else + { + ERR!("dst exists {:?}",self.dst); + } + } +} impl InvokeStart for Link { - fn res_conf(&self,_context : &mut Context) ->BoolR + fn res_conf(&self,context : &mut Context) ->BoolR { - let dst_path = Path::new(self.dst.as_str()) ; - let src_path = Path::new(self.src.as_str()) ; + let dst = context.must_get::("dst") ; + let src = context.must_get::("src") ; + let dst_path = Path::new(dst.as_str()) ; + let src_path = Path::new(src.as_str()) ; if dst_path.exists() { - if dst_path.read_link().is_ok() - { - let (code,stdout,stderr )= sh!("unlink {}",self.dst); - if code != 0 { - ERR!("{:?} {:?} ",stdout,stderr); - } - } - else - { - ERR!("dst exists {:?}",self.dst); - } + self.clear_link(dst_path) ? } - //TODO: - /* if src_path.exists() { let parent = dst_path.parent(); @@ -90,9 +109,6 @@ impl InvokeStart for Link } } ERR!("{} not exists",self.src); - */ - Ok(()) - } } @@ -118,8 +134,8 @@ mod tests let mut god = ResFatory::new() ; res_regist(&mut god); let data = map!( - "dst" =>"${HOME}/devspace/rigger-nx/extends/meterial/run_ngx.yaml", - "src" =>"${HOME}/devspace/rigger-nx/extends/meterial/run_tpl.yaml") ; + "dst" =>"${HOME}/devspace/rigger/extends/meterial/run_ngx.yaml", + "src" =>"${HOME}/devspace/rigger/extends/meterial/run_tpl.yaml") ; let obj = god.create(&Link::key(),&data ).unwrap(); res_check(&obj); } diff --git a/lib/src/eexp.rs b/lib/src/eexp.rs index 1d6ef4e..dcb5e85 100644 --- a/lib/src/eexp.rs +++ b/lib/src/eexp.rs @@ -1,6 +1,7 @@ use std::io::prelude::*; use regex::{ Regex ,Captures }; use def::StrMap; +use std::env ; pub struct EExpress { @@ -16,6 +17,14 @@ impl EExpress let regex = Regex::new(r"(\$\{([[:alnum:]]+)\})").unwrap(); EExpress{ regex, data } } + pub fn from_env() -> EExpress + { + let mut data = StrMap::new() ; + for (key, value) in env::vars() { + data.insert(key,value) ; + } + EExpress::new(data) + } pub fn evar_val<'a>(&'a self, key : &str) -> Option<&'a String> { self.data.get(key) @@ -28,7 +37,11 @@ impl EExpress } return format!("__NO[{}]__", key) ; } - pub fn parse(&self, content :&str) -> String + pub fn parse(&self, content :&String) -> String + { + self.parse_str(content.as_str()) + } + pub fn parse_str(&self, content :&str) -> String { let fun = | caps: &Captures| { format!("{}", self.safe_evar_val(&caps[2])) } ; String::from(self.regex.replace_all( content, &fun)) diff --git a/lib/src/lib.rs b/lib/src/lib.rs index f0e95fb..b3beb9c 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -5,8 +5,8 @@ extern crate regex; mod def ; #[macro_use] pub mod macros ; -pub mod yaml; -pub mod eexp ; +mod yaml; +mod eexp ; pub use def::StrMap ; pub use eexp::EExpress ; diff --git a/lib/src/macros.rs b/lib/src/macros.rs index 10a1300..e8ba7d7 100644 --- a/lib/src/macros.rs +++ b/lib/src/macros.rs @@ -1,13 +1,13 @@ #[macro_export] macro_rules! map { - {} => ($crate::def::StrMap::new()); + {} => ($crate::StrMap::new()); // In this implementation, key/value pairs separated by commas. { $( $key:expr => $value:expr ),* } => { map!( $( $key => $value, )* ) }; { $( $key:expr => $value:expr, )* } => ({ - use $crate::def::StrMap; + use $crate::StrMap; let mut map = StrMap::new(); $( map.insert(String::from($key), String::from($value));