bevy/tools/build-wasm-example/src/main.rs
Lura 856b39d821
Apply Clippy lints regarding lazy evaluation and closures (#14015)
# Objective

- Lazily evaluate
[default](https://rust-lang.github.io/rust-clippy/master/index.html#/unwrap_or_default)~~/[or](https://rust-lang.github.io/rust-clippy/master/index.html#/or_fun_call)~~
values where it makes sense
  - ~~`unwrap_or(foo())` -> `unwrap_or_else(|| foo())`~~
  - `unwrap_or(Default::default())` -> `unwrap_or_default()`
  - etc.
- Avoid creating [redundant
closures](https://rust-lang.github.io/rust-clippy/master/index.html#/redundant_closure),
even for [method
calls](https://rust-lang.github.io/rust-clippy/master/index.html#/redundant_closure_for_method_calls)
  - `map(|something| something.into())` -> `map(Into:into)`

## Solution

- Apply Clippy lints:
-
~~[or_fun_call](https://rust-lang.github.io/rust-clippy/master/index.html#/or_fun_call)~~
-
[unwrap_or_default](https://rust-lang.github.io/rust-clippy/master/index.html#/unwrap_or_default)
-
[redundant_closure_for_method_calls](https://rust-lang.github.io/rust-clippy/master/index.html#/redundant_closure_for_method_calls)
([redundant
closures](https://rust-lang.github.io/rust-clippy/master/index.html#/redundant_closure)
is already enabled)

## Testing

- Tested on Windows 11 (`stable-x86_64-pc-windows-gnu`, 1.79.0)
- Bevy compiles without errors or warnings and examples seem to work as
intended
  - `cargo clippy` 
  - `cargo run -p ci -- compile` 

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2024-07-01 15:54:40 +00:00

106 lines
3.1 KiB
Rust

//! Tool used to build Bevy examples for wasm.
use std::{fs::File, io::Write};
use clap::{Parser, ValueEnum};
use xshell::{cmd, Shell};
#[derive(Debug, Copy, Clone, ValueEnum)]
enum WebApi {
Webgl2,
Webgpu,
}
#[derive(Parser, Debug)]
struct Args {
/// Examples to build
examples: Vec<String>,
#[arg(short, long)]
/// Run tests
test: bool,
#[arg(short, long)]
/// Run on the given browsers. By default, chromium, firefox, webkit
browsers: Vec<String>,
#[arg(short, long)]
/// Stop after this number of frames
frames: Option<usize>,
#[arg(value_enum, short, long, default_value_t = WebApi::Webgl2)]
/// Browser API to use for rendering
api: WebApi,
#[arg(short, long)]
/// Optimize the wasm file for size with wasm-opt
optimize_size: bool,
#[arg(long)]
/// Additional features to enable
features: Vec<String>,
}
fn main() {
let cli = Args::parse();
assert!(!cli.examples.is_empty(), "must have at least one example");
let default_features = true;
let mut features: Vec<&str> = cli.features.iter().map(String::as_str).collect();
if let Some(frames) = cli.frames {
let mut file = File::create("ci_testing_config.ron").unwrap();
file.write_fmt(format_args!("(events: [({frames}, AppExit)])"))
.unwrap();
features.push("bevy_ci_testing");
}
match cli.api {
WebApi::Webgl2 => (),
WebApi::Webgpu => {
features.push("webgpu");
}
}
for example in cli.examples {
let sh = Shell::new().unwrap();
let features_string = features.join(",");
let mut parameters = vec![];
if !default_features {
parameters.push("--no-default-features");
}
if !features.is_empty() {
parameters.push("--features");
parameters.push(&features_string);
}
let cmd = cmd!(
sh,
"cargo build {parameters...} --profile release --target wasm32-unknown-unknown --example {example}"
);
cmd.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");
if cli.optimize_size {
cmd!(sh, "wasm-opt -Oz --output examples/wasm/target/wasm_example_bg.wasm.optimized examples/wasm/target/wasm_example_bg.wasm")
.run().expect("Failed to optimize for size. Do you have wasm-opt correctly set up?");
}
if cli.test {
let _dir = sh.push_dir(".github/start-wasm-example");
let mut browsers = cli.browsers.clone();
if !browsers.is_empty() {
browsers.insert(0, "--project".to_string());
}
cmd!(sh, "npx playwright test --headed {browsers...}")
.env("SCREENSHOT_PREFIX", format!("screenshot-{example}"))
.run()
.expect("Error running playwright test");
}
}
}