From 7ce7a2ad4b8de14b9e5778e734f07b63e35ab2c0 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Wed, 25 Jul 2018 10:52:03 +0800 Subject: [PATCH] rebase --- src/command/build.rs | 67 ++++++----------- src/command/init.rs | 159 ++++------------------------------------- src/command/utils.rs | 11 ++- tests/manifest/main.rs | 10 +-- 4 files changed, 53 insertions(+), 194 deletions(-) diff --git a/src/command/build.rs b/src/command/build.rs index e7a16a294..c9ebf59f9 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -8,12 +8,13 @@ use manifest; use progressbar::Step; use readme; use slog::Logger; +use std::path::PathBuf; use std::time::Instant; use PBAR; /// Everything required to configure and run the `wasm-pack init` command. pub(crate) struct Build { - pub crate_path: String, + pub crate_path: PathBuf, pub scope: Option, pub disable_dts: bool, pub target: String, @@ -36,7 +37,8 @@ pub enum BuildMode { #[derive(Debug, StructOpt)] pub struct BuildOptions { /// The path to the Rust crate. - pub path: Option, + #[structopt(parse(from_os_str))] + pub path: Option, /// The npm scope to use in package.json, if any. #[structopt(long = "scope", short = "s")] @@ -99,15 +101,16 @@ impl Build { info!(&log, "Done in {}.", &duration); info!( &log, - "Your WASM pkg is ready to publish at {}/pkg.", &self.crate_path + "Your WASM pkg is ready to publish at {:#?}.", + &self.crate_path.join("pkg") ); PBAR.message(&format!("{} Done in {}", emoji::SPARKLE, &duration)); PBAR.message(&format!( - "{} Your WASM pkg is ready to publish at {}/pkg.", + "{} Your WASM pkg is ready to publish at {:#?}.", emoji::PACKAGE, - &self.crate_path + &self.crate_path.join("pkg") )); Ok(()) } @@ -163,15 +166,14 @@ impl Build { info!(&log, "Building wasm..."); build::cargo_build_wasm(&self.crate_path, self.debug, step)?; - #[cfg(not(target_os = "windows"))] info!( &log, - "wasm built at {}/target/wasm32-unknown-unknown/release.", &self.crate_path - ); - #[cfg(target_os = "windows")] - info!( - &log, - "wasm built at {}\\target\\wasm32-unknown-unknown\\release.", &self.crate_path + "wasm built at {:#?}.", + &self + .crate_path + .join("target") + .join("wasm32-unknown-unknown") + .join("release") ); Ok(()) } @@ -179,7 +181,7 @@ impl Build { fn step_create_dir(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { info!(&log, "Creating a pkg directory..."); create_pkg_dir(&self.crate_path, step)?; - info!(&log, "Created a pkg directory at {}.", &self.crate_path); + info!(&log, "Created a pkg directory at {:#?}.", &self.crate_path); Ok(()) } @@ -192,15 +194,10 @@ impl Build { &self.target, step, )?; - #[cfg(not(target_os = "windows"))] - info!( - &log, - "Wrote a package.json at {}/pkg/package.json.", &self.crate_path - ); - #[cfg(target_os = "windows")] info!( &log, - "Wrote a package.json at {}\\pkg\\package.json.", &self.crate_path + "Wrote a package.json at {:#?}.", + &self.crate_path.join("pkg").join("package.json") ); Ok(()) } @@ -208,15 +205,10 @@ impl Build { fn step_copy_readme(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { info!(&log, "Copying readme from crate..."); readme::copy_from_crate(&self.crate_path, step)?; - #[cfg(not(target_os = "windows"))] - info!( - &log, - "Copied readme from crate to {}/pkg.", &self.crate_path - ); - #[cfg(target_os = "windows")] info!( &log, - "Copied readme from crate to {}\\pkg.", &self.crate_path + "Copied readme from crate to {:#?}.", + &self.crate_path.join("pkg") ); Ok(()) } @@ -228,19 +220,11 @@ impl Build { info!(&log, "Getting the crate name from the manifest..."); self.crate_name = manifest::get_crate_name(&self.crate_path)?; - #[cfg(not(target_os = "windows"))] - info!( - &log, - "Got crate name {} from the manifest at {}/Cargo.toml.", - &self.crate_name, - &self.crate_path - ); - #[cfg(target_os = "windows")] info!( &log, - "Got crate name {} from the manifest at {}\\Cargo.toml.", + "Got crate name {:#?} from the manifest at {:#?}.", &self.crate_name, - &self.crate_path + &self.crate_path.join("Cargo.toml") ); Ok(()) } @@ -255,15 +239,10 @@ impl Build { self.debug, step, )?; - #[cfg(not(target_os = "windows"))] - info!( - &log, - "wasm bindings were built at {}/pkg.", &self.crate_path - ); - #[cfg(target_os = "windows")] info!( &log, - "wasm bindings were built at {}\\pkg.", &self.crate_path + "wasm bindings were built at {:#?}.", + &self.crate_path.join("pkg") ); Ok(()) } diff --git a/src/command/init.rs b/src/command/init.rs index 7f6213b1a..29ad44ec4 100644 --- a/src/command/init.rs +++ b/src/command/init.rs @@ -8,33 +8,10 @@ use manifest; use progressbar::Step; use readme; use slog::Logger; -use std::fs; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::time::Instant; use PBAR; -/// Construct our `pkg` directory in the crate. -pub fn create_pkg_dir(path: &Path, step: &Step) -> Result<(), Error> { - let msg = format!("{}Creating a pkg directory...", emoji::FOLDER); - PBAR.step(step, &msg); - let pkg_dir_path = path.join("pkg"); - fs::create_dir_all(pkg_dir_path)?; - Ok(()) -} - -/// The `InitMode` determines which mode of initialization we are running, and -/// what build and install steps we perform. -pub enum InitMode { - /// Perform all the build and install steps. - Normal, - /// Don't build the crate as a `.wasm` but do install tools and create - /// meta-data. - Nobuild, - /// Don't install tools like `wasm-bindgen`, just use the global - /// environment's existing versions to do builds. - Noinstall, -} - /// Everything required to configure and run the `wasm-pack init` command. pub struct Init { crate_path: PathBuf, @@ -47,7 +24,8 @@ pub struct Init { #[derive(Debug, StructOpt)] pub struct InitOptions { /// The path to the Rust crate. - pub path: Option, + #[structopt(parse(from_os_str))] + pub path: Option, /// The npm scope to use in package.json, if any. #[structopt(long = "scope", short = "s")] @@ -109,34 +87,18 @@ impl Init { Ok(()) } - fn step_check_crate_config(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { - info!(&log, "Checking crate configuration..."); - manifest::check_crate_config(&self.crate_path, step)?; - info!(&log, "Crate is correctly configured."); - Ok(()) - } - - fn step_add_wasm_target(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { - info!(&log, "Adding wasm-target..."); - build::rustup_add_wasm_target(step)?; - info!(&log, "Adding wasm-target was successful."); - Ok(()) - } - - fn step_build_wasm(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { - info!(&log, "Building wasm..."); - build::cargo_build_wasm(&self.crate_path, self.debug, step)?; - - info!( - &log, - "wasm built at {:#?}.", - &self - .crate_path - .join("target") - .join("wasm32-unknown-unknown") - .join("release") - ); - Ok(()) + fn set_process_steps() -> Vec<(&'static str, InitStep)> { + macro_rules! steps { + ($($name:ident),+) => { + { + let mut steps: Vec<(&'static str, InitStep)> = Vec::new(); + $(steps.push((stringify!($name), Init::$name));)* + steps + } + }; + ($($name:ident,)*) => (steps![$($name),*]) + } + steps![step_create_dir, step_create_json, step_copy_readme,] } fn step_create_dir(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { @@ -173,95 +135,4 @@ impl Init { ); Ok(()) } - - fn step_install_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { - info!(&log, "Installing wasm-bindgen-cli..."); - bindgen::cargo_install_wasm_bindgen(step)?; - info!(&log, "Installing wasm-bindgen-cli was successful."); - - info!(&log, "Getting the crate name from the manifest..."); - self.crate_name = manifest::get_crate_name(&self.crate_path)?; - info!( - &log, - "Got crate name {:#?} from the manifest at {:#?}.", - &self.crate_name, - &self.crate_path.join("Cargo.toml") - ); - Ok(()) - } - - fn step_run_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { - info!(&log, "Building the wasm bindings..."); - bindgen::wasm_bindgen_build( - &self.crate_path, - &self.crate_name, - self.disable_dts, - &self.target, - self.debug, - step, - )?; - info!( - &log, - "wasm bindings were built at {:#?}.", - &self.crate_path.join("pkg") - ); - Ok(()) - } -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn init_normal_build() { - let steps: Vec<&str> = Init::get_process_steps(InitMode::Normal) - .into_iter() - .map(|(n, _)| n) - .collect(); - assert_eq!( - steps, - [ - "step_check_crate_config", - "step_add_wasm_target", - "step_build_wasm", - "step_create_dir", - "step_create_json", - "step_copy_readme", - "step_install_wasm_bindgen", - "step_run_wasm_bindgen" - ] - ); - } - - #[test] - fn init_skip_build() { - let steps: Vec<&str> = Init::get_process_steps(InitMode::Nobuild) - .into_iter() - .map(|(n, _)| n) - .collect(); - assert_eq!( - steps, - ["step_create_dir", "step_create_json", "step_copy_readme"] - ); - } - - #[test] - fn init_skip_install() { - let steps: Vec<&str> = Init::get_process_steps(InitMode::Noinstall) - .into_iter() - .map(|(n, _)| n) - .collect(); - assert_eq!( - steps, - [ - "step_check_crate_config", - "step_build_wasm", - "step_create_dir", - "step_create_json", - "step_copy_readme", - "step_run_wasm_bindgen" - ] - ); - } } diff --git a/src/command/utils.rs b/src/command/utils.rs index 220f60c34..c66b089e7 100644 --- a/src/command/utils.rs +++ b/src/command/utils.rs @@ -1,10 +1,10 @@ //! Utility functions for commands. -use std::path::{Path, PathBuf}; use emoji; use error::Error; use progressbar::Step; use std::fs; +use std::path::{Path, PathBuf}; use PBAR; /// If an explicit path is given, then use it, otherwise assume the current @@ -18,6 +18,15 @@ pub fn set_crate_path(path: Option) -> PathBuf { crate_path } +/// Construct our `pkg` directory in the crate. +pub fn create_pkg_dir(path: &Path, step: &Step) -> Result<(), Error> { + let msg = format!("{}Creating a pkg directory...", emoji::FOLDER); + PBAR.step(step, &msg); + let pkg_dir_path = path.join("pkg"); + fs::create_dir_all(pkg_dir_path)?; + Ok(()) +} + /// Locates the pkg directory from a specific path /// Returns None if unable to find the 'pkg' directory pub fn find_pkg_directory(path: &Path) -> Option { diff --git a/tests/manifest/main.rs b/tests/manifest/main.rs index e9e765cad..41142fddd 100644 --- a/tests/manifest/main.rs +++ b/tests/manifest/main.rs @@ -62,7 +62,7 @@ fn it_recognizes_a_map_during_depcheck() { fn it_creates_a_package_json_default_path() { let step = wasm_pack::progressbar::Step::new(1); let path = PathBuf::from("."); - wasm_pack::command::init::create_pkg_dir(&path, &step).unwrap(); + wasm_pack::command::utils::create_pkg_dir(&path, &step).unwrap(); assert!(manifest::write_package_json(&path, &None, false, "", &step).is_ok()); let package_json_path = &path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); @@ -90,7 +90,7 @@ fn it_creates_a_package_json_default_path() { fn it_creates_a_package_json_provided_path() { let step = wasm_pack::progressbar::Step::new(1); let path = PathBuf::from("tests/fixtures/js-hello-world"); - wasm_pack::command::init::create_pkg_dir(&path, &step).unwrap(); + wasm_pack::command::utils::create_pkg_dir(&path, &step).unwrap(); assert!(manifest::write_package_json(&path, &None, false, "", &step).is_ok()); let package_json_path = &path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); @@ -111,7 +111,7 @@ fn it_creates_a_package_json_provided_path() { fn it_creates_a_package_json_provided_path_with_scope() { let step = wasm_pack::progressbar::Step::new(1); let path = PathBuf::from("tests/fixtures/scopes"); - wasm_pack::command::init::create_pkg_dir(&path, &step).unwrap(); + wasm_pack::command::utils::create_pkg_dir(&path, &step).unwrap(); assert!( manifest::write_package_json(&path, &Some("test".to_string()), false, "", &step).is_ok() ); @@ -134,7 +134,7 @@ fn it_creates_a_package_json_provided_path_with_scope() { fn it_creates_a_pkg_json_with_correct_files_on_node() { let step = wasm_pack::progressbar::Step::new(1); let path = PathBuf::from("."); - wasm_pack::command::init::create_pkg_dir(&path, &step).unwrap(); + wasm_pack::command::utils::create_pkg_dir(&path, &step).unwrap(); assert!(manifest::write_package_json(&path, &None, false, "nodejs", &step).is_ok()); let package_json_path = &path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); @@ -163,7 +163,7 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() { fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() { let step = wasm_pack::progressbar::Step::new(1); let path = PathBuf::from("."); - wasm_pack::command::init::create_pkg_dir(&path, &step).unwrap(); + wasm_pack::command::utils::create_pkg_dir(&path, &step).unwrap(); assert!(manifest::write_package_json(&path, &None, true, "", &step).is_ok()); let package_json_path = &path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok());