Further optimize config generation

This commit is contained in:
Leonard Steppy 2025-02-14 03:42:22 +01:00
parent da9813a2cf
commit 91875dd607
8 changed files with 28 additions and 36 deletions

5
.gitignore vendored
View File

@ -1,3 +1,2 @@
Cargo.lock
/target
/dist
**/Cargo.lock
**/target

View File

@ -5,8 +5,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
default-run = "jana_sessions_webpage"
[dependencies]
chrono = { version = "0.4", features = ["serde"] }
leptos = { version = "0.7", features = ["csr"] }

View File

@ -6,4 +6,4 @@ public_url = "."
[[hooks]]
stage = "post_build"
command = "cargo"
command_arguments = ["run", "--bin", "create_static_configs"]
command_arguments = ["run", "--manifest-path", "config/Cargo.toml"]

View File

@ -5,3 +5,4 @@ edition = "2021"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

View File

@ -3,12 +3,4 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionConfig {
pub motd: String,
}
impl Default for SessionConfig {
fn default() -> Self {
Self {
motd: "Proben Dienstags um 18:30 Uhr und Sonntags um 10:00 Uhr".to_string(),
}
}
}

24
config/src/main.rs Normal file
View File

@ -0,0 +1,24 @@
use config::SessionConfig;
use serde::Serialize;
use std::env;
use std::fs::File;
use std::path::Path;
fn main() {
let out_dir = env::var_os("TRUNK_STAGING_DIR").unwrap_or("target".into());
create_config("session_config", &out_dir, &SessionConfig {
motd: "Proben Dienstags um 18:30 Uhr und Sonntags um 10:00 Uhr".to_string(),
})
}
fn create_config<T, P>(name: &str, out_dir: P, default_config: &T)
where
T: Serialize,
P: AsRef<Path>,
{
//TODO return, if config already exists and is up to date
let out_path = out_dir.as_ref().join(format!("{name}.json"));
let out_file = File::create(&out_path).unwrap();
serde_json::to_writer_pretty(out_file, default_config).unwrap();
}

View File

@ -7,7 +7,6 @@
color:red;
}
</style>
<link data-trunk rel="rust" data-bin="jana_sessions_webpage" />
</head>
<body></body>
</html>

View File

@ -1,21 +0,0 @@
use config::SessionConfig;
use serde::Serialize;
use std::env;
use std::fs::File;
use std::path::Path;
fn main() {
let out_dir = env::var_os("TRUNK_STAGING_DIR").expect("Not called during trunk build lifecycle");
create_default_config::<SessionConfig, _>("session_config", &out_dir);
}
fn create_default_config<T, P>(name: &str, out_dir: P)
where
T: Default + Serialize,
P: AsRef<Path>,
{
let out_path = out_dir.as_ref().join(format!("{name}.json"));
let out_file = File::create(&out_path).unwrap();
let default_config = T::default();
serde_json::to_writer_pretty(out_file, &default_config).unwrap();
}