aboutsummaryrefslogtreecommitdiff
path: root/mling/build.rs
diff options
context:
space:
mode:
Diffstat (limited to 'mling/build.rs')
-rw-r--r--mling/build.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/mling/build.rs b/mling/build.rs
new file mode 100644
index 0000000..abbcc50
--- /dev/null
+++ b/mling/build.rs
@@ -0,0 +1,62 @@
+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!("mling {version} ({commit_hash} {date})");
+
+ let out_dir = Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
+ .join("src")
+ .join("helps")
+ .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("cargo-mling").unwrap();
+ build_comp_scripts("mling").unwrap();
+}