-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
build.rs
51 lines (44 loc) · 1.42 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
/// This build script generates a version string.
use std::env;
use std::process;
/// The version of the tldr client specification being implemented.
const CLIENT_SPEC: &str = "2.2";
fn is_debug_build() -> bool {
env::var("PROFILE").unwrap() == "debug"
}
/// Get the short hash of the latest commit.
fn commit_hash() -> Option<String> {
let result = process::Command::new("git")
.args(["rev-parse", "--short", "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)
}
})
}
/// Get `CARGO_PKG_VERSION` and the client spec version.
fn pkgver_and_spec() -> String {
format!(
"v{} (implementing the tldr client specification v{CLIENT_SPEC})",
env!("CARGO_PKG_VERSION")
)
}
fn main() {
let ver = if is_debug_build() {
if let Some(hash) = commit_hash() {
format!("{} - debug build ({hash})", pkgver_and_spec())
} else {
// If git is not available, proceed with the compilation without the commit hash.
format!("{} - debug build", pkgver_and_spec())
}
} else {
// Same for release builds.
pkgver_and_spec()
};
// Put the version string inside an environment variable during the build.
println!("cargo:rustc-env=VERSION_STRING={ver}");
}