#[macro_export]
macro_rules! crate_version {
(VERSION) => {{
use std::{env, fs::File, io::Write, path::Path, process::Command};
let main_version = env!("CARGO_PKG_VERSION");
let branch = Command::new("git")
.args(["branch", "--show-current"])
.output()
.map(|o| String::from_utf8(o.stdout).unwrap())
.unwrap();
let commit = Command::new("git")
.args(["describe", "--always"])
.output()
.map(|o| String::from_utf8(o.stdout).unwrap())
.unwrap();
let release_mode = if cfg!(debug_assertions) || cfg!(test) {
"DEBUG"
} else {
"RELEASE"
};
let version =
format!("{}-{}-{}-{}", main_version, branch, commit, release_mode).replace('\n', "");
File::create(Path::new(&env::var("OUT_DIR")?).join("VERSION"))?
.write_all(version.trim().as_bytes())?;
}};
(BUILD_TIME) => {{
use std::{env, fs::File, io::Write, path::Path};
let now = chrono::Local::now().to_rfc3339();
File::create(Path::new(&env::var("OUT_DIR")?).join("BUILD_TIME"))?
.write_all(now.trim().as_bytes())?;
}};
(VERSION => $vis:vis $name:ident) => {
$vis static $name: &'static str = include_str!(concat!(env!("OUT_DIR"), "/VERSION"));
};
(VERSION => $vis:vis $name:ident, $($c:tt)*) => {
$vis static $name: &'static str = concat!($($c)*, include_str!(concat!(env!("OUT_DIR"), "/VERSION")));
};
(BUILD_TIME => $vis:vis $name:ident) => {
$vis static $name: &'static str = include_str!(concat!(env!("OUT_DIR"), "/BUILD_TIME"));
};
}