-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
37 lines (29 loc) · 1007 Bytes
/
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
use std::env;
use std::fs;
use std::path::Path;
use vergen::EmitBuilder;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Generate build information
EmitBuilder::builder()
.build_timestamp()
.all_build()
.emit()?;
// Get the output directory for generated files
let out_dir = env::var("OUT_DIR")?;
let dest_path = Path::new(&out_dir).join("version_gen.rs");
// Get the package version from Cargo.toml
let version = env!("CARGO_PKG_VERSION");
// Create the version string with current date
let now = chrono::Local::now();
let build_date = now.format("%Y%m%d");
// Generate the contents of version_gen.rs
let contents = format!(
"pub const VERSION: &str = \"{}-{}\";\n",
version, build_date
);
// Write the generated code to the file
fs::write(&dest_path, contents)?;
// Tell cargo to rerun this script if Cargo.toml changes
println!("cargo:rerun-if-changed=Cargo.toml");
Ok(())
}