rust/crates/rust-analyzer/build.rs

44 lines
1.4 KiB
Rust
Raw Normal View History

2019-12-09 08:59:04 -06:00
//! Just embed git-hash to `--version`
2020-02-20 20:04:03 -06:00
use std::{env, path::PathBuf, process::Command};
2019-12-09 08:59:04 -06:00
fn main() {
2020-02-20 02:47:12 -06:00
set_rerun();
2020-02-20 20:04:03 -06:00
2019-12-09 08:59:04 -06:00
let rev = rev().unwrap_or_else(|| "???????".to_string());
println!("cargo:rustc-env=REV={}", rev)
}
2020-02-20 20:04:03 -06:00
fn set_rerun() {
let mut manifest_dir = PathBuf::from(
env::var("CARGO_MANIFEST_DIR").expect("`CARGO_MANIFEST_DIR` is always set by cargo."),
);
while manifest_dir.parent().is_some() {
if manifest_dir.join(".git/HEAD").exists() {
let git_dir = manifest_dir.join(".git");
println!("cargo:rerun-if-changed={}", git_dir.join("HEAD").display());
// current branch ref
if let Ok(output) =
Command::new("git").args(&["rev-parse", "--symbolic-full-name", "HEAD"]).output()
{
if let Ok(ref_link) = String::from_utf8(output.stdout) {
println!("cargo:rerun-if-changed={}", git_dir.join(ref_link).display());
}
}
return;
}
manifest_dir.pop();
}
println!("cargo:warning=Could not find `.git/HEAD` from manifest dir!");
}
2019-12-09 08:59:04 -06:00
fn rev() -> Option<String> {
let output = Command::new("git").args(&["rev-parse", "HEAD"]).output().ok()?;
let stdout = String::from_utf8(output.stdout).ok()?;
let short_hash = stdout.get(0..7)?;
Some(short_hash.to_owned())
}