Skip to content

Commit 0889cb6

Browse files
TrialDragonBD103alice-i-cecile
authored
Rewrite generate-errors and improve learn/errors (#1089)
Co-authored-by: BD103 <[email protected]> Co-authored-by: Alice Cecile <[email protected]>
1 parent 80c0763 commit 0889cb6

File tree

10 files changed

+289
-237
lines changed

10 files changed

+289
-237
lines changed

Cargo.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

content/learn/links.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ description = "Browse bevy examples compiled to wasm and running directly in you
4242

4343
[[links]]
4444
title = "Bevy Errors"
45-
url = "/learn/errors"
45+
url = "/learn/errors/introduction"
4646
image = "/assets/bevy_icon_dark.svg"
4747
image_alt = "Bevy logo"
4848
description = "List of Bevy's error codes for the current release."

generate-errors/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# Bevy repository
22
bevy/
3+
# Fake content folder for tests
4+
section_content/
5+
pages_content/

generate-errors/Cargo.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
[package]
22
name = "generate-errors"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
authors = [
55
"Bevy Contributors <[email protected]>",
66
"Carter Anderson <[email protected]>",
77
]
88
license = "MIT"
9-
edition = "2018"
10-
11-
[dependencies]
12-
serde = { version = "1", features = [ "derive" ] }
13-
toml = "0.5"
14-
regex = "1.5"
9+
edition = "2021"
1510

1611
[lints]
1712
workspace = true
13+
14+
[dependencies]
15+
anyhow = "1.0.80"
16+
clap = { version = "4.5.2", features = ["derive"] }
17+
regex = "1.10.3"

generate-errors/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generate Errors
2+
3+
This website utility tool takes the documentation of Bevy-unique error codes from the Bevy engine repo and makes them valid Zola pages.
4+
5+
For most uses run the bash script from any directory:
6+
`./generate-errors/generate-errors.sh`
7+
or
8+
`./generate-errors.sh`
9+
10+
## Niche Advice
11+
12+
Almost certainly all use cases will be covered by the bash script above, but if you, for whatever reason, need to use the tool straight from cargo then you can run it like:
13+
`cargo run -- --errors-path <ERRORS_PATH> --output-path <OUTPUT_PATH>`
14+
Where `errors-path` is the directory containing the original error files in the Bevy repo. `output-path` is the folder that the errors section folder is output.
15+
16+
You can also see the help page for the tool via:
17+
`cargo run -- -h` or `cargo run -- --help`

generate-errors/generate_errors.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ git sparse-checkout set "errors"
1111
git pull --depth=1 origin latest
1212
cd ..
1313

14-
cargo run --bin generate -- bevy/errors ../content/learn
14+
cargo run --bin generate -- --errors-path bevy/errors --output-path ../content/learn

generate-errors/src/bin/generate.rs

Lines changed: 33 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,34 @@
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(&section_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, &section_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(())
11334
}

0 commit comments

Comments
 (0)