migrate to handlebars and update deps

This commit is contained in:
Konstantin Stepanov 2015-12-10 20:47:58 +03:00
parent 598946c8a8
commit 5dae359a1e
3 changed files with 75 additions and 135 deletions

185
Cargo.lock generated
View File

@ -3,17 +3,17 @@ name = "systemd-crontab-generator"
version = "0.9.0"
dependencies = [
"cronparse 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"docopt 0.6.70 (registry+https://github.com/rust-lang/crates.io-index)",
"docopt 0.6.78 (registry+https://github.com/rust-lang/crates.io-index)",
"glob 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
"handlebars 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"kernlog 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"md5 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rumblebars 0.3.0 (git+https://github.com/nicolas-cherel/rumblebars.git)",
"rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
"tempfile 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
"users 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"tempfile 1.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
"users 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -21,31 +21,18 @@ name = "advapi32-sys"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "aho-corasick"
version = "0.3.0"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "aster"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"syntex_syntax 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "cronparse"
version = "0.4.0"
@ -53,10 +40,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "docopt"
version = "0.6.70"
version = "0.6.78"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"regex 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
"strsim 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -67,11 +54,24 @@ version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "kernel32-sys"
version = "0.1.4"
name = "handlebars"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"num 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)",
"quick-error 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "kernel32-sys"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -80,25 +80,25 @@ name = "kernlog"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"log 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "0.1.14"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.1.10"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "log"
version = "0.3.2"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -108,79 +108,51 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "0.1.5"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "nc_rustlex_codegen"
version = "0.3.1"
name = "num"
version = "0.1.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"log 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"quasi 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"quasi_codegen 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_syntax 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quasi"
version = "0.3.1"
name = "quick-error"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"syntex_syntax 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quasi_codegen"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aster 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_syntax 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rand"
version = "0.3.10"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "0.1.41"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"aho-corasick 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.2.1"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rumblebars"
version = "0.3.0"
source = "git+https://github.com/nicolas-cherel/rumblebars.git#2f3df2f6cfd0f0338e9b223ced05d0f8078ba0b0"
dependencies = [
"lazy_static 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
"nc_rustlex_codegen 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rustc-serialize"
version = "0.3.16"
@ -191,73 +163,38 @@ name = "strsim"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "syntex"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"syntex_syntax 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syntex_syntax"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
"term 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tempfile"
version = "1.1.1"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.10 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "term"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "time"
version = "0.1.32"
version = "0.1.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-xid"
version = "0.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "users"
version = "0.4.3"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi"
version = "0.2.2"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]

View File

@ -25,10 +25,7 @@ persistent = []
[build-dependencies]
rustc-serialize = "*"
[build-dependencies.rumblebars]
version = "*"
git = "https://github.com/nicolas-cherel/rumblebars.git"
handlebars = "*"
[dependencies]
libc = "*"

View File

@ -1,4 +1,4 @@
extern crate rumblebars;
extern crate handlebars;
extern crate rustc_serialize;
use std::env;
@ -7,8 +7,8 @@ use std::fs::{self, File};
use std::io::{Read, Write};
use std::collections::BTreeMap;
use rumblebars::{Template, EvalContext};
use rustc_serialize::json::Json;
use handlebars::{Handlebars, Context, Template};
use rustc_serialize::json::{Json, ToJson};
static UNITS_DIR: &'static str = "units";
static MAN_DIR: &'static str = "man";
@ -26,8 +26,9 @@ fn main() {
writeln!(config, "pub static LIB_DIR: &'static str = {:?};", data["libdir"].as_string().unwrap()).unwrap();
let mut data = Json::Object(data);
let schedules = get_required_schedules();
for schedule in get_required_schedules() {
for schedule in schedules.iter() {
data.as_object_mut().unwrap().insert("schedule".to_owned(), Json::String(schedule.clone()));
for schedule_unit in [ "target", "timer", "service" ].iter() {
compile_template(
@ -37,6 +38,8 @@ fn main() {
}
}
data.as_object_mut().unwrap().insert("schedules".to_owned(), schedules.to_json());
compile_templates(UNITS_DIR, output, &data);
compile_templates(MAN_DIR, output, &data);
}
@ -44,13 +47,16 @@ fn main() {
fn compile_template<S: AsRef<Path>, T: AsRef<Path>>(source_file: S, target_file: T, data: &Json) {
println!("compiling from template: {:?} -> {:?}...", source_file.as_ref(), target_file.as_ref());
let ctx = EvalContext::new();
let tmpl = File::open(source_file).and_then(|mut file| {
let mut buf = String::new();
file.read_to_string(&mut buf).map(|_| Template::new(&*buf).unwrap())
file.read_to_string(&mut buf).map(|_| Template::compile(&*buf).unwrap())
}).unwrap();
tmpl.eval(data, &mut File::create(target_file).unwrap(), &ctx).unwrap();
let mut handle = Handlebars::new();
handle.register_template("default", tmpl);
let ctx = Context::wraps(data);
handle.renderw("default", &ctx, &mut File::create(target_file).unwrap()).unwrap();
}
fn compile_templates<P: AsRef<Path>>(source_dir: &str, output_dir: P, data: &Json) {