fix(core): more sophisticated crate detection (#1056)

* fix(core): more sophisticated crate detection

* fix: remove RA hack
This commit is contained in:
Matthias Seitz 2022-03-17 20:49:38 +01:00 committed by GitHub
parent d1f46f4c2c
commit 2af28be283
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 40 additions and 8 deletions

View File

@ -1,5 +1,6 @@
use cargo_metadata::{DependencyKind, MetadataCommand}; use cargo_metadata::MetadataCommand;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use syn::Path; use syn::Path;
/// See `determine_ethers_crates` /// See `determine_ethers_crates`
@ -32,6 +33,11 @@ pub fn ethers_providers_crate() -> Path {
/// macros This will attempt to parse the current `Cargo.toml` and check the /// macros This will attempt to parse the current `Cargo.toml` and check the
/// ethers related dependencies. /// ethers related dependencies.
/// ///
/// This determines
/// - `ethers_*` idents if `ethers-core`, `ethers-contract`, `ethers-providers` are present in
/// the manifest or the `ethers` is _not_ present
/// - `ethers::*` otherwise
///
/// This process is a bit hacky, we run `cargo metadata` internally which /// This process is a bit hacky, we run `cargo metadata` internally which
/// resolves the current package but creates a new `Cargo.lock` file in the /// resolves the current package but creates a new `Cargo.lock` file in the
/// process. This is not a problem for regular workspaces but becomes an issue /// process. This is not a problem for regular workspaces but becomes an issue
@ -62,15 +68,41 @@ pub fn determine_ethers_crates() -> (&'static str, &'static str, &'static str) {
.ok() .ok()
.and_then(|metadata| { .and_then(|metadata| {
metadata.root_package().and_then(|pkg| { metadata.root_package().and_then(|pkg| {
pkg.dependencies.iter().filter(|dep| dep.kind == DependencyKind::Normal).find_map( let sub_crates = Some(("ethers_core", "ethers_contract", "ethers_providers"));
|dep| { if pkg.name == "ethers-contract" {
(dep.name == "ethers") // Note(mattsse): this is super hacky but required in order to compile the tests
.then(|| ("ethers::core", "ethers::contract", "ethers::providers")) // in the `ethers-contract` crate
}, return sub_crates
) }
let mut has_ethers_core = false;
let mut has_ethers_contract = false;
let mut has_ethers_providers = false;
for dep in pkg.dependencies.iter() {
match dep.name.as_str() {
"ethers-core" => {
has_ethers_core = true;
}
"ethers-contract" => {
has_ethers_contract = true;
}
"ethers-providers" => {
has_ethers_providers = true;
}
"ethers" => return None,
_ => {}
}
}
if has_ethers_core && has_ethers_contract && has_ethers_providers {
return sub_crates
}
None
}) })
}) })
.unwrap_or(("ethers_core", "ethers_contract", "ethers_providers")); .unwrap_or(("ethers::core", "ethers::contract", "ethers::providers"));
if needs_lock_file_cleanup { if needs_lock_file_cleanup {
// delete the `Cargo.lock` file that was created by `cargo metadata` // delete the `Cargo.lock` file that was created by `cargo metadata`