Skip to content

Commit

Permalink
cmd(init/build): rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
csmoe committed Jul 27, 2018
1 parent d0ac2fa commit ddab496
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 189 deletions.
67 changes: 23 additions & 44 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub disable_dts: bool,
pub target: String,
Expand All @@ -36,7 +37,8 @@ pub enum BuildMode {
#[derive(Debug, StructOpt)]
pub struct BuildOptions {
/// The path to the Rust crate.
pub path: Option<String>,
#[structopt(parse(from_os_str))]
pub path: Option<PathBuf>,

/// The npm scope to use in package.json, if any.
#[structopt(long = "scope", short = "s")]
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -163,23 +166,22 @@ 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(())
}

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(())
}

Expand All @@ -192,31 +194,21 @@ 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(())
}

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(())
}
Expand All @@ -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(())
}
Expand All @@ -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(())
}
Expand Down
159 changes: 15 additions & 144 deletions src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -47,7 +24,8 @@ pub struct Init {
#[derive(Debug, StructOpt)]
pub struct InitOptions {
/// The path to the Rust crate.
pub path: Option<String>,
#[structopt(parse(from_os_str))]
pub path: Option<PathBuf>,

/// The npm scope to use in package.json, if any.
#[structopt(long = "scope", short = "s")]
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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"
]
);
}
}
11 changes: 10 additions & 1 deletion src/command/utils.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,6 +18,15 @@ pub fn set_crate_path(path: Option<PathBuf>) -> 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<PathBuf> {
Expand Down

0 comments on commit ddab496

Please sign in to comment.