-
Notifications
You must be signed in to change notification settings - Fork 16
/
build.rs
220 lines (197 loc) · 6.4 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use serde::{Deserialize, Serialize};
use std::{
fs,
fs::{DirEntry, Metadata},
io::Write,
path::Path,
};
fn main() {
println!("cargo:rerun-if-changed=extras");
let folders = fs::read_dir("extras").unwrap();
if let Err(e) = fs::create_dir("src/extras") {
if e.kind() != std::io::ErrorKind::AlreadyExists {
println!("{e:?}");
}
}
copy_dir_all("extras/proyectos/assets", "assets/gen_assets").unwrap();
copy_dir_all("extras/comunidades/assets", "assets/gen_assets").unwrap();
// Generate src/extras/mod.rs
let mut out = fs::File::create("src/extras/mod.rs").unwrap();
write!(out, "#[rustfmt::skip]\nmod other_communities;\nmod rust_communities;\n#[rustfmt::skip]\nmod projects;\npub use other_communities::*;\npub use rust_communities::*;\npub use projects::*;\n").unwrap();
for folder in folders {
let folder = folder.unwrap();
let meta = folder.metadata().unwrap();
if !meta.is_dir() {
continue;
}
let mut path = std::env::current_dir().unwrap();
path.push(folder.path());
match folder.file_name().to_str().unwrap() {
"comunidades" => generate_community(&path),
"proyectos" => generate_projects(&path),
_ => {}
}
}
}
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> {
fs::create_dir_all(&dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}
fn generate_community(path: &Path) {
let folders = fs::read_dir(path).unwrap();
let mut communities = Vec::new();
for file in folders {
let file = file.unwrap();
let meta = file.metadata().unwrap();
if meta.is_dir() {
continue;
}
let file_path = file.path();
let toml_str = fs::read_to_string(&file_path).unwrap();
let toml_str = toml::from_str::<CommunityItem>(&toml_str).unwrap();
communities.push((file_path, toml_str));
}
let mut other_file = fs::File::create("src/extras/other_communities.rs").unwrap();
write!(
other_file,
"use crate::models::CommunityItem;\npub const OTHER_COMMUNITIES: &[CommunityItem] = &[\n"
)
.expect("No se pudo crear el archivo src/extras/other_communities.rs");
let mut rust_file = fs::File::create("src/extras/rust_communities.rs").unwrap();
write!(
rust_file,
"use crate::models::CommunityItem;\npub const RUST_COMMUNITIES: &[CommunityItem] = &[\n"
)
.expect("No se pudo crear el archivo src/extras/rust_communities.rs");
for (_p, community) in communities {
let mut output_file = if community.name.join("").to_lowercase().contains("rust") {
&rust_file
} else {
&other_file
};
let CommunityItem {
name,
description,
link,
icon,
brand_src,
brand_alt,
} = community;
let brand_src = brand_src.replace("./", "/gen_assets/");
write!(
output_file,
r#"
CommunityItem {{
name: &{name:?},
description: "{description}",
link: "{link}",
icon: "{icon}",
brand_src: "{brand_src}",
brand_alt: "{brand_alt}",
}},"#
)
.unwrap();
}
write!(other_file, "\n];").unwrap();
write!(rust_file, "\n];").unwrap();
}
fn iter_dir(path: &Path, mut callback: impl FnMut(DirEntry, Metadata)) {
let folders = fs::read_dir(path).unwrap();
for folder in folders {
let folder = folder.unwrap();
let meta = folder.metadata().unwrap();
callback(folder, meta);
}
}
fn generate_projects(path: &Path) {
let mut projects = Vec::new();
iter_dir(path, |folder, meta| {
if meta.is_file() {
return;
}
let category = folder.file_name();
let category = category.to_str().unwrap();
let category = category.to_string();
iter_dir(&folder.path(), |file, meta| {
if meta.is_dir() {
return;
}
let file_path = file.path();
if !file_path.extension().is_some_and(|e| e == "toml") {
let file_name = file.file_name();
let file_name = file_name.to_str().unwrap();
// Copy images or other files
fs::copy(&file_path, format!("assets/gen_assets/{file_name}")).unwrap();
return;
}
let toml_str = fs::read_to_string(&file_path).unwrap();
let toml_str = toml::from_str::<ProjectItem>(&toml_str).unwrap();
projects.push((category.clone(), file_path, toml_str));
});
});
let mut out = fs::File::create("src/extras/projects.rs").unwrap();
write!(
out,
"use crate::models::ProjectItem;\npub const COMUNITY_PROJECTS: &[ProjectItem] = &[\n"
)
.unwrap();
for (c, _p, t) in projects {
let ProjectItem {
name,
description,
link,
brand_src,
button_link,
button_text,
brand_as_letter,
button_bg_color,
} = t;
let brand_src = brand_src.replace("./", "/gen_assets/");
write!(
out,
r#"
ProjectItem {{
name: &{name:?},
category: "{c}",
description: "{description}",
link: "{link}",
brand_src: "{brand_src}",
button_link: "{button_link}",
button_text: "{button_text}",
brand_as_letter: {brand_as_letter},
button_bg_color: "{button_bg_color}",
}},"#
)
.unwrap();
}
write!(out, "\n];").unwrap();
}
#[derive(Serialize, Deserialize)]
struct CommunityItem {
pub name: Vec<String>,
pub description: String,
pub link: String,
pub icon: String,
pub brand_src: String,
pub brand_alt: String,
}
#[derive(Serialize, Deserialize)]
struct ProjectItem {
pub name: Vec<String>,
pub description: String,
pub link: String,
pub brand_src: String,
pub button_link: String,
pub button_text: String,
pub brand_as_letter: bool,
pub button_bg_color: String,
}