support build-time theme selection via build.rs

This commit is contained in:
Peter Cai 2020-04-11 09:52:45 +08:00
parent 292b204262
commit 23d899dd0e
No known key found for this signature in database
GPG Key ID: 71F5FB4E4F3FD54F
3 changed files with 26 additions and 2 deletions

View File

@ -10,6 +10,10 @@ crate-type = ["cdylib", "rlib"]
[features] [features]
default = ["console_error_panic_hook", "wee_alloc"] default = ["console_error_panic_hook", "wee_alloc"]
[build-dependencies]
serde = "1.0"
serde_json = "1.0"
[dependencies] [dependencies]
cfg-if = "0.1.2" cfg-if = "0.1.2"
chrono = "0.4" chrono = "0.4"

19
build.rs Normal file
View 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();
}

View File

@ -11,8 +11,9 @@ use serde::Serialize;
use std::vec::Vec; use std::vec::Vec;
use web_sys::*; use web_sys::*;
// TODO: allow static configuration of which theme to use // Allows user-configurable theme at build-time
const THEME_DIR: Dir = include_dir!("theme/default"); // See build.rs
include!(concat!(env!("OUT_DIR"), "/load_theme.rs"));
pub fn build_routes(router: &mut Router) { pub fn build_routes(router: &mut Router) {
router.add_route("/static/", &serve_static); router.add_route("/static/", &serve_static);