From f8014e0d01e9a48a94e5f7e5e38f33f4c873653b Mon Sep 17 00:00:00 2001 From: AndrewDanial <110853278+AndrewDanial@users.noreply.github.com> Date: Tue, 18 Jun 2024 23:23:02 -0400 Subject: [PATCH] Shader code paths (#13908) # Objective Add extra metadata for the shader examples that contains the location of their associated shader file(s). This is to be used for the bevy website shader examples so that the shader code is underneath the rust code. ## Solution Parse the example rust files for mentions of `.wgsl`, `.frag`, and `.vert`, then append the found paths to a field called `shader_code_paths` in the generated `index.md`s for each shader example. --- tools/example-showcase/Cargo.toml | 1 + tools/example-showcase/src/main.rs | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/tools/example-showcase/Cargo.toml b/tools/example-showcase/Cargo.toml index 262df1e44b..83ac5ef592 100644 --- a/tools/example-showcase/Cargo.toml +++ b/tools/example-showcase/Cargo.toml @@ -13,6 +13,7 @@ toml_edit = { version = "0.22.7", default-features = false, features = [ "parse", ] } pbr = "1.1" +regex = "1.10.5" [lints] workspace = true diff --git a/tools/example-showcase/src/main.rs b/tools/example-showcase/src/main.rs index dbdbbec64d..484cbe4a6f 100644 --- a/tools/example-showcase/src/main.rs +++ b/tools/example-showcase/src/main.rs @@ -1,7 +1,7 @@ //! Tool to run all examples or generate a showcase page for the Bevy website. use std::{ - collections::{hash_map::DefaultHasher, HashMap}, + collections::{hash_map::DefaultHasher, HashMap, HashSet}, fmt::Display, fs::{self, File}, hash::{Hash, Hasher}, @@ -14,6 +14,7 @@ use std::{ use clap::{error::ErrorKind, CommandFactory, Parser, ValueEnum}; use pbr::ProgressBar; +use regex::Regex; use toml_edit::DocumentMut; use xshell::{cmd, Shell}; @@ -546,6 +547,7 @@ technical_name = \"{}\" link = \"/examples{}/{}/{}\" image = \"../static/screenshots/{}/{}.png\" code_path = \"content/examples{}/{}\" +shader_code_paths = {:?} github_code_path = \"{}\" header_message = \"Examples ({})\" +++", @@ -580,6 +582,7 @@ header_message = \"Examples ({})\" .skip(1) .collect::() .display(), + to_show.shader_paths, &to_show.path, match api { WebApi::Webgpu => "WebGPU", @@ -723,6 +726,18 @@ fn parse_examples() -> Vec { .flat_map(|val| { let technical_name = val.get("name").unwrap().as_str().unwrap().to_string(); + let source_code = fs::read_to_string(val["path"].as_str().unwrap()).unwrap(); + let shader_regex = + Regex::new(r"(shaders\/\w+\.wgsl)|(shaders\/\w+\.frag)|(shaders\/\w+\.vert)") + .unwrap(); + + // Find all instances of references to shader files, collect into set to avoid duplicates, then convert to vec of strings. + let shader_paths = Vec::from_iter( + shader_regex + .find_iter(&source_code) + .map(|matches| matches.as_str().to_owned()) + .collect::>(), + ); if metadatas .get(&technical_name) .and_then(|metadata| metadata.get("hidden")) @@ -736,6 +751,7 @@ fn parse_examples() -> Vec { metadatas.get(&technical_name).map(|metadata| Example { technical_name, path: val["path"].as_str().unwrap().to_string(), + shader_paths, name: metadata["name"].as_str().unwrap().to_string(), description: metadata["description"].as_str().unwrap().to_string(), category: metadata["category"].as_str().unwrap().to_string(), @@ -780,9 +796,10 @@ struct Example { technical_name: String, /// Path to the example file path: String, + /// Path to the associated wgsl file if it exists + shader_paths: Vec, /// List of non default required features required_features: Vec, - // From the example metadata /// Pretty name, used for display name: String,