Skip to content

Commit

Permalink
Merge branch 'master' into wasm-pack-test
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleygwilliams authored and fitzgen committed Sep 7, 2018
2 parents 041438e + d0fc382 commit dab809d
Show file tree
Hide file tree
Showing 23 changed files with 349 additions and 175 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
target/
**/target
**/*.rs.bk
**/pkg
tests/.crates.toml
tests/bin
wasm-pack.log
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ INSTALL_NODE_VIA_NVM: &INSTALL_NODE_VIA_NVM
rustup target add wasm32-unknown-unknown
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
source ~/.nvm/nvm.sh
nvm install v10.5
nvm install lts/carbon

# Cache `cargo install`ed tools, but don't cache the project's `target`
# directory (which ends up over-caching and filling all disk space!)
cache:
directories:
- /home/travis/.cargo

DEPLOY_TO_GITHUB: &DEPLOY_TO_GITHUB
before_deploy:
Expand Down
102 changes: 65 additions & 37 deletions src/binaries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,49 @@ use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use tar;
use target;
use which::which;
use zip;

/// Get the path for a crate's directory of locally-installed binaries.
///
/// This does not check whether or ensure that the directory exists.
pub fn local_bin_dir(crate_path: &Path) -> PathBuf {
crate_path.join("bin")
}

/// Ensure that the crate's directory for locally-installed binaries exists.
pub fn ensure_local_bin_dir(crate_path: &Path) -> io::Result<()> {
fs::create_dir_all(local_bin_dir(crate_path))
}

/// Get the path for where `bin` would be if we have a crate-local install for
/// it.
///
/// This does *not* check whether there is a file at that path or not.
///
/// This will automatically add the `.exe` extension for windows.
pub fn local_bin_path(crate_path: &Path, bin: &str) -> PathBuf {
let mut p = local_bin_dir(crate_path).join(bin);
if target::WINDOWS {
p.set_extension("exe");
}
p
}

/// Get the local (at `$CRATE/bin/$BIN`; preferred) or global (on `$PATH`) path
/// for the given binary.
///
/// If this function returns `Some(path)`, then a file at that path exists (or
/// at least existed when we checked! In general, we aren't really worried about
/// racing with an uninstall of a tool that we rely on.)
pub fn bin_path(log: &Logger, crate_path: &Path, bin: &str) -> Option<PathBuf> {
assert!(!bin.ends_with(".exe"));
debug!(log, "Searching for {} binary...", bin);

// Return the path to the local binary, if it exists.
let local_path = |crate_path: &Path| -> Option<PathBuf> {
let mut p = crate_path.to_path_buf();
p.push("bin");
if cfg!(target_os = "windows") {
let mut bin = bin.to_string();
bin.push_str(".exe");
p.push(bin);
} else {
p.push(bin);
}

let p = local_bin_path(crate_path, bin);
debug!(log, "Checking for local {} binary at {}", bin, p.display());
if p.is_file() {
Some(p)
Expand Down Expand Up @@ -62,40 +84,46 @@ pub fn bin_path(log: &Logger, crate_path: &Path, bin: &str) -> Option<PathBuf> {
})
}

fn with_url_context<T, E>(url: &str, r: Result<T, E>) -> Result<T, impl failure::Fail>
where
Result<T, E>: failure::ResultExt<T, E>,
{
use failure::ResultExt;
r.with_context(|_| format!("when requesting {}", url))
}

fn transfer(
url: &str,
easy: &mut curl::easy::Easy,
data: &mut Vec<u8>,
) -> Result<(), failure::Error> {
let mut transfer = easy.transfer();
with_url_context(
url,
transfer.write_function(|part| {
data.extend_from_slice(part);
Ok(part.len())
}),
)?;
with_url_context(url, transfer.perform())?;
Ok(())
}

fn curl(url: &str) -> Result<Vec<u8>, failure::Error> {
let mut data = Vec::new();

fn with_url_context<T, E>(url: &str, r: Result<T, E>) -> Result<T, impl failure::Fail>
where
Result<T, E>: failure::ResultExt<T, E>,
{
use failure::ResultExt;
r.with_context(|_| format!("when requesting {}", url))
}

let mut easy = curl::easy::Easy::new();
with_url_context(url, easy.follow_location(true))?;
with_url_context(url, easy.url(url))?;
transfer(url, &mut easy, &mut data)?;

{
let mut transfer = easy.transfer();
with_url_context(
url,
transfer.write_function(|part| {
data.extend_from_slice(part);
Ok(part.len())
}),
)?;
with_url_context(url, transfer.perform())?;
}

let code = with_url_context(url, easy.response_code())?;
if 200 <= code && code < 300 {
let status_code = with_url_context(url, easy.response_code())?;
if 200 <= status_code && status_code < 300 {
Ok(data)
} else {
Err(Error::http(&format!(
"received a bad HTTP status code ({}) when requesting {}",
code, url
status_code, url
)).into())
}
}
Expand All @@ -117,8 +145,8 @@ where
let tarball = curl(&url).map_err(|e| Error::http(&e.to_string()))?;
let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(&tarball[..]));

let bin = crate_path.join("bin");
fs::create_dir_all(&bin)?;
ensure_local_bin_dir(crate_path)?;
let bin = local_bin_dir(crate_path);

for entry in archive.entries()? {
let mut entry = entry?;
Expand Down Expand Up @@ -166,8 +194,8 @@ where
let data = io::Cursor::new(data);
let mut zip = zip::ZipArchive::new(data)?;

let bin = crate_path.join("bin");
fs::create_dir_all(&bin)?;
ensure_local_bin_dir(crate_path)?;
let bin = local_bin_dir(crate_path);

for i in 0..zip.len() {
let mut entry = zip.by_index(i).unwrap();
Expand Down
16 changes: 6 additions & 10 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Functionality related to installing and running `wasm-bindgen`.

use binaries::{bin_path, install_binaries_from_targz_at_url};
use binaries::{self, bin_path, install_binaries_from_targz_at_url};
use emoji;
use error::Error;
use progressbar::Step;
Expand Down Expand Up @@ -77,17 +77,17 @@ pub fn download_prebuilt_wasm_bindgen(root_path: &Path, version: &str) -> Result
)
}

/// Use `cargo install` to install the `wasm-bindgen` CLI to the given root
/// path.
pub fn cargo_install_wasm_bindgen(root_path: &Path, version: &str) -> Result<(), Error> {
/// 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")
.arg("--force")
.arg("wasm-bindgen-cli")
.arg("--version")
.arg(version)
.arg("--root")
.arg(root_path)
.arg(crate_path)
.output()?;
if !output.status.success() {
let message = "Installing wasm-bindgen failed".to_string();
Expand All @@ -97,11 +97,7 @@ pub fn cargo_install_wasm_bindgen(root_path: &Path, version: &str) -> Result<(),
stderr: s.to_string(),
})
} else {
if cfg!(target_os = "windows") {
assert!(root_path.join("bin").join("wasm-bindgen.exe").is_file());
} else {
assert!(root_path.join("bin").join("wasm-bindgen").is_file());
}
assert!(binaries::local_bin_path(crate_path, "wasm-bindgen").is_file());
Ok(())
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/command/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ use progressbar::Step;
use slog::Logger;
use std::path::PathBuf;
use std::time::Instant;
use test;
use webdriver;
use test::{self, webdriver};
use PBAR;

#[derive(Debug, Default, StructOpt)]
Expand Down Expand Up @@ -129,8 +128,10 @@ impl Test {
}

if headless && !any_browser {
return Error::crate_config("The `--headless` flag only applies to browser tests")
.map(|_| unreachable!());
return Error::crate_config(
"The `--headless` flag only applies to browser tests. Node does not provide a UI, \
so it doesn't make sense to talk about a headless version of Node tests.",
).map(|_| unreachable!());
}

Ok(Test {
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub mod progressbar;
pub mod readme;
pub mod target;
pub mod test;
pub mod webdriver;

use progressbar::ProgressOutput;

Expand Down
Loading

0 comments on commit dab809d

Please sign in to comment.