Better file hashing and scripts organization
parent
c50785a27b
commit
bd4fb25a6a
|
@ -1,4 +1,4 @@
|
|||
import { debounce } from "./modules/common/helper.js";
|
||||
import { debounce } from "../common/helper.js";
|
||||
|
||||
let charFilter = "";
|
||||
let typeFilter = "";
|
|
@ -2,6 +2,6 @@ import {
|
|||
kindFilter,
|
||||
showOnlyTracked,
|
||||
track,
|
||||
} from "./modules/common/mat-kind-filter.js";
|
||||
} from "../common/mat-kind-filter.js";
|
||||
|
||||
Object.assign(window, { track });
|
|
@ -1,9 +1,3 @@
|
|||
import {
|
||||
kindFilter,
|
||||
showOnlyTracked,
|
||||
track,
|
||||
} from "./modules/common/mat-kind-filter.js";
|
||||
|
||||
document.addEventListener("DOMContentLoaded", (event) => {
|
||||
const recipes = document.querySelectorAll(".recipe");
|
||||
|
||||
|
@ -68,5 +62,3 @@ function calc_total_ingredients() {
|
|||
}
|
||||
matsList.appendChild(uiList);
|
||||
}
|
||||
|
||||
Object.assign(window, { track });
|
|
@ -0,0 +1,7 @@
|
|||
import {
|
||||
kindFilter,
|
||||
showOnlyTracked,
|
||||
track,
|
||||
} from "../common/mat-kind-filter.js";
|
||||
|
||||
Object.assign(window, { track });
|
|
@ -1,4 +1,3 @@
|
|||
unstable_features = true
|
||||
reorder_imports = true
|
||||
hard_tabs = true
|
||||
control_brace_style = "ClosingNextLine"
|
||||
|
|
10
src/bbs.rs
10
src/bbs.rs
|
@ -1,14 +1,13 @@
|
|||
use std::{collections::HashMap, sync::OnceLock};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use ability::Ability;
|
||||
use askama::Template;
|
||||
use blake3::Hash;
|
||||
use command::Command;
|
||||
use finisher::Finisher;
|
||||
use itertools::Itertools;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{RuntimeModule, create_file, create_hashes};
|
||||
use crate::{RuntimeModule, create_file};
|
||||
|
||||
mod ability;
|
||||
mod command;
|
||||
|
@ -18,7 +17,6 @@ mod melding;
|
|||
const ABILITIES_PATH: &str = "./input/bbs/abilities.json";
|
||||
const FINISHERS_PATH: &str = "./input/bbs/finish-commands.json";
|
||||
const COMMANDS_PATH: &str = "./input/bbs/commands.json";
|
||||
static JS_HASH: OnceLock<Hash> = OnceLock::new();
|
||||
|
||||
#[derive(Debug, Deserialize, PartialEq, Eq)]
|
||||
enum Character {
|
||||
|
@ -73,8 +71,4 @@ impl RuntimeModule for Module {
|
|||
|
||||
create_file("./out/bbs", "melding", melding_template).unwrap();
|
||||
}
|
||||
|
||||
fn get_js_hash() -> String {
|
||||
JS_HASH.get_or_init(|| create_hashes("bbs")).to_string()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use serde::Deserialize;
|
||||
|
||||
use super::{ability::Ability, Character};
|
||||
use super::{Character, ability::Ability};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct CommandRecipe {
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use itertools::Itertools;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
use std::path::PathBuf;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use crate::ddd::ability::AbilityType;
|
||||
use crate::{RuntimeModule, create_file, create_hashes};
|
||||
use crate::{RuntimeModule, create_file};
|
||||
use askama::Template;
|
||||
use blake3::Hash;
|
||||
use board::Board;
|
||||
|
||||
mod ability;
|
||||
|
@ -13,7 +11,6 @@ mod board_position;
|
|||
mod route;
|
||||
|
||||
const ABILITIES_PATH: &str = "./input/ddd/abilities";
|
||||
static JS_HASH: OnceLock<Hash> = OnceLock::new();
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "pages/ddd/boards.html")]
|
||||
|
@ -57,8 +54,4 @@ impl RuntimeModule for Module {
|
|||
|
||||
create_file("./out/ddd", "boards", boards_template).unwrap();
|
||||
}
|
||||
|
||||
fn get_js_hash() -> String {
|
||||
JS_HASH.get_or_init(|| create_hashes("ddd")).to_string()
|
||||
}
|
||||
}
|
||||
|
|
10
src/kh1.rs
10
src/kh1.rs
|
@ -1,18 +1,14 @@
|
|||
use std::sync::OnceLock;
|
||||
|
||||
use askama::Template;
|
||||
use blake3::Hash;
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::{
|
||||
RuntimeModule,
|
||||
common::{Game, enemy::Enemy, materials::MaterialDrops, synthesis::SynthesisData},
|
||||
create_file, create_hashes,
|
||||
create_file,
|
||||
};
|
||||
|
||||
const ENEMIES_PATH: &str = "./input/kh1/enemies";
|
||||
const SYNTHESIS_PATH: &str = "./input/kh1/synthesis.toml";
|
||||
static JS_HASH: OnceLock<Hash> = OnceLock::new();
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "pages/kh1/drops.html")]
|
||||
|
@ -59,8 +55,4 @@ impl RuntimeModule for Module {
|
|||
|
||||
create_file("./out/kh1", "synth", synth_template).unwrap();
|
||||
}
|
||||
|
||||
fn get_js_hash() -> String {
|
||||
JS_HASH.get_or_init(|| create_hashes("kh1")).to_string()
|
||||
}
|
||||
}
|
||||
|
|
10
src/kh2.rs
10
src/kh2.rs
|
@ -1,17 +1,13 @@
|
|||
use std::sync::OnceLock;
|
||||
|
||||
use askama::Template;
|
||||
use blake3::Hash;
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::{
|
||||
RuntimeModule,
|
||||
common::{Game, enemy::Enemy, materials::MaterialDrops},
|
||||
create_file, create_hashes,
|
||||
create_file,
|
||||
};
|
||||
|
||||
const ENEMIES_PATH: &str = "./input/kh2/enemies";
|
||||
static JS_HASH: OnceLock<Hash> = OnceLock::new();
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "pages/kh2/drops.html")]
|
||||
|
@ -44,8 +40,4 @@ impl RuntimeModule for Module {
|
|||
|
||||
create_file("./out/kh2", "drops", drops_template).unwrap();
|
||||
}
|
||||
|
||||
fn get_js_hash() -> String {
|
||||
JS_HASH.get_or_init(|| create_hashes("kh2")).to_string()
|
||||
}
|
||||
}
|
||||
|
|
10
src/kh3.rs
10
src/kh3.rs
|
@ -1,15 +1,11 @@
|
|||
use std::sync::OnceLock;
|
||||
|
||||
use askama::Template;
|
||||
use blake3::Hash;
|
||||
use food::Recipes;
|
||||
|
||||
use crate::{RuntimeModule, create_file, create_hashes};
|
||||
use crate::{RuntimeModule, create_file};
|
||||
|
||||
mod food;
|
||||
|
||||
const RECIPES_PATH: &str = "./input/kh3/recipes.toml";
|
||||
static JS_HASH: OnceLock<Hash> = OnceLock::new();
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "pages/kh3/food-sim.html")]
|
||||
|
@ -30,8 +26,4 @@ impl RuntimeModule for Module {
|
|||
|
||||
create_file("./out/kh3", "food-sim", food_template).unwrap();
|
||||
}
|
||||
|
||||
fn get_js_hash() -> String {
|
||||
JS_HASH.get_or_init(|| create_hashes("kh3")).to_string()
|
||||
}
|
||||
}
|
||||
|
|
55
src/main.rs
55
src/main.rs
|
@ -1,6 +1,6 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
use std::{fs, path::PathBuf};
|
||||
use std::{collections::HashMap, fs, path::PathBuf, sync::LazyLock};
|
||||
|
||||
use askama::Template;
|
||||
use blake3::{Hash, Hasher};
|
||||
|
@ -25,11 +25,43 @@ mod kh3;
|
|||
|
||||
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
pub const ASSETS_FOLDER_PATH: &str = "./public/assets";
|
||||
pub const SCRIPTS_FOLDER_PATH: &str = "./public/scripts";
|
||||
static FILE_HASHES: LazyLock<HashMap<String, Hash>> = LazyLock::new(|| {
|
||||
let mut map = HashMap::new();
|
||||
|
||||
fn parse_path(path: PathBuf, map: &mut HashMap<String, Hash>) {
|
||||
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()) {
|
||||
parse_path(path, map);
|
||||
continue;
|
||||
}
|
||||
|
||||
let dir = path.parent().unwrap().to_str().unwrap();
|
||||
let path = path.to_str().unwrap();
|
||||
let is_module = path.contains("/common");
|
||||
|
||||
let mut hasher = Hasher::new();
|
||||
hash_file(path.to_string().into(), &mut hasher);
|
||||
if !is_module {
|
||||
hash_files_in_dir(format!("{dir}/common").into(), &mut hasher);
|
||||
}
|
||||
|
||||
let hash = hasher.finalize();
|
||||
map.insert(path.to_string(), hash);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
parse_path(SCRIPTS_FOLDER_PATH.into(), &mut map);
|
||||
|
||||
map
|
||||
});
|
||||
|
||||
pub trait RuntimeModule {
|
||||
fn start_module();
|
||||
|
||||
fn get_js_hash() -> String;
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
|
@ -72,15 +104,14 @@ fn start_module<M: RuntimeModule>() {
|
|||
M::start_module();
|
||||
}
|
||||
|
||||
fn create_hashes(module: &str) -> Hash {
|
||||
let mut hasher = 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 find_hash(file: &str) -> String {
|
||||
let map = &*FILE_HASHES;
|
||||
let hash = map.get(&format!(".{file}"));
|
||||
if let Some(hash) = hash {
|
||||
return format!("{file}?hash={hash}");
|
||||
}
|
||||
|
||||
file.to_string()
|
||||
}
|
||||
|
||||
fn hash_files_in_dir(path: PathBuf, hasher: &mut Hasher) {
|
||||
|
|
|
@ -4,7 +4,10 @@
|
|||
|
||||
{% block head %}
|
||||
<style> {% include "components/bbs/style.css" %}</style>
|
||||
<script type="module" src="/public/scripts/bbs.js?v={{Module::get_js_hash()}}"></script>
|
||||
<script
|
||||
type="module"
|
||||
src="{{ crate::find_hash("/public/scripts/bbs/melding.js") }}"
|
||||
></script>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
|
|
@ -5,7 +5,10 @@
|
|||
|
||||
{% block head %}
|
||||
<style>{% include "components/kh2/style.css" %}</style>
|
||||
<script type="module" src="/public/scripts/kh1.js?v={{Module::get_js_hash()}}"></script>
|
||||
<script
|
||||
type="module"
|
||||
src="{{ crate::find_hash("/public/scripts/kh1/drops.js") }}"
|
||||
></script>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<style>{% include "components/kh1/style.css" %}</style>
|
||||
<script
|
||||
type="module"
|
||||
src="/public/scripts/kh1.js?v={{ Module::get_js_hash() }}"
|
||||
src="{{ crate::find_hash("/public/scripts/kh1/synth.js") }}"
|
||||
></script>
|
||||
{% endblock %}
|
||||
|
||||
|
|
|
@ -5,7 +5,10 @@
|
|||
|
||||
{% block head %}
|
||||
<style>{% include "components/kh2/style.css" %}</style>
|
||||
<script type="module" src="/public/scripts/kh2.js?v={{Module::get_js_hash()}}"></script>
|
||||
<script
|
||||
type="module"
|
||||
src="{{ crate::find_hash("/public/scripts/kh2/drops.js") }}"
|
||||
></script>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
|
|
@ -4,7 +4,10 @@
|
|||
|
||||
{% block head %}
|
||||
<style>{% include "components/kh3/style.css" %}</style>
|
||||
<script type="module" src="/public/scripts/kh3.js?v={{Module::get_js_hash()}}"></script>
|
||||
<script
|
||||
type="module"
|
||||
src="{{ crate::find_hash("/public/scripts/kh3/food-sim.js") }}"
|
||||
></script>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
|
Loading…
Reference in New Issue