From bd6ca069999352012ca8664db199213e9b3b3269 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 11 Nov 2024 21:02:05 +0100 Subject: feat(ecs-macros): add support for ecs crate path --- ecs-macros/src/lib.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/ecs-macros/src/lib.rs b/ecs-macros/src/lib.rs index 3d0de6d..bc2d5ed 100644 --- a/ecs-macros/src/lib.rs +++ b/ecs-macros/src/lib.rs @@ -1,3 +1,4 @@ +use std::env; use std::path::PathBuf as FsPathBuf; use proc_macro::TokenStream; @@ -263,11 +264,33 @@ fn find_engine_ecs_crate_path() -> Option { let cargo_manifest_dir = FsPathBuf::from(std::env::var("CARGO_MANIFEST_DIR").ok()?); + let cargo_crate_name = std::env::var("CARGO_CRATE_NAME").ok()?; + let cargo_pkg_name = std::env::var("CARGO_PKG_NAME").ok()?; + + if cargo_pkg_name == "ecs" && cargo_crate_name != "ecs" { + // Macro is used by a ecs crate example/test/benchmark + return Some(syn_path!(ecs)); + } + let crate_manifest = std::fs::read_to_string(cargo_manifest_dir.join("Cargo.toml")) .ok()? .parse::() .expect("Failed to parse crate manifest file"); + let package = match crate_manifest.get("package")? { + TomlValue::Table(package) => Some(package), + _ => None, + }?; + + let package_name = match package.get("name")? { + TomlValue::String(package_name) => Some(package_name), + _ => None, + }?; + + if package_name == "ecs" { + return Some(syn_path!(crate)); + } + let crate_dependencies = match crate_manifest.get("dependencies")? { TomlValue::Table(dependencies) => Some(dependencies), _ => None, -- cgit v1.2.3-18-g5258