Skip to content

Commit 4ea1228

Browse files
committed
Make copying of a license-file that exists outside a package into function that can be used by other files
1 parent b36cc6e commit 4ea1228

File tree

1 file changed

+61
-40
lines changed

1 file changed

+61
-40
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -260,46 +260,16 @@ fn build_ar_list(
260260
}
261261
if let Some(license_file) = &pkg.manifest().metadata().license_file {
262262
let license_path = Path::new(license_file);
263-
let abs_license_path = paths::normalize_path(&pkg.root().join(license_path));
264-
if abs_license_path.exists() {
265-
match abs_license_path.strip_prefix(&pkg.root()) {
266-
Ok(rel_license_path) => {
267-
if !result.iter().any(|ar| ar.rel_path == rel_license_path) {
268-
result.push(ArchiveFile {
269-
rel_path: rel_license_path.to_path_buf(),
270-
rel_str: rel_license_path
271-
.to_str()
272-
.expect("everything was utf8")
273-
.to_string(),
274-
contents: FileContents::OnDisk(abs_license_path),
275-
});
276-
}
277-
}
278-
Err(_) => {
279-
// The license exists somewhere outside of the package.
280-
let license_name = license_path.file_name().unwrap();
281-
if result
282-
.iter()
283-
.any(|ar| ar.rel_path.file_name().unwrap() == license_name)
284-
{
285-
ws.config().shell().warn(&format!(
286-
"license-file `{}` appears to be a path outside of the package, \
287-
but there is already a file named `{}` in the root of the package. \
288-
The archived crate will contain the copy in the root of the package. \
289-
Update the license-file to point to the path relative \
290-
to the root of the package to remove this warning.",
291-
license_file,
292-
license_name.to_str().unwrap()
293-
))?;
294-
} else {
295-
result.push(ArchiveFile {
296-
rel_path: PathBuf::from(license_name),
297-
rel_str: license_name.to_str().unwrap().to_string(),
298-
contents: FileContents::OnDisk(abs_license_path),
299-
});
300-
}
301-
}
302-
}
263+
let abs_file_path = paths::normalize_path(&pkg.root().join(license_path));
264+
if abs_file_path.exists() {
265+
check_for_file_and_add(
266+
"license-file",
267+
license_path,
268+
abs_file_path,
269+
pkg,
270+
&mut result,
271+
ws,
272+
)?;
303273
} else {
304274
let rel_msg = if license_path.is_absolute() {
305275
"".to_string()
@@ -321,6 +291,57 @@ fn build_ar_list(
321291
Ok(result)
322292
}
323293

294+
fn check_for_file_and_add(
295+
label: &str,
296+
file_path: &Path,
297+
abs_file_path: PathBuf,
298+
pkg: &Package,
299+
result: &mut Vec<ArchiveFile>,
300+
ws: &Workspace<'_>,
301+
) -> CargoResult<()> {
302+
match abs_file_path.strip_prefix(&pkg.root()) {
303+
Ok(rel_file_path) => {
304+
if !result.iter().any(|ar| ar.rel_path == rel_file_path) {
305+
result.push(ArchiveFile {
306+
rel_path: rel_file_path.to_path_buf(),
307+
rel_str: rel_file_path
308+
.to_str()
309+
.expect("everything was utf8")
310+
.to_string(),
311+
contents: FileContents::OnDisk(abs_file_path),
312+
})
313+
}
314+
}
315+
Err(_) => {
316+
// The file exists somewhere outside of the package.
317+
let file_name = file_path.file_name().unwrap();
318+
if result
319+
.iter()
320+
.any(|ar| ar.rel_path.file_name().unwrap() == file_name)
321+
{
322+
ws.config().shell().warn(&format!(
323+
"{} `{}` appears to be a path outside of the package, \
324+
but there is already a file named `{}` in the root of the package. \
325+
The archived crate will contain the copy in the root of the package. \
326+
Update the {} to point to the path relative \
327+
to the root of the package to remove this warning.",
328+
label,
329+
file_path.display(),
330+
file_name.to_str().unwrap(),
331+
label,
332+
))?;
333+
} else {
334+
result.push(ArchiveFile {
335+
rel_path: PathBuf::from(file_name),
336+
rel_str: file_name.to_str().unwrap().to_string(),
337+
contents: FileContents::OnDisk(abs_file_path),
338+
})
339+
}
340+
}
341+
}
342+
Ok(())
343+
}
344+
324345
/// Construct `Cargo.lock` for the package to be published.
325346
fn build_lock(ws: &Workspace<'_>, orig_pkg: &Package) -> CargoResult<String> {
326347
let config = ws.config();

0 commit comments

Comments
 (0)