refactor: Move definition writing into separate function

This commit is contained in:
2025-10-17 03:12:40 +02:00
parent 5801421378
commit b557afe2fc
2 changed files with 19 additions and 8 deletions

View File

@@ -744,6 +744,7 @@ device_manager:schedule("0 0 20 * * *", function()
bedroom_air_filter:set_on(false)
end)
---@type Config
return {
fulfillment = {
openid_url = "https://login.huizinga.dev/api/oidc",

View File

@@ -6,23 +6,33 @@ use tracing::{info, warn};
extern crate automation_devices;
fn main() -> std::io::Result<()> {
tracing_subscriber::fmt::init();
fn write_definitions(filename: &str, definitions: &str) -> std::io::Result<()> {
let definitions_directory =
std::path::Path::new(std::env!("CARGO_MANIFEST_DIR")).join("definitions");
fs::create_dir_all(&definitions_directory)?;
let mut file = File::create(definitions_directory.join(filename))?;
file.write_all(b"-- DO NOT MODIFY, FILE IS AUTOMATICALLY GENERATED\n")?;
file.write_all(definitions.as_bytes())?;
// Make sure we have a trailing new line
if !definitions.ends_with("\n") {
file.write_all(b"\n")?;
}
Ok(())
}
fn main() -> std::io::Result<()> {
tracing_subscriber::fmt::init();
for module in inventory::iter::<Module> {
if let Some(definitions) = module.definitions() {
info!(name = module.get_name(), "Generating definitions");
let filename = format!("{}.lua", module.get_name());
let mut file = File::create(definitions_directory.join(filename))?;
file.write_all(b"-- DO NOT MODIFY, FILE IS AUTOMATICALLY GENERATED\n")?;
file.write_all(definitions.as_bytes())?;
file.write_all(b"\n")?;
write_definitions(&filename, &definitions)?;
} else {
warn!(name = module.get_name(), "No definitions");
}