extern crate rustache;
use std::fs;
use std::fs::File;
use std::io::Cursor;
use self::rustache::{HashBuilder, Render};
use std::io::prelude::*;
pub trait Create {
fn create_dirs(&self, name: &str) -> ();
}
impl <T:ToString>Create for Vec<T> {
fn create_dirs(&self, name: &str) -> () {
self.into_iter()
.map(|dir| { let mut subdir = name.to_string() ;
subdir.push('/') ;
subdir.push_str(&dir.to_string()) ;
fs::create_dir(subdir) } ).count();
}
}
pub fn render_templates(project: &str, name: &str, hash: HashBuilder, templates_pre: Option<Vec<String>>) -> () {
if let Some(t) = templates_pre {
let templates: Vec<String> = t.clone().into_iter()
.map(|file| { let mut p = project.to_string();
p.push('/');
p.push_str(&file);
p } ).collect();
let template_files: Vec<String> = templates.into_iter()
.map(|p| {
let template_f = File::open(p) ;
let mut t = String::new();
template_f.expect("Failed to open file")
.read_to_string(&mut t)
.expect("File read failed.");
t }).collect();
let templates_new: Vec<String> = t.into_iter()
.map(|file| { let mut p = name.to_string();
p.push('/');
p.push_str(&file);
p }).collect();
let templates_named: Vec<String> = templates_new.into_iter()
.map(|name| { let mut o = Cursor::new(Vec::new());
hash.render(&name, &mut o).unwrap();
String::from_utf8(o.into_inner()).unwrap() })
.collect();
let s: Vec<String> = template_files.clone().into_iter()
.map(|file| { let mut o = Cursor::new(Vec::new());
hash.render(&file, &mut o).unwrap();
String::from_utf8(o.into_inner()).unwrap()})
.collect();
let files_to_write = templates_named.iter().zip(s.iter());
let _ = files_to_write.into_iter()
.map(|(path, contents)| {
let mut c = File::create(path)
.expect("File create failed.");
c.write(contents.as_bytes()) }
).count();
}
}