-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(zk_toolbox): Verbose version message #2884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const GIT_VERSION: &str = git_version_macro::build_git_revision!(); | ||
const GIT_BRANCH: &str = git_version_macro::build_git_branch!(); | ||
const GIT_SUBMODULES: &[(&str, &str)] = git_version_macro::build_git_submodules!(); | ||
const BUILD_TIMESTAMP: &str = git_version_macro::build_timestamp!(); | ||
|
||
/// Returns a multi-line version message that includes: | ||
/// - provided crate version | ||
/// - git revision | ||
/// - git branch | ||
/// - git submodules | ||
/// - build timestamp | ||
pub fn version_message(crate_version: &str) -> String { | ||
let mut version = format!("v{}-{}\n", crate_version, GIT_VERSION); | ||
version.push_str(&format!("Branch: {}\n", GIT_BRANCH)); | ||
#[allow(clippy::const_is_empty)] // Proc-macro generated. | ||
if !GIT_SUBMODULES.is_empty() { | ||
version.push_str("Submodules:\n"); | ||
for (name, rev) in GIT_SUBMODULES { | ||
version.push_str(&format!(" - {}: {}\n", name, rev)); | ||
} | ||
} | ||
version.push_str(&format!("Build timestamp: {}\n", BUILD_TIMESTAMP)); | ||
version | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "git_version_macro" | ||
edition = "2021" | ||
description = "Procedural macro to generate metainformation about build in compile time" | ||
version.workspace = true | ||
homepage.workspace = true | ||
license.workspace = true | ||
authors.workspace = true | ||
exclude.workspace = true | ||
repository.workspace = true | ||
keywords.workspace = true | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
chrono.workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
extern crate proc_macro; | ||
use std::{process::Command, str::FromStr}; | ||
|
||
use proc_macro::TokenStream; | ||
|
||
/// Outputs the current date and time as a string literal. | ||
/// Can be used to include the build timestamp in the binary. | ||
#[proc_macro] | ||
pub fn build_timestamp(_item: TokenStream) -> TokenStream { | ||
let now = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(); | ||
encode_as_str(&now) | ||
} | ||
|
||
/// Outputs the current git branch as a string literal. | ||
#[proc_macro] | ||
pub fn build_git_branch(_item: TokenStream) -> TokenStream { | ||
let out = run_cmd("git", &["rev-parse", "--abbrev-ref", "HEAD"]); | ||
encode_as_str(&out) | ||
} | ||
|
||
/// Outputs the current git commit hash as a string literal. | ||
#[proc_macro] | ||
pub fn build_git_revision(_item: TokenStream) -> TokenStream { | ||
let out = run_cmd("git", &["rev-parse", "--short", "HEAD"]); | ||
encode_as_str(&out) | ||
} | ||
|
||
/// Creates a slice of `&[(&str, &str)]` tuples that correspond to | ||
/// the submodule name -> revision. | ||
/// Results in an empty list if there are no submodules or if | ||
/// the command fails. | ||
#[proc_macro] | ||
pub fn build_git_submodules(_item: TokenStream) -> TokenStream { | ||
let Some(out) = run_cmd_opt("git", &["submodule", "status"]) else { | ||
return TokenStream::from_str("&[]").unwrap(); | ||
}; | ||
let submodules = out | ||
.lines() | ||
.filter_map(|line| { | ||
let parts: Vec<&str> = line.split_whitespace().collect(); | ||
// Index 0 is commit hash, index 1 is the path to the folder, and there | ||
// may be some metainformation after that. | ||
if parts.len() >= 2 { | ||
let folder_name = parts[1].split('/').last().unwrap_or(parts[1]); | ||
Some((folder_name, parts[0])) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect::<Vec<_>>(); | ||
let submodules = submodules | ||
.iter() | ||
.map(|(name, rev)| format!("(\"{}\", \"{}\")", name, rev)) | ||
.collect::<Vec<_>>() | ||
.join(", "); | ||
TokenStream::from_str(format!("&[{}]", submodules).as_str()) | ||
.unwrap_or_else(|_| panic!("Unable to encode submodules: {}", submodules)) | ||
} | ||
|
||
/// Tries to run the command, only returns `Some` if the command | ||
/// succeeded and the output was valid utf8. | ||
fn run_cmd(cmd: &str, args: &[&str]) -> String { | ||
run_cmd_opt(cmd, args).unwrap_or("unknown".to_string()) | ||
} | ||
|
||
fn run_cmd_opt(cmd: &str, args: &[&str]) -> Option<String> { | ||
let output = Command::new(cmd).args(args).output().ok()?; | ||
if output.status.success() { | ||
String::from_utf8(output.stdout) | ||
.ok() | ||
.map(|s| s.trim().to_string()) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Encodes string as a literal. | ||
fn encode_as_str(s: &str) -> TokenStream { | ||
TokenStream::from_str(format!("\"{}\"", s).as_str()) | ||
.unwrap_or_else(|_| panic!("Unable to encode string: {}", s)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.