-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
141 lines (132 loc) · 4.93 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use core::result::Result;
use std::{
error::Error,
fs::{self, File},
io::Write,
path::{Path, PathBuf},
};
use flate2::{write::GzEncoder, Compression};
fn walk_assets(path: impl AsRef<Path>) -> Result<Vec<PathBuf>, std::io::Error> {
let mut files: Vec<PathBuf> = Vec::new();
if let Ok(entries) = fs::read_dir(path) {
for entry in entries {
if let Ok(entry) = entry {
let file_type = entry.file_type()?;
if file_type.is_dir() {
let mut other_files = walk_assets(entry.path())?;
files.append(&mut other_files);
} else if file_type.is_file() {
let path = entry.path();
let ext = path.extension();
if ext.is_none() {
continue;
}
let ext = ext.unwrap().to_os_string().into_string().unwrap();
if ext.find("gz").is_some() {
continue;
}
files.push(entry.path());
// files.push(entry.file_name().to_os_string().into_string().unwrap());
}
}
}
}
Ok(files)
}
fn gz_files(raw_asset_files: Vec<PathBuf>) -> Result<Vec<PathBuf>, std::io::Error> {
let mut gz_files: Vec<PathBuf> = Vec::new();
for asset_file in raw_asset_files.iter() {
let cache: Vec<u8> = Vec::with_capacity(65535);
let mut e = GzEncoder::new(cache, Compression::default());
let b = fs::read(asset_file)?;
e.write_all(b.as_slice())?;
let compressed_bytes = e.finish()?;
let mut extension = asset_file
.extension()
.unwrap()
.to_os_string()
.into_string()
.unwrap();
extension.push_str(".gz");
let gz_file = asset_file.with_extension(extension.as_str());
fs::write(gz_file.as_path(), compressed_bytes.as_slice())?;
gz_files.push(gz_file);
}
Ok(gz_files)
}
fn get_content_type(filename: String) -> String {
if filename.rfind(".css").is_some() {
String::from("text/css")
} else if filename.rfind(".js").is_some() {
String::from("text/javascript")
} else if filename.rfind(".html").is_some() {
String::from("text/html; charset=utf-8")
} else if filename.rfind(".wasm").is_some() {
String::from("application/wasm")
} else if filename.rfind(".ico").is_some() {
String::from("image/x-icon")
} else {
String::new()
}
}
fn main() -> Result<(), Box<dyn Error>> {
println!("cargo:rerun-if-changed=build.rs,resources/assets/index.html");
// embed all static resource asset files
let asset_root = Path::new("src").join("resources").join("assets");
let asset_root = format!("{}/", asset_root.display());
let asset_root = asset_root.as_str();
let all_static_asset_files = walk_assets(asset_root)?;
let gz_files = gz_files(all_static_asset_files)?;
let mut file_list: Vec<String> = Vec::new();
let mut service_asset_file = File::create(Path::new("src").join("web").join("asset.txt"))?;
writeln!(&mut service_asset_file, r##"["##,)?;
for f in gz_files.iter() {
let file_name = format!("{}", f.display())
.replace(asset_root, "")
.replace(".gz", "")
.replace("\\", std::path::MAIN_SEPARATOR_STR);
file_list.push(file_name);
writeln!(
&mut service_asset_file,
r##"(include_bytes!(r#"{file_path}"#), "{mime}"),"##,
file_path = format!("{}", f.display())
.replace("\\", std::path::MAIN_SEPARATOR_STR)
.replace("src", ".."),
mime = get_content_type(format!("{}", f.display())),
)?;
}
writeln!(&mut service_asset_file, r##"]"##,)?;
let mut service_asset_file = File::create(Path::new("src").join("web").join("asset.rs"))?;
writeln!(
&mut service_asset_file,
r##"use std::collections::HashMap;"##,
)?;
writeln!(&mut service_asset_file, r##""##,)?;
writeln!(&mut service_asset_file, r##"use std::sync::LazyLock;"##,)?;
writeln!(&mut service_asset_file, r##""##,)?;
// use std::cell::LazyCell;
writeln!(
&mut service_asset_file,
r##"pub(crate) static ASSETS_MAP: LazyLock<HashMap<&str, usize>> = LazyLock::new(|| {{"##,
)?;
writeln!(&mut service_asset_file, r##"HashMap::from(["##,)?;
let mut i = 0u8;
for f in file_list.iter() {
if f.eq("index.html") {
writeln!(
&mut service_asset_file,
r##"("/", {counter}),"##,
counter = i,
)?;
}
writeln!(
&mut service_asset_file,
r##"(r"/{name}", {counter}),"##,
name = f.replace("\\", "/"),
counter = i,
)?;
i = i + 1;
}
writeln!(&mut service_asset_file, r##"])}});"##,)?;
Ok(())
}