1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
use std::env; use std::fs::{self, DirEntry, File}; use std::io::{self, Read}; use std::path::{Path, PathBuf}; fn template_dir() -> PathBuf { let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); path.push("templates"); path } pub fn get_template_source(tpl_file: &str) -> String { let mut path = template_dir(); path.push(Path::new(tpl_file)); let mut f = File::open(path).unwrap(); let mut s = String::new(); f.read_to_string(&mut s).unwrap(); s } fn visit_dirs(dir: &Path, cb: &Fn(&DirEntry)) -> io::Result<()> { if dir.is_dir() { for entry in try!(fs::read_dir(dir)) { let entry = try!(entry); let path = entry.path(); if path.is_dir() { try!(visit_dirs(&path, cb)); } else { cb(&entry); } } } Ok(()) } pub fn rerun_if_templates_changed() { visit_dirs(&template_dir(), &|e: &DirEntry| { println!("cargo:rerun-if-changed={}", e.path().to_str().unwrap()); }).unwrap(); }