Skip to content

Commit

Permalink
feat(bindgen): check that wasm-bindgen versions match, with dummy data
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleygwilliams committed Jul 16, 2019
1 parent a354119 commit 6a3e980
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 68 deletions.
31 changes: 13 additions & 18 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,26 @@ pub fn wasm_bindgen_build(
cmd.arg("--keep-debug");
}

let versions_match = versions_match(&bindgen_path, "0.0.0")?;
assert!(versions_match, "Something went wrong! wasm-bindgen CLI and dependencies version don't match. You should file an issue: https://github.com/rustwasm/wasm-pack/issues/new?template=bug_report.md.");

child::run(cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?;
Ok(())
}

/// Check if the `wasm-bindgen` dependency is locally satisfied.
fn wasm_bindgen_version_check(bindgen_path: &PathBuf, dep_version: &str) -> bool {
wasm_bindgen_get_version(bindgen_path)
.map(|v| {
info!(
"Checking installed `wasm-bindgen` version == expected version: {} == {}",
v, dep_version
);
v == dep_version
})
.unwrap_or(false)
fn versions_match(cli_path: &PathBuf, dep_version: &str) -> Result<bool, failure::Error> {
let cli_version = cli_version(cli_path)?;
Ok(cli_version == dep_version)
}

/// Get the `wasm-bindgen` version
fn wasm_bindgen_get_version(bindgen_path: &PathBuf) -> Option<String> {
/// Get the `wasm-bindgen` CLI version
fn cli_version(bindgen_path: &PathBuf) -> Result<String, failure::Error> {
let mut cmd = Command::new(bindgen_path);
cmd.arg("--version");
child::run_capture_stdout(cmd, &Tool::WasmBindgen)
.map(|stdout| match stdout.trim().split_whitespace().nth(1) {
Some(v) => return Some(v.to_owned()),
None => return None,
})
.unwrap_or(None)
let stdout = child::run_capture_stdout(cmd, &Tool::WasmBindgen)?;
match stdout.trim().split_whitespace().nth(1) {
Some(v) => Ok(v.to_string()),
None => bail!("Something went wrong! No wasm-bindgen CLI found! You should file an issue: https://github.com/rustwasm/wasm-pack/issues/new?template=bug_report.md."),
}
}
64 changes: 14 additions & 50 deletions src/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
use failure::Error;
use install::Tool;
use log::info;
use std::io::Error as StdError;
use std::process::{Command, ExitStatus, Stdio};
use std::process::{Command, Stdio};

/// Return a new Command object
pub fn new_command(program: &str) -> Command {
Expand All @@ -25,35 +24,6 @@ pub fn new_command(program: &str) -> Command {
}
}

/// Error from running Command processes
/// This captures the standard error output
#[derive(Fail, Debug)]
#[fail(display = "failed to execute `{}`: {}", command_name, fail_reason)]
pub struct CommandError {
command_name: String,
fail_reason: String,
/// the output printed to stderr, if any
pub stderr: Option<String>,
}

impl CommandError {
fn from_status(command_name: &str, status: ExitStatus, stderr: Option<String>) -> CommandError {
CommandError {
command_name: command_name.to_string(),
fail_reason: format!("exited with {}", status),
stderr: stderr,
}
}

fn from_error(command_name: &str, err: StdError) -> CommandError {
CommandError {
command_name: command_name.to_string(),
fail_reason: err.to_string(),
stderr: None,
}
}
}

/// Run the given command and return on success.
pub fn run(mut command: Command, command_name: &str) -> Result<(), Error> {
info!("Running {:?}", command);
Expand All @@ -72,27 +42,21 @@ pub fn run(mut command: Command, command_name: &str) -> Result<(), Error> {
}

/// Run the given command and return its stdout.
pub fn run_capture_stdout(
mut command: Command,
command_name: &Tool,
) -> Result<String, CommandError> {
pub fn run_capture_stdout(mut command: Command, command_name: &Tool) -> Result<String, Error> {
info!("Running {:?}", command);

let cmd_display = command_name.to_string();
let output = command
.stderr(Stdio::inherit())
.stdin(Stdio::inherit())
.output()?;

let cmd_output = command.stdin(Stdio::inherit()).output();
match cmd_output {
Ok(output) => {
if output.status.success() {
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
} else {
Err(CommandError::from_status(
&cmd_display,
output.status,
Some(String::from_utf8_lossy(&output.stderr).into_owned()),
))
}
}
Err(e) => Err(CommandError::from_error(&cmd_display, e)),
if output.status.success() {
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
} else {
bail!(
"failed to execute `{}`: exited with {}",
command_name,
output.status
)
}
}

0 comments on commit 6a3e980

Please # to comment.