-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
49 lines (40 loc) · 1.5 KB
/
Copy pathbuild.rs
File metadata and controls
49 lines (40 loc) · 1.5 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
fn main() {
let output = std::process::Command::new("date")
.arg("+%b %d %Y|%H:%M:%S|%Y")
.output()
.ok()
.filter(|o| o.status.success())
.and_then(|o| String::from_utf8(o.stdout).ok());
let (date, time, year) = output.map_or_else(
|| (unknown(), unknown(), unknown()),
|raw| {
let trimmed = raw.trim();
let parts: Vec<&str> = trimmed.splitn(3, '|').collect();
(
parts.first().copied().unwrap_or("Unknown").to_string(),
parts.get(1).copied().unwrap_or("Unknown").to_string(),
parts.get(2).copied().unwrap_or("Unknown").to_string(),
)
},
);
println!("cargo:rustc-env=BUILD_DATE={date}");
println!("cargo:rustc-env=BUILD_TIME={time}");
println!("cargo:rustc-env=BUILD_YEAR={year}");
generate_inc();
}
fn unknown() -> String {
"Unknown".to_string()
}
fn generate_inc() {
use std::fs;
let template_path = "include/env_samp.inc.in";
let output_path = "include/env_samp.inc";
let template = fs::read_to_string(template_path)
.unwrap_or_else(|e| panic!("failed to read {template_path}: {e}"));
let version = env!("CARGO_PKG_VERSION");
let rendered = template.replace("{{VERSION}}", version);
if fs::read_to_string(output_path).ok().as_deref() != Some(rendered.as_str()) {
fs::write(output_path, &rendered)
.unwrap_or_else(|e| panic!("failed to write {output_path}: {e}"));
}
}