-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
48 lines (45 loc) · 1.3 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
const ENV_FILE: &str = include_str!(".env");
fn main() {
parse_dotenv();
env_commit_hash();
}
fn parse_dotenv() {
for line in ENV_FILE.lines() {
let line = line.trim();
if line.starts_with('#') || line.is_empty() {
continue;
}
if line.ends_with('=') {
println!(
"cargo:warning={} environment variable is not set",
line.trim_end_matches('=')
);
continue;
}
println!("cargo:rustc-env={line}");
}
}
fn env_commit_hash() {
#[cfg(feature = "prod")]
{
let output = match std::process::Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
{
Ok(output) => match String::from_utf8(output.stdout) {
Ok(s) => s,
Err(e) => {
println!("cargo:warning=cannot get output of \"git rev-parse --short HEAD\" command: {e}");
return;
}
},
Err(e) => {
println!("cargo:warning=cannot get git commit hash: {e}");
return;
}
};
println!("cargo:rustc-env=GIT_HASH={output}")
}
#[cfg(not(feature = "prod"))]
println!("cargo:rustc-env=GIT_HASH=dev")
}