-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
41 lines (37 loc) · 1.22 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
32
33
34
35
36
37
38
39
40
41
fn main() {
#[cfg(feature = "cli")]
cli::generate_version_info();
}
#[cfg(feature = "cli")]
mod cli {
use std::{env, fs, path::PathBuf, process::Command, str};
pub fn generate_version_info() {
let pkg_version = env::var("CARGO_PKG_VERSION").expect("missing package version");
let git_version = git_version();
let git_prefix = git_version
.is_some()
.then_some("commit-")
.unwrap_or_default();
let git_version = git_version.as_deref().unwrap_or("unknown");
let version = format!("{pkg_version}-git.{git_prefix}{git_version}");
let out: PathBuf = env::var_os("OUT_DIR").expect("build output path").into();
fs::write(
out.join("cli.rs"),
format!("pub const VERSION: &str = {version:?};\n"),
)
.expect("write cli.rs");
}
fn git_version() -> Option<String> {
let output = Command::new("git")
.arg("describe")
.arg("--always")
.arg("--dirty=-modified")
.output()
.ok()?;
if output.status.success() {
Some(str::from_utf8(&output.stdout).ok()?.trim().into())
} else {
None
}
}
}