-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
64 lines (60 loc) · 1.88 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
use chrono::prelude::*;
use std::env;
use std::fs::{self, File};
use std::path::Path;
use std::process;
fn main() {
// OUT_DIR is set by Cargo and it's where any additional build artifacts
// are written.
let outdir = match env::var_os("OUT_DIR") {
Some(outdir) => outdir,
None => {
eprintln!(
"OUT_DIR environment variable not defined. \
Please file a bug: \
https://github.com/rodmoioliveira/cnj/issues/new"
);
process::exit(1);
}
};
fs::create_dir_all(&outdir).unwrap();
let stamp_path = Path::new(&outdir).join("cnj-stamp");
if let Err(err) = File::create(&stamp_path) {
panic!("failed to write {}: {}", stamp_path.display(), err);
}
// Make the current git hash available to the build.
if let Some(rev) = git_revision_hash() {
println!("cargo:rustc-env=CNJ_BUILD_GIT_HASH={rev}");
}
if let Some(branch) = git_branch() {
println!("cargo:rustc-env=CNJ_BUILD_GIT_BRANCH={branch}");
}
let date = Utc::now().format("%Y-%m-%dT%H:%M:%SZ");
println!("cargo:rustc-env=CNJ_BUILD_DATE={date}");
}
fn git_revision_hash() -> Option<String> {
let result = process::Command::new("git")
.args(["rev-parse", "--short=10", "HEAD"])
.output();
result.ok().and_then(|output| {
let v = String::from_utf8_lossy(&output.stdout).trim().to_string();
if v.is_empty() {
None
} else {
Some(v)
}
})
}
fn git_branch() -> Option<String> {
let result = process::Command::new("git")
.args(["rev-parse", "--abbrev-ref", "HEAD"])
.output();
result.ok().and_then(|output| {
let v = String::from_utf8_lossy(&output.stdout).trim().to_string();
if v.is_empty() {
None
} else {
Some(v)
}
})
}