extern crate glob;
use glob::glob;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::fmt;
use std::env;
use std::path::Path;
fn main() {
let mut output = String::new();
fmt::write(
&mut output,
format_args!("static PATTERNS: &[(&'static str, &'static str)] = &[\n"),
).unwrap();
for line in glob("patterns/*.pattern").unwrap() .map(|e| e.unwrap())
.map(|path| File::open(path).unwrap())
.flat_map(|f| BufReader::new(f).lines())
.map(|line| line.unwrap())
.filter(|line| !line.starts_with("#"))
.filter(|line| !line.is_empty())
{
let (key, value) = line.split_at(line.find(" ").unwrap());
fmt::write(
&mut output,
format_args!("\t(\"{}\", r#\"{}\"#),\n", key, &value[1..]),
).unwrap();
}
fmt::write(&mut output, format_args!("];\n")).unwrap();
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("patterns.rs");
let mut file = File::create(&dest_path).unwrap();
file.write_all(output.as_bytes()).unwrap();
}