-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathbuild.rs
More file actions
40 lines (34 loc) · 1.42 KB
/
build.rs
File metadata and controls
40 lines (34 loc) · 1.42 KB
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
fn main() {
println!("cargo::rerun-if-env-changed=LAPIN_CODEGEN_DIR");
println!("cargo::rerun-if-env-changed=LAPIN_CODEGEN_FILE");
println!("cargo::rerun-if-changed=templates/channel.rs");
println!("cargo::rerun-if-changed=templates/lapin.json");
#[cfg(feature = "codegen-internal")]
codegen::generate()
}
#[cfg(feature = "codegen-internal")]
mod codegen {
use amq_protocol_codegen::{CodeGenerator, HandlebarsAMQPExtension};
use serde_json::Value;
pub(super) fn generate() {
let out_dir = std::env::var("LAPIN_CODEGEN_DIR")
.or_else(|_| std::env::var("OUT_DIR"))
.expect("OUT_DIR is not defined");
let extra = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/lapin.json"));
let data = serde_json::from_str::<Value>(extra).expect("Failed to parse extra file");
std::fs::create_dir_all(&out_dir).expect("failed to create codegen directory");
codegen(&out_dir, data, "channel");
}
fn codegen(out_dir: &str, data: Value, out_file: &str) {
let template_path = format!("{0}/templates/{out_file}.rs", env!("CARGO_MANIFEST_DIR"));
let template = std::fs::read_to_string(template_path).expect("failed to read template");
CodeGenerator::simple_codegen_with_data(
out_dir,
out_file,
out_file,
&template,
"protocol",
Some(data),
);
}
}