1extern crate proc_macro;
2use std::{process::Command, str::FromStr};
3
4use chrono::{Datelike, Local, Timelike};
5use proc_macro::TokenStream;
6
7#[proc_macro]
8pub fn build_time(_item: TokenStream) -> TokenStream {
9 let now = Local::now();
10
11 let now = format!(
12 "\"{}-{:02}-{:02} {:02}:{:02}:{:02}\"",
13 now.year(),
14 now.month(),
15 now.day(),
16 now.hour(),
17 now.minute(),
18 now.second(),
19 );
20 TokenStream::from_str(now.as_str()).unwrap()
21}
22
23#[proc_macro]
24pub fn build_git_branch(_item: TokenStream) -> TokenStream {
25 let cmd = Command::new("git")
26 .args(&["describe", "--all", "--dirty"])
27 .output()
28 .unwrap();
29
30 let result = if cmd.status.success() {
31 match String::from_utf8(cmd.stdout) {
32 Ok(v) => format!("\"{}\"", v.trim()),
33 Err(e) => format!("\"{}\"", e),
34 }
35 } else {
36 match String::from_utf8(cmd.stderr) {
37 Ok(v) => format!("\"{}\"", v.trim()),
38 Err(e) => format!("\"{}\"", e),
39 }
40 };
41
42 TokenStream::from_str(result.as_str()).unwrap()
43}
44
45#[proc_macro]
46pub fn build_git_version(_item: TokenStream) -> TokenStream {
47 let cmd = Command::new("git")
48 .args(&["rev-parse", "--short", "HEAD"])
49 .output()
50 .unwrap();
51
52 let result = if cmd.status.success() {
53 match String::from_utf8(cmd.stdout) {
54 Ok(v) => format!("\"{}\"", v.trim()),
55 Err(e) => format!("\"{}\"", e),
56 }
57 } else {
58 match String::from_utf8(cmd.stderr) {
59 Ok(v) => format!("\"{}\"", v.trim()),
60 Err(e) => format!("\"{}\"", e),
61 }
62 };
63
64 TokenStream::from_str(result.as_str()).unwrap()
65}