automatically detect bevy meta crate in derive macros

This commit is contained in:
Carter Anderson 2020-05-24 12:39:23 -07:00
parent d3e0196cbb
commit c5ab7df98f
12 changed files with 64 additions and 101 deletions

View file

@ -12,4 +12,5 @@ syn = "1.0"
proc-macro2 = "1.0"
quote = "1.0"
Inflector = { version = "0.11.4", default-features = false }
darling = "0.10.2"
darling = "0.10.2"
proc-macro-crate = "0.1.4"

View file

@ -1,8 +1,9 @@
use darling::FromMeta;
use proc_macro::TokenStream;
use syn::{DeriveInput, Path};
use proc_macro_crate::crate_name;
#[derive(FromMeta, Debug)]
#[derive(FromMeta, Debug, Default)]
pub struct ModuleAttributeArgs {
#[darling(default)]
pub bevy_render: Option<String>,
@ -16,11 +17,12 @@ pub struct ModuleAttributeArgs {
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
/// 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)]
pub meta: bool,
pub meta: Option<bool>,
}
#[derive(Debug)]
pub struct Modules {
pub bevy_render: String,
pub bevy_asset: String,
@ -51,17 +53,8 @@ impl Modules {
}
}
impl Default for ModuleAttributeArgs {
fn default() -> Self {
ModuleAttributeArgs {
bevy_asset: None,
bevy_render: None,
bevy_core: None,
bevy_app: None,
legion: None,
meta: true,
}
}
fn use_meta() -> bool {
crate_name("bevy").is_ok()
}
pub static MODULE_ATTRIBUTE_NAME: &'static str = "module";
@ -71,37 +64,37 @@ pub fn get_modules(ast: &DeriveInput) -> Modules {
.attrs
.iter()
.find(|a| a.path.get_ident().as_ref().unwrap().to_string() == MODULE_ATTRIBUTE_NAME)
.map(|a| {
ModuleAttributeArgs::from_meta(&a.parse_meta().unwrap())
.unwrap_or_else(|_err| ModuleAttributeArgs::default())
});
if let Some(module_attribute_args) = module_attribute_args {
let mut modules = if module_attribute_args.meta {
Modules::meta()
} else {
Modules::external()
};
.map_or_else(
|| ModuleAttributeArgs::default(),
|a| {
ModuleAttributeArgs::from_meta(&a.parse_meta().unwrap())
.unwrap_or_else(|_err| ModuleAttributeArgs::default())
},
);
if let Some(path) = module_attribute_args.bevy_asset {
modules.bevy_asset = path;
}
if let Some(path) = module_attribute_args.bevy_render {
modules.bevy_render = path;
}
if let Some(path) = module_attribute_args.bevy_core {
modules.bevy_core = path;
}
if let Some(path) = module_attribute_args.bevy_app {
modules.bevy_app = path;
}
modules
} else {
let mut modules = if module_attribute_args.meta.unwrap_or_else(|| use_meta()) {
Modules::meta()
} else {
Modules::external()
};
if let Some(path) = module_attribute_args.bevy_asset {
modules.bevy_asset = path;
}
if let Some(path) = module_attribute_args.bevy_render {
modules.bevy_render = path;
}
if let Some(path) = module_attribute_args.bevy_core {
modules.bevy_core = path;
}
if let Some(path) = module_attribute_args.bevy_app {
modules.bevy_app = path;
}
modules
}
pub fn get_path(path_str: &str) -> Path {

View file

@ -4,8 +4,8 @@ use bevy_derive::EntityArchetype;
use bevy_render::{mesh::Mesh, Renderable};
use bevy_transform::prelude::{LocalToWorld, Rotation, Scale, Translation};
#[derive(EntityArchetype, Default)]
#[module(meta = false)]
pub struct MeshEntity {
// #[tag]
pub mesh: Handle<Mesh>,
@ -19,7 +19,6 @@ pub struct MeshEntity {
}
#[derive(EntityArchetype, Default)]
#[module(meta = false)]
pub struct LightEntity {
pub light: Light,
pub local_to_world: LocalToWorld,

View file

@ -3,7 +3,6 @@ use bevy_derive::Uniforms;
use bevy_render::{texture::Texture, Color};
#[derive(Uniforms)]
#[module(meta = false)]
pub struct StandardMaterial {
#[uniform(instance)]
pub albedo: Color,

View file

@ -4,9 +4,6 @@ version = "0.1.0"
authors = ["Carter Anderson <mcanders1@gmail.com>"]
edition = "2018"
[features]
default_bevy_meta = []
[lib]
proc-macro = true
@ -14,4 +11,5 @@ proc-macro = true
syn = "1.0"
proc-macro2 = "1.0"
quote = "1.0"
darling = "0.10.2"
darling = "0.10.2"
proc-macro-crate = "0.1.4"

View file

@ -1,17 +1,18 @@
use darling::FromMeta;
use proc_macro::TokenStream;
use syn::{DeriveInput, Path};
use proc_macro_crate::crate_name;
#[derive(FromMeta, Debug)]
#[derive(FromMeta, Debug, Default)]
pub struct ModuleAttributeArgs {
#[darling(default)]
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
/// 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)]
pub meta: bool,
pub meta: Option<bool>,
}
#[derive(Debug)]
pub struct Modules {
pub bevy_property: String,
}
@ -30,27 +31,10 @@ impl Modules {
}
}
#[cfg(feature = "default_bevy_meta")]
impl Default for ModuleAttributeArgs {
fn default() -> Self {
ModuleAttributeArgs {
bevy_property: None,
meta: true,
}
}
fn use_meta() -> bool {
crate_name("bevy").is_ok()
}
#[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 fn get_modules(ast: &DeriveInput) -> Modules {
@ -58,25 +42,25 @@ pub fn get_modules(ast: &DeriveInput) -> Modules {
.attrs
.iter()
.find(|a| a.path.get_ident().as_ref().unwrap().to_string() == MODULE_ATTRIBUTE_NAME)
.map(|a| {
ModuleAttributeArgs::from_meta(&a.parse_meta().unwrap())
.unwrap_or_else(|_err| ModuleAttributeArgs::default())
});
if let Some(module_attribute_args) = module_attribute_args {
let mut modules = if module_attribute_args.meta {
Modules::meta()
} else {
Modules::external()
};
.map_or_else(
|| ModuleAttributeArgs::default(),
|a| {
ModuleAttributeArgs::from_meta(&a.parse_meta().unwrap())
.unwrap_or_else(|_err| ModuleAttributeArgs::default())
},
);
if let Some(path) = module_attribute_args.bevy_property {
modules.bevy_property = path;
}
modules
} else {
let mut modules = if module_attribute_args.meta.unwrap_or_else(|| use_meta()) {
Modules::meta()
} else {
Modules::external()
};
if let Some(path) = module_attribute_args.bevy_property {
modules.bevy_property = path;
}
modules
}
pub fn get_path(path_str: &str) -> Path {

View file

@ -4,7 +4,6 @@ use bevy_derive::EntityArchetype;
use bevy_transform::components::{LocalToWorld, Rotation, Scale, Translation};
#[derive(EntityArchetype, Default)]
#[module(meta = false)]
pub struct MeshMaterialEntity<T: Default + Send + Sync + 'static> {
pub mesh: Handle<Mesh>,
pub material: Handle<T>,
@ -16,7 +15,6 @@ pub struct MeshMaterialEntity<T: Default + Send + Sync + 'static> {
}
#[derive(EntityArchetype, Default)]
#[module(meta = false)]
pub struct CameraEntity {
pub camera: Camera,
pub active_camera: ActiveCamera,
@ -24,7 +22,6 @@ pub struct CameraEntity {
}
#[derive(EntityArchetype)]
#[module(meta = false)]
pub struct Camera2dEntity {
pub camera: Camera,
pub active_camera_2d: ActiveCamera2d,

View file

@ -110,7 +110,6 @@ impl Texture {
}
#[derive(Resource)]
#[module(meta = false)]
pub struct TextureResourceSystemState {
event_reader: EventReader<AssetEvent<Texture>>,
}

View file

@ -1,12 +1,10 @@
use zerocopy::{AsBytes, FromBytes};
use bevy_asset;
use bevy_core;
use bevy_derive::Uniforms;
#[repr(C)]
#[derive(Clone, Copy, AsBytes, FromBytes, Uniforms)]
#[module(meta = false, bevy_render = "crate")]
#[module(bevy_render = "crate")]
pub struct Vertex {
#[uniform(vertex)]
pub position: [f32; 3],

View file

@ -3,7 +3,6 @@ use bevy_derive::Uniforms;
use bevy_render::{texture::Texture, Color};
#[derive(Uniforms)]
#[module(meta = false)]
pub struct ColorMaterial {
pub color: Color,
#[uniform(shader_def)]

View file

@ -7,7 +7,6 @@ use bevy_derive::EntityArchetype;
use bevy_render::{mesh::Mesh, Renderable};
#[derive(EntityArchetype)]
#[module(meta = false)]
pub struct UiEntity {
pub node: Node,
pub rect: Rect,
@ -32,7 +31,6 @@ impl Default for UiEntity {
}
#[derive(EntityArchetype)]
#[module(meta = false)]
pub struct LabelEntity {
pub node: Node,
pub rect: Rect,
@ -60,7 +58,6 @@ impl Default for LabelEntity {
}
#[derive(EntityArchetype)]
#[module(meta = false)]
pub struct SpriteEntity {
pub sprite: Sprite,
pub rect: Rect,

View file

@ -4,7 +4,6 @@ use glam::Vec2;
use zerocopy::AsBytes;
#[repr(C)]
#[derive(Default, Clone, Copy, Debug, Uniform, AsBytes)]
#[module(meta = "false")]
pub struct Rect {
pub position: Vec2,
pub size: Vec2,