Check the Cagro.toml for a license and if one is found glob for LICENSE* files to copy to the out directorymaster
parent
51e6351c28
commit
2d837f795c
@ -0,0 +1,76 @@ |
|||||||
|
//! Copy `LICENSE` file(s) for the packaged wasm.
|
||||||
|
|
||||||
|
use failure; |
||||||
|
use std::fs; |
||||||
|
use std::path::Path; |
||||||
|
|
||||||
|
use emoji; |
||||||
|
use glob::glob; |
||||||
|
use manifest; |
||||||
|
use progressbar::Step; |
||||||
|
use PBAR; |
||||||
|
|
||||||
|
fn get_license(path: &Path) -> Option<String> { |
||||||
|
match manifest::get_crate_license(path) { |
||||||
|
Ok(license) => license, |
||||||
|
Err(_) => None, |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fn glob_license_files(path: &Path) -> Result<Vec<String>, failure::Error> { |
||||||
|
let mut license_files: Vec<String> = Vec::new(); |
||||||
|
for entry in glob(path.join("LICENSE*").to_str().unwrap())? { |
||||||
|
match entry { |
||||||
|
Ok(globed_path) => { |
||||||
|
license_files.push(String::from( |
||||||
|
globed_path.file_name().unwrap().to_str().unwrap(), |
||||||
|
)); |
||||||
|
} |
||||||
|
Err(e) => println!("{:?}", e), |
||||||
|
} |
||||||
|
} |
||||||
|
Ok(license_files) |
||||||
|
} |
||||||
|
|
||||||
|
/// Copy the crate's license into the `pkg` directory.
|
||||||
|
pub fn copy_from_crate(path: &Path, out_dir: &Path, step: &Step) -> 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 get_license(path) { |
||||||
|
Some(_) => { |
||||||
|
let msg = format!("{}Copying over your LICENSE...", emoji::DANCERS); |
||||||
|
PBAR.step(step, &msg); |
||||||
|
let license_files = glob_license_files(path); |
||||||
|
|
||||||
|
match license_files { |
||||||
|
Ok(files) => { |
||||||
|
if files.len() == 0 { |
||||||
|
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 let Err(_) = fs::copy(&crate_license_path, &new_license_path) { |
||||||
|
PBAR.info("origin crate has no LICENSE"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
Err(_) => PBAR.info("origin crate has no LICENSE"), |
||||||
|
} |
||||||
|
} |
||||||
|
None => { |
||||||
|
PBAR.step(step, "No LICENSE found in Cargo.toml skipping..."); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
Ok(()) |
||||||
|
} |
@ -0,0 +1,125 @@ |
|||||||
|
extern crate failure; |
||||||
|
extern crate wasm_pack; |
||||||
|
|
||||||
|
use std::fs; |
||||||
|
|
||||||
|
use utils::{self, fixture}; |
||||||
|
use wasm_pack::license; |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn it_copies_a_license_default_path() { |
||||||
|
let fixture = fixture::single_license(); |
||||||
|
let out_dir = fixture.path.join("pkg"); |
||||||
|
fs::create_dir(&out_dir).expect("should create pkg directory OK"); |
||||||
|
|
||||||
|
let step = wasm_pack::progressbar::Step::new(1); |
||||||
|
assert!(license::copy_from_crate(&fixture.path, &out_dir, &step).is_ok()); |
||||||
|
|
||||||
|
let crate_license_path = fixture.path.join("LICENSE-WTFPL"); |
||||||
|
let pkg_license_path = out_dir.join("LICENSE-WTFPL"); |
||||||
|
println!( |
||||||
|
"wasm-pack: should have copied LICENSE from '{}' to '{}'", |
||||||
|
crate_license_path.display(), |
||||||
|
pkg_license_path.display() |
||||||
|
); |
||||||
|
assert!(fs::metadata(&crate_license_path).is_ok()); |
||||||
|
|
||||||
|
assert!(fs::metadata(&pkg_license_path).is_ok()); |
||||||
|
|
||||||
|
let crate_license = utils::file::read_file(&crate_license_path).unwrap(); |
||||||
|
let pkg_license = utils::file::read_file(&pkg_license_path).unwrap(); |
||||||
|
assert_eq!(crate_license, pkg_license); |
||||||
|
} |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn it_copies_a_license_provied_path() { |
||||||
|
let fixture = fixture::single_license(); |
||||||
|
let out_dir = fixture.path.join("pkg"); |
||||||
|
fs::create_dir(&out_dir).expect("should create pkg directory OK"); |
||||||
|
|
||||||
|
let step = wasm_pack::progressbar::Step::new(1); |
||||||
|
assert!(license::copy_from_crate(&fixture.path, &out_dir, &step).is_ok()); |
||||||
|
let crate_license_path = fixture.path.join("LICENSE-WTFPL"); |
||||||
|
let pkg_license_path = out_dir.join("LICENSE-WTFPL"); |
||||||
|
println!( |
||||||
|
"wasm-pack: should have copied LICENSE-WTFPL from '{}' to '{}'", |
||||||
|
crate_license_path.display(), |
||||||
|
pkg_license_path.display() |
||||||
|
); |
||||||
|
assert!(fs::metadata(&crate_license_path).is_ok()); |
||||||
|
assert!(fs::metadata(&pkg_license_path).is_ok()); |
||||||
|
|
||||||
|
let crate_license = utils::file::read_file(&crate_license_path).unwrap(); |
||||||
|
let pkg_license = utils::file::read_file(&pkg_license_path).unwrap(); |
||||||
|
assert_eq!(crate_license, pkg_license); |
||||||
|
} |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn it_copies_all_licenses_default_path() { |
||||||
|
let fixture = fixture::dual_license(); |
||||||
|
let out_dir = fixture.path.join("pkg"); |
||||||
|
fs::create_dir(&out_dir).expect("should create pkg directory OK"); |
||||||
|
|
||||||
|
let step = wasm_pack::progressbar::Step::new(1); |
||||||
|
assert!(license::copy_from_crate(&fixture.path, &out_dir, &step).is_ok()); |
||||||
|
|
||||||
|
let crate_license_path = fixture.path.join("LICENSE-WTFPL"); |
||||||
|
let pkg_license_path = out_dir.join("LICENSE-WTFPL"); |
||||||
|
|
||||||
|
let crate_license_path_2 = fixture.path.join("LICENSE-MIT"); |
||||||
|
let pkg_license_path_2 = out_dir.join("LICENSE-MIT"); |
||||||
|
|
||||||
|
println!( |
||||||
|
"wasm-pack: should have copied LICENSE from '{}' to '{}'", |
||||||
|
crate_license_path.display(), |
||||||
|
pkg_license_path.display() |
||||||
|
); |
||||||
|
assert!(fs::metadata(&crate_license_path).is_ok()); |
||||||
|
assert!(fs::metadata(&pkg_license_path).is_ok()); |
||||||
|
|
||||||
|
assert!(fs::metadata(&crate_license_path_2).is_ok()); |
||||||
|
assert!(fs::metadata(&pkg_license_path_2).is_ok()); |
||||||
|
|
||||||
|
let crate_license = utils::file::read_file(&crate_license_path).unwrap(); |
||||||
|
let pkg_license = utils::file::read_file(&pkg_license_path).unwrap(); |
||||||
|
assert_eq!(crate_license, pkg_license); |
||||||
|
|
||||||
|
let crate_license_2 = utils::file::read_file(&crate_license_path_2).unwrap(); |
||||||
|
let pkg_license_2 = utils::file::read_file(&pkg_license_path_2).unwrap(); |
||||||
|
assert_eq!(crate_license_2, pkg_license_2); |
||||||
|
} |
||||||
|
|
||||||
|
#[test] |
||||||
|
fn it_copies_all_licenses_provided_path() { |
||||||
|
let fixture = fixture::dual_license(); |
||||||
|
let out_dir = fixture.path.join("pkg"); |
||||||
|
fs::create_dir(&out_dir).expect("should create pkg directory OK"); |
||||||
|
|
||||||
|
let step = wasm_pack::progressbar::Step::new(1); |
||||||
|
assert!(license::copy_from_crate(&fixture.path, &out_dir, &step).is_ok()); |
||||||
|
|
||||||
|
let crate_license_path = fixture.path.join("LICENSE-WTFPL"); |
||||||
|
let pkg_license_path = out_dir.join("LICENSE-WTFPL"); |
||||||
|
|
||||||
|
let crate_license_path_2 = fixture.path.join("LICENSE-MIT"); |
||||||
|
let pkg_license_path_2 = out_dir.join("LICENSE-MIT"); |
||||||
|
|
||||||
|
println!( |
||||||
|
"wasm-pack: should have copied LICENSE from '{}' to '{}'", |
||||||
|
crate_license_path.display(), |
||||||
|
pkg_license_path.display() |
||||||
|
); |
||||||
|
assert!(fs::metadata(&crate_license_path).is_ok()); |
||||||
|
assert!(fs::metadata(&pkg_license_path).is_ok()); |
||||||
|
|
||||||
|
assert!(fs::metadata(&crate_license_path_2).is_ok()); |
||||||
|
assert!(fs::metadata(&pkg_license_path_2).is_ok()); |
||||||
|
|
||||||
|
let crate_license = utils::file::read_file(&crate_license_path).unwrap(); |
||||||
|
let pkg_license = utils::file::read_file(&pkg_license_path).unwrap(); |
||||||
|
assert_eq!(crate_license, pkg_license); |
||||||
|
|
||||||
|
let crate_license_2 = utils::file::read_file(&crate_license_path_2).unwrap(); |
||||||
|
let pkg_license_2 = utils::file::read_file(&pkg_license_path_2).unwrap(); |
||||||
|
assert_eq!(crate_license_2, pkg_license_2); |
||||||
|
} |
Loading…
Reference in new issue