-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbuild.rs
83 lines (70 loc) · 2.32 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#![allow(unused_imports)]
extern crate chrono;
// <% if param.with_protobuf %>
extern crate protoc_rust;
// <% endif %>
use chrono::Local;
// <% if param.with_protobuf %>
use protoc_rust::{Args, Customize};
// <% endif %>
use std::{env, fs, process};
fn main() {
// <% if param.with_protobuf %>
// Generate protocol buffer code
let out_dir = env::var("OUT_DIR").expect("Cannot get OUT_DIR");
protoc_rust::run(Args {
out_dir: &out_dir,
input: &["src/protos/$name_snake_case$.proto"],
includes: &["src/protos"],
customize: Customize {
serde_derive: Some(true),
..Default::default()
},
})
.expect("Protoc compile");
let path = format!("{}/$name_snake_case$.rs", out_dir);
let content = fs::read_to_string(&path).expect("cannot read autogen protobuf $name_snake_case$.rs");
let mut new_content = vec![];
// remove unwanted line of code
for line in content.split("\n") {
if line.starts_with("#![") || line.starts_with("//!") {
continue;
}
new_content.push(line);
}
let new_content: String = new_content.join("\n");
let _ = fs::write(&path, new_content);
// protocol buffer code generator ends
// <% endif %>
let output = process::Command::new("git").arg("rev-parse").arg("HEAD").output();
let mut git_rev:String = if let Ok(output) = output {
String::from_utf8_lossy(&output.stdout).to_string()
} else {
"".to_string()
};
git_rev = git_rev.trim().to_string();
if git_rev.is_empty() {
// get from file
if let Ok(_git_rev) = fs::read_to_string("GIT_REV") {
git_rev = _git_rev.trim().to_string();
}
}
// <% if param.with_protobuf %>
println!("cargo:rerun-if-changed={}", "src/protos/$name_snake_case$.proto");
// <% endif %>
println!("cargo:rustc-env=GIT_REV={}", git_rev);
if env::var("BUILD_FOR") == Ok("nightly".to_string()) {
println!(
"cargo:rustc-env=BUILD_INFO=ngihtly build {} @ {}",
env::var("TARGET").unwrap(),
Local::now()
);
} else {
println!(
"cargo:rustc-env=BUILD_INFO={} build {} @ {}",
env::var("PROFILE").unwrap(),
env::var("TARGET").unwrap(),
Local::now()
);
}
}