Skip to content
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
feature-set: [ default, inventory, compat_generics_angles ]
feature-set: [ default, inventory, compat_generics_angles, default_plugin ]
steps:
- name: Checkout code
uses: actions/checkout@v3
Expand Down Expand Up @@ -85,7 +85,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
feature-set: [ default, inventory ]
feature-set: [ default, inventory, default_plugin ]
steps:
- name: Checkout code
uses: actions/checkout@v3
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,5 @@
- Allow action macros on `use` statements; each imported name becomes its own entry (no `*`, `self`, or `_`).
- Add `pipe_in` for `auto_add_system`/`auto_system` to build system pipelines.
- Accept `generics = <...>` as shorthand for `generics(...)` in attributes (feature: `compat_generics_angles`).
- This feature will eventually be removed unless a petition is opened.
- This feature will eventually be removed unless a petition is opened.
- Add optional `default_plugin` feature to allow omitting `plugin = ...` when a default plugin is set via `#[auto_plugin(default_plugin)]`.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ compat_generics_angles = [
"bevy_auto_plugin_proc_macros/compat_generics_angles",
"bevy_auto_plugin_shared/compat_generics_angles",
]
default_plugin = [
"bevy_auto_plugin_proc_macros/default_plugin",
"bevy_auto_plugin_shared/default_plugin",
]
web = [
"bevy_auto_plugin_shared/web",
]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ fn plugin(app: &mut App) {
```

There is `auto_plugin` arguments if your plugin has generics.
Optional: enable feature `default_plugin` and add `#[auto_plugin(default_plugin)]` to allow `auto_*` macros to omit `plugin = ...`.

See [tests](tests/e2e) for other examples

Expand Down
1 change: 1 addition & 0 deletions crates/bevy_auto_plugin_proc_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ inventory = ["bevy_auto_plugin_shared/inventory"]
debug_log_plugin_registry = ["bevy_auto_plugin_shared/debug_log_plugin_registry"]
log_plugin_build = ["bevy_auto_plugin_shared/log_plugin_build"]
compat_generics_angles = ["bevy_auto_plugin_shared/compat_generics_angles"]
default_plugin = ["bevy_auto_plugin_shared/default_plugin"]

[dependencies]
bevy_auto_plugin_shared = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_auto_plugin_shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ inventory = []
debug_log_plugin_registry = ["log"]
log_plugin_build = ["log"]
compat_generics_angles = []
default_plugin = []
web = [
"bevy_app/web"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ pub fn expand_derive_auto_plugin(input: MacroStream) -> MacroStream {
});
}

#[cfg(feature = "default_plugin")]
if params.auto_plugin.default_plugin.is_present() {
if !params.generics.params.is_empty() {
compile_warnings.extend(
syn::Error::new(
params.auto_plugin.default_plugin.span(),
"`default_plugin` is not supported for generic plugins; use a concrete wrapper type",
)
.to_compile_error(),
);
} else {
output.extend(quote! {
#[doc(hidden)]
#[allow(dead_code)]
type __bevy_auto_plugin_default_plugin = #ident;
});
}
}

quote! {
#compile_warnings
#output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct AutoPluginStructOrEnumArgs {
#[darling(multiple)]
pub generics: Vec<TypeList>,
pub impl_plugin_trait: Flag,
#[cfg(feature = "default_plugin")]
pub default_plugin: Flag,
#[deprecated(
since = "0.8.0",
note = "always implemented - remove `impl_generic_auto_plugin_trait`"
Expand Down
14 changes: 14 additions & 0 deletions crates/bevy_auto_plugin_shared/src/macro_api/composed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ where
let generics_keys: HashSet<&str> = MGenerics::keys().iter().copied().collect();

let mut plugin_bucket = Vec::<NestedMeta>::new();
#[cfg(feature = "default_plugin")]
let mut has_plugin_key = false;
let mut generics_bucket = Vec::<NestedMeta>::new();
let mut base_bucket = Vec::<NestedMeta>::new();

Expand All @@ -73,6 +75,10 @@ where
};

let routed = if let Some(ref k) = key_opt {
#[cfg(feature = "default_plugin")]
if k == "plugin" {
has_plugin_key = true;
}
if plugin_keys.contains(k.as_str()) {
plugin_bucket.push(nm.clone());
true
Expand All @@ -91,6 +97,14 @@ where
}
}

#[cfg(feature = "default_plugin")]
if !has_plugin_key && plugin_keys.contains("plugin") {
plugin_bucket.insert(
0,
NestedMeta::Meta(parse_quote!(plugin = __bevy_auto_plugin_default_plugin)),
);
}

// Parse each bucket
let base = CBase::from_list(&base_bucket)?;
let plugin = MPlugin::from_list(&plugin_bucket)?;
Expand Down
3 changes: 2 additions & 1 deletion docs/derives/AutoPlugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ events, resources, and systems.

# Parameters
- `impl_plugin_trait` - Optional. When present, it automatically implements the Plugin trait.
- `default_plugin` - Optional (feature: `default_plugin`). Emits a default plugin alias so `auto_*` macros can omit `plugin = ...`.

# Example
```rust
Expand All @@ -15,4 +16,4 @@ struct MyPlugin;

// Plugin will automatically implement the Plugin trait
// and include all registered components, events, resources, etc.
```
```
31 changes: 31 additions & 0 deletions tests/e2e/actions/auto_default_plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use bevy_app::prelude::*;
use bevy_auto_plugin::prelude::*;
use bevy_ecs::prelude::*;
use bevy_reflect::prelude::*;
use internal_test_proc_macro::xtest;
use internal_test_util::type_id_of;

#[derive(AutoPlugin)]
#[auto_plugin(impl_plugin_trait, default_plugin)]
struct DefaultPlugin;

#[auto_register_type]
#[derive(Reflect)]
struct DefaultedType;

fn app() -> App {
let mut app = internal_test_util::create_minimal_app();
app.add_plugins(DefaultPlugin);
app
}

#[xtest]
fn test_auto_default_plugin_register_type() {
let app = app();
let type_registry = app.world().resource::<AppTypeRegistry>().0.clone();
let type_registry = type_registry.read();
assert!(
type_registry.contains(type_id_of::<DefaultedType>()),
"did not auto register DefaultedType"
);
}
2 changes: 2 additions & 0 deletions tests/e2e/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ mod auto_bind_plugin_after_build;
mod auto_configure_system_set;
mod auto_configure_system_set_schedule_config;
mod auto_configure_system_set_schedule_config_multiple_groups;
#[cfg(feature = "default_plugin")]
mod auto_default_plugin;
mod auto_init_resource;
mod auto_init_resource_generic;
mod auto_init_state;
Expand Down
Loading