From c2a94c0f6a0f5b1e95eae91fffa218571d7fd2d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sun, 8 May 2022 19:45:00 +0200 Subject: [PATCH] helper tool to build examples in wasm --- Cargo.toml | 2 +- examples/README.md | 8 +++++++- tools/build-wasm-example/Cargo.toml | 11 +++++++++++ tools/build-wasm-example/src/main.rs | 18 ++++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tools/build-wasm-example/Cargo.toml create mode 100644 tools/build-wasm-example/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 035209b68de8b..4dfe5fc8c7119 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ repository = "https://github.com/bevyengine/bevy" [workspace] exclude = ["benches", "crates/bevy_ecs_compile_fail_tests"] -members = ["crates/*", "examples/ios", "tools/ci", "tools/spancmp", "errors"] +members = ["crates/*", "examples/ios", "tools/ci", "tools/spancmp", "tools/build-wasm-example", "errors"] [features] default = [ diff --git a/examples/README.md b/examples/README.md index 7e69d518582c9..ccc044792134e 100644 --- a/examples/README.md +++ b/examples/README.md @@ -422,7 +422,13 @@ cargo install wasm-bindgen-cli ### Build & Run Following is an example for `lighting`. For other examples, change the `lighting` in the -following commands. +following command. + +```sh +cargo run -p build-wasm-example -- lighting +``` + +This is the same as running ```sh cargo build --release --example lighting --target wasm32-unknown-unknown diff --git a/tools/build-wasm-example/Cargo.toml b/tools/build-wasm-example/Cargo.toml new file mode 100644 index 0000000000000..fdd3199a7fe6e --- /dev/null +++ b/tools/build-wasm-example/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "build-wasm-example" +version = "0.1.0" +edition = "2021" +description = "Build an example for wasm" +publish = false +license = "MIT OR Apache-2.0" +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +xshell = "0.2" diff --git a/tools/build-wasm-example/src/main.rs b/tools/build-wasm-example/src/main.rs new file mode 100644 index 0000000000000..39d43e15a6250 --- /dev/null +++ b/tools/build-wasm-example/src/main.rs @@ -0,0 +1,18 @@ +use xshell::{cmd, Shell}; + +fn main() { + let example = std::env::args().nth(1).expect("abbb"); + let sh = Shell::new().unwrap(); + cmd!( + sh, + "cargo build --release --target wasm32-unknown-unknown --example {example}" + ) + .run() + .expect("Error building example"); + cmd!( + sh, + "wasm-bindgen --out-dir examples/wasm/target --out-name wasm_example --target web target/wasm32-unknown-unknown/release/examples/{example}.wasm" + ) + .run() + .expect("Error creating wasm binding"); +}