-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
72 lines (61 loc) · 1.95 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
use std::{
env,
error::Error,
fs::File,
io::{BufWriter, Write},
path::Path,
};
use chrono;
fn get_git_version() -> Result<String, Box<dyn Error>> {
use std::process::Command;
use std::str;
let branch_name = Command::new("git")
.arg("branch")
.arg("--show-current")
.output()?;
let branch_name = str::from_utf8(&branch_name.stdout)?
.trim_end();
let latest_tag = Command::new("git")
.arg("describe")
.arg("--tags")
.arg("--abbrev=0")
.output()?;
let latest_tag = str::from_utf8(&latest_tag.stdout)?
.trim_end();
let commit_short = Command::new("git")
.arg("rev-parse")
.arg("--short")
.arg("HEAD")
.output()?;
let commit_short = str::from_utf8(&commit_short.stdout)?
.trim_end();
let tag_short = Command::new("git")
.arg("rev-parse")
.arg("--short")
.arg(&latest_tag)
.output()?;
let tag_short = str::from_utf8(&tag_short.stdout)?
.trim_end();
if tag_short == commit_short {
Ok(String::new()) // Favor version at Cargo.toml
} else {
Ok(String::from(format!("{}-{}", commit_short, branch_name)))
}
}
fn main() -> Result<(), Box<dyn Error>> {
let outdir = env::var("OUT_DIR")?;
let timestamp = chrono::offset::Local::now();
let timestamp = timestamp.format("%Y-%m-%d %H:%M:%S");
let destpath = Path::new(&outdir).join("timestamp.txt");
let mut file = BufWriter::new(File::create(&destpath)?);
write!(file, "{}", timestamp)?;
let version = get_git_version()?;
let destpath = Path::new(&outdir).join("version.txt");
let mut file = BufWriter::new(File::create(&destpath)?);
write!(file, "{}", version)?;
let target = env::var("TARGET")?;
let destpath = Path::new(&outdir).join("target.txt");
let mut file = BufWriter::new(File::create(&destpath)?);
write!(file, "{}", target)?;
Ok(())
}