Skip to content

Commit 9907d87

Browse files
committed
simplify git-hash crate;
fix rustfmt-check fail on windows (rel issue: rust-lang/rustfmt#4477)
1 parent 886b05a commit 9907d87

File tree

6 files changed

+39
-114
lines changed

6 files changed

+39
-114
lines changed

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ members = [
66
"movec",
77
"resources",
88
"common/git-hash",
9-
"common/git-hash/macro",
109
]

common/git-hash/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ version = "0.1.0"
44
authors = ["Alex Koz. <[email protected]>"]
55
edition = "2018"
66

7-
[dependencies]
8-
macros = { package = "git-hash-proc-macro", path = "macro" }
7+
[lib]
8+
proc-macro = true

common/git-hash/macro/Cargo.toml

Lines changed: 0 additions & 9 deletions
This file was deleted.

common/git-hash/macro/lib.rs

Lines changed: 0 additions & 59 deletions
This file was deleted.

common/git-hash/src/lib.rs

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
/*!
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>`.
33
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:
225
236
```rust
247
// main.rs
@@ -31,24 +14,42 @@
3114
```
3215
*/
3316

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;
3720

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()
4525
}
4626

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()
5455
}

0 commit comments

Comments
 (0)