Skip to content

Commit

Permalink
Merge pull request #21 from xcodecraft/dev-zwj
Browse files Browse the repository at this point in the history
impl context for #9
  • Loading branch information
zuowenjian authored Apr 26, 2018
2 parents 2f234ae + 04a8c08 commit 1c8e3ec
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 92 deletions.
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ lazy_static = "1.0"
futures = "0.1"
pretty_env_logger = "0.2"
toml = "0.4"
rg_lib = { path = "../lib", version = "*" }
1 change: 1 addition & 0 deletions core/src/creator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rg_lib::* ;
use model::* ;
use res::* ;
use def::* ;
Expand Down
5 changes: 3 additions & 2 deletions core/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std ;
use err ;
pub type Result<T > = std::result::Result<T, err::Error>;
pub type BoolR = std::result::Result<(), err::Error>;
use std::collections::HashMap ;
pub type StrMap = HashMap<String,String> ;
use rg_lib::StrMap ;

pub trait Mustable
{
Expand All @@ -18,6 +17,7 @@ impl Mustable for StrMap {

}

/*
#[macro_export]
macro_rules! map {
// Empty object.
Expand All @@ -41,3 +41,4 @@ macro_rules! map {
})
}
*/
2 changes: 2 additions & 0 deletions core/src/inner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 1 addition & 4 deletions core/src/inner/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

use model::* ;
use def::* ;
use res::* ;
inner_use!() ;
#[derive(Debug)]
pub struct ResProxy
{
Expand Down
50 changes: 8 additions & 42 deletions core/src/inner/var.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

use model::* ;
use def::* ;
use res::* ;
inner_use!() ;
#[derive(Debug)]
pub struct Vars
{
Expand Down Expand Up @@ -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 { }
4 changes: 2 additions & 2 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
85 changes: 72 additions & 13 deletions core/src/model.rs
Original file line number Diff line number Diff line change
@@ -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<String> for CtxValue{
fn from(val: String) -> Self {
CtxValue::Str(val)
}
}
impl Into<String> 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<String,CtxValue> ,
}
impl Context
{
pub fn new() -> Context {
Context{}
Context{ maps: HashMap::new() }
}
pub fn sset<T>(&mut self,key : String , val : T)
where CtxValue: std::convert::From<T>
{
let obj = CtxValue::from(val);
self.maps.insert(key,obj ) ;

}
pub fn set<T>(&mut self,key : &str, val : T)
where CtxValue: std::convert::From<T>
{
let obj = CtxValue::from(val);
self.maps.insert(String::from(key),obj ) ;

}
pub fn must_get<T>(& self, key :&str)->T
where CtxValue: std::convert::Into<T>
{
self.must_sget(&String::from(key))
}
pub fn must_sget<T>(& self, key :&String)->T
where CtxValue: std::convert::Into<T>
{
let fund = self.maps.get(key) ;
if let Some(val) = fund
{
return val.clone().into();

}
panic!("bad") ;
}

pub fn keep(&self)
{

Expand All @@ -33,19 +86,25 @@ 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<Res> ;
pub type ResVec = Vec<Box<Res>> ;

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<Res> ; pub type ResVec = Vec<Box<Res>> ; impl std::fmt::Debug for Res {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "info :{}", self.info())
}

}


#[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") ;
}
}
1 change: 1 addition & 0 deletions core/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rg_lib::* ;
use def::* ;
#[derive(Debug,Clone)]
pub enum RgvType{
Expand Down
2 changes: 1 addition & 1 deletion core/src/res.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

use rg_lib::* ;
use err ;
use def::* ;
use model::* ;
Expand Down
1 change: 1 addition & 0 deletions extends/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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 ;

Expand Down
62 changes: 39 additions & 23 deletions extends/src/unix/link.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rg_lib::* ;
use rg_core::err::* ;
use rg_core::def::* ;
use rg_core::model::* ;
Expand Down Expand Up @@ -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::<String>("dst") ;
let src = context.must_get::<String>("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();
Expand All @@ -90,9 +109,6 @@ impl InvokeStart for Link
}
}
ERR!("{} not exists",self.src);
*/
Ok(())

}
}

Expand All @@ -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);
}
Expand Down
15 changes: 14 additions & 1 deletion lib/src/eexp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::prelude::*;
use regex::{ Regex ,Captures };
use def::StrMap;
use std::env ;

pub struct EExpress
{
Expand All @@ -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)
Expand All @@ -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))
Expand Down
Loading

0 comments on commit 1c8e3ec

Please sign in to comment.