forked from apognu/tuigreet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
28 lines (24 loc) · 1.04 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
use std::{env, error::Error, path::Path, process::Command};
fn main() {
let version = if Path::new(".git").exists() {
get_git_version().unwrap_or_else(|_| String::from("unknown"))
} else {
env!("CARGO_PKG_VERSION").to_string()
};
println!("cargo:rustc-env=VERSION={}", version);
println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
}
fn get_git_version() -> Result<String, Box<dyn Error>> {
let tag = Command::new("git").args(&["describe", "--abbrev=0"]).output()?;
let tag = match tag.status.code() {
Some(0) => String::from_utf8(tag.stdout)?,
_ => "0.0.0".to_string(),
};
let count = String::from_utf8(Command::new("git").args(&["rev-list", "--count", "HEAD"]).output()?.stdout)?;
let commit = String::from_utf8(Command::new("git").args(&["rev-parse", "--short", "HEAD"]).output()?.stdout)?;
let version = format!("{}.r{}.{}", tag.trim(), count.trim(), commit.trim());
match version.as_str() {
"0.0.0.r." => Err("could not retrieve version".into()),
version => Ok(version.to_string()),
}
}