Commit b57ee574 authored by Nugine's avatar Nugine
Browse files

feat(s3s-test): collect build info

parent f736b549
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -20,3 +20,6 @@ aws-sdk-sts = { version = "1.46.0", features = ["behavior-version-latest"] }
version = "1.5.8"
default-features = false
features = ["behavior-version-latest"]

[build-dependencies]
s3s-test = { version = "0.11.0-dev", path = "../s3s-test" }
+3 −0
Original line number Diff line number Diff line
fn main() {
    s3s_test::build::collect_info();
}
+1 −0
Original line number Diff line number Diff line
@@ -22,3 +22,4 @@ colored = "2.1.0"
regex = "1.11.0"
nugine-rust-utils = "0.3.1"
backtrace = "0.3.74"
const-str = { version = "0.5.7", features = ["std", "proc"] }
+48 −0
Original line number Diff line number Diff line
use std::process::Command;

pub fn collect_info() {
    if let Some(val) = git_commit() {
        println!("cargo:rustc-env=S3S_GIT_COMMIT={val}");
    }
    if let Some(branch) = git_branch() {
        println!("cargo:rustc-env=S3S_GIT_BRANCH={branch}");
    }
    if let Some(tag) = git_tag() {
        println!("cargo:rustc-env=S3S_GIT_TAG={tag}");
    }
}

#[must_use]
fn git(args: &[&str]) -> Option<String> {
    let output = Command::new("git").args(args).output().ok()?;
    if output.status.success() {
        Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
    } else {
        None
    }
}

#[must_use]
pub fn git_commit() -> Option<String> {
    git(&["rev-parse", "HEAD"])
}

#[must_use]
pub fn git_branch() -> Option<String> {
    git(&["rev-parse", "--abbrev-ref", "HEAD"])
}

#[must_use]
pub fn git_tag() -> Option<String> {
    git(&["describe", "--tags", "--exact-match"])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_collect_info() {
        collect_info();
    }
}
+23 −1
Original line number Diff line number Diff line
@@ -17,6 +17,9 @@ type StdError = Box<dyn std::error::Error + Send + Sync + 'static>;
#[doc(hidden)]
pub use clap;

#[doc(hidden)]
pub use const_str;

#[doc(hidden)]
pub struct Options {
    pub json: Option<PathBuf>,
@@ -153,13 +156,32 @@ pub fn main(reg: impl FnOnce(&mut TestContext), opt: &Options) -> impl Terminati
    async_main(reg, opt)
}

#[doc(hidden)]
#[must_use]
pub const fn unwrap<'a>(s: Option<&'a str>, default: &'a str) -> &'a str {
    match s {
        Some(s) => s,
        None => default,
    }
}

#[macro_export]
macro_rules! main {
    ($register:expr) => {
        use s3s_test::cli::clap;

        const LONG_VERSION: &str = {
            use s3s_test::cli::const_str;
            use s3s_test::cli::unwrap;
            const PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
            const GIT_COMMIT: &str = unwrap(option_env!("S3S_GIT_COMMIT"), "-");
            const GIT_BRANCH: &str = unwrap(option_env!("S3S_GIT_BRANCH"), "-");
            const GIT_TAG: &str = unwrap(option_env!("S3S_GIT_TAG"), "-");
            const_str::format!("{}\nbranch: {}\ncommit: {}\ntag: {}", PKG_VERSION, GIT_BRANCH, GIT_COMMIT, GIT_TAG)
        };

        #[derive(Debug, clap::Parser)]
        #[clap(version)]
        #[clap(version, long_version = LONG_VERSION)]
        struct Opt {
            #[clap(long)]
            json: Option<::std::path::PathBuf>,
Loading