version_macro/
lib.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
extern crate proc_macro;
use std::{process::Command, str::FromStr};

use chrono::{Datelike, Local, Timelike};
use proc_macro::TokenStream;

#[proc_macro]
pub fn build_time(_item: TokenStream) -> TokenStream {
    let now = Local::now();

    let now = format!(
        "\"{}-{:02}-{:02} {:02}:{:02}:{:02}\"",
        now.year(),
        now.month(),
        now.day(),
        now.hour(),
        now.minute(),
        now.second(),
    );
    TokenStream::from_str(now.as_str()).unwrap()
}

#[proc_macro]
pub fn build_git_branch(_item: TokenStream) -> TokenStream {
    let cmd = Command::new("git")
        .args(&["describe", "--all", "--dirty"])
        .output()
        .unwrap();

    let result = if cmd.status.success() {
        match String::from_utf8(cmd.stdout) {
            Ok(v) => format!("\"{}\"", v.trim()),
            Err(e) => format!("\"{}\"", e),
        }
    } else {
        match String::from_utf8(cmd.stderr) {
            Ok(v) => format!("\"{}\"", v.trim()),
            Err(e) => format!("\"{}\"", e),
        }
    };

    TokenStream::from_str(result.as_str()).unwrap()
}

#[proc_macro]
pub fn build_git_version(_item: TokenStream) -> TokenStream {
    let cmd = Command::new("git")
        .args(&["rev-parse", "--short", "HEAD"])
        .output()
        .unwrap();

    let result = if cmd.status.success() {
        match String::from_utf8(cmd.stdout) {
            Ok(v) => format!("\"{}\"", v.trim()),
            Err(e) => format!("\"{}\"", e),
        }
    } else {
        match String::from_utf8(cmd.stderr) {
            Ok(v) => format!("\"{}\"", v.trim()),
            Err(e) => format!("\"{}\"", e),
        }
    };

    TokenStream::from_str(result.as_str()).unwrap()
}