Skip to content

Commit 1cb18ea

Browse files
committed
basic external generation
1 parent f736bae commit 1cb18ea

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

build.rs

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
use std::io::Write;
2+
use std::{
3+
fs,
4+
fs::{DirEntry, Metadata},
5+
path::PathBuf,
6+
};
7+
8+
fn main() {
9+
println!("cargo:rerun-if-changed=extras");
10+
11+
let folders = fs::read_dir("extras").unwrap();
12+
if let Err(e) = fs::create_dir("src/extras") {
13+
if e.kind() != std::io::ErrorKind::AlreadyExists {
14+
println!("{e:?}");
15+
}
16+
}
17+
18+
// Generate src/extras/mod.rs
19+
let mut out = fs::File::create("src/extras/mod.rs").unwrap();
20+
write!(out, "#[rustfmt::skip]\nmod comunities;\n#[rustfmt::skip]\nmod projects;\npub use comunities::*;\npub use projects::*;").unwrap();
21+
22+
for folder in folders {
23+
let folder = folder.unwrap();
24+
let meta = folder.metadata().unwrap();
25+
if !meta.is_dir() {
26+
continue;
27+
}
28+
29+
let mut path = std::env::current_dir().unwrap();
30+
path.push(folder.path());
31+
32+
match folder.file_name().to_str().unwrap() {
33+
"comunidades" => generate_comunity(path),
34+
"proyectos" => generate_projects(path),
35+
_ => {}
36+
}
37+
}
38+
}
39+
40+
fn generate_comunity(path: PathBuf) {
41+
let folders = fs::read_dir(path.as_path()).unwrap();
42+
let mut comunities = Vec::new();
43+
44+
for file in folders {
45+
let file = file.unwrap();
46+
let meta = file.metadata().unwrap();
47+
if meta.is_dir() {
48+
continue;
49+
}
50+
let file_path = file.path();
51+
let toml_str = fs::read_to_string(&file_path).unwrap();
52+
comunities.push((file_path, toml_str));
53+
}
54+
let mut out = fs::File::create("src/extras/comunities.rs").unwrap();
55+
write!(out, "use crate::models::CommunityItem;\npub const OTHER_COMUNITIES: &[CommunityItem] = &[\n").unwrap();
56+
for (_p, t) in comunities {
57+
write!(out, r#"
58+
CommunityItem {{
59+
{t}
60+
}},"#).unwrap();
61+
}
62+
write!(out, "\n];").unwrap();
63+
}
64+
65+
fn iter_dir(path: PathBuf, mut callback: impl FnMut(DirEntry, Metadata)) {
66+
println!("Read Dir: {path:?}");
67+
let folders = fs::read_dir(path.as_path()).unwrap();
68+
for folder in folders {
69+
let folder = folder.unwrap();
70+
let meta = folder.metadata().unwrap();
71+
callback(folder, meta);
72+
}
73+
}
74+
75+
fn generate_projects(path: PathBuf) {
76+
let mut projects = Vec::new();
77+
iter_dir(path, |folder, meta| {
78+
if meta.is_file() {
79+
return;
80+
}
81+
let category = folder.file_name();
82+
let category = category.to_str().unwrap();
83+
println!("Category: {category}");
84+
85+
let category = category.to_string();
86+
iter_dir(folder.path(), |file, meta| {
87+
if meta.is_dir() {
88+
return;
89+
}
90+
let file_path = file.path();
91+
let toml_str = fs::read_to_string(&file_path).unwrap();
92+
projects.push((category.clone(), file_path, toml_str));
93+
});
94+
});
95+
96+
let mut out = fs::File::create("src/extras/projects.rs").unwrap();
97+
write!(out, "use crate::models::ProjectItem;\npub const COMUNITY_PROJECTS: &[ProjectItem] = &[\n").unwrap();
98+
for (_c, _p, t) in projects {
99+
// name: ,
100+
// description: ,
101+
// link: ,
102+
// brand_src: ,
103+
// button_link: ,
104+
// button_text: ,
105+
// brand_as_letter:
106+
// button_bg_color: ,
107+
write!(out, r#"
108+
ProjectItem {{
109+
{t}
110+
}},"#).unwrap();
111+
}
112+
write!(out, "\n];").unwrap();
113+
}

0 commit comments

Comments
 (0)