/
build.rs
161 lines (129 loc) · 5.33 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
use std::{
env,
error::Error,
fs,
io::Write,
path::{Path, PathBuf},
};
use git2::{Repository, ResetType};
use minidom::Element;
fn convert(style: &str, path: &Path) -> Result<(), Box<dyn Error>> {
let lower = &style.to_lowercase();
let source = &path.join("optimized").join(lower);
let target = &PathBuf::from(".").join("src").join(lower);
if !target.exists() {
fs::create_dir(target)?;
}
let mut outline_mods: String = String::new();
outline_mods.push_str(&format!("mod {}_trait;\n", lower));
outline_mods.push_str(&format!("pub use {}_trait::{};\n\n", lower, style));
outline_mods.push_str(&format!("mod {}_trait_private;\n\n", lower));
for entry in fs::read_dir(&source)? {
if let Ok(entry) = entry {
if !&entry.metadata()?.is_file() {
continue;
}
let source_filename = &entry.file_name();
let file_name = if let Some(first) = source_filename
.to_string_lossy()
.split('.')
.collect::<Vec<&str>>()
.first()
{
first.replace('-', "_")
} else {
continue;
};
outline_mods.push_str(&format!("mod {};\n", file_name));
let file_path = format!("{}/{}.rs", target.to_string_lossy(), file_name);
let mut file = fs::File::create(file_path)?;
let mut file_content = String::from("use seed::{*, prelude::*};\n\n");
file_content.push_str(&format!(
"use super::{{{}, {}_trait_private::{}Private}};\n\n",
style, lower, style
));
let struct_name = file_name.split('_').collect::<Vec<&str>>().iter().fold(
"".to_string(),
|mut res, part| {
let mut c = part.chars();
let s = match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().chain(c).collect(),
};
res.push_str(&s);
res
},
);
outline_mods.push_str(&format!("pub use {}::{};\n\n", file_name, struct_name));
file_content.push_str(&format!("pub struct {};\n\n", struct_name));
file_content.push_str(&format!("impl {}Private for {} {{\n", style, struct_name));
file_content.push_str("fn base<T>(classes: impl ToClasses) -> Node<T> {\n");
file_content.push_str("svg![\n");
file_content.push_str("C![classes],\n");
file_content.push_str("attrs!(\n");
let contents = fs::read_to_string(source.join(source_filename))?;
let root: Element = contents.parse()?;
for attr in root.attrs() {
file_content.push_str(&format!("At::from(\"{}\") => \"{}\",\n", attr.0, attr.1));
}
file_content.push_str("),\n");
for child in root.children() {
file_content.push_str("path![\n");
file_content.push_str("attrs!(\n");
for attr in child.attrs() {
file_content
.push_str(&format!("At::from(\"{}\") => \"{}\",\n", attr.0, attr.1));
}
file_content.push_str("),\n");
file_content.push_str("],\n");
}
file_content.push_str("]\n");
file_content.push_str("}\n");
file_content.push_str("}\n\n");
file_content.push_str(&format!("impl {} for {} {{}}\n", style, struct_name));
if let Err(error) = write!(file, "{}", file_content) {
return Err(Box::new(error));
}
} else {
panic!();
}
}
let mut outline_mods_file = fs::File::create(target.join("mod.rs"))?;
if let Err(error) = writeln!(outline_mods_file, "{}", outline_mods) {
Err(Box::new(error))
} else {
Ok(())
}
}
fn main() -> Result<(), Box<dyn Error>> {
if env::var_os("DO_ICON_GENERATION").is_none() {
return Ok(());
}
if let Some(out_dir) = env::var_os("OUT_DIR") {
let path = PathBuf::from(out_dir).join("heroicons");
if !path.exists() {
let url = "https://github.com/tailwindlabs/heroicons";
Repository::clone(url, &path)?;
} else {
let repo = Repository::open(&path)?;
let branch = "master";
repo.reset(&repo.revparse_single("HEAD")?, ResetType::Hard, None)?;
repo.find_remote("origin")?.fetch(&[branch], None, None)?;
let fetch_head = repo.find_reference("FETCH_HEAD")?;
let fetch_commit = repo.reference_to_annotated_commit(&fetch_head)?;
let analysis = repo.merge_analysis(&[&fetch_commit])?;
if analysis.0.is_fast_forward() {
let refname = format!("refs/heads/{}", branch);
let mut reference = repo.find_reference(&refname)?;
reference.set_target(fetch_commit.id(), "Fast-Forward")?;
repo.set_head(&refname)?;
repo.checkout_head(Some(git2::build::CheckoutBuilder::default().force()))?;
}
}
convert("Outline", &path)?;
convert("Solid", &path)?;
Ok(())
} else {
panic!()
}
}