Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate code from dataset and testsets #2

Merged
merged 6 commits into from
May 13, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
wip: code generate
hhatto committed May 12, 2016
commit 875273f3eaa72be2b4e436d332f865c623e0b5ee
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -7,7 +7,17 @@ repository = "https://github.com/hhatto/woothee-rust"
keywords = ["useragent", "ua"]
description = "user-agent strings parser"
documentation = "http://hhatto.github.io/woothee-rust/woothee/"
build = "build.rs"

[dependencies]
regex = "0.1"
lazy_static = "0.2.1"

[features]
default = []
code-update = ["yaml-rust", "tera", "tempdir"]

[build-dependencies]
tera = { version = "*", optional = true }
yaml-rust = { version = "*", optional = true }
tempdir = { version = "0.3", optional = true }
125 changes: 125 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#[cfg(feature = "code-update")]
mod inner {
extern crate yaml_rust;
extern crate tempdir;
use std::env;
use std::fs::File;
use std::io::{Write, Read};
use std::path::{Path, PathBuf};
use std::process::Command;
use self::tempdir::TempDir;
use self::yaml_rust::YamlLoader;

fn gen_dataset(woothee_dir: PathBuf, dest: PathBuf) {
let mut f = File::create(&dest).unwrap();

match env::set_current_dir(woothee_dir.as_path()) {
Err(why) => panic!("set_current_dir() error. {}", why),
Ok(_) => (),
};
let yaml_file = woothee_dir.join(Path::new("dataset.yaml"));

let path = Path::new(&yaml_file);
let mut y = File::open(&path).unwrap();
let mut s = String::new();
match y.read_to_string(&mut s) {
Err(why) => panic!("yaml.read_to_string() {}", why),
Ok(_) => (),
}

let header = "// This file is auto-generated! Any changes to this file will be lost!
use \
std::collections::HashMap;
use parser::WootheeResult;
pub fn \
get_default_dataset() -> HashMap<String, WootheeResult> {
let mut \
dataset: HashMap<String, WootheeResult> = HashMap::new();
";
let footer = " dataset\n}";
f.write_all(header.as_bytes()).unwrap();

let docs = YamlLoader::load_from_str(s.as_str()).unwrap();
let doc = &docs[0];
for vecc in doc.as_vec().unwrap() {
for (key, value) in vecc.as_hash().unwrap() {
match key.as_str().unwrap() {
"label" => {
f.write_all(format!(" dataset.insert(\"{}\".to_string(),\n",
value.as_str().unwrap())
.as_bytes())
.unwrap();
f.write_all(b" WootheeResult{").unwrap();
}
"name" => {
f.write_all(format!(" name: \"{}\".to_string(),\n",
value.as_str().unwrap())
.as_bytes())
.unwrap();
}
"type" => {
f.write_all(format!(" browser_type: \"{}\".to_string(),\n",
value.as_str().unwrap())
.as_bytes())
.unwrap();
}
"vendor" => {
f.write_all(format!(" vendor: \"{}\".to_string(),\n",
value.as_str().unwrap())
.as_bytes())
.unwrap();
}
"category" => {
f.write_all(format!(" category: \"{}\".to_string(),\n",
value.as_str().unwrap())
.as_bytes());
}
"os" => {
f.write_all(format!(" os: \"{}\".to_string(),\n",
value.as_str().unwrap())
.as_bytes());
}
_ => (),
}
}
f.write_all(b" ..WootheeResult::default()\n });\n").unwrap();
}

// f.write_all(output.as_bytes()).unwrap();

f.write_all(footer.as_bytes()).unwrap();
}

pub fn gen() {
let dest_path = env::current_dir().unwrap().join("./src/dataset.rs");

// git clone in tempdir
let dir = TempDir::new("fooo").unwrap();
let mut curdir = dir.path();
match env::set_current_dir(&mut curdir) {
Err(why) => panic!("set_current_dir() error. {}", why),
Ok(_) => (),
}

let _ = Command::new("git")
.arg("clone")
.arg("-q")
.arg("https://github.com/woothee/woothee.git")
.output()
.unwrap();

let woothee_dir = curdir.join(Path::new("woothee"));
gen_dataset(woothee_dir, dest_path);
}
}


#[cfg(not(feature = "code-update"))]
mod inner {
pub fn gen() {}
}

fn main() {
inner::gen();
}