diff options
author | Nick Collier <nick.collier@sophos.com> | 2024-02-26 15:10:38 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-26 15:10:38 +0000 |
commit | 33860abf8c3c28f9a1f5b3477347650bdaee9cc3 (patch) | |
tree | c221b3435f039985cbc5f198edc61cf2b4308680 | |
parent | 777f3e5c5d280b14d37bb7574a88f1cbfb3f8d46 (diff) | |
download | bazelbuild-rules_rust-33860abf8c3c28f9a1f5b3477347650bdaee9cc3.tar.gz |
Locate license file in packge root if not specified in cargo metadata (#2521)
#2476 added rules_license license metadata to crate BUILD files but many
crates to do not have a license file specified in their cargo metadata.
This PR adds a fallback that attempts to locate a license file in the
crate package root directory.
-rw-r--r-- | crate_universe/src/context/crate_context.rs | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/crate_universe/src/context/crate_context.rs b/crate_universe/src/context/crate_context.rs index 36435230..7eaf8cb9 100644 --- a/crate_universe/src/context/crate_context.rs +++ b/crate_universe/src/context/crate_context.rs @@ -450,7 +450,7 @@ impl CrateContext { } } - let license_file = package.license_file.as_ref().map(|path| path.to_string()); + let license_file = Self::locate_license_file(package); let package_url: Option<String> = match package.repository { Some(..) => package.repository.clone(), @@ -650,6 +650,33 @@ impl CrateContext { self } + fn locate_license_file(package: &Package) -> Option<String> { + if let Some(license_file_path) = &package.license_file { + return Some(license_file_path.to_string()); + } + let package_root = package + .manifest_path + .as_std_path() + .parent() + .expect("Every manifest should have a parent directory"); + if package_root.exists() { + let mut paths: Vec<_> = package_root + .read_dir() + .unwrap() + .map(|r| r.unwrap()) + .collect(); + paths.sort_by_key(|dir| dir.path()); + for path in paths { + if let Some(file_name) = path.file_name().to_str() { + if file_name.to_uppercase().starts_with("LICENSE") { + return Some(file_name.to_string()); + } + } + } + } + None + } + /// Determine whether or not a crate __should__ include a build script /// (build.rs) if it happens to have one. fn crate_includes_build_script( |