]> git.proxmox.com Git - rustc.git/blob - src/tools/rustdoc-themes/main.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / rustdoc-themes / main.rs
1 use std::env::args;
2 use std::fs::{create_dir_all, File};
3 use std::io::{BufRead, BufReader, BufWriter, Write};
4 use std::path::Path;
5 use std::process::{exit, Command};
6
7 fn get_themes<P: AsRef<Path>>(style_path: P) -> Vec<String> {
8 let mut ret = Vec::with_capacity(10);
9
10 const BEGIN_THEME_MARKER: &'static str = "/* Begin theme: ";
11 const END_THEME_MARKER: &'static str = "/* End theme: ";
12
13 let timestamp =
14 std::time::SystemTime::UNIX_EPOCH.elapsed().expect("time is after UNIX epoch").as_millis();
15
16 let mut in_theme = None;
17 create_dir_all("build/tmp").expect("failed to create temporary test directory");
18 for line in BufReader::new(File::open(style_path).expect("read rustdoc.css failed")).lines() {
19 let line = line.expect("read line from rustdoc.css failed");
20 let line = line.trim();
21 if line.starts_with(BEGIN_THEME_MARKER) {
22 let theme_name = &line[BEGIN_THEME_MARKER.len()..].trim().trim_end_matches("*/").trim();
23 let filename = format!("build/tmp/rustdoc.bootstrap.{timestamp}.{theme_name}.css");
24 in_theme = Some(BufWriter::new(
25 File::create(&filename).expect("failed to create temporary test css file"),
26 ));
27 ret.push(filename);
28 }
29 if let Some(in_theme) = in_theme.as_mut() {
30 in_theme.write_all(line.as_bytes()).expect("write to temporary test css file");
31 in_theme.write_all(b"\n").expect("write to temporary test css file");
32 }
33 if line.starts_with(END_THEME_MARKER) {
34 in_theme = None;
35 }
36 }
37 ret
38 }
39
40 fn main() {
41 let argv: Vec<String> = args().collect();
42
43 if argv.len() < 3 {
44 eprintln!("Needs rustdoc binary path");
45 exit(1);
46 }
47 let rustdoc_bin = &argv[1];
48 let style_path = &argv[2];
49 let themes = get_themes(&style_path);
50 if themes.is_empty() {
51 eprintln!("No themes found in \"{}\"...", style_path);
52 exit(1);
53 }
54 let arg_name = "--check-theme".to_owned();
55 let status = Command::new(rustdoc_bin)
56 .args(&themes.iter().flat_map(|t| vec![&arg_name, t].into_iter()).collect::<Vec<_>>())
57 .status()
58 .expect("failed to execute child");
59 if !status.success() {
60 exit(1);
61 }
62 }