2021-04-24 05:36:45 -05:00
|
|
|
//! Construct version in the `commit-hash date chanel` format
|
2019-12-09 08:59:04 -06:00
|
|
|
|
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();
|
2021-10-03 07:39:43 -05:00
|
|
|
println!("cargo:rustc-env=REV={}", rev());
|
2019-12-09 08:59:04 -06:00
|
|
|
}
|
|
|
|
|
2020-02-20 20:04:03 -06:00
|
|
|
fn set_rerun() {
|
2020-06-18 07:31:32 -05:00
|
|
|
println!("cargo:rerun-if-env-changed=RUST_ANALYZER_REV");
|
|
|
|
|
2020-02-20 20:04:03 -06:00
|
|
|
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() {
|
2021-04-24 05:36:45 -05:00
|
|
|
let head_ref = manifest_dir.join(".git/HEAD");
|
|
|
|
if head_ref.exists() {
|
|
|
|
println!("cargo:rerun-if-changed={}", head_ref.display());
|
2020-02-20 20:04:03 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
manifest_dir.pop();
|
|
|
|
}
|
2021-04-24 05:36:45 -05:00
|
|
|
|
2020-02-20 20:04:03 -06:00
|
|
|
println!("cargo:warning=Could not find `.git/HEAD` from manifest dir!");
|
|
|
|
}
|
|
|
|
|
2021-04-24 05:36:45 -05:00
|
|
|
fn rev() -> String {
|
|
|
|
if let Ok(rev) = env::var("RUST_ANALYZER_REV") {
|
|
|
|
return rev;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(commit_hash) = commit_hash() {
|
|
|
|
let mut buf = commit_hash;
|
|
|
|
|
|
|
|
if let Some(date) = build_date() {
|
|
|
|
buf.push(' ');
|
|
|
|
buf.push_str(&date);
|
|
|
|
}
|
|
|
|
|
|
|
|
let channel = env::var("RUST_ANALYZER_CHANNEL").unwrap_or_else(|_| "dev".to_string());
|
|
|
|
buf.push(' ');
|
|
|
|
buf.push_str(&channel);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
"???????".to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn commit_hash() -> Option<String> {
|
2021-05-07 13:46:25 -05:00
|
|
|
exec("git rev-parse --short HEAD").ok()
|
2021-04-24 05:36:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn build_date() -> Option<String> {
|
2021-05-07 13:46:25 -05:00
|
|
|
exec("date -u +%Y-%m-%d").ok()
|
2021-04-24 05:36:45 -05:00
|
|
|
}
|
|
|
|
|
2021-05-07 13:46:25 -05:00
|
|
|
fn exec(command: &str) -> std::io::Result<String> {
|
2021-04-24 05:36:45 -05:00
|
|
|
let args = command.split_ascii_whitespace().collect::<Vec<_>>();
|
2021-05-07 13:46:25 -05:00
|
|
|
let output = Command::new(args[0]).args(&args[1..]).output()?;
|
|
|
|
if !output.status.success() {
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::InvalidData,
|
|
|
|
format!("command {:?} returned non-zero code", command,),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
let stdout = String::from_utf8(output.stdout)
|
|
|
|
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?;
|
|
|
|
Ok(stdout.trim().to_string())
|
2019-12-09 08:59:04 -06:00
|
|
|
}
|