-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.rs
More file actions
69 lines (62 loc) · 1.92 KB
/
build.rs
File metadata and controls
69 lines (62 loc) · 1.92 KB
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
use std::env;
use std::process::Command;
fn non_empty_env(name: &str) -> Option<String> {
env::var(name)
.ok()
.map(|value| value.trim().to_string())
.filter(|value| !value.is_empty())
}
fn git_short_sha() -> Option<String> {
let output = Command::new("git")
.args(["rev-parse", "--short=12", "HEAD"])
.output()
.ok()?;
if !output.status.success() {
return None;
}
let sha = String::from_utf8_lossy(&output.stdout).trim().to_string();
if sha.is_empty() {
return None;
}
Some(sha)
}
fn git_head_path() -> Option<String> {
let output = Command::new("git")
.args(["rev-parse", "--git-path", "HEAD"])
.output()
.ok()?;
if !output.status.success() {
return None;
}
let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
if path.is_empty() {
return None;
}
Some(path)
}
fn compute_default_version() -> String {
let pkg_version = env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "0.0.0".to_string());
match git_short_sha() {
Some(sha) => format!("{pkg_version}-canary.{sha}"),
None => format!("{pkg_version}-canary.dev"),
}
}
fn main() {
let version = non_empty_env("BT_VERSION_STRING").unwrap_or_else(compute_default_version);
println!("cargo:rustc-env=BT_VERSION_STRING={version}");
let channel = non_empty_env("BT_UPDATE_CHANNEL").unwrap_or_else(|| {
if version.contains("-canary") {
"canary".to_string()
} else {
"stable".to_string()
}
});
println!("cargo:rustc-env=BT_UPDATE_CHANNEL={channel}");
println!("cargo:rerun-if-env-changed=BT_VERSION_STRING");
println!("cargo:rerun-if-env-changed=BT_UPDATE_CHANNEL");
if let Some(path) = git_head_path() {
println!("cargo:rerun-if-changed={path}");
} else {
println!("cargo:rerun-if-changed=.git/HEAD");
}
}