From 961ee2eb342259398916dfe7458098f7c9297c11 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 18 Jun 2026 05:04:21 +0800 Subject: feat: add build script for version info and shell completions Introduce a build.rs that generates a version.txt file with the package version, git commit hash, and date. Also generate shell completion scripts using mingling's build utilities. Migrate the `-v/--version` flag to read from the generated version.txt and replace BasicProgramSetup with HelpFlagSetup. --- rola-cli/build.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 rola-cli/build.rs (limited to 'rola-cli/build.rs') diff --git a/rola-cli/build.rs b/rola-cli/build.rs new file mode 100644 index 0000000..a409a17 --- /dev/null +++ b/rola-cli/build.rs @@ -0,0 +1,58 @@ +use std::path::Path; +use std::process::Command; + +use mingling::build::build_comp_scripts; + +fn main() { + build_version_info(); + build_completion(); +} + +fn build_version_info() { + // Read version from CARGO_PKG_VERSION (inherited from workspace Cargo.toml) + let version = env!("CARGO_PKG_VERSION"); + + // Get git commit hash (first 9 characters) + let commit_hash = Command::new("git") + .args(["rev-parse", "--short=9", "HEAD"]) + .output() + .ok() + .and_then(|output| { + if output.status.success() { + String::from_utf8(output.stdout).ok() + } else { + None + } + }) + .map(|s| s.trim().to_string()) + .unwrap_or_else(|| "unknown".to_string()); + + // Get date from git commit, fallback to current date + let date = Command::new("git") + .args(["log", "-1", "--format=%ad", "--date=format:%Y-%m-%d"]) + .output() + .ok() + .and_then(|output| { + if output.status.success() { + String::from_utf8(output.stdout).ok() + } else { + None + } + }) + .map(|s| s.trim().to_string()) + .unwrap_or_else(|| chrono::Local::now().format("%Y-%m-%d").to_string()); + + let version_string = format!("rola {version} ({commit_hash} {date})"); + + let out_dir = Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("version.txt"); + + std::fs::write(&out_dir, version_string).expect("failed to write version.txt"); + + println!("cargo::rerun-if-changed=../Cargo.toml"); + println!("cargo::rerun-if-changed=../Cargo.lock"); + println!("cargo::rerun-if-changed=.git/HEAD"); +} + +fn build_completion() { + build_comp_scripts("rola").unwrap(); +} -- cgit