Skip to content

Rewrite generate-errors and improve learn/errors #1089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e5eb1e3
Purge old error generator
TrialDragon Mar 10, 2024
9aa88e2
Add get error pages
TrialDragon Mar 11, 2024
2e1f860
Add errors section and introduction
TrialDragon Mar 11, 2024
efd34db
Add error code pages
TrialDragon Mar 11, 2024
c4a59ea
Make error page title not duplicate
TrialDragon Mar 11, 2024
b7514e7
Change learn errors link card URL
TrialDragon Mar 11, 2024
d277b6c
Update .gitignore to include folder used by tests
TrialDragon Mar 11, 2024
8744329
Modify tests to reflect usage better
TrialDragon Mar 11, 2024
6cbcd9f
Update Cargo
TrialDragon Mar 11, 2024
f085ca0
Add clean up to tests
TrialDragon Mar 11, 2024
e2d935f
Add new error page generator CLI
TrialDragon Mar 11, 2024
d7f4d25
Update generate errors script to use new CLI
TrialDragon Mar 11, 2024
14f7c95
Remove unnecessary template
TrialDragon Mar 11, 2024
c261c0e
Update base layout to include errors page stuff
TrialDragon Mar 11, 2024
e6efe3a
Add README.md
TrialDragon Mar 11, 2024
58d7d19
Update variable names to make semantic sense
TrialDragon Mar 11, 2024
4c25c53
Fix typo
TrialDragon Mar 11, 2024
8ee871d
Satisfy the formatter / linter
TrialDragon Mar 11, 2024
b38323c
Correct an old part in the docs
TrialDragon Mar 11, 2024
b6d162a
Fix outputted to output
TrialDragon Mar 11, 2024
a173e00
Improve comments
TrialDragon Mar 11, 2024
319a910
Clarify a word
TrialDragon Mar 11, 2024
6cf6227
Correct directory name referenced in README.md
TrialDragon Mar 11, 2024
28efcde
Use `fs::read_to_string`
TrialDragon Mar 11, 2024
10371d1
Clarify error
TrialDragon Mar 11, 2024
78242b3
Fix annotation ordering
TrialDragon Mar 11, 2024
c50a4a9
Format code to be readable
TrialDragon Mar 11, 2024
bf9d8df
Update generate-errors/src/lib.rs
TrialDragon Mar 16, 2024
99a2153
Update generate-errors/src/lib.rs
TrialDragon Mar 16, 2024
21caba3
Clarify RegEx statement purpose
TrialDragon Mar 16, 2024
557ea7d
Update generate-errors to use Rust 2021
TrialDragon Mar 16, 2024
9f92445
Simplify returning anyhow error
TrialDragon Mar 16, 2024
0732512
Update generate-errors to 0.2.0
TrialDragon Mar 16, 2024
932b0cb
Add bail import
TrialDragon Mar 16, 2024
43db94c
Decouple tests so they can be run parallel
TrialDragon Mar 16, 2024
da991c5
Improve entry unwrapping comment
TrialDragon Mar 16, 2024
2b78532
Add title logic comment
TrialDragon Mar 16, 2024
1868115
Explain some RegEx logic
TrialDragon Mar 16, 2024
f5a22a2
Make some page content const
TrialDragon Mar 16, 2024
cd1c5e4
Update .gitgnore
alice-i-cecile Mar 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion content/learn/links.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ description = "Browse bevy examples compiled to wasm and running directly in you

[[links]]
title = "Bevy Errors"
url = "/learn/errors"
url = "/learn/errors/introduction"
image = "/assets/bevy_icon_dark.svg"
image_alt = "Bevy logo"
description = "List of Bevy's error codes for the current release."
Expand Down
3 changes: 3 additions & 0 deletions generate-errors/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Bevy repository
bevy/
# Fake content folder for tests
section_content/
pages_content/
14 changes: 7 additions & 7 deletions generate-errors/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[package]
name = "generate-errors"
version = "0.1.0"
version = "0.2.0"
authors = [
"Bevy Contributors <[email protected]>",
"Carter Anderson <[email protected]>",
]
license = "MIT"
edition = "2018"

[dependencies]
serde = { version = "1", features = [ "derive" ] }
toml = "0.5"
regex = "1.5"
edition = "2021"

[lints]
workspace = true

[dependencies]
anyhow = "1.0.80"
clap = { version = "4.5.2", features = ["derive"] }
regex = "1.10.3"
17 changes: 17 additions & 0 deletions generate-errors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generate Errors

This website utility tool takes the documentation of Bevy-unique error codes from the Bevy engine repo and makes them valid Zola pages.

For most uses run the bash script from any directory:
`./generate-errors/generate-errors.sh`
or
`./generate-errors.sh`

## Niche Advice

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:
`cargo run -- --errors-path <ERRORS_PATH> --output-path <OUTPUT_PATH>`
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.

You can also see the help page for the tool via:
`cargo run -- -h` or `cargo run -- --help`
2 changes: 1 addition & 1 deletion generate-errors/generate_errors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ git sparse-checkout set "errors"
git pull --depth=1 origin latest
cd ..

cargo run --bin generate -- bevy/errors ../content/learn
cargo run --bin generate -- --errors-path bevy/errors --output-path ../content/learn
145 changes: 33 additions & 112 deletions generate-errors/src/bin/generate.rs
Original file line number Diff line number Diff line change
@@ -1,113 +1,34 @@
use generate_errors::{parse_errors, ErrorCode, FrontMatterErrorCode, Section};
use serde::Serialize;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::{fs, io};

fn main() -> io::Result<()> {
let errors_dir = std::env::args()
.nth(1)
.expect("First argument should specify the errors directory");
let content_dir = std::env::args()
.nth(2)
.expect("Second argument should specify the content directory");
let _ = fs::create_dir(content_dir.clone());
let errors_root_section = parse_errors(&errors_dir)?;

errors_root_section.write(Path::new(&content_dir), Path::new(""), 0)
}

trait FrontMatterWriter {
fn write(&self, root_path: &Path, current_path: &Path, weight: usize) -> io::Result<()>;
}

#[derive(Serialize)]
struct FrontMatterSection {
title: String,
sort_by: String,
template: Option<String>,
weight: usize,
extra: FrontMatterSectionExtra,
}

#[derive(Serialize)]
struct FrontMatterSectionExtra {
header_message: Option<String>,
sort_order_reversed: bool,
}

impl From<&Section> for FrontMatterSection {
fn from(section: &Section) -> Self {
FrontMatterSection {
title: section.name.clone(),
sort_by: "weight".to_string(),
template: section.template.clone(),
weight: section.order.unwrap_or(0),
extra: section.into(),
}
}
}

impl From<&Section> for FrontMatterSectionExtra {
fn from(section: &Section) -> Self {
FrontMatterSectionExtra {
header_message: section.header.clone(),
sort_order_reversed: section.sort_order_reversed,
}
}
}

impl FrontMatterWriter for Section {
fn write(&self, root_path: &Path, current_path: &Path, weight: usize) -> io::Result<()> {
let section_path = current_path.join(self.name.to_ascii_lowercase());
let path = root_path.join(&section_path);
fs::create_dir(path.clone())?;

let mut frontmatter = FrontMatterSection::from(self);
if self.order.is_none() {
frontmatter.weight = weight;
}

let mut file = File::create(path.join("_index.md"))?;
file.write_all(
format!(
r#"+++
{}
+++
"#,
toml::to_string(&frontmatter).unwrap(),
)
.as_bytes(),
)?;

for (i, content) in self.content.iter().enumerate() {
content.write(root_path, &section_path, i)?;
}
Ok(())
}
}

impl FrontMatterWriter for ErrorCode {
fn write(&self, root_path: &Path, current_path: &Path, weight: usize) -> io::Result<()> {
let path = root_path.join(current_path);

let mut frontmatter = FrontMatterErrorCode::from(self);
frontmatter.weight = weight;

let mut file = File::create(path.join(format!("{}.md", self.code)))?;
file.write_all(
format!(
r#"+++
{}
+++
{}"#,
toml::to_string(&frontmatter).unwrap(),
self.content
)
.as_bytes(),
)?;

Ok(())
}
use generate_errors::*;
use std::path::PathBuf;

use clap::Parser;

/// Generate error reference pages from Bevy engine
/// for use on the Bevy website.
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Path to the directory containing the
/// error files stored in the
/// local Bevy GitHub repo.
#[arg(long)]
errors_path: PathBuf,
/// Path to the folder which the
/// errors section should be generated in.
#[arg(long)]
output_path: PathBuf,
}

fn main() -> anyhow::Result<()> {
let args = Args::parse();

println!("Writing section index & introduction . . .");
write_section(&args.output_path)?;
println!("Getting error page contents . . .");
let error_page_content = get_error_pages(&args.errors_path)?;
println!("Writing error pages content to output path . . .");
write_pages(&args.output_path, error_page_content)?;

println!("All good!");
Ok(())
}
Loading