Adds auto versionning

This commit is contained in:
Dandaedre 2024-07-29 22:26:58 +02:00
parent 82b9515a8e
commit bb0cdffaee
1 changed files with 51 additions and 2 deletions

View File

@ -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<String, String>) {
for k in args.only.iter() {