1use std::process::Command;
2
3pub fn version() -> String {
4 String::from(env!("CARGO_PKG_VERSION"))
5}
6
7pub fn full_version() -> String {
8 let git_ref_str = git_ref()
9 .map(|r| format!("+git.{r:.12}"))
10 .unwrap_or_default();
11
12 format!("{version}{git_ref_str}", version = version())
13}
14
15pub fn project_dir() -> std::path::PathBuf {
16 std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
17 .parent()
18 .expect("invalid CARGO_MANIFEST_DIR")
19 .to_path_buf()
20}
21
22pub fn git_ref() -> Option<String> {
23 Command::new("git")
24 .arg("-C")
25 .arg(project_dir())
26 .args(["rev-parse", "HEAD"])
27 .output()
28 .ok()
29 .and_then(|output| {
30 if output.status.success() {
31 Some(output)
32 } else {
33 None
34 }
35 })
36 .and_then(|output| String::from_utf8(output.stdout).ok())
37 .map(|git_ref| git_ref.trim().to_string())
38}
39
40pub fn git_msg() -> Option<String> {
41 Command::new("git")
42 .arg("-C")
43 .arg(project_dir())
44 .args(["log", "-1", "--pretty=%B"])
45 .output()
46 .ok()
47 .and_then(|output| String::from_utf8(output.stdout).ok())
48 .map(|git_msg| git_msg.trim().to_string())
49}