Skip to content

Commit

Permalink
Make copying of a license-file that exists outside a package into f…
Browse files Browse the repository at this point in the history
…unction that can be used by other files
  • Loading branch information
Muscraft committed Apr 8, 2022
1 parent b36cc6e commit 4ea1228
Showing 1 changed file with 61 additions and 40 deletions.
101 changes: 61 additions & 40 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,46 +260,16 @@ fn build_ar_list(
}
if let Some(license_file) = &pkg.manifest().metadata().license_file {
let license_path = Path::new(license_file);
let abs_license_path = paths::normalize_path(&pkg.root().join(license_path));
if abs_license_path.exists() {
match abs_license_path.strip_prefix(&pkg.root()) {
Ok(rel_license_path) => {
if !result.iter().any(|ar| ar.rel_path == rel_license_path) {
result.push(ArchiveFile {
rel_path: rel_license_path.to_path_buf(),
rel_str: rel_license_path
.to_str()
.expect("everything was utf8")
.to_string(),
contents: FileContents::OnDisk(abs_license_path),
});
}
}
Err(_) => {
// The license exists somewhere outside of the package.
let license_name = license_path.file_name().unwrap();
if result
.iter()
.any(|ar| ar.rel_path.file_name().unwrap() == license_name)
{
ws.config().shell().warn(&format!(
"license-file `{}` appears to be a path outside of the package, \
but there is already a file named `{}` in the root of the package. \
The archived crate will contain the copy in the root of the package. \
Update the license-file to point to the path relative \
to the root of the package to remove this warning.",
license_file,
license_name.to_str().unwrap()
))?;
} else {
result.push(ArchiveFile {
rel_path: PathBuf::from(license_name),
rel_str: license_name.to_str().unwrap().to_string(),
contents: FileContents::OnDisk(abs_license_path),
});
}
}
}
let abs_file_path = paths::normalize_path(&pkg.root().join(license_path));
if abs_file_path.exists() {
check_for_file_and_add(
"license-file",
license_path,
abs_file_path,
pkg,
&mut result,
ws,
)?;
} else {
let rel_msg = if license_path.is_absolute() {
"".to_string()
Expand All @@ -321,6 +291,57 @@ fn build_ar_list(
Ok(result)
}

fn check_for_file_and_add(
label: &str,
file_path: &Path,
abs_file_path: PathBuf,
pkg: &Package,
result: &mut Vec<ArchiveFile>,
ws: &Workspace<'_>,
) -> CargoResult<()> {
match abs_file_path.strip_prefix(&pkg.root()) {
Ok(rel_file_path) => {
if !result.iter().any(|ar| ar.rel_path == rel_file_path) {
result.push(ArchiveFile {
rel_path: rel_file_path.to_path_buf(),
rel_str: rel_file_path
.to_str()
.expect("everything was utf8")
.to_string(),
contents: FileContents::OnDisk(abs_file_path),
})
}
}
Err(_) => {
// The file exists somewhere outside of the package.
let file_name = file_path.file_name().unwrap();
if result
.iter()
.any(|ar| ar.rel_path.file_name().unwrap() == file_name)
{
ws.config().shell().warn(&format!(
"{} `{}` appears to be a path outside of the package, \
but there is already a file named `{}` in the root of the package. \
The archived crate will contain the copy in the root of the package. \
Update the {} to point to the path relative \
to the root of the package to remove this warning.",
label,
file_path.display(),
file_name.to_str().unwrap(),
label,
))?;
} else {
result.push(ArchiveFile {
rel_path: PathBuf::from(file_name),
rel_str: file_name.to_str().unwrap().to_string(),
contents: FileContents::OnDisk(abs_file_path),
})
}
}
}
Ok(())
}

/// Construct `Cargo.lock` for the package to be published.
fn build_lock(ws: &Workspace<'_>, orig_pkg: &Package) -> CargoResult<String> {
let config = ws.config();
Expand Down

0 comments on commit 4ea1228

Please sign in to comment.