From bb0cdffaeef54dc195ca9f874dea54d737b4b78f Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Mon, 29 Jul 2024 22:26:58 +0200 Subject: [PATCH] Adds auto versionning --- src/makepdf_functions.rs | 53 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/src/makepdf_functions.rs b/src/makepdf_functions.rs index 3b0b7a7..1f9de8c 100644 --- a/src/makepdf_functions.rs +++ b/src/makepdf_functions.rs @@ -5,6 +5,7 @@ use std::{ io::{Read, Write}, path::{Path, PathBuf}, process::Command, + str::from_utf8, sync::LazyLock, }; @@ -41,12 +42,19 @@ pub(super) fn make_file(args: &MakeConfig) -> BuilderParseResult { let mut of = File::create(&path).unwrap(); let mut contents = String::new(); let mut chapters = Vec::new(); + let mut first = true; // Warn about wrong keys clear_keys(args, &m); - - f.read_to_string(&mut contents).unwrap(); + f.read_to_string(&mut contents) + .expect("Couldn't read the file"); for line in contents.lines() { if let Some(c) = INPUT_REGEX.captures(line) { + // Check if it's the first inport, then adds the version number + if first { + first = false; + writeln!(of, "\\def\\buildversion{{ {} }}", get_build_version()) + .expect("Couldn't write version number"); + } // Check wether it should be added if add_checker(&c[1], &m, args) { if args.no_context { @@ -82,6 +90,47 @@ pub(super) fn make_file(args: &MakeConfig) -> BuilderParseResult { } } +/// Ask git for the current version +fn get_build_version() -> String { + let mut cmd = Command::new("git"); + cmd.args(["describe", "--tags"]); + + let tag = match cmd.output() { + Ok(v) => from_utf8(&v.stdout) + .expect("Couldn't parse the git tag result") + .to_owned(), + Err(e) => { + println!( + "[ERRR] An error has occured while trying to retrieve the Ravenous Version\n {:#?}", + e + ); + "Unknown".to_string() + } + }; + + let mut cmd = Command::new("git"); + cmd.args(["rev-parse", "--short", "HEAD"]); + + let hsh = match cmd.output() { + Ok(v) => from_utf8(&v.stdout) + .expect("Couldn't parse the git tag result") + .to_uppercase() + .to_owned(), + Err(e) => { + println!( + "[ERRR] An error has occured while trying to retrieve the Ravenous Version\n {:#?}", + e + ); + "Unknown".to_string() + } + }; + format!( + "Version: {} ({})", + tag.replace("\n", ""), + hsh.replace("\n", "") + ) +} + /// Clear the list of keys (for now only warn) fn clear_keys(args: &MakeConfig, dict_entries: &IndexMap) { for k in args.only.iter() {