khguide/src/main.rs

110 lines
2.2 KiB
Rust
Raw Normal View History

#![allow(dead_code)]
use std::{fs, path::PathBuf};
use askama::Template;
use blake3::{Hash, Hasher};
use tracing_subscriber::EnvFilter;
mod common;
#[cfg(feature = "bbs")]
mod bbs;
#[cfg(feature = "ddd")]
mod ddd;
#[cfg(feature = "kh1")]
mod kh1;
2025-02-08 13:35:55 +02:00
#[cfg(feature = "kh2")]
mod kh2;
#[cfg(feature = "kh3")]
mod kh3;
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub trait RuntimeModule {
fn start_module();
fn get_js_hash() -> String;
}
#[derive(Template)]
#[template(path = "pages/index.html")]
struct IndexTemplate {}
fn main() {
// Initialize tracing
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.event_format(tracing_subscriber::fmt::format().with_source_location(true))
.init();
tracing::info!("Generating the main menu template");
let template = IndexTemplate {};
std::fs::write("./out/index.html", template.render().unwrap()).unwrap();
2025-02-10 00:47:36 +02:00
std::process::Command::new("cp")
.args(["-r", "./assets", "./out"])
.output()
.unwrap();
#[cfg(feature = "bbs")]
start_module::<bbs::Module>();
#[cfg(feature = "ddd")]
start_module::<ddd::Module>();
#[cfg(feature = "kh1")]
start_module::<kh1::Module>();
2025-02-08 13:35:55 +02:00
#[cfg(feature = "kh2")]
start_module::<kh2::Module>();
2025-02-08 13:35:55 +02:00
#[cfg(feature = "kh3")]
start_module::<kh3::Module>();
}
fn start_module<M: RuntimeModule>() {
M::start_module();
}
fn create_hashes(module: &str) -> Hash {
let mut hasher = blake3::Hasher::new();
hash_file(format!("./public/scripts/{module}.js").into(), &mut hasher);
hash_files_in_dir("./public/scripts/modules/common".into(), &mut hasher);
hash_files_in_dir(
format!("./public/scripts/modules/{module}").into(),
&mut hasher,
);
hasher.finalize()
}
fn hash_files_in_dir(path: PathBuf, hasher: &mut Hasher) {
if let Ok(paths) = fs::read_dir(path) {
for path in paths.flatten() {
let path = path.path();
if path.metadata().is_ok_and(|p| p.is_dir()) {
hash_files_in_dir(path, hasher);
continue;
}
hash_file(path, hasher);
}
}
}
fn hash_file(path: PathBuf, hasher: &mut Hasher) {
if let Ok(file) = fs::read(path) {
hasher.update(&file);
}
}
fn create_file(dir: &str, file: &str, buf: String) -> std::io::Result<()> {
std::fs::create_dir_all(dir)?;
std::fs::write(format!("{}/{}.html", dir, file), buf)?;
Ok(())
}