|
1 |
| -use generate_errors::{parse_errors, ErrorCode, FrontMatterErrorCode, Section}; |
2 |
| -use serde::Serialize; |
3 |
| -use std::fs::File; |
4 |
| -use std::io::Write; |
5 |
| -use std::path::Path; |
6 |
| -use std::{fs, io}; |
7 |
| - |
8 |
| -fn main() -> io::Result<()> { |
9 |
| - let errors_dir = std::env::args() |
10 |
| - .nth(1) |
11 |
| - .expect("First argument should specify the errors directory"); |
12 |
| - let content_dir = std::env::args() |
13 |
| - .nth(2) |
14 |
| - .expect("Second argument should specify the content directory"); |
15 |
| - let _ = fs::create_dir(content_dir.clone()); |
16 |
| - let errors_root_section = parse_errors(&errors_dir)?; |
17 |
| - |
18 |
| - errors_root_section.write(Path::new(&content_dir), Path::new(""), 0) |
19 |
| -} |
20 |
| - |
21 |
| -trait FrontMatterWriter { |
22 |
| - fn write(&self, root_path: &Path, current_path: &Path, weight: usize) -> io::Result<()>; |
23 |
| -} |
24 |
| - |
25 |
| -#[derive(Serialize)] |
26 |
| -struct FrontMatterSection { |
27 |
| - title: String, |
28 |
| - sort_by: String, |
29 |
| - template: Option<String>, |
30 |
| - weight: usize, |
31 |
| - extra: FrontMatterSectionExtra, |
32 |
| -} |
33 |
| - |
34 |
| -#[derive(Serialize)] |
35 |
| -struct FrontMatterSectionExtra { |
36 |
| - header_message: Option<String>, |
37 |
| - sort_order_reversed: bool, |
38 |
| -} |
39 |
| - |
40 |
| -impl From<&Section> for FrontMatterSection { |
41 |
| - fn from(section: &Section) -> Self { |
42 |
| - FrontMatterSection { |
43 |
| - title: section.name.clone(), |
44 |
| - sort_by: "weight".to_string(), |
45 |
| - template: section.template.clone(), |
46 |
| - weight: section.order.unwrap_or(0), |
47 |
| - extra: section.into(), |
48 |
| - } |
49 |
| - } |
50 |
| -} |
51 |
| - |
52 |
| -impl From<&Section> for FrontMatterSectionExtra { |
53 |
| - fn from(section: &Section) -> Self { |
54 |
| - FrontMatterSectionExtra { |
55 |
| - header_message: section.header.clone(), |
56 |
| - sort_order_reversed: section.sort_order_reversed, |
57 |
| - } |
58 |
| - } |
59 |
| -} |
60 |
| - |
61 |
| -impl FrontMatterWriter for Section { |
62 |
| - fn write(&self, root_path: &Path, current_path: &Path, weight: usize) -> io::Result<()> { |
63 |
| - let section_path = current_path.join(self.name.to_ascii_lowercase()); |
64 |
| - let path = root_path.join(§ion_path); |
65 |
| - fs::create_dir(path.clone())?; |
66 |
| - |
67 |
| - let mut frontmatter = FrontMatterSection::from(self); |
68 |
| - if self.order.is_none() { |
69 |
| - frontmatter.weight = weight; |
70 |
| - } |
71 |
| - |
72 |
| - let mut file = File::create(path.join("_index.md"))?; |
73 |
| - file.write_all( |
74 |
| - format!( |
75 |
| - r#"+++ |
76 |
| -{} |
77 |
| -+++ |
78 |
| -"#, |
79 |
| - toml::to_string(&frontmatter).unwrap(), |
80 |
| - ) |
81 |
| - .as_bytes(), |
82 |
| - )?; |
83 |
| - |
84 |
| - for (i, content) in self.content.iter().enumerate() { |
85 |
| - content.write(root_path, §ion_path, i)?; |
86 |
| - } |
87 |
| - Ok(()) |
88 |
| - } |
89 |
| -} |
90 |
| - |
91 |
| -impl FrontMatterWriter for ErrorCode { |
92 |
| - fn write(&self, root_path: &Path, current_path: &Path, weight: usize) -> io::Result<()> { |
93 |
| - let path = root_path.join(current_path); |
94 |
| - |
95 |
| - let mut frontmatter = FrontMatterErrorCode::from(self); |
96 |
| - frontmatter.weight = weight; |
97 |
| - |
98 |
| - let mut file = File::create(path.join(format!("{}.md", self.code)))?; |
99 |
| - file.write_all( |
100 |
| - format!( |
101 |
| - r#"+++ |
102 |
| -{} |
103 |
| -+++ |
104 |
| -{}"#, |
105 |
| - toml::to_string(&frontmatter).unwrap(), |
106 |
| - self.content |
107 |
| - ) |
108 |
| - .as_bytes(), |
109 |
| - )?; |
110 |
| - |
111 |
| - Ok(()) |
112 |
| - } |
| 1 | +use generate_errors::*; |
| 2 | +use std::path::PathBuf; |
| 3 | + |
| 4 | +use clap::Parser; |
| 5 | + |
| 6 | +/// Generate error reference pages from Bevy engine |
| 7 | +/// for use on the Bevy website. |
| 8 | +#[derive(Parser, Debug)] |
| 9 | +#[command(version, about, long_about = None)] |
| 10 | +struct Args { |
| 11 | + /// Path to the directory containing the |
| 12 | + /// error files stored in the |
| 13 | + /// local Bevy GitHub repo. |
| 14 | + #[arg(long)] |
| 15 | + errors_path: PathBuf, |
| 16 | + /// Path to the folder which the |
| 17 | + /// errors section should be generated in. |
| 18 | + #[arg(long)] |
| 19 | + output_path: PathBuf, |
| 20 | +} |
| 21 | + |
| 22 | +fn main() -> anyhow::Result<()> { |
| 23 | + let args = Args::parse(); |
| 24 | + |
| 25 | + println!("Writing section index & introduction . . ."); |
| 26 | + write_section(&args.output_path)?; |
| 27 | + println!("Getting error page contents . . ."); |
| 28 | + let error_page_content = get_error_pages(&args.errors_path)?; |
| 29 | + println!("Writing error pages content to output path . . ."); |
| 30 | + write_pages(&args.output_path, error_page_content)?; |
| 31 | + |
| 32 | + println!("All good!"); |
| 33 | + Ok(()) |
113 | 34 | }
|
0 commit comments