1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! Copy `LICENSE` file(s) for the packaged wasm.

use failure;
use std::fs;
use std::path::Path;

use glob::glob;
use manifest::CrateData;
use PBAR;

fn glob_license_files(path: &Path) -> Result<Vec<String>, failure::Error> {
    let mut license_files: Vec<String> = Vec::new();
    let path_string = match path.join("LICENSE*").to_str() {
        Some(path_string) => path_string.to_owned(),
        None => {
            return Err(format_err!(
                "Could not convert joined license path to String"
            ));
        }
    };

    for entry in glob(&path_string)? {
        match entry {
            Ok(globed_path) => {
                let file_name = match globed_path.file_name() {
                    Some(file_name) => file_name,
                    None => return Err(format_err!("Could not get file name from path")),
                };
                let file_name_string = match file_name.to_str() {
                    Some(file_name_string) => file_name_string.to_owned(),
                    None => return Err(format_err!("Could not convert filename to String")),
                };
                license_files.push(file_name_string);
            }
            Err(e) => println!("{:?}", e),
        }
    }
    Ok(license_files)
}

/// Copy the crate's license into the `pkg` directory.
pub fn copy_from_crate(
    crate_data: &CrateData,
    path: &Path,
    out_dir: &Path,
) -> Result<(), failure::Error> {
    assert!(
        fs::metadata(path).ok().map_or(false, |m| m.is_dir()),
        "crate directory should exist"
    );

    assert!(
        fs::metadata(&out_dir).ok().map_or(false, |m| m.is_dir()),
        "crate's pkg directory should exist"
    );

    match (crate_data.crate_license(), crate_data.crate_license_file()) {
        (Some(_), _) => {
            let license_files = glob_license_files(path);

            match license_files {
                Ok(files) => {
                    if files.is_empty() {
                        PBAR.info("License key is set in Cargo.toml but no LICENSE file(s) were found; Please add the LICENSE file(s) to your project directory");
                        return Ok(());
                    }
                    for license_file in files {
                        let crate_license_path = path.join(&license_file);
                        let new_license_path = out_dir.join(&license_file);
                        if fs::copy(&crate_license_path, &new_license_path).is_err() {
                            PBAR.info("origin crate has no LICENSE");
                        }
                    }
                }
                Err(_) => PBAR.info("origin crate has no LICENSE"),
            }
        }
        (None, Some(license_file)) => {
            let crate_license_path = path.join(&license_file);
            let new_license_path = out_dir.join(&license_file);
            if fs::copy(&crate_license_path, &new_license_path).is_err() {
                PBAR.info("origin crate has no LICENSE");
            }
        }
        (None, None) => {}
    };

    Ok(())
}