diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-06-18 05:04:21 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-06-18 05:04:21 +0800 |
| commit | 961ee2eb342259398916dfe7458098f7c9297c11 (patch) | |
| tree | e8b3f2aa4cbd3afc8ce9da9751c4c0ad483f4273 /rola-cli | |
| parent | 133ab48d0d9ac1d0f8ad87b7a71b8e6cd4f7d402 (diff) | |
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.
Diffstat (limited to 'rola-cli')
| -rw-r--r-- | rola-cli/.gitignore | 2 | ||||
| -rw-r--r-- | rola-cli/Cargo.toml | 14 | ||||
| -rw-r--r-- | rola-cli/build.rs | 58 | ||||
| -rw-r--r-- | rola-cli/src/bin/.gitignore | 1 | ||||
| -rw-r--r-- | rola-cli/src/bin/rola.rs | 16 | ||||
| -rw-r--r-- | rola-cli/src/lib.rs | 13 |
6 files changed, 93 insertions, 11 deletions
diff --git a/rola-cli/.gitignore b/rola-cli/.gitignore new file mode 100644 index 0000000..b0b27d9 --- /dev/null +++ b/rola-cli/.gitignore @@ -0,0 +1,2 @@ +src/bin/debug.rs +version.txt diff --git a/rola-cli/Cargo.toml b/rola-cli/Cargo.toml index a04a403..6b45c5a 100644 --- a/rola-cli/Cargo.toml +++ b/rola-cli/Cargo.toml @@ -23,5 +23,17 @@ rev = "002f3fd390f64b1d7632f8530a0db81d45edf6c2" features = [ "parser", "extra_macros", - "dispatch_tree" + "dispatch_tree", + "comp" ] + +[build-dependencies.mingling] +git = "https://github.com/mingling-rs/mingling.git" +rev = "002f3fd390f64b1d7632f8530a0db81d45edf6c2" +features = [ + "builds", + "comp" +] + +[build-dependencies] +chrono = "0.4.45" 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(); +} diff --git a/rola-cli/src/bin/.gitignore b/rola-cli/src/bin/.gitignore deleted file mode 100644 index 6754721..0000000 --- a/rola-cli/src/bin/.gitignore +++ /dev/null @@ -1 +0,0 @@ -debug.rs diff --git a/rola-cli/src/bin/rola.rs b/rola-cli/src/bin/rola.rs index 1ebc3ae..0cfe675 100644 --- a/rola-cli/src/bin/rola.rs +++ b/rola-cli/src/bin/rola.rs @@ -3,19 +3,13 @@ use std::{env::current_dir, process::exit}; use mingling::{ Program, macros::program_setup, - setup::{BasicProgramSetup, ExitCodeSetup, QuietFlagSetup}, + setup::{ExitCodeSetup, HelpFlagSetup, QuietFlagSetup}, }; use rola_cli::{ThisProgram, locale, res::current_dir::ResCurrentDir}; fn main() { let mut program = ThisProgram::new(); - program.global_flag(["-v", "--version"], |_| { - let help = locale::helps::Basic::help().trim(); - eprintln!("{}", help); - exit(0) - }); - // Language locale::set_lang( program @@ -23,14 +17,20 @@ fn main() { .unwrap_or(locale::current_locales()), ); + // Version + program.global_flag(["-v", "--version"], |_| { + eprintln!("{}", include_str!("../../version.txt")); + exit(0) + }); + // Resources program.with_resource(ResCurrentDir { cwd: current_dir().unwrap(), }); // Setup + program.with_setup(HelpFlagSetup::new(["-h", "--help"])); program.with_setup(StandardOutputSetup); - program.with_setup(BasicProgramSetup); program.with_setup(ExitCodeSetup::default()); // Execute diff --git a/rola-cli/src/lib.rs b/rola-cli/src/lib.rs index f81c34e..54ff09a 100644 --- a/rola-cli/src/lib.rs +++ b/rola-cli/src/lib.rs @@ -1,4 +1,6 @@ -use mingling::macros::gen_program; +use std::process::exit; + +use mingling::macros::{gen_program, help}; pub mod res; pub mod tokio_wrapper; @@ -9,6 +11,15 @@ use bucket_mgr::*; mod error; use error::*; +#[help] +fn handle_error_dispatch_not_found(_err: ErrorDispatcherNotFound) { + let help = locale::helps::Basic::help().trim(); + + // Print directly to stderr and exit with code 0 + eprintln!("{}", help); + exit(0) +} + gen_program!(); pub mod locale { |
