-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathbuild.rs
42 lines (36 loc) · 1.63 KB
/
build.rs
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
41
42
use std::fs::File;
use std::io::prelude::*;
use std::process::Command;
fn main() {
proto();
version();
}
fn proto() {
let out = std::env::var("OUT_DIR").unwrap();
println!("out: {}", out);
let build_res = tonic_build::configure().out_dir(out).compile_protos(&["pb.proto"], &["src/grpc/proto"]);
println!("compile proto result! {:?}", build_res);
build_res.unwrap();
}
fn version() {
let mut cargo_text = String::new();
File::open("../Cargo.toml").and_then(|mut f| f.read_to_string(&mut cargo_text)).unwrap();
let decoded: toml::Value = toml::from_str(&cargo_text).unwrap();
let version =
decoded.get("workspace").unwrap().get("package").unwrap().get("version").unwrap().as_str().unwrap();
let build_time = chrono::Local::now().format("%Y%m%d%H%M%S").to_string();
let server_version = format!("rmqtt/{}-{}", &version, &build_time);
let out = std::env::var("OUT_DIR").unwrap();
let mut version_file = File::create(format!("{}/{}", out, "version.rs")).unwrap();
version_file.write_all(b"\n/// rmqtt version").unwrap();
version_file
.write_all(format!("\npub const VERSION: &str = \"{}\";", server_version).as_bytes())
.unwrap();
let rustc_version_out = Command::new("rustc").arg("--version").output().expect("Failed to execute rustc");
let rustc_version = String::from_utf8_lossy(&rustc_version_out.stdout);
version_file.write_all(b"\n/// rustc version").unwrap();
version_file
.write_all(format!("\npub const RUSTC_VERSION: &str = \"{}\";", rustc_version.trim()).as_bytes())
.unwrap();
println!("cargo:rerun-if-changed=build.rs");
}