-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
31 lines (28 loc) · 1 KB
/
build.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
use git2::DescribeFormatOptions;
use git2::DescribeOptions;
use git2::Repository;
use std::path::Path;
fn main() {
let out_dir = std::env::var_os("OUT_DIR").expect("Failed to determine output directory");
let dest_path = Path::new(&out_dir).join("version.rs");
let version: Option<String> = std::env::var_os("CARGO_MANIFEST_DIR")
.map(|dir| {
Repository::open(dir)?
.describe(DescribeOptions::new().describe_tags().pattern("v*"))?
.format(Some(
DescribeFormatOptions::new().always_use_long_format(true),
))
})
.and_then(Result::ok)
.map(|v| v.trim().replacen('-', ".", 1));
std::fs::write(
dest_path,
format!(
"mod version {{ pub fn git_version() -> Option<&'static str> {{ {} }} }}",
version
.map(|v| format!("Some(\"{v}\")"))
.unwrap_or("None".to_string())
),
)
.expect("Failed to write version.rs");
}