From b1182d4f7b23adc8042998980afa53b4c5c3c5f4 Mon Sep 17 00:00:00 2001 From: bytedream Date: Mon, 28 Nov 2022 11:54:04 +0100 Subject: [PATCH] Add (short) commit hash and build time to version hash --- build.rs | 7 ------- crunchy-cli-core/Cargo.toml | 5 ++++- crunchy-cli-core/build.rs | 33 +++++++++++++++++++++++++++++++++ crunchy-cli-core/src/lib.rs | 30 +++++++++++++++++++++++++----- 4 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 crunchy-cli-core/build.rs diff --git a/build.rs b/build.rs index cc6a0cc..1e4d71f 100644 --- a/build.rs +++ b/build.rs @@ -2,14 +2,7 @@ use clap::{Command, CommandFactory}; use clap_complete::shells; use std::path::{Path, PathBuf}; -// this build file generates completions for various shells as well as manual pages - fn main() -> std::io::Result<()> { - // do not generate anything when building non release - if cfg!(debug_assertions) { - return Ok(()); - } - // note that we're using an anti-pattern here / violate the rust conventions. build script are // not supposed to write outside of 'OUT_DIR'. to have the generated files in the build "root" // (the same directory where the output binary lives) is much simpler than in 'OUT_DIR' since diff --git a/crunchy-cli-core/Cargo.toml b/crunchy-cli-core/Cargo.toml index 067679b..00635b0 100644 --- a/crunchy-cli-core/Cargo.toml +++ b/crunchy-cli-core/Cargo.toml @@ -14,7 +14,7 @@ static-ssl = ["crunchyroll-rs/static-ssl"] [dependencies] anyhow = "1.0" async-trait = "0.1" -clap = { version = "4.0", features = ["derive"] } +clap = { version = "4.0", features = ["derive", "string"] } chrono = "0.4" crunchyroll-rs = { git = "https://github.com/crunchy-labs/crunchyroll-rs", default-features = false, features = ["stream", "parse"] } ctrlc = "3.2" @@ -28,3 +28,6 @@ tempfile = "3.3" terminal_size = "0.2" tokio = { version = "1.21", features = ["macros", "rt-multi-thread", "time"] } sys-locale = "0.2" + +[build-dependencies] +chrono = "0.4" diff --git a/crunchy-cli-core/build.rs b/crunchy-cli-core/build.rs new file mode 100644 index 0000000..f7d5974 --- /dev/null +++ b/crunchy-cli-core/build.rs @@ -0,0 +1,33 @@ +fn main() -> std::io::Result<()> { + println!( + "cargo:rustc-env=GIT_HASH={}", + get_short_commit_hash()?.unwrap_or_default() + ); + println!( + "cargo:rustc-env=BUILD_DATE={}", + chrono::Utc::now().format("%F") + ); + + Ok(()) +} + +fn get_short_commit_hash() -> std::io::Result> { + let git = std::process::Command::new("git") + .arg("rev-parse") + .arg("--short") + .arg("HEAD") + .output(); + + match git { + Ok(cmd) => Ok(Some( + String::from_utf8_lossy(cmd.stdout.as_slice()).to_string(), + )), + Err(e) => { + if e.kind() != std::io::ErrorKind::NotFound { + Err(e) + } else { + Ok(None) + } + } + } +} diff --git a/crunchy-cli-core/src/lib.rs b/crunchy-cli-core/src/lib.rs index faef927..4571337 100644 --- a/crunchy-cli-core/src/lib.rs +++ b/crunchy-cli-core/src/lib.rs @@ -20,13 +20,15 @@ trait Execute { } #[derive(Debug, Parser)] -#[clap(author, version, about)] +#[clap(author, version = version(), about)] #[clap(name = "crunchy-cli")] pub struct Cli { #[clap(flatten)] verbosity: Option, - #[arg(help = "Overwrite the language in which results are returned. Default is your system language")] + #[arg( + help = "Overwrite the language in which results are returned. Default is your system language" + )] #[arg(long)] lang: Option, @@ -37,6 +39,18 @@ pub struct Cli { command: Command, } +fn version() -> String { + let package_version = env!("CARGO_PKG_VERSION"); + let git_commit_hash = env!("GIT_HASH"); + let build_date = env!("BUILD_DATE"); + + if git_commit_hash.is_empty() { + format!("{}", package_version) + } else { + format!("{} ({} {})", package_version, git_commit_hash, build_date) + } +} + #[derive(Debug, Subcommand)] enum Command { Archive(Archive), @@ -51,7 +65,9 @@ struct Verbosity { v: bool, #[arg(help = "Quiet output. Does not print anything unless it's a error")] - #[arg(long_help = "Quiet output. Does not print anything unless it's a error. Can be helpful if you pipe the output to stdout")] + #[arg( + long_help = "Quiet output. Does not print anything unless it's a error. Can be helpful if you pipe the output to stdout" + )] #[arg(short)] q: bool, } @@ -59,11 +75,15 @@ struct Verbosity { #[derive(Debug, Parser)] struct LoginMethod { #[arg(help = "Login with credentials (username or email and password)")] - #[arg(long_help = "Login with credentials (username or email and password). Must be provided as user:password")] + #[arg( + long_help = "Login with credentials (username or email and password). Must be provided as user:password" + )] #[arg(long)] credentials: Option, #[arg(help = "Login with the etp-rt cookie")] - #[arg(long_help = "Login with the etp-rt cookie. This can be obtained when you login on crunchyroll.com and extract it from there")] + #[arg( + long_help = "Login with the etp-rt cookie. This can be obtained when you login on crunchyroll.com and extract it from there" + )] #[arg(long)] etp_rt: Option, }