From 272ef97befcca09cccd667181c786b1664d57c99 Mon Sep 17 00:00:00 2001 From: Mason Stallmo Date: Sat, 17 Nov 2018 10:53:44 -0600 Subject: [PATCH] Refactor: make `crate_license` a method of CrateData. --- src/command/build.rs | 2 +- src/lib.rs | 3 +-- src/license.rs | 18 ++++++++---------- src/manifest/mod.rs | 5 +++++ tests/all/license.rs | 13 +++++++++---- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/command/build.rs b/src/command/build.rs index 2779851..c495cdc 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -306,7 +306,7 @@ impl Build { fn step_copy_license(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> { 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); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index bebdb2a..03cd436 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,8 +9,8 @@ extern crate dirs; #[macro_use] extern crate failure; extern crate flate2; -extern crate hex; extern crate glob; +extern crate hex; extern crate indicatif; #[macro_use] extern crate lazy_static; @@ -37,7 +37,6 @@ pub mod build; pub mod child; pub mod command; pub mod emoji; -pub mod error; pub mod license; pub mod lockfile; pub mod logger; diff --git a/src/license.rs b/src/license.rs index 44391e7..0b91e78 100644 --- a/src/license.rs +++ b/src/license.rs @@ -6,17 +6,10 @@ use std::path::Path; use emoji; use glob::glob; -use manifest; +use manifest::CrateData; use progressbar::Step; use PBAR; -fn get_license(path: &Path) -> Option { - match manifest::get_crate_license(path) { - Ok(license) => license, - Err(_) => None, - } -} - fn glob_license_files(path: &Path) -> Result, failure::Error> { let mut license_files: Vec = Vec::new(); for entry in glob(path.join("LICENSE*").to_str().unwrap())? { @@ -33,7 +26,12 @@ fn glob_license_files(path: &Path) -> Result, failure::Error> { } /// 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!( fs::metadata(path).ok().map_or(false, |m| m.is_dir()), "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" ); - match get_license(path) { + match crate_data.crate_license() { Some(_) => { let msg = format!("{}Copying over your LICENSE...", emoji::DANCERS); PBAR.step(step, &msg); diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 8c3e390..0be6c28 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -289,6 +289,11 @@ impl CrateData { } } + /// Get the license for the crate at the given path. + pub fn crate_license(&self) -> &Option { + &self.manifest.package.license + } + /// Returns the path to this project's target directory where artifacts are /// located after a cargo build. pub fn target_directory(&self) -> &Path { diff --git a/tests/all/license.rs b/tests/all/license.rs index 376bd4d..ac7d377 100644 --- a/tests/all/license.rs +++ b/tests/all/license.rs @@ -5,15 +5,17 @@ use std::fs; use utils::{self, fixture}; use wasm_pack::license; +use wasm_pack::manifest::CrateData; #[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 crate_data = CrateData::new(&fixture.path); 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 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 out_dir = fixture.path.join("pkg"); 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); - 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 pkg_license_path = out_dir.join("LICENSE-WTFPL"); println!( @@ -59,9 +62,10 @@ 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 crate_data = CrateData::new(&fixture.path); 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 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 out_dir = fixture.path.join("pkg"); 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); - 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 pkg_license_path = out_dir.join("LICENSE-WTFPL");