automatically detect bevy meta crate in derive macros
This commit is contained in:
parent
d3e0196cbb
commit
c5ab7df98f
@ -13,3 +13,4 @@ proc-macro2 = "1.0"
|
|||||||
quote = "1.0"
|
quote = "1.0"
|
||||||
Inflector = { version = "0.11.4", default-features = false }
|
Inflector = { version = "0.11.4", default-features = false }
|
||||||
darling = "0.10.2"
|
darling = "0.10.2"
|
||||||
|
proc-macro-crate = "0.1.4"
|
@ -1,8 +1,9 @@
|
|||||||
use darling::FromMeta;
|
use darling::FromMeta;
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use syn::{DeriveInput, Path};
|
use syn::{DeriveInput, Path};
|
||||||
|
use proc_macro_crate::crate_name;
|
||||||
|
|
||||||
#[derive(FromMeta, Debug)]
|
#[derive(FromMeta, Debug, Default)]
|
||||||
pub struct ModuleAttributeArgs {
|
pub struct ModuleAttributeArgs {
|
||||||
#[darling(default)]
|
#[darling(default)]
|
||||||
pub bevy_render: Option<String>,
|
pub bevy_render: Option<String>,
|
||||||
@ -16,11 +17,12 @@ pub struct ModuleAttributeArgs {
|
|||||||
pub legion: Option<String>,
|
pub legion: Option<String>,
|
||||||
|
|
||||||
/// If true, it will use the meta "bevy" crate for dependencies by default (ex: bevy:app). If this is set to false, the individual bevy crates
|
/// If true, it will use the meta "bevy" crate for dependencies by default (ex: bevy:app). If this is set to false, the individual bevy crates
|
||||||
/// will be used (ex: "bevy_app"). Defaults to "true"
|
/// will be used (ex: "bevy_app"). Defaults to "true" if the "bevy" crate is in your cargo.toml
|
||||||
#[darling(default)]
|
#[darling(default)]
|
||||||
pub meta: bool,
|
pub meta: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Modules {
|
pub struct Modules {
|
||||||
pub bevy_render: String,
|
pub bevy_render: String,
|
||||||
pub bevy_asset: String,
|
pub bevy_asset: String,
|
||||||
@ -51,17 +53,8 @@ impl Modules {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ModuleAttributeArgs {
|
fn use_meta() -> bool {
|
||||||
fn default() -> Self {
|
crate_name("bevy").is_ok()
|
||||||
ModuleAttributeArgs {
|
|
||||||
bevy_asset: None,
|
|
||||||
bevy_render: None,
|
|
||||||
bevy_core: None,
|
|
||||||
bevy_app: None,
|
|
||||||
legion: None,
|
|
||||||
meta: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub static MODULE_ATTRIBUTE_NAME: &'static str = "module";
|
pub static MODULE_ATTRIBUTE_NAME: &'static str = "module";
|
||||||
@ -71,12 +64,15 @@ pub fn get_modules(ast: &DeriveInput) -> Modules {
|
|||||||
.attrs
|
.attrs
|
||||||
.iter()
|
.iter()
|
||||||
.find(|a| a.path.get_ident().as_ref().unwrap().to_string() == MODULE_ATTRIBUTE_NAME)
|
.find(|a| a.path.get_ident().as_ref().unwrap().to_string() == MODULE_ATTRIBUTE_NAME)
|
||||||
.map(|a| {
|
.map_or_else(
|
||||||
|
|| ModuleAttributeArgs::default(),
|
||||||
|
|a| {
|
||||||
ModuleAttributeArgs::from_meta(&a.parse_meta().unwrap())
|
ModuleAttributeArgs::from_meta(&a.parse_meta().unwrap())
|
||||||
.unwrap_or_else(|_err| ModuleAttributeArgs::default())
|
.unwrap_or_else(|_err| ModuleAttributeArgs::default())
|
||||||
});
|
},
|
||||||
if let Some(module_attribute_args) = module_attribute_args {
|
);
|
||||||
let mut modules = if module_attribute_args.meta {
|
|
||||||
|
let mut modules = if module_attribute_args.meta.unwrap_or_else(|| use_meta()) {
|
||||||
Modules::meta()
|
Modules::meta()
|
||||||
} else {
|
} else {
|
||||||
Modules::external()
|
Modules::external()
|
||||||
@ -99,9 +95,6 @@ pub fn get_modules(ast: &DeriveInput) -> Modules {
|
|||||||
}
|
}
|
||||||
|
|
||||||
modules
|
modules
|
||||||
} else {
|
|
||||||
Modules::meta()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_path(path_str: &str) -> Path {
|
pub fn get_path(path_str: &str) -> Path {
|
||||||
|
@ -4,8 +4,8 @@ use bevy_derive::EntityArchetype;
|
|||||||
use bevy_render::{mesh::Mesh, Renderable};
|
use bevy_render::{mesh::Mesh, Renderable};
|
||||||
use bevy_transform::prelude::{LocalToWorld, Rotation, Scale, Translation};
|
use bevy_transform::prelude::{LocalToWorld, Rotation, Scale, Translation};
|
||||||
|
|
||||||
|
|
||||||
#[derive(EntityArchetype, Default)]
|
#[derive(EntityArchetype, Default)]
|
||||||
#[module(meta = false)]
|
|
||||||
pub struct MeshEntity {
|
pub struct MeshEntity {
|
||||||
// #[tag]
|
// #[tag]
|
||||||
pub mesh: Handle<Mesh>,
|
pub mesh: Handle<Mesh>,
|
||||||
@ -19,7 +19,6 @@ pub struct MeshEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(EntityArchetype, Default)]
|
#[derive(EntityArchetype, Default)]
|
||||||
#[module(meta = false)]
|
|
||||||
pub struct LightEntity {
|
pub struct LightEntity {
|
||||||
pub light: Light,
|
pub light: Light,
|
||||||
pub local_to_world: LocalToWorld,
|
pub local_to_world: LocalToWorld,
|
||||||
|
@ -3,7 +3,6 @@ use bevy_derive::Uniforms;
|
|||||||
use bevy_render::{texture::Texture, Color};
|
use bevy_render::{texture::Texture, Color};
|
||||||
|
|
||||||
#[derive(Uniforms)]
|
#[derive(Uniforms)]
|
||||||
#[module(meta = false)]
|
|
||||||
pub struct StandardMaterial {
|
pub struct StandardMaterial {
|
||||||
#[uniform(instance)]
|
#[uniform(instance)]
|
||||||
pub albedo: Color,
|
pub albedo: Color,
|
||||||
|
@ -4,9 +4,6 @@ version = "0.1.0"
|
|||||||
authors = ["Carter Anderson <mcanders1@gmail.com>"]
|
authors = ["Carter Anderson <mcanders1@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[features]
|
|
||||||
default_bevy_meta = []
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
proc-macro = true
|
proc-macro = true
|
||||||
|
|
||||||
@ -15,3 +12,4 @@ syn = "1.0"
|
|||||||
proc-macro2 = "1.0"
|
proc-macro2 = "1.0"
|
||||||
quote = "1.0"
|
quote = "1.0"
|
||||||
darling = "0.10.2"
|
darling = "0.10.2"
|
||||||
|
proc-macro-crate = "0.1.4"
|
@ -1,17 +1,18 @@
|
|||||||
use darling::FromMeta;
|
use darling::FromMeta;
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use syn::{DeriveInput, Path};
|
use syn::{DeriveInput, Path};
|
||||||
|
use proc_macro_crate::crate_name;
|
||||||
|
|
||||||
#[derive(FromMeta, Debug)]
|
#[derive(FromMeta, Debug, Default)]
|
||||||
pub struct ModuleAttributeArgs {
|
pub struct ModuleAttributeArgs {
|
||||||
#[darling(default)]
|
|
||||||
pub bevy_property: Option<String>,
|
pub bevy_property: Option<String>,
|
||||||
/// If true, it will use the meta "bevy" crate for dependencies by default (ex: bevy:app). If this is set to false, the individual bevy crates
|
/// If true, it will use the meta "bevy" crate for dependencies by default (ex: bevy:app). If this is set to false, the individual bevy crates
|
||||||
/// will be used (ex: "bevy_app"). Defaults to "true"
|
/// will be used (ex: "bevy_app"). Defaults to "true" if the "bevy" crate is in your cargo.toml
|
||||||
#[darling(default)]
|
#[darling(default)]
|
||||||
pub meta: bool,
|
pub meta: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Modules {
|
pub struct Modules {
|
||||||
pub bevy_property: String,
|
pub bevy_property: String,
|
||||||
}
|
}
|
||||||
@ -30,27 +31,10 @@ impl Modules {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "default_bevy_meta")]
|
fn use_meta() -> bool {
|
||||||
impl Default for ModuleAttributeArgs {
|
crate_name("bevy").is_ok()
|
||||||
fn default() -> Self {
|
|
||||||
ModuleAttributeArgs {
|
|
||||||
bevy_property: None,
|
|
||||||
meta: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "default_bevy_meta"))]
|
|
||||||
impl Default for ModuleAttributeArgs {
|
|
||||||
fn default() -> Self {
|
|
||||||
ModuleAttributeArgs {
|
|
||||||
bevy_property: None,
|
|
||||||
meta: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub static MODULE_ATTRIBUTE_NAME: &'static str = "module";
|
pub static MODULE_ATTRIBUTE_NAME: &'static str = "module";
|
||||||
|
|
||||||
pub fn get_modules(ast: &DeriveInput) -> Modules {
|
pub fn get_modules(ast: &DeriveInput) -> Modules {
|
||||||
@ -58,12 +42,15 @@ pub fn get_modules(ast: &DeriveInput) -> Modules {
|
|||||||
.attrs
|
.attrs
|
||||||
.iter()
|
.iter()
|
||||||
.find(|a| a.path.get_ident().as_ref().unwrap().to_string() == MODULE_ATTRIBUTE_NAME)
|
.find(|a| a.path.get_ident().as_ref().unwrap().to_string() == MODULE_ATTRIBUTE_NAME)
|
||||||
.map(|a| {
|
.map_or_else(
|
||||||
|
|| ModuleAttributeArgs::default(),
|
||||||
|
|a| {
|
||||||
ModuleAttributeArgs::from_meta(&a.parse_meta().unwrap())
|
ModuleAttributeArgs::from_meta(&a.parse_meta().unwrap())
|
||||||
.unwrap_or_else(|_err| ModuleAttributeArgs::default())
|
.unwrap_or_else(|_err| ModuleAttributeArgs::default())
|
||||||
});
|
},
|
||||||
if let Some(module_attribute_args) = module_attribute_args {
|
);
|
||||||
let mut modules = if module_attribute_args.meta {
|
|
||||||
|
let mut modules = if module_attribute_args.meta.unwrap_or_else(|| use_meta()) {
|
||||||
Modules::meta()
|
Modules::meta()
|
||||||
} else {
|
} else {
|
||||||
Modules::external()
|
Modules::external()
|
||||||
@ -74,9 +61,6 @@ pub fn get_modules(ast: &DeriveInput) -> Modules {
|
|||||||
}
|
}
|
||||||
|
|
||||||
modules
|
modules
|
||||||
} else {
|
|
||||||
Modules::meta()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_path(path_str: &str) -> Path {
|
pub fn get_path(path_str: &str) -> Path {
|
||||||
|
@ -4,7 +4,6 @@ use bevy_derive::EntityArchetype;
|
|||||||
use bevy_transform::components::{LocalToWorld, Rotation, Scale, Translation};
|
use bevy_transform::components::{LocalToWorld, Rotation, Scale, Translation};
|
||||||
|
|
||||||
#[derive(EntityArchetype, Default)]
|
#[derive(EntityArchetype, Default)]
|
||||||
#[module(meta = false)]
|
|
||||||
pub struct MeshMaterialEntity<T: Default + Send + Sync + 'static> {
|
pub struct MeshMaterialEntity<T: Default + Send + Sync + 'static> {
|
||||||
pub mesh: Handle<Mesh>,
|
pub mesh: Handle<Mesh>,
|
||||||
pub material: Handle<T>,
|
pub material: Handle<T>,
|
||||||
@ -16,7 +15,6 @@ pub struct MeshMaterialEntity<T: Default + Send + Sync + 'static> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(EntityArchetype, Default)]
|
#[derive(EntityArchetype, Default)]
|
||||||
#[module(meta = false)]
|
|
||||||
pub struct CameraEntity {
|
pub struct CameraEntity {
|
||||||
pub camera: Camera,
|
pub camera: Camera,
|
||||||
pub active_camera: ActiveCamera,
|
pub active_camera: ActiveCamera,
|
||||||
@ -24,7 +22,6 @@ pub struct CameraEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(EntityArchetype)]
|
#[derive(EntityArchetype)]
|
||||||
#[module(meta = false)]
|
|
||||||
pub struct Camera2dEntity {
|
pub struct Camera2dEntity {
|
||||||
pub camera: Camera,
|
pub camera: Camera,
|
||||||
pub active_camera_2d: ActiveCamera2d,
|
pub active_camera_2d: ActiveCamera2d,
|
||||||
|
@ -110,7 +110,6 @@ impl Texture {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource)]
|
#[derive(Resource)]
|
||||||
#[module(meta = false)]
|
|
||||||
pub struct TextureResourceSystemState {
|
pub struct TextureResourceSystemState {
|
||||||
event_reader: EventReader<AssetEvent<Texture>>,
|
event_reader: EventReader<AssetEvent<Texture>>,
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
use zerocopy::{AsBytes, FromBytes};
|
use zerocopy::{AsBytes, FromBytes};
|
||||||
|
|
||||||
use bevy_asset;
|
|
||||||
use bevy_core;
|
|
||||||
use bevy_derive::Uniforms;
|
use bevy_derive::Uniforms;
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Clone, Copy, AsBytes, FromBytes, Uniforms)]
|
#[derive(Clone, Copy, AsBytes, FromBytes, Uniforms)]
|
||||||
#[module(meta = false, bevy_render = "crate")]
|
#[module(bevy_render = "crate")]
|
||||||
pub struct Vertex {
|
pub struct Vertex {
|
||||||
#[uniform(vertex)]
|
#[uniform(vertex)]
|
||||||
pub position: [f32; 3],
|
pub position: [f32; 3],
|
||||||
|
@ -3,7 +3,6 @@ use bevy_derive::Uniforms;
|
|||||||
use bevy_render::{texture::Texture, Color};
|
use bevy_render::{texture::Texture, Color};
|
||||||
|
|
||||||
#[derive(Uniforms)]
|
#[derive(Uniforms)]
|
||||||
#[module(meta = false)]
|
|
||||||
pub struct ColorMaterial {
|
pub struct ColorMaterial {
|
||||||
pub color: Color,
|
pub color: Color,
|
||||||
#[uniform(shader_def)]
|
#[uniform(shader_def)]
|
||||||
|
@ -7,7 +7,6 @@ use bevy_derive::EntityArchetype;
|
|||||||
use bevy_render::{mesh::Mesh, Renderable};
|
use bevy_render::{mesh::Mesh, Renderable};
|
||||||
|
|
||||||
#[derive(EntityArchetype)]
|
#[derive(EntityArchetype)]
|
||||||
#[module(meta = false)]
|
|
||||||
pub struct UiEntity {
|
pub struct UiEntity {
|
||||||
pub node: Node,
|
pub node: Node,
|
||||||
pub rect: Rect,
|
pub rect: Rect,
|
||||||
@ -32,7 +31,6 @@ impl Default for UiEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(EntityArchetype)]
|
#[derive(EntityArchetype)]
|
||||||
#[module(meta = false)]
|
|
||||||
pub struct LabelEntity {
|
pub struct LabelEntity {
|
||||||
pub node: Node,
|
pub node: Node,
|
||||||
pub rect: Rect,
|
pub rect: Rect,
|
||||||
@ -60,7 +58,6 @@ impl Default for LabelEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(EntityArchetype)]
|
#[derive(EntityArchetype)]
|
||||||
#[module(meta = false)]
|
|
||||||
pub struct SpriteEntity {
|
pub struct SpriteEntity {
|
||||||
pub sprite: Sprite,
|
pub sprite: Sprite,
|
||||||
pub rect: Rect,
|
pub rect: Rect,
|
||||||
|
@ -4,7 +4,6 @@ use glam::Vec2;
|
|||||||
use zerocopy::AsBytes;
|
use zerocopy::AsBytes;
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Default, Clone, Copy, Debug, Uniform, AsBytes)]
|
#[derive(Default, Clone, Copy, Debug, Uniform, AsBytes)]
|
||||||
#[module(meta = "false")]
|
|
||||||
pub struct Rect {
|
pub struct Rect {
|
||||||
pub position: Vec2,
|
pub position: Vec2,
|
||||||
pub size: Vec2,
|
pub size: Vec2,
|
||||||
|
Loading…
Reference in New Issue
Block a user