Skip to content

Commit

Permalink
feat: separate ladfile into ladfile_builder and ladfile crates (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
makspll authored Feb 16, 2025
1 parent f7a22ab commit a5ea437
Show file tree
Hide file tree
Showing 17 changed files with 755 additions and 591 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ members = [
"crates/bevy_mod_scripting_derive",
"crates/ladfile",
"crates/lad_backends/mdbook_lad_preprocessor",
"crates/ladfile_builder",
]
resolver = "2"
exclude = ["crates/bevy_api_gen", "crates/macro_tests"]
Expand Down
13 changes: 12 additions & 1 deletion crates/lad_backends/mdbook_lad_preprocessor/src/sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,20 @@ pub(crate) fn section_to_chapter(

if let Some(original) = original_chapter {
// override content only
log::debug!(
"Setting .md extension for chapter paths: {:?}, {:?}.",
original.path,
original.source_path
);

Chapter {
content: parent_builder.build(),
sub_items: children_chapters,
path: original.path.as_ref().map(|p| p.with_extension("md")),
source_path: original
.source_path
.as_ref()
.map(|p| p.with_extension("md")),
..original.clone()
}
} else {
Expand Down Expand Up @@ -119,7 +130,7 @@ impl Section<'_> {
}

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

pub(crate) fn section_items(&self) -> Vec<SectionItem> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ fn copy_ladfile_to_book_dir(book_dir: &std::path::Path, ladfile: &str) {
std::fs::copy(ladfile_path, book_ladfile_path).expect("failed to copy LAD file");
}

fn all_files_in_dir_recursive(dir: &std::path::Path) -> Vec<std::path::PathBuf> {
let mut files = vec![];
for entry in std::fs::read_dir(dir).expect("failed to read dir") {
let entry = entry.expect("failed to get entry");
let path = entry.path();
if path.is_dir() {
files.extend(all_files_in_dir_recursive(&path));
} else {
files.push(path);
}
}
files
}

#[test]
fn test_on_example_ladfile() {
// invoke mdbook build
Expand All @@ -47,14 +61,35 @@ fn test_on_example_ladfile() {
let books_dir = get_books_dir();
let book = "example_ladfile";

let ladfile_path = "../../../../ladfile/test_assets/test.lad.json";
let ladfile_path = "../../../../ladfile_builder/test_assets/test.lad.json";

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

Command::new("mdbook")
.env("RUST_LOG", "debug")
.env("RUST_LOG", "trace")
.current_dir(books_dir.join(book))
.arg("build")
.assert()
.success();

// 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
let expected_dir = books_dir.join(book).join("expected");
let book_dir = books_dir.join(book).join("book");

let expected_files = all_files_in_dir_recursive(&expected_dir);
let book_files = all_files_in_dir_recursive(&book_dir);

for expected_file in expected_files {
let relative_path = expected_file.strip_prefix(&expected_dir).unwrap();
let book_file = book_dir.join(relative_path);
assert!(
book_files.contains(&book_file),
"File not found: {:?}",
book_file
);
let expected_content =
std::fs::read_to_string(&expected_file).expect("failed to read file");
let book_content = std::fs::read_to_string(&book_file).expect("failed to read file");
pretty_assertions::assert_eq!(expected_content, book_content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ description = "Documentation for the Bevy Scripting library"


[preprocessor.lad-preprocessor]


[output.markdown]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# EnumType

### Unit

### Struct

- **field** : usize

### TupleStruct

1. usize
2. String

## Description

> None available\. 🚧
## Functions

| Function | Summary |
| --- | --- |
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# StructType

### StructType

- **field** : usize
- **field2** : usize

## Description

> I am a struct
## Functions

| Function | Summary |
| --- | --- |
| `hello_world(ref_, tuple, option_vec_ref_wrapper)` | [No documentation available\. 🚧](#helloworld) |
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# TupleStructType

### TupleStructType

1. usize
2. String

## Description

> I am a tuple test type
## Functions

| Function | Summary |
| --- | --- |
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# UnitType

### UnitType



## Description

> I am a unit test type
## Functions

| Function | Summary |
| --- | --- |
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Nested Lad

## Globals

| Instance | Type |
| --- | --- |
| `my_static_instance` | ladfile\_builder::test::StructType<usize> |
| `my_non_static_instance` | ladfile\_builder::test::UnitType |

## Types

| Type | Summary |
| --- | --- |
| `EnumType` | [No documentation available\. 🚧](#enumtype) |
| `StructType` | [I am a struct](#structtype) |
| `TupleStructType` | [I am a tuple test type](#tuplestructtype) |
| `UnitType` | [I am a unit test type](#unittype) |
3 changes: 0 additions & 3 deletions crates/ladfile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ include = ["readme.md", "/src", "/test_assets"]
readme = "readme.md"

[dependencies]
bevy_mod_scripting_core = { workspace = true }
# I don't think bevy has a top level feature for this :C
bevy_reflect = { version = "0.15.2", features = ["documentation"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
indexmap = { version = "2.7", features = ["serde"] }
Expand Down
Loading

0 comments on commit a5ea437

Please sign in to comment.