Skip to content

Commit

Permalink
One time manifest parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
YohDeadfall committed May 7, 2021
1 parent 0282eed commit 2fedad5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
13 changes: 7 additions & 6 deletions crates/bevy_derive/src/modules.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy_macro_utils::{get_module_path, get_path};
use bevy_macro_utils::{get_manifest, get_module_path_by_manifest, get_path};
use syn::Attribute;

pub struct Modules {
Expand All @@ -16,12 +16,13 @@ fn validate_as_crate_attribute(tokens: &str) -> bool {
}

pub fn get_modules(attributes: &[Attribute]) -> Modules {
let manifest = get_manifest();
let mut modules = Modules {
bevy_app: get_module_path("bevy_app"),
bevy_asset: get_module_path("bevy_asset"),
bevy_core: get_module_path("bevy_core"),
bevy_render: get_module_path("bevy_render"),
bevy_utils: get_module_path("bevy_utils"),
bevy_app: get_module_path_by_manifest("bevy_app", &manifest),
bevy_asset: get_module_path_by_manifest("bevy_asset", &manifest),
bevy_core: get_module_path_by_manifest("bevy_core", &manifest),
bevy_render: get_module_path_by_manifest("bevy_render", &manifest),
bevy_utils: get_module_path_by_manifest("bevy_utils", &manifest),
};
for attribute in attributes.iter() {
if *attribute.path.get_ident().as_ref().unwrap() == AS_CRATE_ATTRIBUTE_NAME {
Expand Down
35 changes: 21 additions & 14 deletions crates/bevy_macro_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ use cargo_manifest::{DepsSet, Manifest};
use proc_macro::TokenStream;
use std::{env, path::PathBuf};

pub fn get_path(path: &str) -> syn::Path {
parse_str(path)
}

pub fn get_module_path(name: &str) -> syn::Path {
get_module_path_by_manifest(name, &get_manifest())
}

pub fn get_module_path_by_manifest(name: &str, manifest: &Manifest) -> syn::Path {
const BEVY: &str = "bevy";
const BEVY_INTERNAL: &str = "bevy_internal";

let find_in_deps = |deps: DepsSet| -> Option<syn::Path> {
let find_in_deps = |deps: &DepsSet| -> Option<syn::Path> {
let package = if let Some(dep) = deps.get(BEVY) {
Some(dep.package().unwrap_or(BEVY))
} else if let Some(dep) = deps.get(BEVY_INTERNAL) {
Expand All @@ -19,31 +27,30 @@ pub fn get_module_path(name: &str) -> syn::Path {

package.map(get_path).map(|mut p| {
if let Some(module) = name.strip_prefix("bevy_") {
p.segments.push(parse_path(module));
p.segments.push(parse_str(module));
}
p
})
};

let manifest = env::var_os("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.map(|mut path| {
path.push("Cargo.toml");
Manifest::from_path(path).unwrap()
})
.unwrap();
let deps = manifest.dependencies;
let deps_dev = manifest.dev_dependencies;
let deps = manifest.dependencies.as_ref();
let deps_dev = 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))
}

pub fn get_path(path: &str) -> syn::Path {
parse_path(path)
pub fn get_manifest() -> Manifest {
env::var_os("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.map(|mut path| {
path.push("Cargo.toml");
Manifest::from_path(path).unwrap()
})
.unwrap()
}

fn parse_path<T: syn::parse::Parse>(path: &str) -> T {
fn parse_str<T: syn::parse::Parse>(path: &str) -> T {
syn::parse(path.parse::<TokenStream>().unwrap()).unwrap()
}

0 comments on commit 2fedad5

Please sign in to comment.