forked from devguardio/carrier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
105 lines (88 loc) · 3.05 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#![recursion_limit = "256"]
extern crate prost_build;
extern crate rand;
extern crate clap;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use std::env;
use std::fs::File;
use std::io::{Write, Read};
use std::iter;
use std::path::Path;
use std::process::{Command, Stdio};
use std::io::Error;
use clap::Shell;
include!("src/cli.rs");
pub fn from_git() -> Result<String, Error>
{
let cmd = Command::new("git")
.args(&["rev-list", "--count", "HEAD"])
.stderr(Stdio::inherit())
.output()
.unwrap();
if !cmd.status.success() {
return Err(Error::new(std::io::ErrorKind::Other, "git failed"))
}
let gitcount = String::from_utf8_lossy(&cmd.stdout).to_owned();
let gitcount = gitcount.trim();
let gitcount: u64 = gitcount.parse().unwrap();
let dest_path = "src/revision.rs";
let mut ft : Vec<u8> = Vec::new();
ft.write_all(b"pub const REVISION: u32 = ").unwrap();
ft.write_all(format!("{}", gitcount).as_bytes()).unwrap();
ft.write_all(b";\n").unwrap();
let mut diff = true;
if let Ok(mut f) = File::open(&dest_path) {
let mut ft_exists = Vec::new();
f.read_to_end(&mut ft_exists).ok();
diff = ft_exists != ft;
}
if diff {
let mut f = File::create(&dest_path).unwrap();
f.write_all(&ft).unwrap();
}
let cmd = Command::new("git")
.args(&["describe", "--tags", "--always", "--dirty=-dirty"])
.stderr(Stdio::inherit())
.output()
.unwrap();
if !cmd.status.success() {
return Err(Error::new(std::io::ErrorKind::Other, "git failed"))
}
let gitver = String::from_utf8_lossy(&cmd.stdout).to_owned();
let gitver = gitver.trim().to_string();
Ok(gitver)
}
pub fn main() {
let gitver = match from_git() {
Ok(v) => v,
Err(_) => format!("{}-cargo", env::var("CARGO_PKG_VERSION").unwrap())
};
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("build_id.rs");
let mut f = File::create(&dest_path).unwrap();
let mut rng = thread_rng();
let chars: String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(10).collect();
f.write_all(b"pub const BUILD_ID : &'static str = \"").unwrap();
f.write_all(gitver.as_bytes()).unwrap();
f.write_all(b"-").unwrap();
f.write_all(chars.as_bytes()).unwrap();
f.write_all(b"\";\n").unwrap();
let mut app = build_cli();
app.gen_completions("carrier", // We need to specify the bin name manually
Shell::Bash, // Then say which shell to build completions for
out_dir);// Then say where write the completions to
let mut config = prost_build::Config::new();
config
.compile_protos(
&[
"proto/carrier.broker.v1.proto",
"proto/carrier.certificate.v1.proto",
"proto/carrier.sysinfo.v1.proto",
"proto/carrier.discovery.v1.proto",
"proto/genesis.v1.proto",
],
&["proto"],
)
.unwrap();
}