Skip to content

Commit

Permalink
Merge pull request #392 from fitzgen/child-process-and-output-management
Browse files Browse the repository at this point in the history
Child process and output management
  • Loading branch information
ashleygwilliams authored Oct 9, 2018
2 parents 921b724 + e9276e0 commit 2ca946e
Show file tree
Hide file tree
Showing 19 changed files with 378 additions and 199 deletions.
62 changes: 26 additions & 36 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Functionality related to installing and running `wasm-bindgen`.
use binaries::{self, bin_path, install_binaries_from_targz_at_url};
use child;
use emoji;
use error::Error;
use failure::{self, ResultExt};
use progressbar::Step;
use slog::Logger;
use std::path::{Path, PathBuf};
Expand All @@ -22,7 +24,7 @@ pub fn install_wasm_bindgen(
install_permitted: bool,
step: &Step,
log: &Logger,
) -> Result<(), Error> {
) -> Result<(), failure::Error> {
// If the `wasm-bindgen` dependency is already met, print a message and return.
if wasm_bindgen_path(log, root_path)
.map(|bindgen_path| wasm_bindgen_version_check(&bindgen_path, version, log))
Expand All @@ -37,7 +39,7 @@ pub fn install_wasm_bindgen(
// permitted, return a configuration error.
if !install_permitted {
let msg = format!("wasm-bindgen v{} is not installed!", version);
return Error::crate_config(&msg);
return Err(Error::crate_config(&msg).into());
}

let msg = format!("{}Installing wasm-bindgen...", emoji::DOWN_ARROW);
Expand All @@ -48,7 +50,7 @@ pub fn install_wasm_bindgen(
log,
"could not download pre-built `wasm-bindgen`: {}. Falling back to `cargo install`.", e
);
cargo_install_wasm_bindgen(root_path, version)
cargo_install_wasm_bindgen(log, root_path, version)
})
}

Expand Down Expand Up @@ -81,28 +83,23 @@ pub fn download_prebuilt_wasm_bindgen(root_path: &Path, version: &str) -> Result

/// Use `cargo install` to install the `wasm-bindgen` CLI locally into the given
/// crate.
pub fn cargo_install_wasm_bindgen(crate_path: &Path, version: &str) -> Result<(), Error> {
let output = Command::new("cargo")
.arg("install")
pub fn cargo_install_wasm_bindgen(
logger: &Logger,
crate_path: &Path,
version: &str,
) -> Result<(), failure::Error> {
let mut cmd = Command::new("cargo");
cmd.arg("install")
.arg("--force")
.arg("wasm-bindgen-cli")
.arg("--version")
.arg(version)
.arg("--root")
.arg(crate_path)
.output()?;
if !output.status.success() {
let message = "Installing wasm-bindgen failed".to_string();
let s = String::from_utf8_lossy(&output.stderr);
Err(Error::Cli {
message,
stderr: s.to_string(),
exit_status: output.status,
})
} else {
assert!(binaries::local_bin_path(crate_path, "wasm-bindgen").is_file());
Ok(())
}
.arg(crate_path);

child::run(logger, cmd, "cargo install").context("Installing wasm-bindgen with cargo")?;
assert!(binaries::local_bin_path(crate_path, "wasm-bindgen").is_file());
Ok(())
}

/// Run the `wasm-bindgen` CLI to generate bindings for the current crate's
Expand All @@ -116,7 +113,7 @@ pub fn wasm_bindgen_build(
debug: bool,
step: &Step,
log: &Logger,
) -> Result<(), Error> {
) -> Result<(), failure::Error> {
let msg = format!("{}Running WASM-bindgen...", emoji::RUNNER);
PBAR.step(step, &msg);

Expand Down Expand Up @@ -153,27 +150,20 @@ pub fn wasm_bindgen_build(
cmd.arg("--debug");
}

let output = cmd.output()?;
if !output.status.success() {
let s = String::from_utf8_lossy(&output.stderr);
Error::cli("wasm-bindgen failed to execute properly", s, output.status)
} else {
Ok(())
}
child::run(log, cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?;
Ok(())
} else {
Error::crate_config("Could not find `wasm-bindgen`")
Err(Error::crate_config("Could not find `wasm-bindgen`").into())
}
}

/// Check if the `wasm-bindgen` dependency is locally satisfied.
fn wasm_bindgen_version_check(bindgen_path: &PathBuf, dep_version: &str, log: &Logger) -> bool {
Command::new(bindgen_path)
.arg("--version")
.output()
.ok()
.filter(|output| output.status.success())
.map(|output| {
String::from_utf8_lossy(&output.stdout)
let mut cmd = Command::new(bindgen_path);
cmd.arg("--version");
child::run(log, cmd, "wasm-bindgen")
.map(|stdout| {
stdout
.trim()
.split_whitespace()
.nth(1)
Expand Down
70 changes: 29 additions & 41 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! Building a Rust crate into a `.wasm` binary.
use child;
use emoji;
use error::Error;
use failure::ResultExt;
use progressbar::Step;
use slog::Logger;
use std::path::Path;
use std::process::Command;
use std::str;
Expand Down Expand Up @@ -54,28 +57,23 @@ fn rustc_minor_version() -> Option<u32> {

/// Ensure that `rustup` has the `wasm32-unknown-unknown` target installed for
/// current toolchain
pub fn rustup_add_wasm_target(step: &Step) -> Result<(), Error> {
pub fn rustup_add_wasm_target(log: &Logger, step: &Step) -> Result<(), failure::Error> {
let msg = format!("{}Adding WASM target...", emoji::TARGET);
PBAR.step(step, &msg);
let output = Command::new("rustup")
.arg("target")
.arg("add")
.arg("wasm32-unknown-unknown")
.output()?;
if !output.status.success() {
let s = String::from_utf8_lossy(&output.stderr);
Error::cli(
"Adding the wasm32-unknown-unknown target failed",
s,
output.status,
)
} else {
Ok(())
}
let mut cmd = Command::new("rustup");
cmd.arg("target").arg("add").arg("wasm32-unknown-unknown");
child::run(log, cmd, "rustup")
.context("Adding the wasm32-unknown-unknown target with rustup")?;
Ok(())
}

/// Run `cargo build` targetting `wasm32-unknown-unknown`.
pub fn cargo_build_wasm(path: &Path, debug: bool, step: &Step) -> Result<(), Error> {
pub fn cargo_build_wasm(
log: &Logger,
path: &Path,
debug: bool,
step: &Step,
) -> Result<(), failure::Error> {
let msg = format!("{}Compiling to WASM...", emoji::CYCLONE);
PBAR.step(step, &msg);
let mut cmd = Command::new("cargo");
Expand All @@ -84,32 +82,22 @@ pub fn cargo_build_wasm(path: &Path, debug: bool, step: &Step) -> Result<(), Err
cmd.arg("--release");
}
cmd.arg("--target").arg("wasm32-unknown-unknown");
let output = cmd.output()?;

if !output.status.success() {
let s = String::from_utf8_lossy(&output.stderr);
Error::cli("Compilation of your program failed", s, output.status)
} else {
Ok(())
}
child::run(log, cmd, "cargo build").context("Compiling your crate to WebAssembly")?;
Ok(())
}

/// Run `cargo build --tests` targetting `wasm32-unknown-unknown`.
pub fn cargo_build_wasm_tests(path: &Path, debug: bool) -> Result<(), Error> {
let output = {
let mut cmd = Command::new("cargo");
cmd.current_dir(path).arg("build").arg("--tests");
if !debug {
cmd.arg("--release");
}
cmd.arg("--target").arg("wasm32-unknown-unknown");
cmd.output()?
};

if !output.status.success() {
let s = String::from_utf8_lossy(&output.stderr);
Error::cli("Compilation of your program failed", s, output.status)
} else {
Ok(())
pub fn cargo_build_wasm_tests(
log: &Logger,
path: &Path,
debug: bool,
) -> Result<(), failure::Error> {
let mut cmd = Command::new("cargo");
cmd.current_dir(path).arg("build").arg("--tests");
if !debug {
cmd.arg("--release");
}
cmd.arg("--target").arg("wasm32-unknown-unknown");
child::run(log, cmd, "cargo build").context("Compilation of your program failed")?;
Ok(())
}
Loading

0 comments on commit 2ca946e

Please sign in to comment.