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 exec("git rev-parse --short HEAD").ok()
57 fn build_date() -> Option<String> {
58 exec("date -u +%Y-%m-%d").ok()
61 fn exec(command: &str) -> std::io::Result<String> {
62 let args = command.split_ascii_whitespace().collect::<Vec<_>>();
63 let output = Command::new(args[0]).args(&args[1..]).output()?;
64 if !output.status.success() {
65 return Err(std::io::Error::new(
66 std::io::ErrorKind::InvalidData,
67 format!("command {:?} returned non-zero code", command,),
70 let stdout = String::from_utf8(output.stdout)
71 .map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
72 Ok(stdout.trim().to_string())