-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use bevy_reflect as path in case of no direct references (#1875)
Fixes #1844 Co-authored-by: Carter Anderson <mcanders1@gmail.com>
- Loading branch information
1 parent
a42343d
commit 653c103
Showing
25 changed files
with
128 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,5 @@ | ||
use find_crate::Manifest; | ||
use proc_macro::TokenStream; | ||
use syn::{Attribute, Path}; | ||
|
||
#[derive(Debug)] | ||
pub struct Modules { | ||
pub bevy_render: String, | ||
pub bevy_asset: String, | ||
pub bevy_core: String, | ||
pub bevy_utils: String, | ||
pub bevy_app: String, | ||
} | ||
|
||
impl Modules { | ||
pub fn meta(name: &str) -> Modules { | ||
Modules { | ||
bevy_asset: format!("{}::asset", name), | ||
bevy_render: format!("{}::render", name), | ||
bevy_core: format!("{}::core", name), | ||
bevy_utils: format!("{}::utils", name), | ||
bevy_app: format!("{}::app", name), | ||
} | ||
} | ||
|
||
pub fn external() -> Modules { | ||
Modules { | ||
bevy_asset: "bevy_asset".to_string(), | ||
bevy_render: "bevy_render".to_string(), | ||
bevy_core: "bevy_core".to_string(), | ||
bevy_utils: "bevy_utils".to_string(), | ||
bevy_app: "bevy_app".to_string(), | ||
} | ||
} | ||
} | ||
|
||
fn get_meta() -> Option<Modules> { | ||
let manifest = Manifest::new().unwrap(); | ||
if let Some(package) = manifest.find(|name| name == "bevy") { | ||
Some(Modules::meta(&package.name)) | ||
} else if let Some(package) = manifest.find(|name| name == "bevy_internal") { | ||
Some(Modules::meta(&package.name)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
const AS_CRATE_ATTRIBUTE_NAME: &str = "as_crate"; | ||
|
||
fn validate_as_crate_attribute(tokens: &str) -> bool { | ||
tokens.len() > 2 && tokens.starts_with('(') && tokens.ends_with(')') | ||
} | ||
|
||
pub fn get_modules(attributes: &[Attribute]) -> Modules { | ||
let mut modules = get_meta().unwrap_or_else(Modules::external); | ||
for attribute in attributes.iter() { | ||
if *attribute.path.get_ident().as_ref().unwrap() == AS_CRATE_ATTRIBUTE_NAME { | ||
let value = attribute.tokens.to_string(); | ||
if !validate_as_crate_attribute(&value) { | ||
panic!("The attribute `#[as_crate{}]` is invalid. It must follow the format `#[as_crate(<crate name>)]`", value); | ||
} else if value[1..value.len() - 1] == modules.bevy_render { | ||
modules.bevy_render = "crate".to_string(); | ||
} | ||
} | ||
} | ||
|
||
modules | ||
} | ||
|
||
pub fn get_path(path_str: &str) -> Path { | ||
syn::parse(path_str.parse::<TokenStream>().unwrap()).unwrap() | ||
} | ||
pub const BEVY_APP: &str = "bevy_app"; | ||
pub const BEVY_ASSET: &str = "bevy_asset"; | ||
pub const BEVY_CORE: &str = "bevy_core"; | ||
pub const BEVY_RENDER: &str = "bevy_render"; | ||
pub const BEVY_UTILS: &str = "bevy_utils"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "bevy_macro_utils" | ||
version = "0.5.0" | ||
edition = "2018" | ||
authors = [ | ||
"Bevy Contributors <bevyengine@gmail.com>", | ||
"Carter Anderson <mcanders1@gmail.com>", | ||
] | ||
description = "A collection of utils for Bevy Engine" | ||
homepage = "https://bevyengine.org" | ||
repository = "https://github.com/bevyengine/bevy" | ||
license = "MIT" | ||
keywords = ["bevy"] | ||
|
||
[dependencies] | ||
cargo-manifest = "0.2.3" | ||
proc-macro2 = "1.0" | ||
syn = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
extern crate proc_macro; | ||
|
||
use cargo_manifest::{DepsSet, Manifest}; | ||
use proc_macro::TokenStream; | ||
use std::{env, path::PathBuf}; | ||
|
||
pub struct BevyManifest { | ||
manifest: Manifest, | ||
} | ||
|
||
impl Default for BevyManifest { | ||
fn default() -> Self { | ||
Self { | ||
manifest: env::var_os("CARGO_MANIFEST_DIR") | ||
.map(PathBuf::from) | ||
.map(|mut path| { | ||
path.push("Cargo.toml"); | ||
Manifest::from_path(path).unwrap() | ||
}) | ||
.unwrap(), | ||
} | ||
} | ||
} | ||
|
||
impl BevyManifest { | ||
pub fn get_path(&self, name: &str) -> syn::Path { | ||
const BEVY: &str = "bevy"; | ||
const BEVY_INTERNAL: &str = "bevy_internal"; | ||
|
||
let find_in_deps = |deps: &DepsSet| -> Option<syn::Path> { | ||
let package = if let Some(dep) = deps.get(BEVY) { | ||
dep.package().unwrap_or(BEVY) | ||
} else if let Some(dep) = deps.get(BEVY_INTERNAL) { | ||
dep.package().unwrap_or(BEVY_INTERNAL) | ||
} else { | ||
return None; | ||
}; | ||
|
||
let mut path = get_path(package); | ||
if let Some(module) = name.strip_prefix("bevy_") { | ||
path.segments.push(parse_str(module)); | ||
} | ||
Some(path) | ||
}; | ||
|
||
let deps = self.manifest.dependencies.as_ref(); | ||
let deps_dev = self.manifest.dev_dependencies.as_ref(); | ||
|
||
deps.and_then(find_in_deps) | ||
.or_else(|| deps_dev.and_then(find_in_deps)) | ||
.unwrap_or_else(|| get_path(name)) | ||
} | ||
} | ||
|
||
fn get_path(path: &str) -> syn::Path { | ||
parse_str(path) | ||
} | ||
|
||
fn parse_str<T: syn::parse::Parse>(path: &str) -> T { | ||
syn::parse(path.parse::<TokenStream>().unwrap()).unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.