-
Notifications
You must be signed in to change notification settings - Fork 38
/
build.rs
41 lines (37 loc) · 1.54 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use std::{
env,
fs::{self, File},
io::{self, BufWriter, Write},
};
// Take all files under `themes` and turn them into a file that contains a hashmap with their
// contents by name. This is pulled in theme.rs to construct themes.
fn build_themes(out_dir: &str) -> io::Result<()> {
let output_path = format!("{out_dir}/themes.rs");
let mut output_file = BufWriter::new(File::create(output_path)?);
output_file.write_all(b"use std::collections::BTreeMap as Map;\n")?;
output_file.write_all(b"use once_cell::sync::Lazy;\n")?;
output_file.write_all(b"static THEMES: Lazy<Map<&'static str, &'static [u8]>> = Lazy::new(|| Map::from([\n")?;
let mut paths = fs::read_dir("themes")?.collect::<io::Result<Vec<_>>>()?;
paths.sort_by_key(|e| e.path());
for theme_file in paths {
let metadata = theme_file.metadata()?;
if !metadata.is_file() {
panic!("found non file in themes directory");
}
let path = theme_file.path();
let contents = fs::read(&path)?;
let file_name = path.file_name().unwrap().to_string_lossy();
let theme_name = file_name.split_once('.').unwrap().0;
// TODO this wastes a bit of space
output_file.write_all(format!("(\"{theme_name}\", {contents:?}.as_slice()),\n").as_bytes())?;
}
output_file.write_all(b"]));\n")?;
// Rebuild if anything changes.
println!("cargo:rerun-if-changed=themes");
Ok(())
}
fn main() -> io::Result<()> {
let out_dir = env::var("OUT_DIR").unwrap();
build_themes(&out_dir)?;
Ok(())
}