Skip to content

Commit

Permalink
Locate license file in packge root if not specified in cargo metadata (
Browse files Browse the repository at this point in the history
…bazelbuild#2521)

bazelbuild#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.
  • Loading branch information
qtica authored and scramm committed Apr 1, 2024
1 parent 3833a79 commit 758f855
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion crate_universe/src/context/crate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 758f855

Please sign in to comment.