This repository has been archived by the owner on Nov 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
build.rs
43 lines (36 loc) · 1.25 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
//! # Build module
//!
//! The build module create rust files at build time
//! in order to inject some source code.
use std::env;
use std::fs::File;
use std::io::Write;
use failure::{Error, ResultExt};
use git2::Repository;
use time::now_utc;
pub fn main() -> Result<(), Error> {
// Load the current git repository and retrieve the last commit using the
// HEAD current reference
let repository = Repository::discover(".").context("Expect to have a git repository")?;
let identifier = repository
.revparse_single("HEAD")
.context("Expect to have at least one commit")?
.id();
// Retrieve the current time use UTC timezone
let now = now_utc();
let profile = env::var("PROFILE").context("Expect to be built using cargo")?;
// Generate the version file
let mut file = File::create("src/version.rs")?;
file.write_all(
format!(
"pub(crate) const BUILD_DATE: &str = \"{}\";\n",
now.rfc3339()
)
.as_bytes(),
)?;
file.write_all(format!("pub(crate) const GITHASH: &str = \"{}\";\n", identifier).as_bytes())?;
file.write_all(format!("pub(crate) const PROFILE: &str = \"{}\";\n", profile).as_bytes())?;
file.flush()?;
file.sync_all()?;
Ok(())
}