Add functionality to overwrite xml metadata with env var

This commit is contained in:
Wataru Otsubo 2025-04-14 07:10:54 +00:00
parent 9444431a61
commit fafd262230
4 changed files with 94 additions and 41 deletions

View file

@ -33,7 +33,7 @@ cargo build --features=bin --bins --release
`target/release`に入ってるバイナリファイルを実行するか、`cargo run -- <XML> <OUT>`で実行できます。 `target/release`に入ってるバイナリファイルを実行するか、`cargo run -- <XML> <OUT>`で実行できます。
詳しくは`--help`を見てください。 詳しくは(`-h`ではなく)`--help`を見てください。
### ライブラリクレートの使い方 ### ライブラリクレートの使い方

View file

@ -36,7 +36,7 @@ to build.
Execute the binary generated in `target/release/`, or run with `cargo run -- <XML> <OUT>`. Execute the binary generated in `target/release/`, or run with `cargo run -- <XML> <OUT>`.
See the `--help` for more information. See the `--help`(not `-h`) for more information.
### Library crate usage ### Library crate usage

120
src/io.rs
View file

@ -1,11 +1,15 @@
//! File IO for generated codes. //! File IO for generated codes.
use std::{collections::HashMap, fs, io, path, process}; use std::{collections::HashMap, env, ffi::OsString, fs, io, path, process};
use thiserror::Error; use thiserror::Error;
use crate::types::XmlGitInfo; use crate::types::XmlGitInfo;
const ENVVAR_XML_GIT_DESCRIBE: &str = "XML_GIT_DESCRIBE";
const ENVVAR_XML_GIT_TIMESTAMP: &str = "XML_GIT_TIMESTAMP";
const ENVVAR_XML_GIT_SHA: &str = "XML_GIT_SHA";
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum XmlGitInfoError { pub enum XmlGitInfoError {
#[error("io error while getting xml git info: {msg} {source}")] #[error("io error while getting xml git info: {msg} {source}")]
@ -16,6 +20,8 @@ pub enum XmlGitInfoError {
}, },
#[error("git failed: {msg}: {stderr}")] #[error("git failed: {msg}: {stderr}")]
CommandFailed { msg: &'static str, stderr: String }, CommandFailed { msg: &'static str, stderr: String },
#[error("env var {var} is not unicode: {}", s.to_string_lossy())]
NotUnicodeEnvVar { var: &'static str, s: OsString },
} }
impl XmlGitInfoError { impl XmlGitInfoError {
@ -25,44 +31,84 @@ impl XmlGitInfoError {
} }
pub(crate) fn get_xml_gitinfo(xml_path: &path::Path) -> Result<XmlGitInfo, XmlGitInfoError> { pub(crate) fn get_xml_gitinfo(xml_path: &path::Path) -> Result<XmlGitInfo, XmlGitInfoError> {
let describe = process::Command::new("git") let describe = match env::var(ENVVAR_XML_GIT_DESCRIBE) {
.args(["describe", "--always", "--dirty"]) Ok(describe) => describe,
.current_dir(xml_path.parent().unwrap()) Err(env::VarError::NotUnicode(s)) => {
.output() return Err(XmlGitInfoError::NotUnicodeEnvVar {
.map_err(|e| XmlGitInfoError::io_error(e, "git describe"))?; var: ENVVAR_XML_GIT_DESCRIBE,
let describe = if describe.status.success() { s,
String::from_utf8_lossy(&describe.stdout).trim().to_owned() });
} else { }
return Err(XmlGitInfoError::CommandFailed { Err(env::VarError::NotPresent) => {
msg: "git describe", let out_describe = process::Command::new("git")
stderr: String::from_utf8_lossy(&describe.stderr).into_owned(), .args(["describe", "--always", "--dirty"])
}); .current_dir(xml_path.parent().unwrap())
.output()
.map_err(|e| XmlGitInfoError::io_error(e, "git describe"))?;
let describe = if out_describe.status.success() {
String::from_utf8_lossy(&out_describe.stdout)
.trim()
.to_owned()
} else {
return Err(XmlGitInfoError::CommandFailed {
msg: "git describe",
stderr: String::from_utf8_lossy(&out_describe.stderr).into_owned(),
});
};
describe
}
}; };
let timestamp = process::Command::new("git") let timestamp = match env::var(ENVVAR_XML_GIT_TIMESTAMP) {
.args(["log", "-1", "--pretty=format:'%cI'"]) Ok(timestamp) => timestamp,
.current_dir(xml_path.parent().unwrap()) Err(env::VarError::NotUnicode(s)) => {
.output() return Err(XmlGitInfoError::NotUnicodeEnvVar {
.map_err(|e| XmlGitInfoError::io_error(e, "git log (timestamp)"))?; var: ENVVAR_XML_GIT_TIMESTAMP,
let timestamp = if timestamp.status.success() { s,
String::from_utf8_lossy(&timestamp.stdout).trim().to_owned() });
} else { }
return Err(XmlGitInfoError::CommandFailed { Err(env::VarError::NotPresent) => {
msg: "git log (timestamp)", let timestamp_out = process::Command::new("git")
stderr: String::from_utf8_lossy(&timestamp.stderr).into_owned(), .args(["log", "-1", "--pretty=format:'%cI'"])
}); .current_dir(xml_path.parent().unwrap())
.output()
.map_err(|e| XmlGitInfoError::io_error(e, "git log (timestamp)"))?;
let timestamp = if timestamp_out.status.success() {
String::from_utf8_lossy(&timestamp_out.stdout)
.trim()
.to_owned()
} else {
return Err(XmlGitInfoError::CommandFailed {
msg: "git log (timestamp)",
stderr: String::from_utf8_lossy(&timestamp_out.stderr).into_owned(),
});
};
timestamp
}
}; };
let sha = process::Command::new("git") let git_sha = match env::var(ENVVAR_XML_GIT_SHA) {
.args(["rev-parse", "HEAD"]) Ok(sha) => sha,
.current_dir(xml_path.parent().unwrap()) Err(env::VarError::NotUnicode(s)) => {
.output() return Err(XmlGitInfoError::NotUnicodeEnvVar {
.map_err(|e| XmlGitInfoError::io_error(e, "git rev-parse (sha)"))?; var: ENVVAR_XML_GIT_SHA,
let git_sha = if sha.status.success() { s,
String::from_utf8_lossy(&sha.stdout).trim().to_owned() });
} else { }
return Err(XmlGitInfoError::CommandFailed { Err(env::VarError::NotPresent) => {
msg: "git rev-parse (sha)", let sha = process::Command::new("git")
stderr: String::from_utf8_lossy(&sha.stderr).into_owned(), .args(["rev-parse", "HEAD"])
}); .current_dir(xml_path.parent().unwrap())
.output()
.map_err(|e| XmlGitInfoError::io_error(e, "git rev-parse (sha)"))?;
let git_sha = if sha.status.success() {
String::from_utf8_lossy(&sha.stdout).trim().to_owned()
} else {
return Err(XmlGitInfoError::CommandFailed {
msg: "git rev-parse (sha)",
stderr: String::from_utf8_lossy(&sha.stderr).into_owned(),
});
};
git_sha
}
}; };
Ok(XmlGitInfo { Ok(XmlGitInfo {
describe, describe,

View file

@ -3,11 +3,16 @@ use std::path;
use anyhow::Result; use anyhow::Result;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
/// Generate register interface from register map xml. /// Validate the register map xml and generate register interface from register map xml.
/// See `help` with each commands to see detailed explanations.
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about)] #[command(version, about, long_about)]
struct Args { struct Args {
/// Input XML register map. /// Input XML register map.
///
/// The path must be a valid git repository to get git metadata.
/// You can also force these metadata with environmental variables `XML_GIT_DESCRIBE`,
/// `XML_GIT_TIMESTAMP` and `XML_GIT_SHA`.
xml: path::PathBuf, xml: path::PathBuf,
#[command(subcommand)] #[command(subcommand)]
command: Commands, command: Commands,
@ -23,6 +28,8 @@ enum Commands {
out: path::PathBuf, out: path::PathBuf,
}, },
/// Generate flattened register map in CSV. /// Generate flattened register map in CSV.
///
/// Also run validation before generation.
#[cfg(feature = "flatmap")] #[cfg(feature = "flatmap")]
Flatmap { Flatmap {
/// Flattened csv out path. /// Flattened csv out path.