-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.rs
57 lines (47 loc) · 1.55 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
use convert_case::{Case, Casing};
use std::{
collections::HashMap,
env, fs,
io::{BufReader, Result},
path::PathBuf,
process::Command,
};
use which::which;
const LIB_SLUG: &str = include_str!("build/lib_slug.rs");
const SLUG: &str = include_str!("build/slug.rs");
const PACKAGE_MANAGERS: &[&str] = &["pnpm", "yarn", "npm"];
#[inline]
fn get_package_manager() -> PathBuf {
PACKAGE_MANAGERS
.into_iter()
.find_map(|&m| which(m).ok())
.unwrap()
}
fn main() -> Result<()> {
Command::new(get_package_manager())
.arg("install")
.status()?;
let file =
fs::File::open(env::current_dir()?.join("node_modules/feather-icons/dist/icons.json"))?;
let collection: HashMap<String, String> = serde_json::from_reader(BufReader::new(file))?;
let lib_content = [
LIB_SLUG.to_owned(),
collection
.into_iter()
.map(|(name, markup)| {
let markup = SLUG
.replace("__ComponentName", name.to_case(Case::Pascal).as_str())
.replace(
"__component_func",
format!("r#{}", name.to_case(Case::Snake)).as_str(),
)
.replace("__component_markup", markup.as_str());
format!("\n{}\n", markup)
})
.collect(),
]
.concat();
fs::create_dir_all("src").expect("could not generate src/lib.rs");
fs::write("src/lib.rs", lib_content.as_bytes()).expect("could not generate src/lib.rs");
Ok(())
}