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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ internal_test_util = { path = "crates/internal_test_util" }
internal_test_proc_macro = { path = "crates/internal_test_proc_macro" }
bevy = { version = "0.19", default-features = false, features = ["bevy_state"] }
bevy_app = { version = "0.19", default-features = false }
bevy_asset = { version = "0.19", default-features = false }
bevy_state = { version = "0.19", default-features = false }
bevy_reflect = { version = "0.19", default-features = false }
bevy_reflect_derive = { version = "0.19", default-features = false }
Expand Down Expand Up @@ -76,6 +77,7 @@ bevy_auto_plugin_shared = { workspace = true }
# required for doc tests
bevy = { workspace = true }
bevy_app = { workspace = true }
bevy_asset = { workspace = true }
bevy_reflect = { workspace = true }
bevy_reflect_derive = { workspace = true }
bevy_ecs = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_auto_plugin_proc_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ proc-macro2 = { workspace = true }
# needed for doc-tests
bevy = { workspace = true }
bevy_app = { workspace = true }
bevy_asset = { workspace = true }
bevy_auto_plugin = { path = "../../.", default-features = false }
bevy_reflect = { workspace = true }
18 changes: 18 additions & 0 deletions crates/bevy_auto_plugin_proc_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub fn auto_register_type(attr: CompilerStream, input: CompilerStream) -> Compil
handle_attribute(expand::attr::auto_register_type, attr, input)
}

#[doc = include_str!(concat!(env!("OUT_DIR"), "/docs/proc_attributes/actions/auto_register_asset_reflect.md"))]
#[proc_macro_attribute]
pub fn auto_register_asset_reflect(attr: CompilerStream, input: CompilerStream) -> CompilerStream {
handle_attribute(expand::attr::auto_register_asset_reflect, attr, input)
}

#[doc = include_str!(concat!(env!("OUT_DIR"), "/docs/proc_attributes/actions/auto_add_message.md"))]
#[proc_macro_attribute]
pub fn auto_add_message(attr: CompilerStream, input: CompilerStream) -> CompilerStream {
Expand All @@ -42,6 +48,12 @@ pub fn auto_configure_system_set(attr: CompilerStream, input: CompilerStream) ->
handle_attribute(expand::attr::auto_configure_system_set, attr, input)
}

#[doc = include_str!(concat!(env!("OUT_DIR"), "/docs/proc_attributes/actions/auto_init_asset.md"))]
#[proc_macro_attribute]
pub fn auto_init_asset(attr: CompilerStream, input: CompilerStream) -> CompilerStream {
handle_attribute(expand::attr::auto_init_asset, attr, input)
}

#[doc = include_str!(concat!(env!("OUT_DIR"), "/docs/proc_attributes/actions/auto_init_resource.md"))]
#[proc_macro_attribute]
pub fn auto_init_resource(attr: CompilerStream, input: CompilerStream) -> CompilerStream {
Expand Down Expand Up @@ -96,6 +108,12 @@ pub fn auto_add_plugin(attr: CompilerStream, input: CompilerStream) -> CompilerS
handle_attribute(expand::attr::auto_add_plugin, attr, input)
}

#[doc = include_str!(concat!(env!("OUT_DIR"), "/docs/proc_attributes/rewrites/auto_asset.md"))]
#[proc_macro_attribute]
pub fn auto_asset(attr: CompilerStream, input: CompilerStream) -> CompilerStream {
handle_attribute(expand::attr::auto_asset, attr, input)
}

#[doc = include_str!(concat!(env!("OUT_DIR"), "/docs/proc_attributes/rewrites/auto_component.md"))]
#[proc_macro_attribute]
pub fn auto_component(attr: CompilerStream, input: CompilerStream) -> CompilerStream {
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 @@ -47,6 +47,7 @@ wasm-bindgen-test = { workspace = true }
anyhow = { workspace = true }
# used in crate resolve tests
bevy_ecs = { workspace = true }
bevy_asset = { workspace = true }
bevy_reflect = { workspace = true }
bevy_state = { workspace = true }
bevy_auto_plugin = { path = "../../.", default-features = false }
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ macro_rules! gen_rewrite_outers {
gen_action_outers! {
auto_run_on_build => IaRunOnBuild,
auto_register_type => IaRegisterType,
auto_register_asset_reflect => IaRegisterAssetReflect,
auto_add_message => IaAddMessage,
auto_init_asset => IaInitAsset,
auto_init_resource => IaInitResource,
auto_insert_resource => IaInsertResource,
auto_init_state => IaInitState,
Expand All @@ -46,6 +48,7 @@ gen_action_outers! {
}

gen_rewrite_outers! {
auto_asset => IaAsset,
auto_component => IaComponent,
auto_resource => IaResource,
auto_system => IaSystem,
Expand Down
20 changes: 20 additions & 0 deletions crates/bevy_auto_plugin_shared/src/__private/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ pub(crate) mod paths {
}
}

pub mod asset {
use super::*;

pub fn resolve() -> Result<syn::Path, String> {
bevy_crate_path!(asset)
}

pub fn asset_root_path() -> syn::Path {
resolve().expect(bevy_crate_err_message!(asset))
}
}

pub mod reflect {
use super::*;

Expand Down Expand Up @@ -119,6 +131,14 @@ mod tests {
);
}

#[xtest]
fn test_crate_resolve_bevy_asset() {
assert_eq!(
map_resolve_crate(super::paths::asset::resolve()),
ok_bevy_crate_path_string!(asset)
);
}

#[xtest]
fn test_crate_resolve_bevy_state() {
assert_eq!(
Expand Down
36 changes: 36 additions & 0 deletions crates/bevy_auto_plugin_shared/src/codegen/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ pub fn derive_component_path() -> NonEmptyPath {
ecs_import!(prelude::Component)
}

pub fn derive_asset_path() -> NonEmptyPath {
let root = crate::__private::paths::asset::asset_root_path();
parse_quote!(#root::Asset)
}

pub fn derive_type_path_path() -> NonEmptyPath {
let root = crate::__private::paths::reflect::reflect_root_path();
parse_quote!(#root::TypePath)
}

pub fn derive_resource_path() -> NonEmptyPath {
ecs_import!(prelude::Resource)
}
Expand Down Expand Up @@ -98,6 +108,26 @@ pub fn derive_component<'a>(
.concat(),
)
}
pub fn derive_asset<'a>(extra_items: impl IntoIterator<Item = &'a NonEmptyPath>) -> TokenStream {
derive_from(
[
vec![&derive_asset_path(), &derive_type_path_path()],
extra_items.into_iter().collect::<Vec<_>>(),
]
.concat(),
)
}
pub fn derive_asset_without_type_path<'a>(
extra_items: impl IntoIterator<Item = &'a NonEmptyPath>,
) -> TokenStream {
derive_from(
[
vec![&derive_asset_path()],
extra_items.into_iter().collect::<Vec<_>>(),
]
.concat(),
)
}
pub fn derive_resource<'a>(extra_items: impl IntoIterator<Item = &'a NonEmptyPath>) -> TokenStream {
derive_from(
[
Expand Down Expand Up @@ -193,6 +223,9 @@ pub fn use_bevy_state_app_ext_states() -> syn::ItemUse {
pub fn auto_register_type(args: RegisterTypeAttrEmitter) -> TokenStream {
args.to_token_stream()
}
pub fn auto_register_asset_reflect(args: RegisterAssetReflectAttrEmitter) -> TokenStream {
args.to_token_stream()
}
pub fn auto_register_state_type(args: RegisterStateTypeAttrEmitter) -> TokenStream {
args.to_token_stream()
}
Expand All @@ -202,6 +235,9 @@ pub fn auto_name(args: NameAttrEmitter) -> TokenStream {
pub fn auto_init_resource(args: InitResourceAttrEmitter) -> TokenStream {
args.to_token_stream()
}
pub fn auto_init_asset(args: InitAssetAttrEmitter) -> TokenStream {
args.to_token_stream()
}
pub fn auto_insert_resource(args: InsertResourceAttrEmitter) -> TokenStream {
args.to_token_stream()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::macro_api::prelude::*;
use darling::FromMeta;
use proc_macro2::TokenStream;
use quote::{
ToTokens,
quote,
};

#[derive(FromMeta, Debug, Default, Clone, PartialEq, Hash)]
#[darling(derive_syn_parse, default)]
pub struct InitAssetArgs {}

impl AttributeIdent for InitAssetArgs {
const IDENT: &'static str = "auto_init_asset";
}

pub type IaInitAsset = ItemAttribute<
Composed<InitAssetArgs, WithPlugin, WithZeroOrManyGenerics>,
AllowStructOrEnumOrUse,
>;
pub type InitAssetAppMutEmitter = AppMutationEmitter<IaInitAsset>;
pub type InitAssetAttrEmitter = AttrEmitter<IaInitAsset>;

impl EmitAppMutationTokens for InitAssetAppMutEmitter {
fn to_app_mutation_tokens(
&self,
tokens: &mut TokenStream,
app_param: &syn::Ident,
) -> syn::Result<()> {
let bevy_asset = crate::__private::paths::asset::asset_root_path();
let concrete_paths = self.args.concrete_paths()?;
for concrete_path in concrete_paths {
tokens.extend(quote! {
#bevy_asset::AssetApp::init_asset::<#concrete_path>(#app_param);
});
}
Ok(())
}
}

impl ToTokens for InitAssetAttrEmitter {
fn to_tokens(&self, tokens: &mut TokenStream) {
let args = self.args.args.extra_args();
tokens.extend(quote! {
#(#args),*
});
*tokens = self.wrap_as_attr(tokens);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::macro_api::prelude::*;
use darling::FromMeta;
use proc_macro2::TokenStream;
use quote::{
ToTokens,
quote,
};

#[derive(FromMeta, Debug, Default, Clone, PartialEq, Hash)]
#[darling(derive_syn_parse, default)]
pub struct RegisterAssetReflectArgs {}

impl AttributeIdent for RegisterAssetReflectArgs {
const IDENT: &'static str = "auto_register_asset_reflect";
}

pub type IaRegisterAssetReflect = ItemAttribute<
Composed<RegisterAssetReflectArgs, WithPlugin, WithZeroOrManyGenerics>,
AllowStructOrEnumOrUse,
>;
pub type RegisterAssetReflectAppMutEmitter = AppMutationEmitter<IaRegisterAssetReflect>;
pub type RegisterAssetReflectAttrEmitter = AttrEmitter<IaRegisterAssetReflect>;

impl EmitAppMutationTokens for RegisterAssetReflectAppMutEmitter {
fn to_app_mutation_tokens(
&self,
tokens: &mut TokenStream,
app_param: &syn::Ident,
) -> syn::Result<()> {
let bevy_asset = crate::__private::paths::asset::asset_root_path();
let concrete_paths = self.args.concrete_paths()?;
for concrete_path in concrete_paths {
tokens.extend(quote! {
#bevy_asset::AssetApp::register_asset_reflect::<#concrete_path>(#app_param);
});
}
Ok(())
}
}

impl ToTokens for RegisterAssetReflectAttrEmitter {
fn to_tokens(&self, tokens: &mut TokenStream) {
let args = self.args.args.extra_args();
tokens.extend(quote! {
#(#args),*
});
*tokens = self.wrap_as_attr(tokens);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ mod auto_add_observer;
mod auto_add_plugin;
mod auto_add_system;
mod auto_configure_system_set;
mod auto_init_asset;
mod auto_init_resource;
mod auto_init_state;
mod auto_init_sub_state;
mod auto_insert_resource;
mod auto_name;
mod auto_plugin_build_hook;
mod auto_register_asset_reflect;
mod auto_register_state_type;
mod auto_register_type;
mod auto_run_on_build;
Expand All @@ -20,12 +22,14 @@ pub mod prelude {
pub use auto_add_plugin::*;
pub use auto_add_system::*;
pub use auto_configure_system_set::*;
pub use auto_init_asset::*;
pub use auto_init_resource::*;
pub use auto_init_state::*;
pub use auto_init_sub_state::*;
pub use auto_insert_resource::*;
pub use auto_name::*;
pub use auto_plugin_build_hook::*;
pub use auto_register_asset_reflect::*;
pub use auto_register_state_type::*;
pub use auto_register_type::*;
pub use auto_run_on_build::*;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::{
codegen::{
ExpandAttrs,
tokens,
},
macro_api::prelude::*,
syntax::{
ast::flag_or_list::FlagOrList,
validated::non_empty_path::NonEmptyPath,
},
util::macros::impl_from_default,
};
use darling::FromMeta;
use proc_macro2::Ident;

#[derive(FromMeta, Debug, Default, Clone, PartialEq, Hash)]
#[darling(derive_syn_parse, default)]
pub struct AssetArgs {
pub derive: FlagOrList<NonEmptyPath>,
pub reflect: FlagOrList<Ident>,
pub register: bool,
pub init: bool,
}

impl AttributeIdent for AssetArgs {
const IDENT: &'static str = "auto_asset";
}

impl<'a> From<&'a AssetArgs> for InitAssetArgs {
fn from(_: &'a AssetArgs) -> Self {
Self::default()
}
}

impl<'a> From<&'a AssetArgs> for RegisterAssetReflectArgs {
fn from(_: &'a AssetArgs) -> Self {
Self::default()
}
}

pub type IaAsset =
ItemAttribute<Composed<AssetArgs, WithPlugin, WithZeroOrManyGenerics>, AllowStructOrEnum>;
pub type AssetAttrExpandEmitter = AttrExpansionEmitter<IaAsset>;

impl AttrExpansionEmitterToExpandAttr for AssetAttrExpandEmitter {
fn to_expand_attrs(&self, expand_attrs: &mut ExpandAttrs) {
if self.args.args.base.derive.present {
let derive_tokens = if self.args.args.base.reflect.present {
tokens::derive_asset_without_type_path(&self.args.args.base.derive.items)
} else {
tokens::derive_asset(&self.args.args.base.derive.items)
};
expand_attrs.attrs.push(derive_tokens);
}
if self.args.args.base.reflect.present {
if self.args.args.base.derive.present {
expand_attrs.attrs.push(tokens::derive_reflect());
}
expand_attrs.append(tokens::reflect(&self.args.args.base.reflect.items))
}
if self.args.args.base.register {
expand_attrs.attrs.push(tokens::auto_register_asset_reflect(self.into()));
}
if self.args.args.base.init {
expand_attrs.attrs.push(tokens::auto_init_asset(self.into()));
}
}
}

impl_from_default!(AssetArgs => (InitAssetArgs, RegisterAssetReflectArgs));
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod auto_asset;
mod auto_component;
mod auto_event;
mod auto_message;
Expand All @@ -9,6 +10,7 @@ mod auto_system;

pub mod prelude {
pub use super::*;
pub use auto_asset::*;
pub use auto_component::*;
pub use auto_event::*;
pub use auto_message::*;
Expand Down
Loading
Loading