Browse Source

Update

master
Adrian Heine 8 years ago
parent
commit
73b2184d24
  1. 33
      src/repository.rs
  2. 46
      src/schema.rs
  3. 16
      src/symbols/dir.rs
  4. 10
      src/symbols/git/checkout.rs
  5. 11
      src/symbols/hook.rs
  6. 20
      src/symbols/list.rs
  7. 3
      src/symbols/mod.rs
  8. 5
      src/symbols/user.rs

33
src/repository.rs

@ -1,5 +1,4 @@
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use symbols::Symbol; use symbols::Symbol;
use resources::Resource; use resources::Resource;
@ -15,11 +14,11 @@ impl<'a, C> SymbolRepository<'a> for C where C: Fn(&Resource) -> Option<Box<Symb
} }
pub struct DispatchingSymbolRepository<'a> { pub struct DispatchingSymbolRepository<'a> {
repositories: HashMap<&'static str, Box<SymbolRepository<'a>>>
repositories: HashMap<&'a str, Box<SymbolRepository<'a> + 'a>>
} }
impl<'a> DispatchingSymbolRepository<'a> { impl<'a> DispatchingSymbolRepository<'a> {
pub fn new(repositories: HashMap<&'static str, Box<SymbolRepository<'a>>>) -> DispatchingSymbolRepository<'a> {
pub fn new(repositories: HashMap<&'a str, Box<SymbolRepository<'a> + 'a>>) -> Self {
DispatchingSymbolRepository { repositories: repositories } DispatchingSymbolRepository { repositories: repositories }
} }
} }
@ -29,29 +28,3 @@ impl<'a> SymbolRepository<'a> for DispatchingSymbolRepository<'a> {
self.repositories.get(resource.get_type()).and_then(|repo| repo.get_symbol(resource)) self.repositories.get(resource.get_type()).and_then(|repo| repo.get_symbol(resource))
} }
} }
use std::marker::PhantomData;
pub struct NonRepeatingSymbolRepository<'a, R> where R: SymbolRepository<'a> {
upstream: R,
done: RefCell<HashSet<Resource>>,
d: PhantomData<Box<Symbol + 'a>>
}
impl<'a, R> NonRepeatingSymbolRepository<'a, R> where R: SymbolRepository<'a> {
pub fn new(repository: R) -> Self {
NonRepeatingSymbolRepository { upstream: repository, done: RefCell::new(HashSet::new()), d: PhantomData }
}
}
impl<'a, R> SymbolRepository<'a> for NonRepeatingSymbolRepository<'a, R> where R: SymbolRepository<'a> {
fn get_symbol(&self, resource: &Resource) -> Option<Box<Symbol + 'a>> {
let mut done = self.done.borrow_mut();
if done.contains(&resource) {
None
} else {
done.insert(resource.clone());
self.upstream.get_symbol(resource)
}
}
}

46
src/schema.rs

@ -100,15 +100,51 @@ impl<'a, R> SymbolRunner for ReportingSymbolRunner<'a, R> where R: SymbolRunner
} }
} }
pub struct RequirementsResolvingSymbolRunner<'s, R: 's + SymbolRunner, G: 's + SymbolRepository<'s>>(&'s R, &'s G);
use std::cell::RefCell;
use std::collections::HashSet;
use resources::Resource;
pub struct NonRepeatingSymbolRunner<'a, R> where R: 'a + SymbolRunner {
upstream: &'a R,
done: RefCell<HashSet<Resource>>
}
impl<'a, R> NonRepeatingSymbolRunner<'a, R> where R: SymbolRunner {
pub fn new(symbol_runner: &'a R) -> Self {
NonRepeatingSymbolRunner{ upstream: symbol_runner, done: RefCell::new(HashSet::new()) }
}
}
impl<'a, R> SymbolRunner for NonRepeatingSymbolRunner<'a, R> where R: SymbolRunner {
fn run_symbol<S: ?Sized + Symbol + fmt::Display>(&self, logger: &mut Logger, symbol: &S) -> Result<(), Box<Error>>
{
if let Some(resources) = symbol.provides() {
let mut done = self.done.borrow_mut();
let mut has_to_run = false;
for resource in resources {
if !done.contains(&resource) {
has_to_run = true;
done.insert(resource.clone());
}
}
if !has_to_run {
return Ok(());
}
}
self.upstream.run_symbol(logger, &*symbol)
}
}
use std::marker::PhantomData;
pub struct RequirementsResolvingSymbolRunner<'a, 's, R: 'a + SymbolRunner, G: 'a + SymbolRepository<'s>>(&'a R, &'a G, PhantomData<Box<Symbol + 's>>);
impl<'s, R, G> RequirementsResolvingSymbolRunner<'s, R, G> where R: SymbolRunner, G: SymbolRepository<'s> {
pub fn new(symbol_runner: &'s R, symbol_repo: &'s G) -> Self {
RequirementsResolvingSymbolRunner(symbol_runner, symbol_repo)
impl<'a, 's, R, G> RequirementsResolvingSymbolRunner<'a, 's, R, G> where R: SymbolRunner, G: SymbolRepository<'s> {
pub fn new(symbol_runner: &'a R, symbol_repo: &'a G) -> Self {
RequirementsResolvingSymbolRunner(symbol_runner, symbol_repo, PhantomData)
} }
} }
impl<'s, R, G> SymbolRunner for RequirementsResolvingSymbolRunner<'s, R, G> where R: SymbolRunner, G: SymbolRepository<'s> {
impl<'a, 's, R, G> SymbolRunner for RequirementsResolvingSymbolRunner<'a, 's, R, G> where R: SymbolRunner, G: SymbolRepository<'s> {
fn run_symbol<S: ?Sized + Symbol + fmt::Display>(&self, logger: &mut Logger, symbol: &S) -> Result<(), Box<Error>> fn run_symbol<S: ?Sized + Symbol + fmt::Display>(&self, logger: &mut Logger, symbol: &S) -> Result<(), Box<Error>>
{ {
for resource in symbol.get_prerequisites() { for resource in symbol.get_prerequisites() {

16
src/symbols/dir.rs

@ -2,7 +2,9 @@ use std::error::Error;
use std::fmt; use std::fmt;
use std::fs; use std::fs;
use std::io; use std::io;
use std::path::Path;
use resources::Resource;
use symbols::Symbol; use symbols::Symbol;
pub struct Dir<D> where D: AsRef<str> + fmt::Display { pub struct Dir<D> where D: AsRef<str> + fmt::Display {
@ -34,7 +36,19 @@ impl<D> Symbol for Dir<D> where D: AsRef<str> + fmt::Display {
} }
fn execute(&self) -> Result<(), Box<Error>> { fn execute(&self) -> Result<(), Box<Error>> {
fs::create_dir_all(self.path.as_ref()).map_err(|e| Box::new(e) as Box<Error>)
fs::create_dir(self.path.as_ref()).map_err(|e| Box::new(e) as Box<Error>)
}
fn get_prerequisites(&self) -> Vec<Resource> {
if let Some(parent) = Path::new(self.path.as_ref()).parent() {
vec![ Resource::new("dir", parent.to_string_lossy()) ]
} else {
vec![]
}
}
fn provides(&self) -> Option<Vec<Resource>> {
Some(vec![ Resource::new("dir", self.path.to_string()) ])
} }
} }

10
src/symbols/git/checkout.rs

@ -1,8 +1,10 @@
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use std::io; use std::io;
use std::path::Path;
use command_runner::CommandRunner; use command_runner::CommandRunner;
use resources::Resource;
use symbols::Symbol; use symbols::Symbol;
pub struct GitCheckout<'a> { pub struct GitCheckout<'a> {
@ -67,6 +69,14 @@ impl<'a> Symbol for GitCheckout<'a> {
try!(self._run_in_target_repo(&["merge", "FETCH_HEAD"])); try!(self._run_in_target_repo(&["merge", "FETCH_HEAD"]));
Ok(()) Ok(())
} }
fn get_prerequisites(&self) -> Vec<Resource> {
vec![ Resource::new("dir", Path::new(self.target).parent().unwrap().to_string_lossy()) ]
}
fn provides(&self) -> Option<Vec<Resource>> {
Some(vec![ Resource::new("dir", self.target.to_string()) ])
}
} }
#[cfg(test)] #[cfg(test)]

11
src/symbols/hook.rs

@ -31,6 +31,17 @@ impl<A, B> Symbol for Hook<A, B> where A: Symbol, B: Symbol {
r.extend(self.b.get_prerequisites().into_iter()); r.extend(self.b.get_prerequisites().into_iter());
r r
} }
fn provides(&self) -> Option<Vec<Resource>> {
let mut r = vec![];
if let Some(provides) = self.a.provides() {
r.extend(provides.into_iter());
}
if let Some(provides) = self.b.provides() {
r.extend(provides.into_iter());
}
if r.len() > 0 { Some(r) } else { None }
}
} }
impl<A, B> fmt::Display for Hook<A, B> where A: Symbol, B: Symbol { impl<A, B> fmt::Display for Hook<A, B> where A: Symbol, B: Symbol {

20
src/symbols/list.rs

@ -32,6 +32,26 @@ impl<'a> Symbol for List<'a> {
} }
Ok(()) Ok(())
} }
fn get_prerequisites(&self) -> Vec<Resource> {
let mut r = vec![];
for symbol in &self.symbols {
for req in symbol.get_prerequisites() {
if self.provides().map_or(true, |p| !p.contains(&req)) { r.push(req) }
}
}
r
}
fn provides(&self) -> Option<Vec<Resource>> {
let mut r = vec![];
for symbol in &self.symbols {
if let Some(provides) = symbol.provides() {
r.extend(provides.into_iter());
}
}
if r.len() > 0 { Some(r) } else { None }
}
} }
impl<'a> fmt::Display for List<'a> { impl<'a> fmt::Display for List<'a> {

3
src/symbols/mod.rs

@ -8,6 +8,9 @@ pub trait Symbol: Display {
fn get_prerequisites(&self) -> Vec<Resource> { fn get_prerequisites(&self) -> Vec<Resource> {
vec![] vec![]
} }
fn provides(&self) -> Option<Vec<Resource>> {
None
}
} }
pub mod acme; pub mod acme;

5
src/symbols/user.rs

@ -4,6 +4,7 @@ use std::fmt;
use std::io::Error as IoError; use std::io::Error as IoError;
use command_runner::CommandRunner; use command_runner::CommandRunner;
use resources::Resource;
use symbols::Symbol; use symbols::Symbol;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -108,6 +109,10 @@ impl<'a, E: 'static + Error, A: UserAdder<SubE=E>> Symbol for User<'a, E, A> {
fn execute(&self) -> Result<(), Box<Error>> { fn execute(&self) -> Result<(), Box<Error>> {
self.user_adder.add_user(&*self.user_name).map_err(|e| Box::new(e) as Box<Error>) self.user_adder.add_user(&*self.user_name).map_err(|e| Box::new(e) as Box<Error>)
} }
fn provides(&self) -> Option<Vec<Resource>> {
Some(vec![Resource::new("user", self.user_name.to_string())])
}
} }
pub struct SystemUserAdder<'a> { pub struct SystemUserAdder<'a> {

Loading…
Cancel
Save