-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild.rs
More file actions
81 lines (66 loc) · 2.02 KB
/
build.rs
File metadata and controls
81 lines (66 loc) · 2.02 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
70
71
72
73
74
75
76
77
78
79
80
81
use std::path::PathBuf;
use sha2::Digest;
fn main() {
// Always rerun.
let index_css = include_str!("static/index.css");
let index_js = include_str!("static/index.js");
let index_css_name = get_file_ref("css", index_css);
let index_js_name = get_file_ref("js", index_js);
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
std::fs::write(
out_dir.join("revs.rs"),
format!(
r#"
pub const INDEX_CSS_NAME: &str = "{index_css_name}";
pub const INDEX_JS_NAME: &str = "{index_js_name}";
"#
),
)
.unwrap();
let version = if let Ok(commit) = try_get_commit() {
match has_no_changes() {
Ok(true) => commit,
Ok(false) => format!("{commit} (*)"),
Err(_) => format!("{commit} (?)"),
}
} else {
"unknown".into()
};
println!("cargo:rustc-env=GIT_COMMIT={version}");
let version_short = if version.len() > 16 {
&version[..16]
} else {
&version
};
println!("cargo:rustc-env=GIT_COMMIT_SHORT={version_short}");
}
fn get_file_ref(ext: &str, content: &str) -> String {
let mut hash = sha2::Sha256::new();
hash.update(content);
let digest = hash.finalize();
let rev = bs58::encode(digest).into_string();
let rev = &rev[..16];
format!("/{rev}.{ext}")
}
fn try_get_commit() -> color_eyre::Result<String> {
if let Ok(overridden) = std::env::var("DOES_IT_BUILD_OVERRIDE_VERSION") {
return Ok(overridden);
}
let stdout = std::process::Command::new("git")
.arg("rev-parse")
.arg("HEAD")
.output()?
.stdout;
let stdout = String::from_utf8(stdout)?;
Ok(stdout.trim()[0..8].to_owned())
}
fn has_no_changes() -> color_eyre::Result<bool> {
if std::env::var("DOES_IT_BUILD_OVERRIDE_VERSION").is_ok() {
return Ok(true);
}
Ok(std::process::Command::new("git")
.args(["diff", "--no-ext-diff", "--quiet", "--exit-code"])
.output()?
.status
.success())
}