macro_toolset/
misc.rs

1//! Misc
2
3#[macro_export]
4/// Helper to create version string or build time for your crate.
5///
6/// Package version with git info is in format `{CARGO_PKG_VERSION}-{git
7/// btanch}-{git commit}-{debug or release}`, like `0.4.0-main-c298bc2-DEBUG`.
8///
9/// Should have `git` installed.
10///
11/// # Example
12///
13/// - Create a `build.rs` in the root dir if not persists, and use this macro in
14///   the main func. The main func should return `Result`.
15///
16///   - For a version string, use `crate_version!(VERSION)`
17///   - For a build time in RFC3339 format, use `crate_version!(BUILD_TIME)`.
18///
19///     Don't forget to add `chrono` to your build dependencies.
20/// - In your crate, place `crate_version!(VERSION => pub VERSION)`,
21///   `crate_version!(VERSION => pub VERSION)` where you like.
22///
23///   You can even do this: `crate_version!(VERSION => pub VERSION,
24/// env!("CARGO_PKG_NAME", "/"))`
25macro_rules! crate_version {
26    (VERSION) => {{
27        use std::{env, fs::File, io::Write, path::Path, process::Command};
28
29        let main_version = env!("CARGO_PKG_VERSION");
30
31        let branch = Command::new("git")
32            .args(["branch", "--show-current"])
33            .output()
34            .map(|o| String::from_utf8(o.stdout).unwrap())
35            .unwrap();
36
37        let commit = Command::new("git")
38            .args(["describe", "--always"])
39            .output()
40            .map(|o| String::from_utf8(o.stdout).unwrap())
41            .unwrap();
42
43        let release_mode = if cfg!(debug_assertions) || cfg!(test) {
44            "DEBUG"
45        } else {
46            "RELEASE"
47        };
48
49        let version =
50            format!("{}-{}-{}-{}", main_version, branch, commit, release_mode).replace('\n', "");
51        File::create(Path::new(&env::var("OUT_DIR")?).join("VERSION"))?
52            .write_all(version.trim().as_bytes())?;
53    }};
54    (BUILD_TIME) => {{
55        use std::{env, fs::File, io::Write, path::Path};
56
57        let now = chrono::Local::now().to_rfc3339();
58        File::create(Path::new(&env::var("OUT_DIR")?).join("BUILD_TIME"))?
59            .write_all(now.trim().as_bytes())?;
60    }};
61    (VERSION => $vis:vis $name:ident) => {
62        /// The git version.
63        $vis static $name: &'static str = include_str!(concat!(env!("OUT_DIR"), "/VERSION"));
64    };
65    (VERSION => $vis:vis $name:ident, $($c:tt)*) => {
66        /// The git version.
67        $vis static $name: &'static str = concat!($($c)*, include_str!(concat!(env!("OUT_DIR"), "/VERSION")));
68    };
69    (BUILD_TIME => $vis:vis $name:ident) => {
70        /// The git version.
71        $vis static $name: &'static str = include_str!(concat!(env!("OUT_DIR"), "/BUILD_TIME"));
72    };
73}