forked from surrealdb/surrealdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
56 lines (50 loc) · 1.4 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
use std::process::Command;
use std::{env, str};
const BUILD_METADATA: &str = "SURREAL_BUILD_METADATA";
fn main() {
println!("cargo:rerun-if-env-changed={BUILD_METADATA}");
println!("cargo:rerun-if-changed=lib");
println!("cargo:rerun-if-changed=src");
println!("cargo:rerun-if-changed=Cargo.toml");
println!("cargo:rerun-if-changed=Cargo.lock");
if let Some(metadata) = build_metadata() {
println!("cargo:rustc-env={BUILD_METADATA}={metadata}");
}
}
fn build_metadata() -> Option<String> {
if let Ok(metadata) = env::var(BUILD_METADATA) {
return Some(metadata);
}
let date = git()
.args(["show", "--no-patch", "--format=%ad", "--date=format:%Y%m%d"])
.output_string()?;
let rev = git().args(["rev-parse", "--short", "HEAD"]).output_string()?;
let repo_clean = git()
.args(["diff", "--quiet"])
.output()
.map(|output| output.status.success())
.unwrap_or_default();
let metadata = if repo_clean {
format!("{date}.{rev}")
} else {
format!("{date}.{rev}.dirty")
};
Some(metadata)
}
fn git() -> Command {
Command::new("git")
}
trait CommandExt {
fn output_string(&mut self) -> Option<String>;
}
impl CommandExt for Command {
fn output_string(&mut self) -> Option<String> {
self.output()
.ok()
.filter(|output| output.status.success())
.and_then(|output| {
str::from_utf8(&output.stdout).ok().map(|output| output.trim().to_string())
})
.filter(|output| !output.is_empty())
}
}