|
1 | 1 | use anyhow::{Context as _, Error, Result}; |
2 | | -use font_awesome_as_a_crate as f_a; |
3 | | -use std::{ |
4 | | - env, |
5 | | - path::{Path, PathBuf}, |
6 | | -}; |
| 2 | +use std::{env, path::Path}; |
7 | 3 |
|
8 | 4 | mod tracked { |
9 | 5 | use once_cell::sync::Lazy; |
@@ -84,194 +80,11 @@ fn main() -> Result<()> { |
84 | 80 | write_known_targets(out_dir)?; |
85 | 81 | compile_syntax(out_dir).context("could not compile syntax files")?; |
86 | 82 |
|
87 | | - println!("cargo::rustc-check-cfg=cfg(icons_out_dir)"); |
88 | | - println!("cargo:rustc-cfg=icons_out_dir"); |
89 | | - |
90 | | - let out_css = "static/icons.css"; |
91 | | - let out_css = if std::env::var("RUN_IN_DOCKER").is_ok() { |
92 | | - let _ = std::fs::create_dir_all("/srv/docsrs/static"); |
93 | | - Path::new("/srv/docsrs/").join(out_css) |
94 | | - } else { |
95 | | - let package_dir = env::var("CARGO_MANIFEST_DIR").context("missing CARGO_MANIFEST_DIR")?; |
96 | | - Path::new(&package_dir).join(out_css) |
97 | | - }; |
98 | | - generate_css_icons(out_css, out_dir)?; |
99 | | - |
100 | 83 | // trigger recompilation when a new migration is added |
101 | 84 | println!("cargo:rerun-if-changed=migrations"); |
102 | 85 | Ok(()) |
103 | 86 | } |
104 | 87 |
|
105 | | -fn capitalize(s: &str) -> String { |
106 | | - let mut c = s.chars(); |
107 | | - match c.next() { |
108 | | - None => String::new(), |
109 | | - Some(f) => f.to_uppercase().chain(c).collect(), |
110 | | - } |
111 | | -} |
112 | | - |
113 | | -fn render_icon( |
114 | | - icon_name: &str, |
115 | | - icon_str: &str, |
116 | | - type_name: String, |
117 | | - code_output: &mut String, |
118 | | - css_output: &mut String, |
119 | | - icon_kind: &str, |
120 | | -) { |
121 | | - let css_class = format!("f-a_{icon_name}_{icon_kind}"); |
122 | | - css_output.push_str(&format!( |
123 | | - "\ |
124 | | -.{css_class} {{ |
125 | | - --svg_{icon_name}_{icon_kind}: url('data:image/svg+xml,{icon_str}'); |
126 | | - -webkit-mask: var(--svg_{icon_name}_{icon_kind}) no-repeat center; |
127 | | - mask: var(--svg_{icon_name}_{icon_kind}) no-repeat center; |
128 | | -}} |
129 | | -", |
130 | | - )); |
131 | | - let type_name = format!("{type_name}{}", capitalize(icon_kind)); |
132 | | - code_output.push_str(&format!( |
133 | | - r#"#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
134 | | -pub struct {type_name}; |
135 | | -impl {type_name} {{ |
136 | | - pub fn render(&self, fw: bool, spin: bool, extra: &str) -> rinja::filters::Safe<String> {{ |
137 | | - render({css_class:?}, fw, spin, extra) |
138 | | - }} |
139 | | -}} |
140 | | -"#, |
141 | | - )); |
142 | | -} |
143 | | - |
144 | | -fn generate_css_icons(css_path: PathBuf, out_dir: &Path) -> Result<()> { |
145 | | - let mut code_output = r#"pub(crate) mod icons { |
146 | | - fn render( |
147 | | - css_class: &str, |
148 | | - fw: bool, |
149 | | - spin: bool, |
150 | | - extra: &str, |
151 | | - ) -> rinja::filters::Safe<String> { |
152 | | - let mut classes = vec!["fa-svg"]; |
153 | | - if fw { |
154 | | - classes.push("fa-svg-fw"); |
155 | | - } |
156 | | - if spin { |
157 | | - classes.push("fa-svg-spin"); |
158 | | - } |
159 | | - if !extra.is_empty() { |
160 | | - classes.push(extra); |
161 | | - } |
162 | | - let icon = format!( |
163 | | - "<span class=\"{css_class} {class}\" aria-hidden=\"true\"></span>", |
164 | | - class = classes.join(" "), |
165 | | - ); |
166 | | -
|
167 | | - rinja::filters::Safe(icon) |
168 | | - }"# |
169 | | - .to_string(); |
170 | | - let mut css_output = r#".svg-clipboard { |
171 | | - /* This icon is copied from crates.io */ |
172 | | - --svg-clipboard: url('data:image/svg+xml,<svg width="24" height="25" viewBox="0 0 24 25" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-label="Copy to clipboard"><path d="M18 20h2v3c0 1-1 2-2 2H2c-.998 0-2-1-2-2V5c0-.911.755-1.667 1.667-1.667h5A3.323 3.323 0 0110 0a3.323 3.323 0 013.333 3.333h5C19.245 3.333 20 4.09 20 5v8.333h-2V9H2v14h16v-3zM3 7h14c0-.911-.793-1.667-1.75-1.667H13.5c-.957 0-1.75-.755-1.75-1.666C11.75 2.755 10.957 2 10 2s-1.75.755-1.75 1.667c0 .911-.793 1.666-1.75 1.666H4.75C3.793 5.333 3 6.09 3 7z"/><path d="M4 19h6v2H4zM12 11H4v2h8zM4 17h4v-2H4zM15 15v-3l-4.5 4.5L15 21v-3l8.027-.032L23 15z"/></svg>'); |
173 | | - -webkit-mask: var(--svg-clipboard) no-repeat center; |
174 | | - mask: var(--svg-clipboard) no-repeat center; |
175 | | -}"#.to_string(); |
176 | | - |
177 | | - let brands: &[&dyn f_a::Brands] = &[ |
178 | | - &f_a::icons::IconFonticons, |
179 | | - &f_a::icons::IconRust, |
180 | | - &f_a::icons::IconMarkdown, |
181 | | - &f_a::icons::IconGitAlt, |
182 | | - ]; |
183 | | - let regular: &[&dyn f_a::Regular] = &[ |
184 | | - &f_a::icons::IconFileLines, |
185 | | - &f_a::icons::IconFolderOpen, |
186 | | - &f_a::icons::IconFile, |
187 | | - &f_a::icons::IconStar, |
188 | | - ]; |
189 | | - let solid: &[&dyn f_a::Solid] = &[ |
190 | | - &f_a::icons::IconCircleInfo, |
191 | | - &f_a::icons::IconGears, |
192 | | - &f_a::icons::IconTable, |
193 | | - &f_a::icons::IconRoad, |
194 | | - &f_a::icons::IconDownload, |
195 | | - &f_a::icons::IconCubes, |
196 | | - &f_a::icons::IconSquareRss, |
197 | | - &f_a::icons::IconFileLines, |
198 | | - &f_a::icons::IconCheck, |
199 | | - &f_a::icons::IconTriangleExclamation, |
200 | | - &f_a::icons::IconGear, |
201 | | - &f_a::icons::IconX, |
202 | | - &f_a::icons::IconHouse, |
203 | | - &f_a::icons::IconCodeBranch, |
204 | | - &f_a::icons::IconStar, |
205 | | - &f_a::icons::IconCircleExclamation, |
206 | | - &f_a::icons::IconCube, |
207 | | - &f_a::icons::IconChevronLeft, |
208 | | - &f_a::icons::IconChevronRight, |
209 | | - &f_a::icons::IconFolderOpen, |
210 | | - &f_a::icons::IconLock, |
211 | | - &f_a::icons::IconFlag, |
212 | | - &f_a::icons::IconBook, |
213 | | - &f_a::icons::IconMagnifyingGlass, |
214 | | - &f_a::icons::IconLeaf, |
215 | | - &f_a::icons::IconChartLine, |
216 | | - &f_a::icons::IconList, |
217 | | - &f_a::icons::IconUser, |
218 | | - &f_a::icons::IconTrash, |
219 | | - &f_a::icons::IconArrowLeft, |
220 | | - &f_a::icons::IconArrowRight, |
221 | | - &f_a::icons::IconLink, |
222 | | - &f_a::icons::IconScaleUnbalancedFlip, |
223 | | - &f_a::icons::IconSpinner, |
224 | | - ]; |
225 | | - |
226 | | - for icon in brands { |
227 | | - render_icon( |
228 | | - icon.icon_name(), |
229 | | - icon.icon_str(), |
230 | | - format!("{icon:?}"), |
231 | | - &mut code_output, |
232 | | - &mut css_output, |
233 | | - "brands", |
234 | | - ); |
235 | | - } |
236 | | - for icon in regular { |
237 | | - render_icon( |
238 | | - icon.icon_name(), |
239 | | - icon.icon_str(), |
240 | | - format!("{icon:?}"), |
241 | | - &mut code_output, |
242 | | - &mut css_output, |
243 | | - "regular", |
244 | | - ); |
245 | | - } |
246 | | - for icon in solid { |
247 | | - render_icon( |
248 | | - icon.icon_name(), |
249 | | - icon.icon_str(), |
250 | | - format!("{icon:?}"), |
251 | | - &mut code_output, |
252 | | - &mut css_output, |
253 | | - "solid", |
254 | | - ); |
255 | | - } |
256 | | - |
257 | | - std::fs::write(&css_path, css_output).map_err(|error| { |
258 | | - Error::msg(format!( |
259 | | - "Failed to write into `{}`: {error:?}", |
260 | | - css_path.display() |
261 | | - )) |
262 | | - })?; |
263 | | - |
264 | | - code_output.push('}'); |
265 | | - let icons_file = out_dir.join("icons.rs"); |
266 | | - std::fs::write(&icons_file, code_output).map_err(|error| { |
267 | | - Error::msg(format!( |
268 | | - "Failed to write `{}`: {error:?}", |
269 | | - icons_file.display() |
270 | | - )) |
271 | | - })?; |
272 | | - Ok(()) |
273 | | -} |
274 | | - |
275 | 88 | fn write_git_version(out_dir: &Path) -> Result<()> { |
276 | 89 | let maybe_hash = get_git_hash()?; |
277 | 90 | let git_hash = maybe_hash.as_deref().unwrap_or("???????"); |
|
0 commit comments