Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [`getrandom`](cli/web/getrandom.md)
- [Default `index.html`](cli/web/default-index-html.md)
- [Linter](cli/linter.md)
- [Running Examples](cli/examples.md)
- [Configuration](cli/configuration.md)
- [Configuration Reference](cli/configuration/reference.md)
- [Troubleshooting](cli/troubleshooting.md)
Expand Down
24 changes: 24 additions & 0 deletions docs/src/cli/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Examples

The CLI makes it easy to build, run and lint examples by automatically enabling required features of the example.

## Build and run a specific example

```sh
# Run the `web_asset` example from https://github.com/bevyengine/bevy that requires the feature `https`.
bevy run --example web_asset
info: enabling required_features: ["https"], for example: web_asset

# Build the `web_asset` example in the web.
bevy build --example web_asset web
info: enabling required_features: ["https"], for example: web_asset
```

## Build all examples

The CLI supports building all examples only for the native target, for the web this is not supported yet.
When building all examples, the CLI will build with `--all-features`.

```sh
bevy build --examples
```
1 change: 1 addition & 0 deletions docs/src/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The CLI is a prototype tool intended to streamline common tasks when working on
- [**Project Scaffolding**](scaffolding.md)
- [**Build and running Bevy Web Apps**](web.md)
- [**Linter Integration**](linter.md)
- [**Enabling required features for examples**](examples.md)

[initial scope document]: https://hackmd.io/cCHAfbtaSviU_MDnbNHKxg
[original issue]: https://github.com/bevyengine/bevy/issues/436
Expand Down
35 changes: 35 additions & 0 deletions src/commands/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Provides functionalities to build a Bevy app targeting either native or web platforms.

pub use args::*;
use cargo_metadata::TargetKind;
use tracing::info;

#[cfg(feature = "web")]
use crate::web::build::build_web;
Expand Down Expand Up @@ -47,6 +49,39 @@ pub fn build(args: &mut BuildArgs) -> anyhow::Result<()> {
args.cargo_args.target_args.example.is_some(),
);

// If a specific example was passed, extend the already present features with the
// required_features from this example.
if let Some(example) = &args.cargo_args.target_args.example
// Search in the current workspace packages for an `example` target that matches the given
// example name.
&& let Some(example_target) = metadata
.workspace_packages()
.iter()
.flat_map(|p| p.targets.clone())
.find(|t| t.name.as_str() == example && t.kind.contains(&TargetKind::Example))
{
let required_features = example_target.required_features;

info!(
"enabling required_features: {:?}, for example: {example}",
required_features
);

args.cargo_args
.feature_args
.features
.extend(required_features);
}
// build `--examples` with all features enabled.
else if args.cargo_args.target_args.is_examples {
args.cargo_args
.feature_args
.features
.push("--all-features".to_owned());

info!("automatically added `--all-features` to build examples with all features enabled");
}

#[cfg(feature = "web")]
if args.is_web() {
build_web(args, &metadata, &bin_target)?;
Expand Down
35 changes: 34 additions & 1 deletion src/commands/lint/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub use args::*;
use tracing::error;
use tracing::{error, info};

#[cfg(feature = "rustup")]
use crate::commands::lint::install::install_linter;
Expand Down Expand Up @@ -155,6 +155,39 @@ fn build_lint_cmd(args: &mut LintArgs) -> anyhow::Result<CommandExt> {
}
}

// If a specific example was passed, extend the already present features with the
// required_features from this example.
if let Some(example) = &args.cargo_args.target_args.example
// Search in the current workspace packages for an `example` target that matches the given
// example name.
&& let Some(example_target) = metadata
.workspace_packages()
.iter()
.flat_map(|p| p.targets.clone())
.find(|t| t.name.as_str() == example && t.kind.contains(&cargo_metadata::TargetKind::Example))
{
let required_features = example_target.required_features;

info!(
"enabling required_features: {:?}, for example: {example}",
required_features
);

args.cargo_args
.feature_args
.features
.extend(required_features);
}
// build `--examples` with all features enabled.
else if args.cargo_args.target_args.is_examples {
args.cargo_args
.feature_args
.features
.push("--all-features".to_owned());

info!("automatically added `--all-features` to check examples with all features enabled");
}

let cargo_args = args.cargo_args_builder();

cmd.args(cargo_args)
Expand Down
25 changes: 25 additions & 0 deletions src/commands/run/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Provides functionalities to run a Bevy app targeting either native or web platforms.

use tracing::info;

pub use self::args::*;
#[cfg(feature = "web")]
use crate::web::run::run_web;
Expand Down Expand Up @@ -45,6 +47,29 @@ pub fn run(args: &mut RunArgs) -> anyhow::Result<()> {
args.cargo_args.target_args.example.is_some(),
);

// Extend the already present features with the required_features from this example.
if let Some(example) = &args.cargo_args.target_args.example
// Search in the current workspace packages for an `example` target that matches the given
// example name.
&& let Some(example_target) = metadata
.workspace_packages()
.iter()
.flat_map(|p| p.targets.clone())
.find(|t| t.name.as_str() == example && t.kind.contains(&cargo_metadata::TargetKind::Example))
{
let required_features = example_target.required_features;

info!(
"enabling required_features: {:?}, for example: {example}",
required_features
);

args.cargo_args
.feature_args
.features
.extend(required_features);
}

#[cfg(feature = "web")]
if args.is_web() {
return run_web(args, &metadata, &bin_target);
Expand Down