]> git.lizzy.rs Git - rust.git/commitdiff
fix: correct version string to contain hash, build date and channel
authorAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 24 Apr 2021 10:36:45 +0000 (13:36 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 24 Apr 2021 10:36:45 +0000 (13:36 +0300)
crates/rust-analyzer/build.rs
xtask/src/dist.rs

index 13b90389197fd356cef39c1dd262df27b42bb870..25627c7e9b2ec76d8b7d52723af0ba17bda2d0a1 100644 (file)
@@ -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<String> {
-    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<String> {
+    output_to_string("git rev-parse --short HEAD")
+}
+
+fn build_date() -> Option<String> {
+    output_to_string("date --iso --utc")
+}
+
+fn output_to_string(command: &str) -> Option<String> {
+    let args = command.split_ascii_whitespace().collect::<Vec<_>>();
+    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())
 }
index 1e26f2eccf462c7a5f49f5c42111072a833a893d..d1c00595438f7042a9a51647c712242cad7ff63a 100644 (file)
@@ -7,7 +7,7 @@
 
 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 @@ pub(crate) fn run(self) -> Result<()> {
             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");