support build-time theme selection via build.rs
This commit is contained in:
parent
292b204262
commit
23d899dd0e
|
@ -10,6 +10,10 @@ crate-type = ["cdylib", "rlib"]
|
|||
[features]
|
||||
default = ["console_error_panic_hook", "wee_alloc"]
|
||||
|
||||
[build-dependencies]
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
|
||||
[dependencies]
|
||||
cfg-if = "0.1.2"
|
||||
chrono = "0.4"
|
||||
|
|
19
build.rs
Normal file
19
build.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
extern crate serde_json;
|
||||
|
||||
use std::io::prelude::*;
|
||||
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=config.json");
|
||||
// Load theme name from config.json and output code to load the theme via include_dir!
|
||||
let config: serde_json::Value =
|
||||
serde_json::from_str(&std::fs::read_to_string("./config.json").unwrap()).unwrap();
|
||||
let theme_name = match config.get("theme") {
|
||||
Some(name) => name,
|
||||
None => panic!("Please define `theme` in `config.json`")
|
||||
};
|
||||
let theme_load_code = format!("const THEME_DIR: Dir = include_dir!(\"theme/{}\");", theme_name.as_str().unwrap());
|
||||
let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
|
||||
let mut out_file = std::fs::File::create(out_path.join("load_theme.rs")).unwrap();
|
||||
out_file.write_all(theme_load_code.as_bytes()).unwrap();
|
||||
out_file.sync_data().unwrap();
|
||||
}
|
|
@ -11,8 +11,9 @@ use serde::Serialize;
|
|||
use std::vec::Vec;
|
||||
use web_sys::*;
|
||||
|
||||
// TODO: allow static configuration of which theme to use
|
||||
const THEME_DIR: Dir = include_dir!("theme/default");
|
||||
// Allows user-configurable theme at build-time
|
||||
// See build.rs
|
||||
include!(concat!(env!("OUT_DIR"), "/load_theme.rs"));
|
||||
|
||||
pub fn build_routes(router: &mut Router) {
|
||||
router.add_route("/static/", &serve_static);
|
||||
|
|
Loading…
Reference in a new issue