Refactor: make `crate_license` a method of CrateData.

master
Mason Stallmo 7 years ago
parent 1c704b7b7c
commit 272ef97bef
  1. 2
      src/command/build.rs
  2. 3
      src/lib.rs
  3. 18
      src/license.rs
  4. 5
      src/manifest/mod.rs
  5. 13
      tests/all/license.rs

@ -306,7 +306,7 @@ impl Build {
fn step_copy_license(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> { fn step_copy_license(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
info!(&log, "Copying license from crate..."); info!(&log, "Copying license from crate...");
license::copy_from_crate(&self.crate_path, &self.out_dir, step)?; license::copy_from_crate(&self.crate_data, &self.crate_path, &self.out_dir, step)?;
info!(&log, "Copied license from crate to {:#?}.", &self.out_dir); info!(&log, "Copied license from crate to {:#?}.", &self.out_dir);
Ok(()) Ok(())
} }

@ -9,8 +9,8 @@ extern crate dirs;
#[macro_use] #[macro_use]
extern crate failure; extern crate failure;
extern crate flate2; extern crate flate2;
extern crate hex;
extern crate glob; extern crate glob;
extern crate hex;
extern crate indicatif; extern crate indicatif;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
@ -37,7 +37,6 @@ pub mod build;
pub mod child; pub mod child;
pub mod command; pub mod command;
pub mod emoji; pub mod emoji;
pub mod error;
pub mod license; pub mod license;
pub mod lockfile; pub mod lockfile;
pub mod logger; pub mod logger;

@ -6,17 +6,10 @@ use std::path::Path;
use emoji; use emoji;
use glob::glob; use glob::glob;
use manifest; use manifest::CrateData;
use progressbar::Step; use progressbar::Step;
use PBAR; 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> { fn glob_license_files(path: &Path) -> Result<Vec<String>, failure::Error> {
let mut license_files: Vec<String> = Vec::new(); let mut license_files: Vec<String> = Vec::new();
for entry in glob(path.join("LICENSE*").to_str().unwrap())? { for entry in glob(path.join("LICENSE*").to_str().unwrap())? {
@ -33,7 +26,12 @@ fn glob_license_files(path: &Path) -> Result<Vec<String>, failure::Error> {
} }
/// Copy the crate's license into the `pkg` directory. /// Copy the crate's license into the `pkg` directory.
pub fn copy_from_crate(path: &Path, out_dir: &Path, step: &Step) -> Result<(), failure::Error> { pub fn copy_from_crate(
crate_data: &CrateData,
path: &Path,
out_dir: &Path,
step: &Step,
) -> Result<(), failure::Error> {
assert!( assert!(
fs::metadata(path).ok().map_or(false, |m| m.is_dir()), fs::metadata(path).ok().map_or(false, |m| m.is_dir()),
"crate directory should exist" "crate directory should exist"
@ -44,7 +42,7 @@ pub fn copy_from_crate(path: &Path, out_dir: &Path, step: &Step) -> Result<(), f
"crate's pkg directory should exist" "crate's pkg directory should exist"
); );
match get_license(path) { match crate_data.crate_license() {
Some(_) => { Some(_) => {
let msg = format!("{}Copying over your LICENSE...", emoji::DANCERS); let msg = format!("{}Copying over your LICENSE...", emoji::DANCERS);
PBAR.step(step, &msg); PBAR.step(step, &msg);

@ -289,6 +289,11 @@ impl CrateData {
} }
} }
/// Get the license for the crate at the given path.
pub fn crate_license(&self) -> &Option<String> {
&self.manifest.package.license
}
/// Returns the path to this project's target directory where artifacts are /// Returns the path to this project's target directory where artifacts are
/// located after a cargo build. /// located after a cargo build.
pub fn target_directory(&self) -> &Path { pub fn target_directory(&self) -> &Path {

@ -5,15 +5,17 @@ use std::fs;
use utils::{self, fixture}; use utils::{self, fixture};
use wasm_pack::license; use wasm_pack::license;
use wasm_pack::manifest::CrateData;
#[test] #[test]
fn it_copies_a_license_default_path() { fn it_copies_a_license_default_path() {
let fixture = fixture::single_license(); let fixture = fixture::single_license();
let out_dir = fixture.path.join("pkg"); let out_dir = fixture.path.join("pkg");
fs::create_dir(&out_dir).expect("should create pkg directory OK"); fs::create_dir(&out_dir).expect("should create pkg directory OK");
let crate_data = CrateData::new(&fixture.path);
let step = wasm_pack::progressbar::Step::new(1); let step = wasm_pack::progressbar::Step::new(1);
assert!(license::copy_from_crate(&fixture.path, &out_dir, &step).is_ok()); assert!(license::copy_from_crate(&crate_data.unwrap(), &fixture.path, &out_dir, &step).is_ok());
let crate_license_path = fixture.path.join("LICENSE-WTFPL"); let crate_license_path = fixture.path.join("LICENSE-WTFPL");
let pkg_license_path = out_dir.join("LICENSE-WTFPL"); let pkg_license_path = out_dir.join("LICENSE-WTFPL");
@ -36,9 +38,10 @@ fn it_copies_a_license_provied_path() {
let fixture = fixture::single_license(); let fixture = fixture::single_license();
let out_dir = fixture.path.join("pkg"); let out_dir = fixture.path.join("pkg");
fs::create_dir(&out_dir).expect("should create pkg directory OK"); fs::create_dir(&out_dir).expect("should create pkg directory OK");
let crate_data = CrateData::new(&fixture.path);
let step = wasm_pack::progressbar::Step::new(1); let step = wasm_pack::progressbar::Step::new(1);
assert!(license::copy_from_crate(&fixture.path, &out_dir, &step).is_ok()); assert!(license::copy_from_crate(&crate_data.unwrap(), &fixture.path, &out_dir, &step).is_ok());
let crate_license_path = fixture.path.join("LICENSE-WTFPL"); let crate_license_path = fixture.path.join("LICENSE-WTFPL");
let pkg_license_path = out_dir.join("LICENSE-WTFPL"); let pkg_license_path = out_dir.join("LICENSE-WTFPL");
println!( println!(
@ -59,9 +62,10 @@ fn it_copies_all_licenses_default_path() {
let fixture = fixture::dual_license(); let fixture = fixture::dual_license();
let out_dir = fixture.path.join("pkg"); let out_dir = fixture.path.join("pkg");
fs::create_dir(&out_dir).expect("should create pkg directory OK"); fs::create_dir(&out_dir).expect("should create pkg directory OK");
let crate_data = CrateData::new(&fixture.path);
let step = wasm_pack::progressbar::Step::new(1); let step = wasm_pack::progressbar::Step::new(1);
assert!(license::copy_from_crate(&fixture.path, &out_dir, &step).is_ok()); assert!(license::copy_from_crate(&crate_data.unwrap(), &fixture.path, &out_dir, &step).is_ok());
let crate_license_path = fixture.path.join("LICENSE-WTFPL"); let crate_license_path = fixture.path.join("LICENSE-WTFPL");
let pkg_license_path = out_dir.join("LICENSE-WTFPL"); let pkg_license_path = out_dir.join("LICENSE-WTFPL");
@ -94,9 +98,10 @@ fn it_copies_all_licenses_provided_path() {
let fixture = fixture::dual_license(); let fixture = fixture::dual_license();
let out_dir = fixture.path.join("pkg"); let out_dir = fixture.path.join("pkg");
fs::create_dir(&out_dir).expect("should create pkg directory OK"); fs::create_dir(&out_dir).expect("should create pkg directory OK");
let crate_data = CrateData::new(&fixture.path);
let step = wasm_pack::progressbar::Step::new(1); let step = wasm_pack::progressbar::Step::new(1);
assert!(license::copy_from_crate(&fixture.path, &out_dir, &step).is_ok()); assert!(license::copy_from_crate(&crate_data.unwrap(), &fixture.path, &out_dir, &step).is_ok());
let crate_license_path = fixture.path.join("LICENSE-WTFPL"); let crate_license_path = fixture.path.join("LICENSE-WTFPL");
let pkg_license_path = out_dir.join("LICENSE-WTFPL"); let pkg_license_path = out_dir.join("LICENSE-WTFPL");

Loading…
Cancel
Save