From ddab496f51758cf9e13c7bab14d9a98270f37739 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] cmd(init/build): rebase --- src/command/build.rs | 67 +++++++----------- src/command/init.rs | 159 ++++--------------------------------------- src/command/utils.rs | 11 ++- 3 files changed, 48 insertions(+), 189 deletions(-) diff --git a/src/command/build.rs b/src/command/build.rs index e7a16a29..c9ebf59f 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 7f6213b1..29ad44ec 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 220f60c3..c66b089e 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 {