Skip to content

Commit 9626963

Browse files
KDecayKDecay
KDecay
and
KDecay
committed
Code validation and format checking (#281)
# Objective Migrates #203 Closes #83 # Solution - [x] Add code validation - [x] ~~Add code format checking~~ Removed to unblock the code validation changes # Problems During the implementation of the code validation and the format checking I encountered some problems which I would appreciate guidance or help with. ## Need to be addressed 1. ~~Zola doesn't highlight the code blocks using the rust syntax if you add more than just the language to the code block attributes. Example `rust` highlights the code correctly, `rust,no_run` doesn't. So currently the code highlighting is broken.~~ Zola is order dependent for attributes (as noted by @mockersf). So changing the attribute order from `rust,no_run` to `no_run,rust` works just fine. 1. Running `cargo fmt` doesn't format the doc examples. Adding the `rustfmt.toml` option `format_code_in_doc_comments = true` enables this, but requires nightly. In my tests it also only works if you add the code examples directly without using `#[doc = include_str!("_index.md")]`. So currently the format checking is not working. ## Nice to have 1. The `lib.rs` file of the `code-validation` crate has to be manually updated each time a new `_index.md` file is added or removed. It might be a good idea to dive deeper and see if we can implement a solution where this work is done automatically. For now this should be more than fine though. Co-authored-by: KDecay <[email protected]>
1 parent 3b49f1b commit 9626963

File tree

11 files changed

+223
-21
lines changed

11 files changed

+223
-21
lines changed

.github/workflows/ci.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,20 @@ jobs:
3434
BUILD_DIR: .
3535
BUILD_ONLY: true
3636
TOKEN: fake-secret
37+
38+
test-code:
39+
runs-on: ubuntu-latest
40+
steps:
41+
- uses: actions/checkout@v2
42+
43+
- uses: actions-rs/toolchain@v1
44+
with:
45+
toolchain: stable
46+
components: rustfmt, clippy
47+
override: true
48+
49+
- name: Install alsa and udev
50+
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev
51+
52+
- name: Build & run tests
53+
run: cd code-validation && cargo test

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ public
44
content/assets
55
content/learn/errors
66
content/examples
7+
code-validation/target
8+
code-validation/Cargo.lock

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,5 @@ When writing and reviewing learning material for the Bevy Book and Quick Start G
147147
8. Examples should not use or rely on third-party plugins.
148148
These may be appropriate to link in "next steps" however at the end of the examples.
149149
1. Third-party crates should be limited to the most essential, such as `rand`.
150+
9. If additional code block attributes like `no_run` or `hide-lines=x-y` need to be specified, you should always order these so that the language is the last attribute. If we would specify `rust,no_run` the syntax highlighting wouldn't work, but changing it to `no_run,rust` makes it work.
151+
10. To validate if local code changes are compiling you can `cd` into the `code-validation` folder and test your code using `cargo test`.

code-validation/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "code-validation"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
bevy = "0.6.0"

code-validation/rustfmt.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
use_field_init_shorthand = true
2+
newline_style = "Unix"

code-validation/src/lib.rs

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
//! This crate is used to validate the rust code of the `bevy` website.
2+
//!
3+
//! It is currently used to validate the rust code of the offical `bevy` book.
4+
//! The modules represents the folder structure of the website.
5+
6+
mod learn {
7+
#[doc = include_str!("../../content/learn/book/_index.md")]
8+
mod book {
9+
#[doc = include_str!("../../content/learn/book/assets/_index.md")]
10+
mod assets {
11+
#[doc = include_str!("../../content/learn/book/assets/custom-assets/_index.md")]
12+
mod custom_assets {}
13+
#[doc = include_str!("../../content/learn/book/assets/hot-reloading/_index.md")]
14+
mod hot_reloading {}
15+
#[doc = include_str!("../../content/learn/book/assets/loading-assets/_index.md")]
16+
mod loading_assets {}
17+
#[doc = include_str!("../../content/learn/book/assets/scenes/_index.md")]
18+
mod scenes {}
19+
#[doc = include_str!("../../content/learn/book/assets/working-with-handles/_index.md")]
20+
mod working_with_handles {}
21+
}
22+
23+
#[doc = include_str!("../../content/learn/book/audio/_index.md")]
24+
mod audio {
25+
#[doc = include_str!("../../content/learn/book/audio/audio-basics/_index.md")]
26+
mod audio_basics {}
27+
}
28+
29+
#[doc = include_str!("../../content/learn/book/development-practices/_index.md")]
30+
mod development_practices {
31+
#[doc = include_str!("../../content/learn/book/development-practices/boilerplate-reduction/_index.md")]
32+
mod boilerplate_reduction {}
33+
#[doc = include_str!("../../content/learn/book/development-practices/error-handling/_index.md")]
34+
mod error_handling {}
35+
#[doc = include_str!("../../content/learn/book/development-practices/fast-compiles/_index.md")]
36+
mod fast_compiles {}
37+
#[doc = include_str!("../../content/learn/book/development-practices/organizing-your-code/_index.md")]
38+
mod organizing_your_code {}
39+
#[doc = include_str!("../../content/learn/book/development-practices/testing/_index.md")]
40+
mod testing {}
41+
}
42+
43+
#[doc = include_str!("../../content/learn/book/ecs/_index.md")]
44+
mod ecs {
45+
#[doc = include_str!("../../content/learn/book/ecs/commands/_index.md")]
46+
mod commands {}
47+
#[doc = include_str!("../../content/learn/book/ecs/entities-components/_index.md")]
48+
mod entities_components {}
49+
#[doc = include_str!("../../content/learn/book/ecs/exclusive-world-access/_index.md")]
50+
mod exclusive_world_access {}
51+
#[doc = include_str!("../../content/learn/book/ecs/filtering-queries/_index.md")]
52+
mod filtering_queries {}
53+
#[doc = include_str!("../../content/learn/book/ecs/generic-systems/_index.md")]
54+
mod generic_systems {}
55+
#[doc = include_str!("../../content/learn/book/ecs/reliable-change-detection/_index.md")]
56+
mod reliable_change_detection {}
57+
#[doc = include_str!("../../content/learn/book/ecs/resources/_index.md")]
58+
mod resources {}
59+
#[doc = include_str!("../../content/learn/book/ecs/systems-queries/_index.md")]
60+
mod system_queries {}
61+
}
62+
63+
#[doc = include_str!("../../content/learn/book/game-logic/_index.md")]
64+
mod game_logic {
65+
#[doc = include_str!("../../content/learn/book/game-logic/async-tasks/_index.md")]
66+
mod async_tasks {}
67+
#[doc = include_str!("../../content/learn/book/game-logic/custom-execution/_index.md")]
68+
mod custom_execution {}
69+
#[doc = include_str!("../../content/learn/book/game-logic/events/_index.md")]
70+
mod events {}
71+
#[doc = include_str!("../../content/learn/book/game-logic/run-criteria/_index.md")]
72+
mod run_criteria {}
73+
#[doc = include_str!("../../content/learn/book/game-logic/states/_index.md")]
74+
mod states {}
75+
#[doc = include_str!("../../content/learn/book/game-logic/system-ordering/_index.md")]
76+
mod system_ordering {}
77+
#[doc = include_str!("../../content/learn/book/game-logic/time/_index.md")]
78+
mod time {}
79+
}
80+
81+
#[doc = include_str!("../../content/learn/book/graphics/_index.md")]
82+
mod graphics {
83+
#[doc = include_str!("../../content/learn/book/graphics/2d/_index.md")]
84+
mod two_dimensional {
85+
#[doc = include_str!("../../content/learn/book/graphics/2d/sprite-sheets/_index.md")]
86+
mod sprite_sheets {}
87+
#[doc = include_str!("../../content/learn/book/graphics/2d/sprites/_index.md")]
88+
mod sprites {}
89+
}
90+
#[doc = include_str!("../../content/learn/book/graphics/3d/_index.md")]
91+
mod three_dimensional {
92+
#[doc = include_str!("../../content/learn/book/graphics/3d/meshes/_index.md")]
93+
mod meshes {}
94+
#[doc = include_str!("../../content/learn/book/graphics/3d/pbr/_index.md")]
95+
mod pbr {}
96+
}
97+
#[doc = include_str!("../../content/learn/book/graphics/cameras/_index.md")]
98+
mod cameras {}
99+
#[doc = include_str!("../../content/learn/book/graphics/parent-child-hierarchy/_index.md")]
100+
mod parent_child_hierarchy {}
101+
#[doc = include_str!("../../content/learn/book/graphics/rendering-internals/_index.md")]
102+
mod rendering_internals {
103+
#[doc = include_str!("../../content/learn/book/graphics/rendering-internals/shader-basics/_index.md")]
104+
mod shader_basics {}
105+
}
106+
#[doc = include_str!("../../content/learn/book/graphics/transforms/_index.md")]
107+
mod transforms {}
108+
#[doc = include_str!("../../content/learn/book/graphics/windowing/_index.md")]
109+
mod windowing {}
110+
}
111+
112+
#[doc = include_str!("../../content/learn/book/input/_index.md")]
113+
mod input {
114+
#[doc = include_str!("../../content/learn/book/input/gamepad/_index.md")]
115+
mod gamepad {}
116+
#[doc = include_str!("../../content/learn/book/input/input-basics/_index.md")]
117+
mod input_basics {}
118+
#[doc = include_str!("../../content/learn/book/input/keyboard/_index.md")]
119+
mod keyboard {}
120+
#[doc = include_str!("../../content/learn/book/input/mouse/_index.md")]
121+
mod mouse {}
122+
#[doc = include_str!("../../content/learn/book/input/touch/_index.md")]
123+
mod touch {}
124+
}
125+
126+
// Not testing migration guides, because of breaking api changes.
127+
mod migration_guides {}
128+
129+
#[doc = include_str!("../../content/learn/book/next-steps/_index.md")]
130+
mod next_steps {}
131+
132+
#[doc = include_str!("../../content/learn/book/performance-optimizations/_index.md")]
133+
mod performance_optimizations {
134+
#[doc = include_str!("../../content/learn/book/performance-optimizations/component-storage/_index.md")]
135+
mod component_storage {}
136+
#[doc = include_str!("../../content/learn/book/performance-optimizations/diagnostics-benchmarking/_index.md")]
137+
mod diagnostics_benchmarking {}
138+
#[doc = include_str!("../../content/learn/book/performance-optimizations/indexes/_index.md")]
139+
mod indexes {}
140+
#[doc = include_str!("../../content/learn/book/performance-optimizations/parallel-iteration/_index.md")]
141+
mod parallel_iteration {}
142+
}
143+
144+
#[doc = include_str!("../../content/learn/book/platforms/_index.md")]
145+
mod platforms {
146+
#[doc = include_str!("../../content/learn/book/platforms/android/_index.md")]
147+
mod android {}
148+
#[doc = include_str!("../../content/learn/book/platforms/ios/_index.md")]
149+
mod ios {}
150+
#[doc = include_str!("../../content/learn/book/platforms/web/_index.md")]
151+
mod web {}
152+
}
153+
154+
#[doc = include_str!("../../content/learn/book/ui/_index.md")]
155+
mod ui {
156+
#[doc = include_str!("../../content/learn/book/ui/ui-basics/_index.md")]
157+
mod ui_basics {}
158+
}
159+
160+
#[doc = include_str!("../../content/learn/book/welcome/_index.md")]
161+
mod welcome {
162+
#[doc = include_str!("../../content/learn/book/welcome/apps/_index.md")]
163+
mod apps {}
164+
#[doc = include_str!("../../content/learn/book/welcome/plugins/_index.md")]
165+
mod plugins {}
166+
#[doc = include_str!("../../content/learn/book/welcome/setup/_index.md")]
167+
mod setup {}
168+
}
169+
}
170+
}

content/learn/book/welcome/apps/_index.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ The process is straightforward: we first create a new [`App`].
1313
Then, we add a simple system, which prints "Hello, Bevy!" when it is run.
1414
Finally once we're done configuring the app, we call [`App`] to actually make our app *do things*.
1515

16-
```rust
16+
```no_run,rust
1717
use bevy::prelude::*;
1818
19-
fn main(){
19+
fn main() {
2020
App::new()
2121
.add_system(hello)
2222
.run();
2323
}
2424
25-
fn hello(){
25+
fn hello() {
2626
println!("Hello, Bevy!")
2727
}
2828
```
@@ -43,7 +43,7 @@ The most basic tools are:
4343
3. Importing other blocks of [`App`]-modifying code using [`Plugins`].
4444
Let's write a very simple demo that shows how those work.
4545

46-
```rust
46+
```no_run,rust
4747
use bevy::prelude::*;
4848
4949
fn main() {
@@ -52,9 +52,9 @@ fn main() {
5252
// imported as a single unit for organization and clarity
5353
.add_plugins(MinimalPlugins)
5454
// Resources are global singleton data stored in the `World`
55-
.insert_resource(Message {string: "Welcome to Bevy!"})
55+
.insert_resource(Message {string: "Welcome to Bevy!".to_string()})
5656
// Systems run every pass of the game loop and perform logic
57-
.add_system(print_message_system)
57+
.add_system(read_message_system)
5858
.run();
5959
}
6060
@@ -65,8 +65,8 @@ struct Message {
6565
6666
// This system reads our Message resource,
6767
// so we add `Res<Message>` to its function parameters
68-
fn read_message(message: Res<Message>) {
69-
println!(message.string);
68+
fn read_message_system(message: Res<Message>) {
69+
println!("{}", message.string);
7070
}
7171
```
7272

content/learn/book/welcome/plugins/_index.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ There's no magic to be found here; they're just a straightforward tool for code
1818

1919
Plugins are types that implement the [`Plugin`] trait:
2020

21-
```rust
21+
```no_run,rust
2222
use bevy::prelude::*;
2323
2424
fn main(){
25-
App::build()
25+
App::new()
2626
// Adds the "default" Bevy Engine plugins.
2727
// We'll cover this in the next section.
2828
.add_plugins(DefaultPlugins)
@@ -40,21 +40,21 @@ impl Plugin for ScorePlugin {
4040
// beginning at the default value of 0
4141
.init_resource::<Score>()
4242
// Increments the score by 1 every pass of the game loop
43-
.add_system(increment_score.system())
43+
.add_system(increment_score)
4444
// Prints the current value of the score
45-
.add_system(report_score.system());
45+
.add_system(report_score);
4646
}
4747
}
4848
4949
#[derive(Default, Debug)]
5050
struct Score(u8);
5151
52-
fn increment_score(score: ResMut<Score>){
52+
fn increment_score(mut score: ResMut<Score>) {
5353
score.0 += 1;
5454
}
5555
56-
fn report_score(score: Res<Score>){
57-
info!(score);
56+
fn report_score(score: Res<Score>) {
57+
info!("{}", score.0);
5858
}
5959
```
6060

@@ -69,8 +69,10 @@ Bevy's [`DefaultPlugins`] is a [`PluginGroup`] that adds the "core engine featur
6969

7070
You can add [`DefaultPlugins`] to your app like this:
7171

72-
```rust
73-
App::new().add_plugins(DefaultPlugins)
72+
```no_run,hide-lines=1-2,rust
73+
use bevy::prelude::*;
74+
75+
App::new().add_plugins(DefaultPlugins);
7476
```
7577

7678
Take a look at the [source](https://github.com/bevyengine/bevy/blob/latest/crates/bevy_internal/src/default_plugins.rs) to see a full list of what's included.

content/learn/book/welcome/setup/_index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Now run `cargo run` to build and run your project. You should see `Hello, world!
5959

6060
`main.rs` is the entry point of your program:
6161

62-
```rs
62+
```rust
6363
fn main() {
6464
println!("Hello, world!");
6565
}
@@ -102,11 +102,11 @@ Now that we have our Bevy project set up, we're ready to start making our first
102102

103103
Within `main.rs`, let's create our first app and check that all the dependencies are working correctly!
104104

105-
```rust
105+
```no_run,rust
106106
use bevy::prelude::*;
107107
108108
fn main(){
109-
App::build().add_plugins(DefaultPlugins).run();
109+
App::new().add_plugins(DefaultPlugins).run();
110110
}
111111
```
112112

@@ -180,7 +180,7 @@ Take a look at these known problems and how to solve them.
180180

181181
### Unable to find a GPU
182182

183-
```
183+
```ignore
184184
thread 'main' panicked at 'Unable to find a GPU! Make sure you have installed required drivers!'
185185
```
186186

0 commit comments

Comments
 (0)