mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
script codegen: Avoid modifying in-place a generated file.
This commit is contained in:
parent
71fb02953c
commit
0b9ff576e0
5 changed files with 29 additions and 34 deletions
|
@ -5,13 +5,13 @@
|
|||
extern crate cmake;
|
||||
extern crate phf_codegen;
|
||||
extern crate phf_shared;
|
||||
extern crate regex;
|
||||
extern crate serde_json;
|
||||
|
||||
use regex::Regex;
|
||||
use serde_json::Value;
|
||||
use std::env;
|
||||
use std::fmt;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::time::Instant;
|
||||
|
||||
|
@ -44,27 +44,17 @@ fn main() {
|
|||
|
||||
println!("Binding generation completed in {}s", start.elapsed().as_secs());
|
||||
|
||||
convert_phf();
|
||||
}
|
||||
|
||||
fn convert_phf() {
|
||||
let filename = PathBuf::from(env::var("OUT_DIR").unwrap()).join("InterfaceObjectMap.rs");
|
||||
let mut source = String::new();
|
||||
File::open(&filename).unwrap().read_to_string(&mut source).unwrap();
|
||||
let map_macro = Regex::new("phf_map! \\{([^}]+)\\}").unwrap().captures(&source).unwrap();
|
||||
let entries_re = Regex::new("b\"([^\"]+)\" => ([^\n]+),\n").unwrap();
|
||||
let entries = entries_re.captures_iter(&map_macro[1]);
|
||||
|
||||
let json = PathBuf::from(env::var("OUT_DIR").unwrap()).join("build").join("InterfaceObjectMapData.json");
|
||||
let json: Value = serde_json::from_reader(File::open(&json).unwrap()).unwrap();
|
||||
let mut map = phf_codegen::Map::new();
|
||||
for entry in entries {
|
||||
map.entry(Bytes(entry.get(1).unwrap().as_str()), entry.get(2).unwrap().as_str());
|
||||
for (key, value) in json.as_object().unwrap() {
|
||||
map.entry(Bytes(key), value.as_str().unwrap());
|
||||
}
|
||||
|
||||
let mut file = File::create(&filename).unwrap();
|
||||
let map_macro = map_macro.get(0).unwrap();
|
||||
file.write_all(source[..map_macro.start()].as_bytes()).unwrap();
|
||||
map.build(&mut file).unwrap();
|
||||
file.write_all(source[map_macro.end()..].as_bytes()).unwrap();
|
||||
let phf = PathBuf::from(env::var("OUT_DIR").unwrap()).join("InterfaceObjectMapPhf.rs");
|
||||
let mut phf = File::create(&phf).unwrap();
|
||||
write!(&mut phf, "pub static MAP: phf::Map<&'static [u8], unsafe fn(*mut JSContext, HandleObject)> = ").unwrap();
|
||||
map.build(&mut phf).unwrap();
|
||||
write!(&mut phf, ";\n").unwrap();
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Hash)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue