-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
71 lines (60 loc) · 1.92 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
/// The version string isn’t the simplest: we want to show the version,
/// current Git hash, and compilation date when building *debug* versions, but
/// just the version for *release* versions so the builds are reproducible.
///
/// This script generates the string from the environment variables that Cargo
/// adds (http://doc.crates.io/environment-variables.html) and runs `git` to
/// get the SHA1 hash. It then writes the string into a file, which exa then
/// includes at build-time.
///
/// - https://stackoverflow.com/q/43753491/3484614
/// - https://crates.io/crates/vergen
extern crate datetime;
use std::env;
use std::io::Result as IOResult;
fn git_hash() -> String {
use std::process::Command;
String::from_utf8_lossy(
&Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.output()
.unwrap()
.stdout,
)
.trim()
.to_string()
}
fn main() {
write_statics().unwrap();
}
fn is_development_version() -> bool {
// Both weekly releases and actual releases are --release releases,
// but actual releases will have a proper version number
cargo_version().ends_with("-pre") || env::var("PROFILE").unwrap() == "debug"
}
fn cargo_version() -> String {
env::var("CARGO_PKG_VERSION").unwrap()
}
fn build_date() -> String {
use datetime::{LocalDateTime, ISO};
let now = LocalDateTime::now();
format!("{}", now.date().iso())
}
fn write_statics() -> IOResult<()> {
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
let ver = if is_development_version() {
format!(
"exa v{} ({} built on {})",
cargo_version(),
git_hash(),
build_date()
)
} else {
format!("exa v{}", cargo_version())
};
let out = PathBuf::from(env::var("OUT_DIR").unwrap());
let mut f = File::create(&out.join("version_string.txt"))?;
write!(f, "{:?}", ver)
}