Skip to content

Commit a5ea437

Browse files
authored
feat: separate ladfile into ladfile_builder and ladfile crates (#293)
* chore: separate ladfile into two builder and serde crates * fix preprocessor * make integration test output markdown files and check them * make source paths map to actual md files * add ladfile_builder to release-plz
1 parent f7a22ab commit a5ea437

File tree

17 files changed

+755
-591
lines changed

17 files changed

+755
-591
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ members = [
9191
"crates/bevy_mod_scripting_derive",
9292
"crates/ladfile",
9393
"crates/lad_backends/mdbook_lad_preprocessor",
94+
"crates/ladfile_builder",
9495
]
9596
resolver = "2"
9697
exclude = ["crates/bevy_api_gen", "crates/macro_tests"]

crates/lad_backends/mdbook_lad_preprocessor/src/sections.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,20 @@ pub(crate) fn section_to_chapter(
5050

5151
if let Some(original) = original_chapter {
5252
// override content only
53+
log::debug!(
54+
"Setting .md extension for chapter paths: {:?}, {:?}.",
55+
original.path,
56+
original.source_path
57+
);
58+
5359
Chapter {
5460
content: parent_builder.build(),
5561
sub_items: children_chapters,
62+
path: original.path.as_ref().map(|p| p.with_extension("md")),
63+
source_path: original
64+
.source_path
65+
.as_ref()
66+
.map(|p| p.with_extension("md")),
5667
..original.clone()
5768
}
5869
} else {
@@ -119,7 +130,7 @@ impl Section<'_> {
119130
}
120131

121132
pub(crate) fn file_name(&self) -> String {
122-
self.title().to_lowercase().replace(" ", "_")
133+
self.title().to_lowercase().replace(" ", "_") + ".md"
123134
}
124135

125136
pub(crate) fn section_items(&self) -> Vec<SectionItem> {

crates/lad_backends/mdbook_lad_preprocessor/tests/book_integration_tests.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ fn copy_ladfile_to_book_dir(book_dir: &std::path::Path, ladfile: &str) {
3737
std::fs::copy(ladfile_path, book_ladfile_path).expect("failed to copy LAD file");
3838
}
3939

40+
fn all_files_in_dir_recursive(dir: &std::path::Path) -> Vec<std::path::PathBuf> {
41+
let mut files = vec![];
42+
for entry in std::fs::read_dir(dir).expect("failed to read dir") {
43+
let entry = entry.expect("failed to get entry");
44+
let path = entry.path();
45+
if path.is_dir() {
46+
files.extend(all_files_in_dir_recursive(&path));
47+
} else {
48+
files.push(path);
49+
}
50+
}
51+
files
52+
}
53+
4054
#[test]
4155
fn test_on_example_ladfile() {
4256
// invoke mdbook build
@@ -47,14 +61,35 @@ fn test_on_example_ladfile() {
4761
let books_dir = get_books_dir();
4862
let book = "example_ladfile";
4963

50-
let ladfile_path = "../../../../ladfile/test_assets/test.lad.json";
64+
let ladfile_path = "../../../../ladfile_builder/test_assets/test.lad.json";
5165

5266
copy_ladfile_to_book_dir(&books_dir.join(book), ladfile_path);
5367

5468
Command::new("mdbook")
55-
.env("RUST_LOG", "debug")
69+
.env("RUST_LOG", "trace")
5670
.current_dir(books_dir.join(book))
5771
.arg("build")
5872
.assert()
5973
.success();
74+
75+
// compare the sub directories (expected vs book in the book dir), existing files in expected must have a corresponding, identical file in the book dir with the same path
76+
let expected_dir = books_dir.join(book).join("expected");
77+
let book_dir = books_dir.join(book).join("book");
78+
79+
let expected_files = all_files_in_dir_recursive(&expected_dir);
80+
let book_files = all_files_in_dir_recursive(&book_dir);
81+
82+
for expected_file in expected_files {
83+
let relative_path = expected_file.strip_prefix(&expected_dir).unwrap();
84+
let book_file = book_dir.join(relative_path);
85+
assert!(
86+
book_files.contains(&book_file),
87+
"File not found: {:?}",
88+
book_file
89+
);
90+
let expected_content =
91+
std::fs::read_to_string(&expected_file).expect("failed to read file");
92+
let book_content = std::fs::read_to_string(&book_file).expect("failed to read file");
93+
pretty_assertions::assert_eq!(expected_content, book_content);
94+
}
6095
}

crates/lad_backends/mdbook_lad_preprocessor/tests/books/example_ladfile/book.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ description = "Documentation for the Bevy Scripting library"
88

99

1010
[preprocessor.lad-preprocessor]
11+
12+
13+
[output.markdown]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# EnumType
2+
3+
### Unit
4+
5+
### Struct
6+
7+
- **field** : usize
8+
9+
### TupleStruct
10+
11+
1. usize
12+
2. String
13+
14+
## Description
15+
16+
> None available\. 🚧
17+
18+
## Functions
19+
20+
| Function | Summary |
21+
| --- | --- |
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# StructType
2+
3+
### StructType
4+
5+
- **field** : usize
6+
- **field2** : usize
7+
8+
## Description
9+
10+
> I am a struct
11+
12+
## Functions
13+
14+
| Function | Summary |
15+
| --- | --- |
16+
| `hello_world(ref_, tuple, option_vec_ref_wrapper)` | [No documentation available\. 🚧](#helloworld) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# TupleStructType
2+
3+
### TupleStructType
4+
5+
1. usize
6+
2. String
7+
8+
## Description
9+
10+
> I am a tuple test type
11+
12+
## Functions
13+
14+
| Function | Summary |
15+
| --- | --- |
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# UnitType
2+
3+
### UnitType
4+
5+
6+
7+
## Description
8+
9+
> I am a unit test type
10+
11+
## Functions
12+
13+
| Function | Summary |
14+
| --- | --- |
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Nested Lad
2+
3+
## Globals
4+
5+
| Instance | Type |
6+
| --- | --- |
7+
| `my_static_instance` | ladfile\_builder::test::StructType<usize> |
8+
| `my_non_static_instance` | ladfile\_builder::test::UnitType |
9+
10+
## Types
11+
12+
| Type | Summary |
13+
| --- | --- |
14+
| `EnumType` | [No documentation available\. 🚧](#enumtype) |
15+
| `StructType` | [I am a struct](#structtype) |
16+
| `TupleStructType` | [I am a tuple test type](#tuplestructtype) |
17+
| `UnitType` | [I am a unit test type](#unittype) |

crates/ladfile/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ include = ["readme.md", "/src", "/test_assets"]
1313
readme = "readme.md"
1414

1515
[dependencies]
16-
bevy_mod_scripting_core = { workspace = true }
17-
# I don't think bevy has a top level feature for this :C
18-
bevy_reflect = { version = "0.15.2", features = ["documentation"] }
1916
serde = { version = "1.0", features = ["derive"] }
2017
serde_json = "1.0"
2118
indexmap = { version = "2.7", features = ["serde"] }

0 commit comments

Comments
 (0)