|
1 | 1 | /*!
|
2 |
| - Utility crate for get current git HEAD hash as |
| 2 | + Utility proc-macro crate for get current git HEAD hash as constant `Option<&'static str>`. |
3 | 3 |
|
4 |
| - - environment variables `GIT_HASH` and `GIT_HASH_SHORT` |
5 |
| - - constant `Option<&'static str>` |
6 |
| -
|
7 |
| - ## Usage examples |
8 |
| -
|
9 |
| - ### Use env var: |
10 |
| -
|
11 |
| - ```rust |
12 |
| - // build.rs |
13 |
| - extern crate git_hash; |
14 |
| -
|
15 |
| - fn main() { git_hash::env_git_hash_short(); } |
16 |
| -
|
17 |
| - // main.rs |
18 |
| - // fn main() { println!("{}", env!("GIT_HASH_SHORT")); } |
19 |
| - ``` |
20 |
| -
|
21 |
| - ### Use proc macro: |
| 4 | + ## Usage example: |
22 | 5 |
|
23 | 6 | ```rust
|
24 | 7 | // main.rs
|
|
31 | 14 | ```
|
32 | 15 | */
|
33 | 16 |
|
34 |
| -pub use macros::{git_hash, git_hash_short}; |
35 |
| -pub use macros::{crate_version_with_git_hash, crate_version_with_git_hash_short}; |
36 |
| -pub mod cmd; |
| 17 | +extern crate proc_macro; |
| 18 | +use proc_macro::{TokenStream, LexError}; |
| 19 | +mod cmd; |
37 | 20 |
|
38 |
| -/// Sets the current git (HEAD) commit SHA to env var `GIT_HASH` |
39 |
| -/// and makes it available for build & source code via standard `env!` macro. |
40 |
| -pub fn env_git_hash() { |
41 |
| - if let Some(git_hash) = cmd::git_hash() { |
42 |
| - println!("cargo:rustc-env=GIT_HASH={}", git_hash); |
43 |
| - println!("cargo:rustc-rerun-if-changed=.git/HEAD"); |
44 |
| - } |
| 21 | +#[proc_macro] |
| 22 | +pub fn git_hash(_: TokenStream) -> TokenStream { |
| 23 | + let git_hash = cmd::git_hash(); |
| 24 | + opt_tokenize(git_hash).unwrap() |
45 | 25 | }
|
46 | 26 |
|
47 |
| -/// Sets the current git (HEAD) commit SHA (as shorten view) to env var `GIT_HASH_SHORT` |
48 |
| -/// and makes it available for build & source code via standard `env!` macro. |
49 |
| -pub fn env_git_hash_short() { |
50 |
| - if let Some(git_hash) = cmd::git_hash_short() { |
51 |
| - println!("cargo:rustc-env=GIT_HASH_SHORT={}", git_hash); |
52 |
| - println!("cargo:rustc-rerun-if-changed=.git/HEAD"); |
53 |
| - } |
| 27 | +#[proc_macro] |
| 28 | +pub fn git_hash_short(_: TokenStream) -> TokenStream { |
| 29 | + let git_hash = cmd::git_hash_short(); |
| 30 | + opt_tokenize(git_hash).unwrap() |
| 31 | +} |
| 32 | + |
| 33 | +#[proc_macro] |
| 34 | +pub fn crate_version_with_git_hash(_: TokenStream) -> TokenStream { |
| 35 | + crate_version_with(cmd::git_hash()).unwrap() |
| 36 | +} |
| 37 | + |
| 38 | +#[proc_macro] |
| 39 | +pub fn crate_version_with_git_hash_short(_: TokenStream) -> TokenStream { |
| 40 | + crate_version_with(cmd::git_hash_short()).unwrap() |
| 41 | +} |
| 42 | + |
| 43 | +fn crate_version_with(s: Option<String>) -> Result<TokenStream, LexError> { |
| 44 | + let res = s |
| 45 | + .map(|rev| format!("-{}", rev)) |
| 46 | + .unwrap_or_else(Default::default); |
| 47 | + |
| 48 | + format!("concat!(env!(\"CARGO_PKG_VERSION\"), \"{}\")", res).parse() |
| 49 | +} |
| 50 | + |
| 51 | +fn opt_tokenize(s: Option<String>) -> Result<TokenStream, LexError> { |
| 52 | + s.map(|s| format!("Some(\"{}\")", s)) |
| 53 | + .unwrap_or_else(|| "None".to_owned()) |
| 54 | + .parse() |
54 | 55 | }
|
0 commit comments