forked from radicle-dev/heartwood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
94 lines (88 loc) · 2.94 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::env;
use std::process::Command;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Set a build-time `GIT_HEAD` env var which includes the commit id;
// such that we can tell which code is running.
let hash = Command::new("git")
.arg("rev-parse")
.arg("--short")
.arg("HEAD")
.output()
.ok()
.and_then(|output| {
if output.status.success() {
String::from_utf8(output.stdout).ok()
} else {
None
}
})
.unwrap_or(env::var("GIT_HEAD").unwrap_or("unknown".into()));
let tags = Command::new("git")
.arg("tag")
.arg("--points-at")
.arg("HEAD")
.output()
.ok()
.and_then(|output| {
if output.status.success() {
String::from_utf8(output.stdout).ok()
} else {
None
}
})
.unwrap_or_default();
let tags = tags
.split_terminator('\n')
.filter_map(|s| s.strip_prefix('v'))
.collect::<Vec<_>>();
if tags.len() > 1 {
return Err("More than one version tag found for commit {hash}: {tags:?}".into());
}
// Used for `RADICLE_VERSION` env.
let version = if let Some(version) = tags.first() {
// There's a tag pointing at this `HEAD`.
// Eg. "1.0.43".
Some((*version).to_owned())
} else {
// If `HEAD` doesn't have a tag pointing to it, this is a development version,
// so find the closest tag starting with `v` and append `-dev` to the version.
// Eg. "1.0.43-dev".
Command::new("git")
.arg("describe")
.arg("--match=v*") // Match tags starting with `v`
.arg("--candidates=1") // Only one result
.arg("--abbrev=0") // Don't add the commit short-hash to the tag name
.arg("HEAD")
.output()
.ok()
.and_then(|output| {
if output.status.success() {
String::from_utf8(output.stdout).ok()
} else {
None
}
})
.map(|last| format!("{}-dev", last.trim()))
}
// If there are no tags found, we'll just call this a pre-release.
.unwrap_or(String::from("1.0.0-rc.1"));
// Set a build-time `GIT_COMMIT_TIME` env var which includes the commit time.
let commit_time = Command::new("git")
.arg("show")
.arg("--format=%ct")
.arg("HEAD")
.output()
.ok()
.and_then(|output| {
if output.status.success() {
String::from_utf8(output.stdout).ok()
} else {
None
}
})
.unwrap_or(0.to_string());
println!("cargo::rustc-env=RADICLE_VERSION={version}");
println!("cargo::rustc-env=GIT_COMMIT_TIME={commit_time}");
println!("cargo::rustc-env=GIT_HEAD={hash}");
Ok(())
}