1use std::str;
2
3use sha2::Digest;
4fn main() {
5 // generate sha256 files
6 // this avoids having to perform hashing at runtime
7 let files = &[
8 "static/css/rustdoc.css",
9 "static/css/noscript.css",
10 "static/css/normalize.css",
11 "static/js/main.js",
12 "static/js/search.js",
13 "static/js/settings.js",
14 "static/js/src-script.js",
15 "static/js/storage.js",
16 "static/js/scrape-examples.js",
17 "static/COPYRIGHT.txt",
18 "static/LICENSE-APACHE.txt",
19 "static/LICENSE-MIT.txt",
20 "static/images/rust-logo.svg",
21 "static/images/favicon.svg",
22 "static/images/favicon-32x32.png",
23 "static/fonts/FiraSans-Italic.woff2",
24 "static/fonts/FiraSans-Regular.woff2",
25 "static/fonts/FiraSans-Medium.woff2",
26 "static/fonts/FiraSans-MediumItalic.woff2",
27 "static/fonts/FiraMono-Regular.woff2",
28 "static/fonts/FiraMono-Medium.woff2",
29 "static/fonts/FiraSans-LICENSE.txt",
30 "static/fonts/SourceSerif4-Regular.ttf.woff2",
31 "static/fonts/SourceSerif4-Semibold.ttf.woff2",
32 "static/fonts/SourceSerif4-Bold.ttf.woff2",
33 "static/fonts/SourceSerif4-It.ttf.woff2",
34 "static/fonts/SourceSerif4-LICENSE.md",
35 "static/fonts/SourceCodePro-Regular.ttf.woff2",
36 "static/fonts/SourceCodePro-Semibold.ttf.woff2",
37 "static/fonts/SourceCodePro-It.ttf.woff2",
38 "static/fonts/SourceCodePro-LICENSE.txt",
39 "static/fonts/NanumBarunGothic.ttf.woff2",
40 "static/fonts/NanumBarunGothic-LICENSE.txt",
41 ];
42 let out_dir = std::env::var("OUT_DIR").expect("standard Cargo environment variable");
43 for path in files {
44 let inpath = format!("html/{path}");
45 println!("cargo::rerun-if-changed={inpath}");
46 let data_bytes = std::fs::read(&inpath).expect("static path exists");
47 let hash_bytes = sha2::Sha256::digest(&data_bytes);
48 let mut digest = format!("-{hash_bytes:x}");
49 digest.truncate(9);
50 let outpath = std::path::PathBuf::from(format!("{out_dir}/{path}.sha256"));
51 std::fs::create_dir_all(outpath.parent().expect("all file paths are in a directory"))
52 .expect("should be able to write to out_dir");
53 std::fs::write(&outpath, digest.as_bytes()).expect("write to out_dir");
54 let minified_path = std::path::PathBuf::from(format!("{out_dir}/{path}.min"));
55 if path.ends_with(".js") || path.ends_with(".css") {
56 let minified: String = if path.ends_with(".css") {
57 minifier::css::minify(str::from_utf8(&data_bytes).unwrap())
58 .unwrap()
59 .to_string()
60 .into()
61 } else {
62 minifier::js::minify(str::from_utf8(&data_bytes).unwrap()).to_string().into()
63 };
64 std::fs::write(&minified_path, minified.as_bytes()).expect("write to out_dir");
65 } else {
66 std::fs::copy(&inpath, &minified_path).unwrap();
67 }
68 }
69}
70

Provided by KDAB

Privacy Policy
Learn Rust with the experts
Find out more