From 09046aa40bc1eba7be9009e2f428d00739f1c93b Mon Sep 17 00:00:00 2001 From: kohensu Date: Tue, 5 Jun 2018 20:12:33 +0200 Subject: [PATCH 1/3] Add --skip-build flag for init command This flag skips: adding target compiling to wasm installing wasm-bindgen running wasm-bindgen --- src/bindgen.rs | 25 ++-- src/build.rs | 22 +-- src/command.rs | 325 ++++++++++++++++++++++++++++------------- src/manifest.rs | 14 +- src/progressbar.rs | 26 ++++ src/readme.rs | 12 +- tests/manifest/main.rs | 15 +- tests/readme/main.rs | 6 +- 8 files changed, 286 insertions(+), 159 deletions(-) diff --git a/src/bindgen.rs b/src/bindgen.rs index 0cd14fd5..3796b470 100644 --- a/src/bindgen.rs +++ b/src/bindgen.rs @@ -1,16 +1,12 @@ -use console::style; use emoji; use error::Error; +use progressbar::Step; use std::process::Command; use PBAR; -pub fn cargo_install_wasm_bindgen() -> Result<(), Error> { - let step = format!( - "{} {}Installing WASM-bindgen...", - style("[6/7]").bold().dim(), - emoji::DOWN_ARROW - ); - let pb = PBAR.message(&step); +pub fn cargo_install_wasm_bindgen(step: &Step) -> Result<(), Error> { + let msg = format!("{}Installing WASM-bindgen...", emoji::DOWN_ARROW); + let pb = PBAR.step(step, &msg); let output = Command::new("cargo") .arg("install") .arg("wasm-bindgen-cli") @@ -33,15 +29,12 @@ pub fn wasm_bindgen_build( path: &str, name: &str, disable_dts: bool, - target: String, + target: &str, debug: bool, + step: &Step, ) -> Result<(), Error> { - let step = format!( - "{} {}Running WASM-bindgen...", - style("[7/7]").bold().dim(), - emoji::RUNNER - ); - let pb = PBAR.message(&step); + let msg = format!("{}Running WASM-bindgen...", emoji::RUNNER); + let pb = PBAR.step(step, &msg); let binary_name = name.replace("-", "_"); let release_or_debug = if debug { "debug" } else { "release" }; let wasm_path = format!( @@ -54,7 +47,7 @@ pub fn wasm_bindgen_build( "--no-typescript" }; - let target_arg = match target.as_str() { + let target_arg = match target { "nodejs" => "--nodejs", _ => "--browser", }; diff --git a/src/build.rs b/src/build.rs index d5cdb31d..95175f6b 100644 --- a/src/build.rs +++ b/src/build.rs @@ -1,16 +1,12 @@ -use console::style; use emoji; use error::Error; +use progressbar::Step; use std::process::Command; use PBAR; -pub fn rustup_add_wasm_target() -> Result<(), Error> { - let step = format!( - "{} {}Adding WASM target...", - style("[1/7]").bold().dim(), - emoji::TARGET - ); - let pb = PBAR.message(&step); +pub fn rustup_add_wasm_target(step: &Step) -> Result<(), Error> { + let msg = format!("{}Adding WASM target...", emoji::TARGET); + let pb = PBAR.step(step, &msg); let output = Command::new("rustup") .arg("target") .arg("add") @@ -25,13 +21,9 @@ pub fn rustup_add_wasm_target() -> Result<(), Error> { } } -pub fn cargo_build_wasm(path: &str, debug: bool) -> Result<(), Error> { - let step = format!( - "{} {}Compiling to WASM...", - style("[2/7]").bold().dim(), - emoji::CYCLONE - ); - let pb = PBAR.message(&step); +pub fn cargo_build_wasm(path: &str, debug: bool, step: &Step) -> Result<(), Error> { + let msg = format!("{}Compiling to WASM...", emoji::CYCLONE); + let pb = PBAR.step(step, &msg); let output = { let mut cmd = Command::new("cargo"); cmd.current_dir(path).arg("build"); diff --git a/src/command.rs b/src/command.rs index bcdcc99c..f818f4ca 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,11 +1,11 @@ use bindgen; use build; -use console::style; use emoji; use error::Error; use indicatif::HumanDuration; use manifest; use npm; +use progressbar::Step; #[allow(unused)] use readme; use slog::Logger; @@ -24,6 +24,10 @@ pub enum Command { #[structopt(long = "scope", short = "s")] scope: Option, + #[structopt(long = "--skip-build")] + /// Do not build, only update metadata + skip_build: bool, + #[structopt(long = "no-typescript")] /// By default a *.d.ts file is generated for the generated JS file, but /// this flag will disable generating this TypeScript file. @@ -86,6 +90,7 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error Command::Init { path, scope, + skip_build, disable_dts, target, debug, @@ -93,14 +98,20 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error info!(&log, "Running init command..."); info!( &log, - "Path: {:?}, Scope: {:?}, Disable Dts: {}, Target: {}, Debug: {}", + "Path: {:?}, Scope: {:?}, Skip build: {}, Disable Dts: {}, Target: {}, Debug: {}", &path, &scope, + &skip_build, &disable_dts, &target, debug ); - init(path, scope, disable_dts, target, &log, debug) + let mode = if skip_build { + InitMode::Nobuild + } else { + InitMode::Normal + }; + Init::new(path, scope, disable_dts, target, debug).process(&log, mode) } Command::Pack { path } => { info!(&log, "Running pack command..."); @@ -151,129 +162,235 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error // quicli::prelude::* imports a different result struct which gets // precedence over the std::result::Result, so have had to specify // the correct type here. -pub fn create_pkg_dir(path: &str) -> result::Result<(), Error> { - let step = format!( - "{} {}Creating a pkg directory...", - style("[3/7]").bold().dim(), - emoji::FOLDER - ); - let pb = PBAR.message(&step); +pub fn create_pkg_dir(path: &str, step: &Step) -> result::Result<(), Error> { + let msg = format!("{}Creating a pkg directory...", emoji::FOLDER); + let pb = PBAR.step(step, &msg); let pkg_dir_path = format!("{}/pkg", path); fs::create_dir_all(pkg_dir_path)?; pb.finish(); Ok(()) } -fn init( - path: Option, +enum InitMode { + Normal, + Nobuild, +} + +struct Init { + crate_path: String, scope: Option, disable_dts: bool, target: String, - log: &Logger, debug: bool, -) -> result::Result<(), Error> { - let started = Instant::now(); - - let crate_path = set_crate_path(path); - - info!(&log, "Checking wasm-bindgen dependency..."); - manifest::check_wasm_bindgen(&crate_path)?; - info!(&log, "wasm-bindgen dependency is correctly declared."); - - info!(&log, "Adding wasm-target..."); - build::rustup_add_wasm_target()?; - info!(&log, "Adding wasm-target was successful."); + crate_name: Option, +} - info!(&log, "Building wasm..."); - build::cargo_build_wasm(&crate_path, debug)?; +impl Init { + pub fn new( + path: Option, + scope: Option, + disable_dts: bool, + target: String, + debug: bool, + ) -> Init { + Init { + crate_path: set_crate_path(path), + scope, + disable_dts, + target, + debug, + crate_name: None, + } + } - #[cfg(not(target_os = "windows"))] - info!( - &log, - "wasm built at {}/target/wasm32-unknown-unknown/release.", &crate_path - ); - #[cfg(target_os = "windows")] - info!( - &log, - "wasm built at {}\\target\\wasm32-unknown-unknown\\release.", &crate_path - ); + pub fn process(&mut self, log: &Logger, mode: InitMode) -> result::Result<(), Error> { + let process_steps: Vec result::Result<(), Error>> = + match mode { + InitMode::Normal => vec![ + Init::step_check_dependency, + Init::step_add_wasm_target, + Init::step_build_wasm, + Init::step_create_dir, + Init::step_create_json, + Init::step_copy_readme, + Init::step_check_create_type, + Init::step_install_wasm_bindgen, + Init::step_running_wasm_bindgen, + ], + InitMode::Nobuild => vec![ + Init::step_check_dependency, + Init::step_create_dir, + Init::step_create_json, + Init::step_copy_readme, + ], + }; + let mut step_counter = Step::new(process_steps.len()); + + let started = Instant::now(); + + for process_step in process_steps { + process_step(self, &step_counter, log)?; + step_counter.inc(); + } - info!(&log, "Creating a pkg directory..."); - create_pkg_dir(&crate_path)?; - info!(&log, "Created a pkg directory at {}.", &crate_path); + let duration = HumanDuration(started.elapsed()); + info!(&log, "Done in {}.", &duration); + info!( + &log, + "Your WASM pkg is ready to publish at {}/pkg.", &self.crate_path + ); + + PBAR.message(&format!("{} Done in {}", emoji::SPARKLE, &duration)); + + PBAR.message(&format!( + "{} Your WASM pkg is ready to publish at {}/pkg.", + emoji::PACKAGE, + &self.crate_path + )); + Ok(()) + } - info!(&log, "Writing a package.json..."); - manifest::write_package_json(&crate_path, scope, disable_dts)?; - #[cfg(not(target_os = "windows"))] - info!( - &log, - "Wrote a package.json at {}/pkg/package.json.", &crate_path - ); - #[cfg(target_os = "windows")] - info!( - &log, - "Wrote a package.json at {}\\pkg\\package.json.", &crate_path - ); + fn step_check_dependency(&mut self, _step: &Step, log: &Logger) -> result::Result<(), Error> { + info!(&log, "Checking wasm-bindgen dependency..."); + manifest::check_wasm_bindgen(&self.crate_path)?; + info!(&log, "wasm-bindgen dependency is correctly declared."); + Ok(()) + } - info!(&log, "Copying readme from crate..."); - readme::copy_from_crate(&crate_path)?; - #[cfg(not(target_os = "windows"))] - info!(&log, "Copied readme from crate to {}/pkg.", &crate_path); - #[cfg(target_os = "windows")] - info!(&log, "Copied readme from crate to {}\\pkg.", &crate_path); + fn step_add_wasm_target(&mut self, step: &Step, log: &Logger) -> result::Result<(), Error> { + info!(&log, "Adding wasm-target..."); + build::rustup_add_wasm_target(step)?; + info!(&log, "Adding wasm-target was successful."); + Ok(()) + } - info!(&log, "Checking the crate type from the manifest..."); - manifest::check_crate_type(&crate_path)?; - #[cfg(not(target_os = "windows"))] - info!( - &log, - "Checked crate type from the manifest at {}/Cargo.toml.", &crate_path - ); - #[cfg(target_os = "windows")] - info!( - &log, - "Checked crate type from the manifest at {}\\Cargo.toml.", &crate_path - ); + fn step_build_wasm(&mut self, step: &Step, log: &Logger) -> result::Result<(), Error> { + 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 + ); + Ok(()) + } - info!(&log, "Installing wasm-bindgen-cli..."); - bindgen::cargo_install_wasm_bindgen()?; - info!(&log, "Installing wasm-bindgen-cli was successful."); + fn step_create_dir(&mut self, step: &Step, log: &Logger) -> result::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); + Ok(()) + } - info!(&log, "Getting the crate name from the manifest..."); - let name = manifest::get_crate_name(&crate_path)?; - #[cfg(not(target_os = "windows"))] - info!( - &log, - "Got crate name {} from the manifest at {}/Cargo.toml.", &name, &crate_path - ); - #[cfg(target_os = "windows")] - info!( - &log, - "Got crate name {} from the manifest at {}\\Cargo.toml.", &name, &crate_path - ); + fn step_create_json(&mut self, step: &Step, log: &Logger) -> result::Result<(), Error> { + info!(&log, "Writing a package.json..."); + manifest::write_package_json(&self.crate_path, &self.scope, self.disable_dts, 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 + ); + Ok(()) + } - info!(&log, "Building the wasm bindings..."); - bindgen::wasm_bindgen_build(&crate_path, &name, disable_dts, target, debug)?; - #[cfg(not(target_os = "windows"))] - info!(&log, "wasm bindings were built at {}/pkg.", &crate_path); - #[cfg(target_os = "windows")] - info!(&log, "wasm bindings were built at {}\\pkg.", &crate_path); + fn step_copy_readme(&mut self, step: &Step, log: &Logger) -> result::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 + ); + Ok(()) + } - let duration = HumanDuration(started.elapsed()); - info!(&log, "Done in {}.", &duration); - info!( - &log, - "Your WASM pkg is ready to publish at {}/pkg.", &crate_path - ); + fn step_check_create_type(&mut self, _step: &Step, log: &Logger) -> result::Result<(), Error> { + info!(&log, "Checking the crate type from the manifest..."); + manifest::check_crate_type(&self.crate_path)?; + #[cfg(not(target_os = "windows"))] + info!( + &log, + "Checked crate type from the manifest at {}/Cargo.toml.", &self.crate_path + ); + #[cfg(target_os = "windows")] + info!( + &log, + "Checked crate type from the manifest at {}\\Cargo.toml.", &self.crate_path + ); + + Ok(()) + } - PBAR.message(&format!("{} Done in {}", emoji::SPARKLE, &duration)); + fn step_install_wasm_bindgen( + &mut self, + step: &Step, + log: &Logger, + ) -> result::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 = Some(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.as_ref().unwrap(), + &self.crate_path + ); + #[cfg(target_os = "windows")] + info!( + &log, + "Got crate name {} from the manifest at {}\\Cargo.toml.", + &self.crate_name.as_ref().unwrap(), + &self.crate_path + ); + Ok(()) + } - PBAR.message(&format!( - "{} Your WASM pkg is ready to publish at {}/pkg.", - emoji::PACKAGE, - &crate_path - )); - Ok(()) + fn step_running_wasm_bindgen( + &mut self, + step: &Step, + log: &Logger, + ) -> result::Result<(), Error> { + info!(&log, "Building the wasm bindings..."); + bindgen::wasm_bindgen_build( + &self.crate_path, + &self.crate_name.as_ref().unwrap(), + self.disable_dts, + &self.target, + 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 + ); + Ok(()) + } } fn pack(path: Option, log: &Logger) -> result::Result<(), Error> { diff --git a/src/manifest.rs b/src/manifest.rs index 872924ef..668947d6 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -4,6 +4,7 @@ use std::io::prelude::*; use console::style; use emoji; use error::Error; +use progressbar::Step; use serde_json; use toml; use PBAR; @@ -67,7 +68,7 @@ fn read_cargo_toml(path: &str) -> Result { } impl CargoManifest { - fn into_npm(mut self, scope: Option, disable_dts: bool) -> NpmPackage { + fn into_npm(mut self, scope: &Option, disable_dts: bool) -> NpmPackage { let filename = self.package.name.replace("-", "_"); let wasm_file = format!("{}_bg.wasm", filename); let js_file = format!("{}.js", filename); @@ -109,14 +110,11 @@ impl CargoManifest { /// Generate a package.json file inside in `./pkg`. pub fn write_package_json( path: &str, - scope: Option, + scope: &Option, disable_dts: bool, + step: &Step, ) -> Result<(), Error> { - let step = format!( - "{} {}Writing a package.json...", - style("[4/7]").bold().dim(), - emoji::MEMO - ); + let msg = format!("{}Writing a package.json...", emoji::MEMO); let warn_fmt = |field| { format!( @@ -125,7 +123,7 @@ pub fn write_package_json( ) }; - let pb = PBAR.message(&step); + let pb = PBAR.step(step, &msg); let pkg_file_path = format!("{}/pkg/package.json", path); let mut pkg_file = File::create(pkg_file_path)?; let crate_data = read_cargo_toml(path)?; diff --git a/src/progressbar.rs b/src/progressbar.rs index a5a6e6e2..a02a548b 100644 --- a/src/progressbar.rs +++ b/src/progressbar.rs @@ -2,6 +2,7 @@ use console::style; use emoji; use error::Error; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; +use std::fmt; pub struct ProgressOutput { bar: MultiProgress, @@ -14,6 +15,11 @@ impl ProgressOutput { } } + pub fn step(&self, step: &Step, message: &str) -> ProgressBar { + let msg = format!("{} {}", style(step).bold().dim(), message); + self.bar.add(Self::progressbar(&msg)) + } + pub fn message(&self, message: &str) -> ProgressBar { self.bar.add(Self::progressbar(message)) } @@ -67,3 +73,23 @@ impl ProgressOutput { self.bar.join_and_clear().map_err(|e| Error::from(e)) } } + +pub struct Step { + current: usize, + total: usize, +} + +impl Step { + pub fn new(total: usize) -> Step { + Step { current: 1, total } + } + pub fn inc(&mut self) { + self.current += 1; + } +} + +impl fmt::Display for Step { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "[{}/{}]", self.current, self.total) + } +} diff --git a/src/readme.rs b/src/readme.rs index 7178cbab..7e0cae26 100644 --- a/src/readme.rs +++ b/src/readme.rs @@ -1,17 +1,13 @@ -use console::style; use error::Error; use std::fs; use emoji; +use progressbar::Step; use PBAR; -pub fn copy_from_crate(path: &str) -> Result<(), Error> { - let step = format!( - "{} {}Copying over your README...", - style("[5/7]").bold().dim(), - emoji::DANCERS - ); - let pb = PBAR.message(&step); +pub fn copy_from_crate(path: &str, step: &Step) -> Result<(), Error> { + let msg = format!("{}Copying over your README...", emoji::DANCERS); + let pb = PBAR.step(step, &msg); let crate_readme_path = format!("{}/README.md", path); let new_readme_path = format!("{}/pkg/README.md", path); if let Err(_) = fs::copy(&crate_readme_path, &new_readme_path) { diff --git a/tests/manifest/main.rs b/tests/manifest/main.rs index cf154380..ebc3e85d 100644 --- a/tests/manifest/main.rs +++ b/tests/manifest/main.rs @@ -42,9 +42,10 @@ fn it_checks_has_cdylib_wrong_crate_type() { #[test] fn it_creates_a_package_json_default_path() { + let step = wasm_pack::progressbar::Step::new(1); let path = ".".to_string(); - wasm_pack::command::create_pkg_dir(&path).unwrap(); - assert!(manifest::write_package_json(&path, None, false).is_ok()); + wasm_pack::command::create_pkg_dir(&path, &step).unwrap(); + assert!(manifest::write_package_json(&path, &None, false, &step).is_ok()); let package_json_path = format!("{}/pkg/package.json", &path); assert!(fs::metadata(package_json_path).is_ok()); assert!(utils::read_package_json(&path).is_ok()); @@ -63,9 +64,10 @@ fn it_creates_a_package_json_default_path() { #[test] fn it_creates_a_package_json_provided_path() { + let step = wasm_pack::progressbar::Step::new(1); let path = "tests/fixtures/js-hello-world".to_string(); - wasm_pack::command::create_pkg_dir(&path).unwrap(); - assert!(manifest::write_package_json(&path, None, false).is_ok()); + wasm_pack::command::create_pkg_dir(&path, &step).unwrap(); + assert!(manifest::write_package_json(&path, &None, false, &step).is_ok()); let package_json_path = format!("{}/pkg/package.json", &path); assert!(fs::metadata(package_json_path).is_ok()); assert!(utils::read_package_json(&path).is_ok()); @@ -75,9 +77,10 @@ fn it_creates_a_package_json_provided_path() { #[test] fn it_creates_a_package_json_provided_path_with_scope() { + let step = wasm_pack::progressbar::Step::new(1); let path = "tests/fixtures/scopes".to_string(); - wasm_pack::command::create_pkg_dir(&path).unwrap(); - assert!(manifest::write_package_json(&path, Some("test".to_string()), false).is_ok()); + wasm_pack::command::create_pkg_dir(&path, &step).unwrap(); + assert!(manifest::write_package_json(&path, &Some("test".to_string()), false, &step).is_ok()); let package_json_path = format!("{}/pkg/package.json", &path); assert!(fs::metadata(package_json_path).is_ok()); assert!(utils::read_package_json(&path).is_ok()); diff --git a/tests/readme/main.rs b/tests/readme/main.rs index 6ab324f6..65d59da4 100644 --- a/tests/readme/main.rs +++ b/tests/readme/main.rs @@ -9,8 +9,9 @@ use wasm_pack::readme; #[test] fn it_copies_a_readme_default_path() { + let step = wasm_pack::progressbar::Step::new(1); let path = ".".to_string(); - assert!(readme::copy_from_crate(&path).is_ok()); + assert!(readme::copy_from_crate(&path, &step).is_ok()); let crate_readme_path = format!("{}/README.md", &path); let pkg_readme_path = format!("{}/pkg/README.md", &path); assert!(fs::metadata(&pkg_readme_path).is_ok()); @@ -21,8 +22,9 @@ fn it_copies_a_readme_default_path() { #[test] fn it_creates_a_package_json_provided_path() { + let step = wasm_pack::progressbar::Step::new(1); let path = "tests/fixtures/js-hello-world".to_string(); - assert!(readme::copy_from_crate(&path).is_ok()); + assert!(readme::copy_from_crate(&path, &step).is_ok()); let crate_readme_path = format!("{}/README.md", &path); let pkg_readme_path = format!("{}/pkg/README.md", &path); assert!(fs::metadata(&pkg_readme_path).is_ok()); From 365813e96eb7a942cc3682884217c0fd45c5830e Mon Sep 17 00:00:00 2001 From: kohensu Date: Wed, 13 Jun 2018 12:39:10 +0200 Subject: [PATCH 2/3] Add doc for --skip-build --- docs/init.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/init.md b/docs/init.md index 9ea239a0..91355c78 100644 --- a/docs/init.md +++ b/docs/init.md @@ -58,3 +58,15 @@ The exact meaning of this flag may evolve as the platform matures. [npm-scope-documentation]: https://docs.npmjs.com/misc/scope [cargo-profile-sections-documentation]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-profile-sections + +## Skipping build + +The init command accepts an optional `--skip-build` argument. + +This will deactivate those steps: +- installing wasm target (via cargo) +- compiling the code to wasm +- installing wasm-bindgen (via rustup) +- running wasm-bindgen on the built wasm + +Basically it will remains only the steps that update the metadata of `package.json`. From 74fe847f77fcff8067842c9001de81fb99a726ed Mon Sep 17 00:00:00 2001 From: kohensu Date: Wed, 13 Jun 2018 12:06:32 +0200 Subject: [PATCH 3/3] Add test of --skip-build --- src/command.rs | 103 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 82 insertions(+), 21 deletions(-) diff --git a/src/command.rs b/src/command.rs index f818f4ca..310d36c6 100644 --- a/src/command.rs +++ b/src/command.rs @@ -185,6 +185,8 @@ struct Init { crate_name: Option, } +type InitStep = fn(&mut Init, &Step, &Logger) -> result::Result<(), Error>; + impl Init { pub fn new( path: Option, @@ -203,32 +205,47 @@ impl Init { } } - pub fn process(&mut self, log: &Logger, mode: InitMode) -> result::Result<(), Error> { - let process_steps: Vec result::Result<(), Error>> = - match mode { - InitMode::Normal => vec![ - Init::step_check_dependency, - Init::step_add_wasm_target, - Init::step_build_wasm, - Init::step_create_dir, - Init::step_create_json, - Init::step_copy_readme, - Init::step_check_create_type, - Init::step_install_wasm_bindgen, - Init::step_running_wasm_bindgen, - ], - InitMode::Nobuild => vec![ - Init::step_check_dependency, - Init::step_create_dir, - Init::step_create_json, - Init::step_copy_readme, - ], + fn get_process_steps(mode: InitMode) -> 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),*]) + } + + match mode { + InitMode::Normal => steps![ + step_check_dependency, + step_add_wasm_target, + step_build_wasm, + step_create_dir, + step_create_json, + step_copy_readme, + step_check_create_type, + step_install_wasm_bindgen, + step_running_wasm_bindgen, + ], + InitMode::Nobuild => steps![ + step_check_dependency, + step_create_dir, + step_create_json, + step_copy_readme, + ], + } + } + + pub fn process(&mut self, log: &Logger, mode: InitMode) -> result::Result<(), Error> { + let process_steps = Init::get_process_steps(mode); + let mut step_counter = Step::new(process_steps.len()); let started = Instant::now(); - for process_step in process_steps { + for (_, process_step) in process_steps { process_step(self, &step_counter, log)?; step_counter.inc(); } @@ -453,3 +470,47 @@ fn set_crate_path(path: Option) -> String { crate_path } + +#[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_dependency", + "step_add_wasm_target", + "step_build_wasm", + "step_create_dir", + "step_create_json", + "step_copy_readme", + "step_check_create_type", + "step_install_wasm_bindgen", + "step_running_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_check_dependency", + "step_create_dir", + "step_create_json", + "step_copy_readme" + ] + ); + } +}