refactor: Use path.exists() instead of using `ls` and `grep`, stop

unwraping
master
Jesper Håkansson 6 years ago
parent 740b8b896e
commit 4b9fd878dc
  1. 89
      src/build.rs

@ -6,10 +6,9 @@ use emoji;
use failure::{Error, ResultExt}; use failure::{Error, ResultExt};
use log::info; use log::info;
use progressbar::Step; use progressbar::Step;
use std::path::Path; use std::path::{Path, PathBuf};
use std::process::{Command, Stdio}; use std::process::Command;
use std::str; use std::str;
use std::string::FromUtf8Error;
use PBAR; use PBAR;
/// Ensure that `rustc` is present and that it is >= 1.30.0 /// Ensure that `rustc` is present and that it is >= 1.30.0
@ -51,46 +50,36 @@ fn rustc_minor_version() -> Option<u32> {
otry!(pieces.next()).parse().ok() otry!(pieces.next()).parse().ok()
} }
/// Get rustc's sysroot as a String /// Get rustc's sysroot as a PathBuf
fn get_rustc_sysroot() -> Result<String, FromUtf8Error> { fn get_rustc_sysroot() -> Result<PathBuf, Error> {
String::from_utf8( let command = Command::new("rustc")
Command::new("rustc") .args(&["--print", "sysroot"])
.args(&["--print", "sysroot"]) .output()?;
.stdout(Stdio::piped())
.output() if command.status.success() {
.unwrap() Ok(String::from_utf8(command.stdout)?.trim().into())
.stdout, } else {
) Err(format_err!(
"Getting rustc's sysroot wasn't successful. Got {}",
command.status
))
}
} }
/// Checks if the wasm32-unknown-unknown is present in rustc's sysroot. /// Checks if the wasm32-unknown-unknown is present in rustc's sysroot.
fn is_wasm32_target_in_sysroot(sysroot: &str) -> Result<bool, Error> { fn is_wasm32_target_in_sysroot(sysroot: &PathBuf) -> bool {
let wasm32_target = "wasm32-unknown-unknown"; let wasm32_target = "wasm32-unknown-unknown";
info!("Looking for {} in {}", wasm32_target, sysroot); let rustlib_path = sysroot.join("lib/rustlib");
let rustlib_path = &format!("{}/lib/rustlib", sysroot.replace("\n", ""));
let ls_command = Command::new("ls")
.arg(rustlib_path)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;
let grep_command_status = Command::new("grep") info!("Looking for {} in {:?}", wasm32_target, rustlib_path);
.args(&["-x", wasm32_target])
.stdin(ls_command.stdout.unwrap())
.status();
match grep_command_status { if rustlib_path.join(wasm32_target).exists() {
Ok(status) if status.success() => { info!("Found {} in {:?}", wasm32_target, rustlib_path);
info!("Found {} in {}", wasm32_target, sysroot); true
Ok(true) } else {
} info!("Failed to find {} in {:?}", wasm32_target, rustlib_path);
_ => { false
info!("Failed to find {} in {}", wasm32_target, sysroot);
Ok(false)
}
} }
} }
@ -98,28 +87,27 @@ fn check_wasm32_target() -> Result<bool, Error> {
let sysroot = get_rustc_sysroot()?; let sysroot = get_rustc_sysroot()?;
// If wasm32-unknown-unknown already exists we're ok. // If wasm32-unknown-unknown already exists we're ok.
match is_wasm32_target_in_sysroot(&sysroot) { if is_wasm32_target_in_sysroot(&sysroot) {
Ok(true) => Ok(true), Ok(true)
// If it doesn't exist, then we need to check if we're using rustup. // If it doesn't exist, then we need to check if we're using rustup.
_ => { } else {
// If sysroot contains .rustup, then we can assume we're using rustup // If sysroot contains .rustup, then we can assume we're using rustup
// and use rustup to add the wasm32-unknown-unknown target. // and use rustup to add the wasm32-unknown-unknown target.
if sysroot.contains(".rustup") { if sysroot.to_string_lossy().contains(".rustup") {
rustup_add_wasm_target() rustup_add_wasm_target().map(|()| true)
} else { } else {
Ok(false) Ok(false)
}
} }
} }
} }
/// Add wasm32-unknown-unknown using `rustup`. /// Add wasm32-unknown-unknown using `rustup`.
fn rustup_add_wasm_target() -> Result<bool, Error> { fn rustup_add_wasm_target() -> Result<(), Error> {
let mut cmd = Command::new("rustup"); let mut cmd = Command::new("rustup");
cmd.arg("target").arg("add").arg("wasm32-unknown-unknown"); cmd.arg("target").arg("add").arg("wasm32-unknown-unknown");
child::run(cmd, "rustup").context("Adding the wasm32-unknown-unknown target with rustup")?; child::run(cmd, "rustup").context("Adding the wasm32-unknown-unknown target with rustup")?;
Ok(true) Ok(())
} }
/// Ensure that `rustup` has the `wasm32-unknown-unknown` target installed for /// Ensure that `rustup` has the `wasm32-unknown-unknown` target installed for
@ -131,7 +119,8 @@ pub fn check_for_wasm32_target(step: &Step) -> Result<(), Error> {
// Check if wasm32 target is present, otherwise bail. // Check if wasm32 target is present, otherwise bail.
match check_wasm32_target() { match check_wasm32_target() {
Ok(true) => Ok(()), Ok(true) => Ok(()),
_ => bail!("wasm32-unknown-unknown target not found!"), Ok(false) => bail!("wasm32-unknown-unknown target not found!"),
Err(err) => Err(err),
} }
} }

Loading…
Cancel
Save