feat(bindgen): check that wasm-bindgen versions match, with dummy data

master
Ashley Williams 6 years ago
parent a3541196c7
commit b72048b8c7
  1. 31
      src/bindgen.rs
  2. 64
      src/child.rs

@ -69,31 +69,26 @@ pub fn wasm_bindgen_build(
cmd.arg("--keep-debug");
}
let versions_match = versions_match(&bindgen_path, "0.2.37")?;
assert!(versions_match, "Something went wrong! wasm-bindgen CLI and dependency version don't match. This is likely not your fault! 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."),
}
}

@ -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 {
@ -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);
@ -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
)
}
}

Loading…
Cancel
Save