From 04e586790bf0f00bd8af41184633423aa68946f2 Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Mon, 1 Jan 2024 07:24:22 -0500 Subject: [PATCH] =?UTF-8?q?feat:=20Use=20vergen=20to=20generate=20version?= =?UTF-8?q?=20message=20=E2=9C=A8=20(#33)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- async/ratatui-counter/Cargo.toml | 4 +++ async/ratatui-counter/build.rs | 51 ++++-------------------------- async/ratatui-counter/src/utils.rs | 6 ++-- async/template/Cargo.toml | 4 +++ async/template/build.rs | 48 ++-------------------------- async/template/src/utils.rs | 7 ++-- 6 files changed, 22 insertions(+), 98 deletions(-) diff --git a/async/ratatui-counter/Cargo.toml b/async/ratatui-counter/Cargo.toml index 3fa5b79..e9e551f 100644 --- a/async/ratatui-counter/Cargo.toml +++ b/async/ratatui-counter/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" description = "Counter application with async-template" authors = ["Dheepak Krishnamurthy"] +build = "build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -34,3 +35,6 @@ tracing = "0.1.37" tracing-error = "0.2.0" tracing-subscriber = { version = "0.3.17", features = ["env-filter", "serde"] } tui-input = { version = "0.8.0", features = ["serde"] } + +[build-dependencies] +vergen = { version = "8", features = [ "build", "git", "gitoxide", "cargo" ]} diff --git a/async/ratatui-counter/build.rs b/async/ratatui-counter/build.rs index bc84301..75bd127 100644 --- a/async/ratatui-counter/build.rs +++ b/async/ratatui-counter/build.rs @@ -1,46 +1,7 @@ -fn main() { - let git_output = std::process::Command::new("git").args(["rev-parse", "--git-dir"]).output().ok(); - let git_dir = git_output.as_ref().and_then(|output| { - std::str::from_utf8(&output.stdout).ok().and_then(|s| s.strip_suffix('\n').or_else(|| s.strip_suffix("\r\n"))) - }); - - // Tell cargo to rebuild if the head or any relevant refs change. - if let Some(git_dir) = git_dir { - let git_path = std::path::Path::new(git_dir); - let refs_path = git_path.join("refs"); - if git_path.join("HEAD").exists() { - println!("cargo:rerun-if-changed={}/HEAD", git_dir); - } - if git_path.join("packed-refs").exists() { - println!("cargo:rerun-if-changed={}/packed-refs", git_dir); - } - if refs_path.join("heads").exists() { - println!("cargo:rerun-if-changed={}/refs/heads", git_dir); - } - if refs_path.join("tags").exists() { - println!("cargo:rerun-if-changed={}/refs/tags", git_dir); - } - } - - let git_output = - std::process::Command::new("git").args(["describe", "--always", "--tags", "--long", "--dirty"]).output().ok(); - let git_info = git_output.as_ref().and_then(|output| std::str::from_utf8(&output.stdout).ok().map(str::trim)); - let cargo_pkg_version = env!("CARGO_PKG_VERSION"); - - // Default git_describe to cargo_pkg_version - let mut git_describe = String::from(cargo_pkg_version); - - if let Some(git_info) = git_info { - // If the `git_info` contains `CARGO_PKG_VERSION`, we simply use `git_info` as it is. - // Otherwise, prepend `CARGO_PKG_VERSION` to `git_info`. - if git_info.contains(cargo_pkg_version) { - // Remove the 'g' before the commit sha - let git_info = &git_info.replace('g', ""); - git_describe = git_info.to_string(); - } else { - git_describe = format!("v{}-{}", cargo_pkg_version, git_info); - } - } - - println!("cargo:rustc-env=RATATUI_COUNTER_GIT_INFO={}", git_describe); +fn main() -> Result<(), Box> { + vergen::EmitBuilder::builder() + .all_build() + .all_git() + .emit()?; + Ok(()) } diff --git a/async/ratatui-counter/src/utils.rs b/async/ratatui-counter/src/utils.rs index 5e7ce88..8e2c67a 100644 --- a/async/ratatui-counter/src/utils.rs +++ b/async/ratatui-counter/src/utils.rs @@ -7,7 +7,7 @@ use tracing::error; use tracing_error::ErrorLayer; use tracing_subscriber::{self, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, Layer}; -pub static GIT_COMMIT_HASH: &'static str = env!("RATATUI_COUNTER_GIT_INFO"); +const VERSION_MESSAGE: &str = concat!(env!("CARGO_PKG_VERSION"), "-", env!("VERGEN_GIT_DESCRIBE"), " (", env!("VERGEN_BUILD_DATE"), ")"); lazy_static! { pub static ref PROJECT_NAME: String = env!("CARGO_CRATE_NAME").to_uppercase().to_string(); @@ -144,15 +144,13 @@ macro_rules! trace_dbg { pub fn version() -> String { let author = clap::crate_authors!(); - let commit_hash = GIT_COMMIT_HASH; - // let current_exe_path = PathBuf::from(clap::crate_name!()).display().to_string(); let config_dir_path = get_config_dir().display().to_string(); let data_dir_path = get_data_dir().display().to_string(); format!( "\ -{commit_hash} +{VERSION_MESSAGE} Authors: {author} diff --git a/async/template/Cargo.toml b/async/template/Cargo.toml index aaec4d4..14dd230 100644 --- a/async/template/Cargo.toml +++ b/async/template/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" description = "{{project-description}}" {% if repository != "" %}repository = "{{repository}}"{% endif %} authors = ["{{authors}}"] +build = "build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -34,3 +35,6 @@ tokio-util = "0.7.9" tracing = "0.1.37" tracing-error = "0.2.0" tracing-subscriber = { version = "0.3.17", features = ["env-filter", "serde"] } + +[build-dependencies] +vergen = { version = "8.2.6", features = [ "build", "git", "gitoxide", "cargo" ]} diff --git a/async/template/build.rs b/async/template/build.rs index cf1aa0e..52484c2 100644 --- a/async/template/build.rs +++ b/async/template/build.rs @@ -1,46 +1,4 @@ -fn main() { - let git_output = std::process::Command::new("git").args(["rev-parse", "--git-dir"]).output().ok(); - let git_dir = git_output.as_ref().and_then(|output| { - std::str::from_utf8(&output.stdout).ok().and_then(|s| s.strip_suffix('\n').or_else(|| s.strip_suffix("\r\n"))) - }); - - // Tell cargo to rebuild if the head or any relevant refs change. - if let Some(git_dir) = git_dir { - let git_path = std::path::Path::new(git_dir); - let refs_path = git_path.join("refs"); - if git_path.join("HEAD").exists() { - println!("cargo:rerun-if-changed={}/HEAD", git_dir); - } - if git_path.join("packed-refs").exists() { - println!("cargo:rerun-if-changed={}/packed-refs", git_dir); - } - if refs_path.join("heads").exists() { - println!("cargo:rerun-if-changed={}/refs/heads", git_dir); - } - if refs_path.join("tags").exists() { - println!("cargo:rerun-if-changed={}/refs/tags", git_dir); - } - } - - let git_output = - std::process::Command::new("git").args(["describe", "--always", "--tags", "--long", "--dirty"]).output().ok(); - let git_info = git_output.as_ref().and_then(|output| std::str::from_utf8(&output.stdout).ok().map(str::trim)); - let cargo_pkg_version = env!("CARGO_PKG_VERSION"); - - // Default git_describe to cargo_pkg_version - let mut git_describe = String::from(cargo_pkg_version); - - if let Some(git_info) = git_info { - // If the `git_info` contains `CARGO_PKG_VERSION`, we simply use `git_info` as it is. - // Otherwise, prepend `CARGO_PKG_VERSION` to `git_info`. - if git_info.contains(cargo_pkg_version) { - // Remove the 'g' before the commit sha - let git_info = &git_info.replace('g', ""); - git_describe = git_info.to_string(); - } else { - git_describe = format!("v{}-{}", cargo_pkg_version, git_info); - } - } - - println!("cargo:rustc-env=_GIT_INFO={}", git_describe); // used in ./src/utils.rs +fn main() -> Result<(), Box> { + vergen::EmitBuilder::builder().all_build().all_git().emit()?; + Ok(()) } diff --git a/async/template/src/utils.rs b/async/template/src/utils.rs index da22959..938e470 100644 --- a/async/template/src/utils.rs +++ b/async/template/src/utils.rs @@ -7,7 +7,8 @@ use tracing::error; use tracing_error::ErrorLayer; use tracing_subscriber::{self, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, Layer}; -pub static GIT_COMMIT_HASH: &'static str = env!("_GIT_INFO"); +const VERSION_MESSAGE: &str = + concat!(env!("CARGO_PKG_VERSION"), "-", env!("VERGEN_GIT_DESCRIBE"), " (", env!("VERGEN_BUILD_DATE"), ")"); lazy_static! { pub static ref PROJECT_NAME: String = env!("CARGO_CRATE_NAME").to_uppercase().to_string(); @@ -144,15 +145,13 @@ macro_rules! trace_dbg { pub fn version() -> String { let author = clap::crate_authors!(); - let commit_hash = GIT_COMMIT_HASH; - // let current_exe_path = PathBuf::from(clap::crate_name!()).display().to_string(); let config_dir_path = get_config_dir().display().to_string(); let data_dir_path = get_data_dir().display().to_string(); format!( "\ -{commit_hash} +{VERSION_MESSAGE} Authors: {author}