1 //! Construct version in the `commit-hash date chanel` format
3 use std::{env, path::PathBuf, process::Command};
7 println!("cargo:rustc-env=REV={}", rev())
11 println!("cargo:rerun-if-env-changed=RUST_ANALYZER_REV");
13 let mut manifest_dir = PathBuf::from(
14 env::var("CARGO_MANIFEST_DIR").expect("`CARGO_MANIFEST_DIR` is always set by cargo."),
17 while manifest_dir.parent().is_some() {
18 let head_ref = manifest_dir.join(".git/HEAD");
19 if head_ref.exists() {
20 println!("cargo:rerun-if-changed={}", head_ref.display());
27 println!("cargo:warning=Could not find `.git/HEAD` from manifest dir!");
31 if let Ok(rev) = env::var("RUST_ANALYZER_REV") {
35 if let Some(commit_hash) = commit_hash() {
36 let mut buf = commit_hash;
38 if let Some(date) = build_date() {
43 let channel = env::var("RUST_ANALYZER_CHANNEL").unwrap_or_else(|_| "dev".to_string());
45 buf.push_str(&channel);
53 fn commit_hash() -> Option<String> {
54 output_to_string("git rev-parse --short HEAD")
57 fn build_date() -> Option<String> {
58 output_to_string("date --iso --utc")
61 fn output_to_string(command: &str) -> Option<String> {
62 let args = command.split_ascii_whitespace().collect::<Vec<_>>();
63 let output = Command::new(args[0]).args(&args[1..]).output().ok()?;
64 let stdout = String::from_utf8(output.stdout).ok()?;
65 Some(stdout.trim().to_string())