From 8d54fd105cbeb159c39fbc803ed45d450f0fe000 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 24 Apr 2021 13:36:45 +0300 Subject: fix: correct version string to contain hash, build date and channel --- crates/rust-analyzer/build.rs | 62 ++++++++++++++++++++++++++++--------------- xtask/src/dist.rs | 8 +++--- 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/crates/rust-analyzer/build.rs b/crates/rust-analyzer/build.rs index 13b903891..25627c7e9 100644 --- a/crates/rust-analyzer/build.rs +++ b/crates/rust-analyzer/build.rs @@ -1,13 +1,10 @@ -//! Just embed git-hash to `--version` +//! Construct version in the `commit-hash date chanel` format use std::{env, path::PathBuf, process::Command}; fn main() { set_rerun(); - - let rev = - env::var("RUST_ANALYZER_REV").ok().or_else(rev).unwrap_or_else(|| "???????".to_string()); - println!("cargo:rustc-env=REV={}", rev) + println!("cargo:rustc-env=REV={}", rev()) } fn set_rerun() { @@ -18,29 +15,52 @@ fn set_rerun() { ); 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()); - } - } + let head_ref = manifest_dir.join(".git/HEAD"); + if head_ref.exists() { + println!("cargo:rerun-if-changed={}", head_ref.display()); return; } manifest_dir.pop(); } + println!("cargo:warning=Could not find `.git/HEAD` from manifest dir!"); } -fn rev() -> Option { - let output = - Command::new("git").args(&["describe", "--tags", "--exclude", "nightly"]).output().ok()?; +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 { + output_to_string("git rev-parse --short HEAD") +} + +fn build_date() -> Option { + output_to_string("date --iso --utc") +} + +fn output_to_string(command: &str) -> Option { + let args = command.split_ascii_whitespace().collect::>(); + let output = Command::new(args[0]).args(&args[1..]).output().ok()?; let stdout = String::from_utf8(output.stdout).ok()?; - Some(stdout) + Some(stdout.trim().to_string()) } diff --git a/xtask/src/dist.rs b/xtask/src/dist.rs index 1e26f2ecc..d1c005954 100644 --- a/xtask/src/dist.rs +++ b/xtask/src/dist.rs @@ -7,7 +7,7 @@ use std::{ use anyhow::Result; use flate2::{write::GzEncoder, Compression}; -use xshell::{cmd, cp, mkdir_p, pushd, read_file, rm_rf, write_file}; +use xshell::{cmd, cp, mkdir_p, pushd, pushenv, read_file, rm_rf, write_file}; use crate::{date_iso, project_root}; @@ -26,7 +26,8 @@ impl DistCmd { let release_tag = if self.nightly { "nightly".to_string() } else { date_iso()? }; dist_client(&version, &release_tag)?; } - dist_server()?; + let release_channel = if self.nightly { "nightly" } else { "stable" }; + dist_server(release_channel)?; Ok(()) } } @@ -59,7 +60,8 @@ fn dist_client(version: &str, release_tag: &str) -> Result<()> { Ok(()) } -fn dist_server() -> Result<()> { +fn dist_server(release_channel: &str) -> Result<()> { + let _e = pushenv("RUST_ANALYZER_CHANNEL", release_channel); let target = get_target(); if target.contains("-linux-gnu") || target.contains("-linux-musl") { env::set_var("CC", "clang"); -- cgit v1.2.3