From c664cfddd1c256a990269cd53c0a2038d55d2205 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 2 Nov 2018 10:54:00 -0700 Subject: [PATCH] Use a global cache for all downloaded binaries This commit switches wasm-pack to using a global cache for all download binaries, living typically in a user's home directory. The intention here is to aovid creating a `bin` folder in all wasm-pack projects and additionally share downloads between projects to ensure that you're downloading a minimal number of binaries from the network. Along the way the downloading code was restructured to support a global cache, but everything should largely be as it was before! Closes #292 --- Cargo.lock | 191 +++++++++++++---- Cargo.toml | 3 + src/binaries.rs | 422 +++++++++++++++++++------------------ src/bindgen.rs | 208 +++++++++--------- src/command/build.rs | 19 +- src/command/test.rs | 29 +-- src/lib.rs | 3 + src/test/webdriver.rs | 137 +++++------- tests/all/bindgen.rs | 31 ++- tests/all/build.rs | 13 +- tests/all/test.rs | 63 +++--- tests/all/utils/fixture.rs | 144 +++++-------- tests/all/webdriver.rs | 7 +- 13 files changed, 670 insertions(+), 600 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7b20a0f..6804bd0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,10 +5,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "aho-corasick" -version = "0.6.8" +version = "0.6.9" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "memchr 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -19,6 +19,23 @@ dependencies = [ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "argon2rs" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "blake2-rfc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", + "scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "arrayvec" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "atty" version = "0.2.11" @@ -55,6 +72,15 @@ name = "bitflags" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "blake2-rfc" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", + "constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "build_const" version = "0.2.1" @@ -62,7 +88,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "byteorder" -version = "1.2.6" +version = "1.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -164,6 +190,11 @@ dependencies = [ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "constant_time_eq" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "crc" version = "1.8.1" @@ -194,13 +225,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", - "libz-sys 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", + "libz-sys 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-sys 0.9.39 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "dirs" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_users 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "error-chain" version = "0.12.0" @@ -224,9 +265,9 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.13 (registry+https://github.com/rust-lang/crates.io-index)", - "synstructure 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.18 (registry+https://github.com/rust-lang/crates.io-index)", + "synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -276,6 +317,19 @@ name = "fuchsia-zircon-sys" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "heck" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-segmentation 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "hex" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "human-panic" version = "1.0.1" @@ -348,7 +402,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "libz-sys" -version = "1.0.24" +version = "1.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", @@ -368,7 +422,7 @@ dependencies = [ [[package]] name = "memchr" -version = "2.1.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -413,6 +467,11 @@ dependencies = [ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "nodrop" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "num-integer" version = "0.1.39" @@ -521,7 +580,7 @@ dependencies = [ [[package]] name = "quote" -version = "0.6.8" +version = "0.6.9" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)", @@ -575,16 +634,27 @@ dependencies = [ "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "redox_users" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "argon2rs 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "regex" version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "aho-corasick 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", - "memchr 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", - "utf8-ranges 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -592,11 +662,11 @@ name = "regex" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "aho-corasick 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", - "memchr 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex-syntax 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", - "utf8-ranges 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -604,7 +674,7 @@ name = "regex-syntax" version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "ucd-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -612,7 +682,7 @@ name = "regex-syntax" version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "ucd-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -650,6 +720,11 @@ dependencies = [ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "scoped_threadpool" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "scopeguard" version = "0.3.3" @@ -680,8 +755,8 @@ version = "1.0.80" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.13 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.18 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -694,6 +769,11 @@ dependencies = [ "serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "siphasher" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "slog" version = "2.4.1" @@ -752,41 +832,42 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "structopt" -version = "0.2.12" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", - "structopt-derive 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "structopt-derive 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "structopt-derive" -version = "0.2.12" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ + "heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.13 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.18 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "syn" -version = "0.15.13" +version = "0.15.18" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "synstructure" -version = "0.10.0" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.13 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.18 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -833,7 +914,7 @@ name = "term" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -899,7 +980,12 @@ dependencies = [ [[package]] name = "ucd-util" -version = "0.1.1" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicode-segmentation" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -922,7 +1008,7 @@ dependencies = [ [[package]] name = "utf8-ranges" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -962,8 +1048,10 @@ dependencies = [ "cargo_metadata 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "console 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "curl 0.4.18 (registry+https://github.com/rust-lang/crates.io-index)", + "dirs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "flate2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "human-panic 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "indicatif 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -972,10 +1060,11 @@ dependencies = [ "serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)", + "siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "slog 2.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "slog-async 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "slog-term 2.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "structopt 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "structopt 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", "tar 0.4.17 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1051,14 +1140,17 @@ dependencies = [ [metadata] "checksum adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7e522997b529f05601e05166c07ed17789691f562762c7f3b987263d2dedee5c" -"checksum aho-corasick 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "68f56c7353e5a9547cbd76ed90f7bb5ffc3ba09d4ea9bd1d8c06c8b1142eeb5a" +"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e" "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +"checksum argon2rs 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3f67b0b6a86dae6e67ff4ca2b6201396074996379fba2b92ff649126f37cb392" +"checksum arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a1e964f9e24d588183fcb43503abda40d288c8657dfc27311516ce2f05675aef" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" "checksum backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "89a47830402e9981c5c41223151efcced65a0510c13097c769cede7efb34782a" "checksum backtrace-sys 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)" = "c66d56ac8dabd07f6aacdaf633f4b8262f5b3601a810a0dcddffd5c22c69daa0" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" +"checksum blake2-rfc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "5d6d530bdd2d52966a6d03b7a964add7ae1a288d25214066fd4b600f0f796400" "checksum build_const 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "39092a32794787acd8525ee150305ff051b0aa6cc2abaf193924f5ab05425f39" -"checksum byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "90492c5858dd7d2e78691cfb89f90d273a2800fc11d98f60786e5d87e2f83781" +"checksum byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94f88df23a25417badc922ab0f5716cc1330e87f71ddd9203b3a3ccd9cedf75d" "checksum bzip2 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "42b7c3cbf0fa9c1b82308d57191728ca0256cb821220f4e2fd410a72ade26e3b" "checksum bzip2-sys 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2c5162604199bbb17690ede847eaa6120a3f33d5ab4dcc8e7c25b16d849ae79b" "checksum cargo_metadata 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1aaa1a9856ae2d188340526d0986feb6899c9ad11c5dfd73453c784fed6e373d" @@ -1069,9 +1161,11 @@ dependencies = [ "checksum clicolors-control 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1f84dec9bc083ce2503908cd305af98bd363da6f54bf8d4bf0ac14ee749ad5d1" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum console 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ecd48adf136733979b49e15bc3b4c43cc0d3c85ece7bd08e6daa414c6fcb13e6" +"checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e" "checksum crc 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d663548de7f5cca343f1e0a48d14dcfb0e9eb4e079ec58883b7251539fa10aeb" "checksum curl 0.4.18 (registry+https://github.com/rust-lang/crates.io-index)" = "a9e5285b49b44401518c947d3b808d14d99a538a6c9ffb3ec0205c11f9fc4389" "checksum curl-sys 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)" = "08459503c415173da1ce6b41036a37b8bfdd86af46d45abb9964d4c61fe670ef" +"checksum dirs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "88972de891f6118092b643d85a0b28e0678e0f948d7f879aa32f2d5aafe97d2a" "checksum error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "07e791d3be96241c77c43846b665ef1384606da2cd2a48730abe606a12906e02" "checksum failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6dd377bcc1b1b7ce911967e3ec24fa19c3224394ec05b54aa7b083d498341ac7" "checksum failure_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "64c2d913fe8ed3b6c6518eedf4538255b989945c14c2a7d5cbff62a5e2120596" @@ -1081,6 +1175,8 @@ dependencies = [ "checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" +"checksum heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ea04fa3ead4e05e51a7c806fc07271fdbde4e246a6c6d1efd52e72230b771b82" +"checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" "checksum human-panic 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "21638c5955a6daf3ecc42cae702335fc37a72a4abcc6959ce457b31a7d43bbdd" "checksum indicatif 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a29b2fa6f00010c268bface64c18bb0310aaa70d46a195d5382d288c477fb016" "checksum isatty 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e31a8281fc93ec9693494da65fbf28c0c2aa60a2eaec25dc58e2f31952e95edc" @@ -1089,13 +1185,14 @@ dependencies = [ "checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" "checksum lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca488b89a5657b0a2ecd45b95609b3e848cf1755da332a0da46e2b2b1cb371a7" "checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d" -"checksum libz-sys 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)" = "4401fe74560a0d46fce3464625ac8aa7a79d291dd28cee021d18852d5191c280" +"checksum libz-sys 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)" = "2eb5e43362e38e2bca2fd5f5134c4d4564a23a5c28e9b95411652021a8675ebe" "checksum lock_api 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "775751a3e69bde4df9b38dd00a1b5d6ac13791e4223d4a0506577f0dd27cfb7a" -"checksum memchr 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4b3629fe9fdbff6daa6c33b90f7c08355c1aca05a3d01fa8063b822fcf185f3b" +"checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16" "checksum miniz-sys 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0300eafb20369952951699b68243ab4334f4b10a88f411c221d444b36c40e649" "checksum miniz_oxide 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5ad30a47319c16cde58d0314f5d98202a80c9083b5f61178457403dfb14e509c" "checksum miniz_oxide_c_api 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "28edaef377517fd9fe3e085c37d892ce7acd1fbeab9239c5a36eec352d8a8b7e" "checksum msdos_time 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "aad9dfe950c057b1bfe9c1f2aa51583a8468ef2a5baba2ebbe06d775efeb7729" +"checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2" "checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" "checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" "checksum openssl 0.10.15 (registry+https://github.com/rust-lang/crates.io-index)" = "5e1309181cdcbdb51bc3b6bedb33dfac2a83b3d585033d3f6d9e22e8c1928613" @@ -1109,13 +1206,14 @@ dependencies = [ "checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c" "checksum podio 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "780fb4b6698bbf9cf2444ea5d22411cef2953f0824b98f33cf454ec5615645bd" "checksum proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)" = "3d7b7eaaa90b4a90a932a9ea6666c95a389e424eff347f0f793979289429feee" -"checksum quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "dd636425967c33af890042c483632d33fa7a18f19ad1d7ea72e8998c6ef8dea5" +"checksum quote 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "63b5829244f52738cfee93b3a165c1911388675be000c888d2fae620dee8fa5b" "checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd" "checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c" "checksum rand_core 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1961a422c4d189dfb50ffa9320bf1f2a9bd54ecb92792fb9477f99a1045f3372" "checksum rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0905b6b7079ec73b314d4c748701f6931eb79fd97c668caa3f1899b22b32c6db" "checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" +"checksum redox_users 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "214a97e49be64fd2c86f568dd0cb2c757d2cc53de95b273b6ad0a1c908482f26" "checksum regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384" "checksum regex 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "2069749032ea3ec200ca51e4a31df41759190a88edca0d2d86ee8bedf7073341" "checksum regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7" @@ -1125,12 +1223,14 @@ dependencies = [ "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum ryu 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7153dd96dade874ab973e098cb62fcdbb89a03682e46b144fd09550998d4a4a7" "checksum schannel 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "0e1a231dc10abf6749cfa5d7767f25888d484201accbd919b66ab5413c502d56" +"checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" "checksum serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)" = "15c141fc7027dd265a47c090bf864cf62b42c4d228bbcf4e51a0c9e2b0d3f7ef" "checksum serde_derive 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)" = "225de307c6302bec3898c51ca302fc94a7a1697ef0845fcee6448f33c032249c" "checksum serde_json 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)" = "43344e7ce05d0d8280c5940cabb4964bea626aa58b1ec0e8c73fa2a8512a38ce" +"checksum siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" "checksum slog 2.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1e1a2eec401952cd7b12a84ea120e2d57281329940c3f93c2bf04f462539508e" "checksum slog-async 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e544d16c6b230d84c866662fe55e31aacfca6ae71e6fc49ae9a311cb379bfc2f" "checksum slog-term 2.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5951a808c40f419922ee014c15b6ae1cd34d963538b57d8a4778b9ca3fff1e0b" @@ -1138,10 +1238,10 @@ dependencies = [ "checksum socket2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "c4d11a52082057d87cb5caa31ad812f4504b97ab44732cd8359df2e9ff9f48e7" "checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" -"checksum structopt 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)" = "d77af7242f18c40fd19cb270985930f239ee1646cfb482050bbae9da1d18743b" -"checksum structopt-derive 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)" = "17ff01fe96de9d16e7372ae5f19dd7ece2c703b51043c3db9ea27f9e393ea311" -"checksum syn 0.15.13 (registry+https://github.com/rust-lang/crates.io-index)" = "7b4439ee8325b4e4b57e59309c3724c9a4478eaeb4eb094b6f3fac180a3b2876" -"checksum synstructure 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ec37f4fab4bafaf6b5621c1d54e6aa5d4d059a8f84929e87abfdd7f9f04c6db2" +"checksum structopt 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "41c4a2479a078509940d82773d90ff824a8c89533ab3b59cd3ce8b0c0e369c02" +"checksum structopt-derive 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "5352090cfae7a2c85e1a31146268b53396106c88ca5d6ccee2e3fae83b6e35c2" +"checksum syn 0.15.18 (registry+https://github.com/rust-lang/crates.io-index)" = "90c39a061e2f412a9f869540471ab679e85e50c6b05604daf28bc3060f75c430" +"checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum take_mut 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60" "checksum tar 0.4.17 (registry+https://github.com/rust-lang/crates.io-index)" = "83b0d14b53dbfd62681933fadd651e815f99e6084b649e049ab99296e05ab3de" "checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" @@ -1154,11 +1254,12 @@ dependencies = [ "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b" "checksum toml 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "4a2ecc31b0351ea18b3fe11274b8db6e4d82bce861bbb22e6dbed40417902c65" -"checksum ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd2be2d6639d0f8fe6cdda291ad456e23629558d466e2789d2c3e9892bda285d" +"checksum ucd-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d0f8bfa9ff0cadcd210129ad9d2c5f145c13e9ced3d3e5d948a6213487d52444" +"checksum unicode-segmentation 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "aa6024fc12ddfd1c6dbc14a80fa2324d4568849869b779f6bd37e5e4c03344d1" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" -"checksum utf8-ranges 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd70f467df6810094968e2fce0ee1bd0e87157aceb026a8c083bcf5e25b9efe4" +"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737" "checksum uuid 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e1436e58182935dcd9ce0add9ea0b558e8a87befe01c1a301e6020aeb0876363" "checksum vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "def296d3eb3b12371b2c7d0e83bfe1403e4db2d7a0bba324a12b21c4ee13143d" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" diff --git a/Cargo.toml b/Cargo.toml index 56392cf..b486071 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,10 @@ atty = "0.2.11" cargo_metadata = "0.6.0" console = "0.6.1" curl = "0.4.13" +dirs = "1.0.4" failure = "0.1.2" flate2 = "1.0.2" +hex = "0.3" human-panic = "1.0.1" indicatif = "0.9.0" lazy_static = "1.1.0" @@ -24,6 +26,7 @@ parking_lot = "0.6" serde = "1.0.74" serde_derive = "1.0.74" serde_json = "1.0.26" +siphasher = "0.2.3" slog = "2.3" slog-term = "2.4" slog-async = "2.3" diff --git a/src/binaries.rs b/src/binaries.rs index ba7a1ec..a34c872 100644 --- a/src/binaries.rs +++ b/src/binaries.rs @@ -1,247 +1,257 @@ //! Utilities for finding and installing binaries that we depend on. use curl; -use error::Error; -use failure; +use dirs; +use failure::{Error, ResultExt}; use flate2; -use slog::Logger; +use hex; +use siphasher::sip::SipHasher13; use std::collections::HashSet; +use std::env; use std::ffi; use std::fs; +use std::hash::{Hash, Hasher}; 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") +/// Global cache for wasm-pack, currently containing binaries downloaded from +/// urls like wasm-bindgen and such. +pub struct Cache { + destination: PathBuf, } -/// 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)) +/// Representation of a downloaded tarball/zip +pub struct Download { + root: PathBuf, } -/// 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"); +impl Cache { + /// Returns the global cache directory, as inferred from env vars and such. + /// + /// This function may return an error if a cache directory cannot be + /// determined. + pub fn new() -> Result { + let destination = dirs::cache_dir() + .map(|p| p.join("wasm-pack")) + .or_else(|| { + let home = dirs::home_dir()?; + Some(home.join(".wasm-pack")) + }) + .ok_or_else(|| format_err!("couldn't find your home directory, is $HOME not set?"))?; + Ok(Cache::at(&destination)) } - 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 { - 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 { - let p = local_bin_path(crate_path, bin); - debug!(log, "Checking for local {} binary at {}", bin, p.display()); - if p.is_file() { - Some(p) - } else { - None + /// Creates a new cache specifically at a particular directory, useful in + /// testing and such. + pub fn at(path: &Path) -> Cache { + Cache { + destination: path.to_path_buf(), } - }; + } - // Return the path to the global binary, if it exists. - let global_path = || -> Option { - debug!(log, "Looking for global {} binary on $PATH", bin); - if let Ok(p) = which(bin) { - Some(p) - } else { - None + /// Joins a path to the destination of this cache, returning the result + pub fn join(&self, path: &Path) -> PathBuf { + self.destination.join(path) + } + + /// Downloads a tarball or zip file from the specified url, extracting it + /// locally and returning the directory that the contents were extracted + /// into. + /// + /// Note that this function requries that the contents of `url` never change + /// as the contents of the url are globally cached on the system and never + /// invalidated. + /// + /// The `name` is a human-readable name used to go into the folder name of + /// the destination, and `binaries` is a list of binaries expected to be at + /// the url. If the URL's extraction doesn't contain all the binaries this + /// function will return an error. + pub fn download( + &self, + install_permitted: bool, + name: &str, + binaries: &[&str], + url: &str, + ) -> Result, Error> { + let mut hasher = SipHasher13::new(); + url.hash(&mut hasher); + let result = hasher.finish(); + let hex = hex::encode(&[ + (result >> 0) as u8, + (result >> 8) as u8, + (result >> 16) as u8, + (result >> 24) as u8, + (result >> 32) as u8, + (result >> 40) as u8, + (result >> 48) as u8, + (result >> 56) as u8, + ]); + let dirname = format!("{}-{}", name, hex); + + let destination = self.destination.join(&dirname); + if destination.exists() { + return Ok(Some(Download { root: destination })); } - }; - - local_path(crate_path) - .or_else(global_path) - .map(|p| { - let p = p.canonicalize().unwrap_or(p); - debug!(log, "Using {} binary at {}", bin, p.display()); - p - }) - .or_else(|| { - debug!(log, "Could not find {} binary.", bin); - None - }) -} -fn with_url_context(url: &str, r: Result) -> Result -where - Result: failure::ResultExt, -{ - use failure::ResultExt; - r.with_context(|_| format!("when requesting {}", url)) -} + if !install_permitted { + return Ok(None); + } -fn transfer( - url: &str, - easy: &mut curl::easy::Easy, - data: &mut Vec, -) -> 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(()) -} + let data = curl(&url).with_context(|_| format!("failed to download from {}", url))?; -fn curl(url: &str) -> Result, failure::Error> { - let mut data = Vec::new(); + // Extract everything in a temporary directory in case we're ctrl-c'd. + // Don't want to leave around corrupted data! + let temp = self.destination.join(&format!(".{}", dirname)); + drop(fs::remove_dir_all(&temp)); + fs::create_dir_all(&temp)?; - 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)?; + if url.ends_with(".tar.gz") { + self.extract_tarball(&data, &temp, binaries) + .with_context(|_| format!("failed to extract tarball from {}", url))?; + } else if url.ends_with(".zip") { + self.extract_zip(&data, &temp, binaries) + .with_context(|_| format!("failed to extract zip from {}", url))?; + } else { + // panic instead of runtime error as it's a static violation to + // download a different kind of url, all urls should be encoded into + // the binary anyway + panic!("don't know how to extract {}", url) + } - 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 {}", - status_code, url - )) - .into()) + // Now that everything is ready move this over to our destination and + // we're good to go. + fs::rename(&temp, &destination)?; + Ok(Some(Download { root: destination })) } -} -/// Download the `.tar.gz` file at the given URL and unpack the given binaries -/// from it into the given crate. -/// -/// Upon success, every `$BIN` in `binaries` will be at `$CRATE/bin/$BIN`. -pub fn install_binaries_from_targz_at_url<'a, I>( - crate_path: &Path, - url: &str, - binaries: I, -) -> Result<(), failure::Error> -where - I: IntoIterator, -{ - let mut binaries: HashSet<_> = binaries.into_iter().map(ffi::OsStr::new).collect(); - - let tarball = curl(&url).map_err(|e| Error::http(&e.to_string()))?; - let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(&tarball[..])); - - ensure_local_bin_dir(crate_path)?; - let bin = local_bin_dir(crate_path); - - for entry in archive.entries()? { - let mut entry = entry?; - - let dest = match entry.path()?.file_stem() { - Some(f) if binaries.contains(f) => { - binaries.remove(f); - bin.join(entry.path()?.file_name().unwrap()) - } - _ => continue, - }; - - entry.unpack(dest)?; - } + fn extract_tarball(&self, tarball: &[u8], dst: &Path, binaries: &[&str]) -> Result<(), Error> { + let mut binaries: HashSet<_> = binaries.into_iter().map(ffi::OsStr::new).collect(); + let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(tarball)); + + for entry in archive.entries()? { + let mut entry = entry?; + + let dest = match entry.path()?.file_stem() { + Some(f) if binaries.contains(f) => { + binaries.remove(f); + dst.join(entry.path()?.file_name().unwrap()) + } + _ => continue, + }; + + entry.unpack(dest)?; + } + + if !binaries.is_empty() { + bail!( + "the tarball was missing expected executables: {}", + binaries + .into_iter() + .map(|s| s.to_string_lossy()) + .collect::>() + .join(", "), + ) + } - if binaries.is_empty() { Ok(()) - } else { - Err(Error::archive(&format!( - "the tarball at {} was missing expected executables: {}", - url, - binaries - .into_iter() - .map(|s| s.to_string_lossy()) - .collect::>() - .join(", "), - )) - .into()) + } + + fn extract_zip(&self, zip: &[u8], dst: &Path, binaries: &[&str]) -> Result<(), Error> { + let mut binaries: HashSet<_> = binaries.into_iter().map(ffi::OsStr::new).collect(); + + let data = io::Cursor::new(zip); + let mut zip = zip::ZipArchive::new(data)?; + + for i in 0..zip.len() { + let mut entry = zip.by_index(i).unwrap(); + let entry_path = entry.sanitized_name(); + match entry_path.file_stem() { + Some(f) if binaries.contains(f) => { + binaries.remove(f); + let mut dest = bin_open_options() + .write(true) + .create_new(true) + .open(dst.join(entry_path.file_name().unwrap()))?; + io::copy(&mut entry, &mut dest)?; + } + _ => continue, + }; + } + + if !binaries.is_empty() { + bail!( + "the zip was missing expected executables: {}", + binaries + .into_iter() + .map(|s| s.to_string_lossy()) + .collect::>() + .join(", "), + ) + } + + return Ok(()); + + #[cfg(unix)] + fn bin_open_options() -> fs::OpenOptions { + use std::os::unix::fs::OpenOptionsExt; + + let mut opts = fs::OpenOptions::new(); + opts.mode(0o755); + opts + } + + #[cfg(not(unix))] + fn bin_open_options() -> fs::OpenOptions { + fs::OpenOptions::new() + } } } -/// Install binaries from within the given zip at the given URL. -/// -/// Upon success, the binaries will be at the `$CRATE/bin/$BIN` path. -pub fn install_binaries_from_zip_at_url<'a, I>( - crate_path: &Path, - url: &str, - binaries: I, -) -> Result<(), failure::Error> -where - I: IntoIterator, -{ - let mut binaries: HashSet<_> = binaries.into_iter().map(ffi::OsStr::new).collect(); - - let data = curl(&url).map_err(|e| Error::http(&e.to_string()))?; - let data = io::Cursor::new(data); - let mut zip = zip::ZipArchive::new(data)?; - - 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(); - let entry_path = entry.sanitized_name(); - match entry_path.file_stem() { - Some(f) if binaries.contains(f) => { - binaries.remove(f); - let mut dest = bin_open_options() - .write(true) - .create_new(true) - .open(bin.join(entry_path.file_name().unwrap()))?; - io::copy(&mut entry, &mut dest)?; - } - _ => continue, - }; +impl Download { + /// Manually constructs a download at the specified path + pub fn at(path: &Path) -> Download { + Download { + root: path.to_path_buf(), + } } - if binaries.is_empty() { - Ok(()) - } else { - Err(Error::archive(&format!( - "the zip at {} was missing expected executables: {}", - url, - binaries - .into_iter() - .map(|s| s.to_string_lossy()) - .collect::>() - .join(", "), - )) - .into()) + /// Returns the path to the binary `name` within this download + pub fn binary(&self, name: &str) -> PathBuf { + let ret = self + .root + .join(name) + .with_extension(env::consts::EXE_EXTENSION); + assert!(ret.exists(), "binary {} doesn't exist", ret.display()); + return ret; } } -#[cfg(unix)] -fn bin_open_options() -> fs::OpenOptions { - use std::os::unix::fs::OpenOptionsExt; +fn curl(url: &str) -> Result, Error> { + let mut data = Vec::new(); - let mut opts = fs::OpenOptions::new(); - opts.mode(0o755); - opts -} + let mut easy = curl::easy::Easy::new(); + easy.follow_location(true)?; + easy.url(url)?; + easy.get(true)?; + { + let mut transfer = easy.transfer(); + transfer.write_function(|part| { + data.extend_from_slice(part); + Ok(part.len()) + })?; + transfer.perform()?; + } -#[cfg(not(unix))] -fn bin_open_options() -> fs::OpenOptions { - fs::OpenOptions::new() + let status_code = easy.response_code()?; + if 200 <= status_code && status_code < 300 { + Ok(data) + } else { + bail!( + "received a bad HTTP status code ({}) when requesting {}", + status_code, + url + ) + } } diff --git a/src/bindgen.rs b/src/bindgen.rs index 296e4ab..46ce8a4 100644 --- a/src/bindgen.rs +++ b/src/bindgen.rs @@ -1,16 +1,17 @@ //! Functionality related to installing and running `wasm-bindgen`. -use binaries::{self, bin_path, install_binaries_from_targz_at_url}; +use binaries::{Cache, Download}; use cargo_metadata; use child; use emoji; -use error::Error; use failure::{self, ResultExt}; use progressbar::Step; use slog::Logger; +use std::fs; use std::path::{Path, PathBuf}; use std::process::Command; use target; +use which::which; use PBAR; /// Install the `wasm-bindgen` CLI. @@ -20,46 +21,66 @@ use PBAR; /// tarball from the GitHub releases page, if this target has prebuilt /// binaries. Finally, falls back to `cargo install`. pub fn install_wasm_bindgen( - root_path: &Path, + cache: &Cache, version: &str, install_permitted: bool, step: &Step, log: &Logger, -) -> 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)) - .unwrap_or(false) - { - let msg = format!("{}wasm-bindgen already installed...", emoji::DOWN_ARROW); - PBAR.step(step, &msg); - return Ok(()); - } - - // If the `wasm-bindgen` dependency was not met, and installs are not - // permitted, return a configuration error. - if !install_permitted { - let msg = format!("wasm-bindgen v{} is not installed!", version); - return Err(Error::crate_config(&msg).into()); +) -> Result { + // If `wasm-bindgen` is installed globally and it has the right version, use + // that. Assume that other tools are installed next to it. + // + // This situation can arise if `wasm-bindgen` is already installed via + // `cargo install`, for example. + if let Ok(path) = which("wasm-bindgen") { + debug!( + log, + "found global wasm-bindgen binary at: {}", + path.display() + ); + if wasm_bindgen_version_check(&path, version, log) { + return Ok(Download::at(path.parent().unwrap())); + } } let msg = format!("{}Installing wasm-bindgen...", emoji::DOWN_ARROW); PBAR.step(step, &msg); - download_prebuilt_wasm_bindgen(root_path, version).or_else(|e| { - warn!( - log, - "could not download pre-built `wasm-bindgen`: {}. Falling back to `cargo install`.", e - ); - cargo_install_wasm_bindgen(log, root_path, version) - }) + let dl = download_prebuilt_wasm_bindgen(&cache, version, install_permitted); + match dl { + Ok(dl) => return Ok(dl), + Err(e) => { + warn!( + log, + "could not download pre-built `wasm-bindgen`: {}. Falling back to `cargo install`.", + e + ); + } + } + + cargo_install_wasm_bindgen(log, &cache, version, install_permitted) } -/// Download a tarball containing a pre-built `wasm-bindgen` binary. +/// Downloads a precompiled copy of wasm-bindgen, if available. pub fn download_prebuilt_wasm_bindgen( - root_path: &Path, + cache: &Cache, version: &str, -) -> Result<(), failure::Error> { + install_permitted: bool, +) -> Result { + let url = match prebuilt_url(version) { + Some(url) => url, + None => bail!("no prebuilt wasm-bindgen binaries are available for this platform"), + }; + let binaries = &["wasm-bindgen", "wasm-bindgen-test-runner"]; + match cache.download(install_permitted, "wasm-bindgen", binaries, &url)? { + Some(download) => Ok(download), + None => bail!("wasm-bindgen v{} is not installed!", version), + } +} + +/// Returns the URL of a precompiled version of wasm-bindgen, if we have one +/// available for our host platform. +fn prebuilt_url(version: &str) -> Option { let target = if target::LINUX && target::x86_64 { "x86_64-unknown-linux-musl" } else if target::MACOS && target::x86_64 { @@ -67,32 +88,40 @@ pub fn download_prebuilt_wasm_bindgen( } else if target::WINDOWS && target::x86_64 { "x86_64-pc-windows-msvc" } else { - return Err(Error::unsupported( - "there are no pre-built `wasm-bindgen` binaries for this target", - ) - .into()); + return None; }; - let url = format!( + Some(format!( "https://github.com/rustwasm/wasm-bindgen/releases/download/{0}/wasm-bindgen-{0}-{1}.tar.gz", version, target - ); - - install_binaries_from_targz_at_url( - root_path, - &url, - vec!["wasm-bindgen", "wasm-bindgen-test-runner"], - ) + )) } /// Use `cargo install` to install the `wasm-bindgen` CLI locally into the given /// crate. pub fn cargo_install_wasm_bindgen( logger: &Logger, - crate_path: &Path, + cache: &Cache, version: &str, -) -> Result<(), failure::Error> { + install_permitted: bool, +) -> Result { + let dirname = format!("wasm-bindgen-cargo-install-{}", version); + let destination = cache.join(dirname.as_ref()); + if destination.exists() { + return Ok(Download::at(&destination)); + } + + if !install_permitted { + bail!("wasm-bindgen v{} is not installed!", version) + } + + // Run `cargo install` to a temporary location to handle ctrl-c gracefully + // and ensure we don't accidentally use stale files in the future + let tmp = cache.join(format!(".{}", dirname).as_ref()); + drop(fs::remove_dir_all(&tmp)); + fs::create_dir_all(&tmp)?; + let mut cmd = Command::new("cargo"); cmd.arg("install") .arg("--force") @@ -100,17 +129,19 @@ pub fn cargo_install_wasm_bindgen( .arg("--version") .arg(version) .arg("--root") - .arg(crate_path); + .arg(&tmp); child::run(logger, cmd, "cargo install").context("Installing wasm-bindgen with cargo")?; - assert!(binaries::local_bin_path(crate_path, "wasm-bindgen").is_file()); - Ok(()) + + fs::rename(&tmp, &destination)?; + Ok(Download::at(&destination)) } /// Run the `wasm-bindgen` CLI to generate bindings for the current crate's /// `.wasm`. pub fn wasm_bindgen_build( path: &Path, + bindgen: &Download, out_dir: &Path, name: &str, disable_dts: bool, @@ -127,46 +158,42 @@ pub fn wasm_bindgen_build( let out_dir = out_dir.to_str().unwrap(); - if let Some(wasm_bindgen_path) = wasm_bindgen_path(log, path) { - let manifest = path.join("Cargo.toml"); - let target_path = cargo_metadata::metadata(Some(&manifest)) - .unwrap() - .target_directory; - let mut wasm_path = PathBuf::from(&target_path) - .join("wasm32-unknown-unknown") - .join(release_or_debug) - .join(binary_name); - wasm_path.set_extension("wasm"); - let wasm_path = wasm_path.display().to_string(); - - let dts_arg = if disable_dts { - "--no-typescript" - } else { - "--typescript" - }; - let target_arg = match target { - "nodejs" => "--nodejs", - "no-modules" => "--no-modules", - _ => "--browser", - }; - let bindgen_path = Path::new(&wasm_bindgen_path); - let mut cmd = Command::new(bindgen_path); - cmd.current_dir(path) - .arg(&wasm_path) - .arg("--out-dir") - .arg(out_dir) - .arg(dts_arg) - .arg(target_arg); - - if debug { - cmd.arg("--debug"); - } - - child::run(log, cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?; - Ok(()) + let manifest = path.join("Cargo.toml"); + let target_path = cargo_metadata::metadata(Some(&manifest)) + .unwrap() + .target_directory; + let mut wasm_path = PathBuf::from(&target_path) + .join("wasm32-unknown-unknown") + .join(release_or_debug) + .join(binary_name); + wasm_path.set_extension("wasm"); + let wasm_path = wasm_path.display().to_string(); + + let dts_arg = if disable_dts { + "--no-typescript" } else { - Err(Error::crate_config("Could not find `wasm-bindgen`").into()) + "--typescript" + }; + let target_arg = match target { + "nodejs" => "--nodejs", + "no-modules" => "--no-modules", + _ => "--browser", + }; + let bindgen_path = bindgen.binary("wasm-bindgen"); + let mut cmd = Command::new(bindgen_path); + cmd.current_dir(path) + .arg(&wasm_path) + .arg("--out-dir") + .arg(out_dir) + .arg(dts_arg) + .arg(target_arg); + + if debug { + cmd.arg("--debug"); } + + child::run(log, cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?; + Ok(()) } /// Check if the `wasm-bindgen` dependency is locally satisfied. @@ -192,16 +219,3 @@ fn wasm_bindgen_version_check(bindgen_path: &PathBuf, dep_version: &str, log: &L }) .unwrap_or(false) } - -/// Return a `PathBuf` containing the path to either the local wasm-bindgen -/// version, or the globally installed version if there is no local version. -fn wasm_bindgen_path(log: &Logger, crate_path: &Path) -> Option { - bin_path(log, crate_path, "wasm-bindgen") -} - -/// Return a `PathBuf` containing the path to either the local -/// wasm-bindgen-test-runner version, or the globally installed version if there -/// is no local version. -pub fn wasm_bindgen_test_runner_path(log: &Logger, crate_path: &Path) -> Option { - bin_path(log, crate_path, "wasm-bindgen-test-runner") -} diff --git a/src/command/build.rs b/src/command/build.rs index 7eed1f0..12381fe 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -1,5 +1,6 @@ //! Implementation of the `wasm-pack build` command. +use binaries::{Cache, Download}; use bindgen; use build; use command::utils::{create_pkg_dir, set_crate_path}; @@ -17,7 +18,8 @@ use std::time::Instant; use PBAR; /// Everything required to configure and run the `wasm-pack init` command. -pub(crate) struct Build { +#[allow(missing_docs)] +pub struct Build { pub crate_path: PathBuf, pub scope: Option, pub disable_dts: bool, @@ -27,6 +29,8 @@ pub(crate) struct Build { // build_config: Option, pub crate_name: String, pub out_dir: PathBuf, + pub bindgen: Option, + pub cache: Cache, } /// The `BuildMode` determines which mode of initialization we are running, and @@ -113,9 +117,16 @@ impl Build { // build_config, crate_name, out_dir, + bindgen: None, + cache: Cache::new()?, }) } + /// Configures the global binary cache used for this build + pub fn set_cache(&mut self, cache: Cache) { + self.cache = cache; + } + /// Execute this `Build` command. pub fn run(&mut self, log: &Logger) -> Result<(), failure::Error> { let process_steps = Build::get_process_steps(&self.mode); @@ -276,13 +287,14 @@ impl Build { BuildMode::Force => true, BuildMode::Noinstall => false, }; - bindgen::install_wasm_bindgen( - &self.crate_path, + let bindgen = bindgen::install_wasm_bindgen( + &self.cache, &bindgen_version, install_permitted, step, log, )?; + self.bindgen = Some(bindgen); info!(&log, "Installing wasm-bindgen-cli was successful."); info!(&log, "Getting the crate name from the manifest..."); @@ -300,6 +312,7 @@ impl Build { info!(&log, "Building the wasm bindings..."); bindgen::wasm_bindgen_build( &self.crate_path, + self.bindgen.as_ref().unwrap(), &self.out_dir, &self.crate_name, self.disable_dts, diff --git a/src/command/test.rs b/src/command/test.rs index 507bd18..0c31dac 100644 --- a/src/command/test.rs +++ b/src/command/test.rs @@ -1,6 +1,7 @@ //! Implementation of the `wasm-pack test` command. use super::build::BuildMode; +use binaries::Cache; use bindgen; use build; use command::utils::set_crate_path; @@ -81,6 +82,7 @@ pub struct TestOptions { /// A configured `wasm-pack test` command. pub struct Test { crate_path: PathBuf, + cache: Cache, node: bool, mode: BuildMode, firefox: bool, @@ -139,6 +141,7 @@ impl Test { } Ok(Test { + cache: Cache::new()?, crate_path, node, mode, @@ -154,6 +157,11 @@ impl Test { }) } + /// Configures the cache that this test command uses + pub fn set_cache(&mut self, cache: Cache) { + self.cache = cache; + } + /// Execute this test command. pub fn run(mut self, log: &Logger) -> Result<(), failure::Error> { let process_steps = self.get_process_steps(); @@ -303,16 +311,15 @@ impl Test { } }; - bindgen::install_wasm_bindgen( - &self.crate_path, + let dl = bindgen::install_wasm_bindgen( + &self.cache, &bindgen_version, install_permitted, step, log, )?; - self.test_runner_path = Some(bindgen::wasm_bindgen_test_runner_path(log, &self.crate_path) - .expect("if installing wasm-bindgen succeeded, then we should have wasm-bindgen-test-runner too")); + self.test_runner_path = Some(dl.binary("wasm-bindgen-test-runner")); info!(&log, "Getting wasm-bindgen-cli was successful."); Ok(()) @@ -335,13 +342,12 @@ impl Test { Ok(()) } - fn step_get_chromedriver(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> { + fn step_get_chromedriver(&mut self, step: &Step, _log: &Logger) -> Result<(), failure::Error> { PBAR.step(step, "Getting chromedriver..."); assert!(self.chrome && self.chromedriver.is_none()); self.chromedriver = Some(webdriver::get_or_install_chromedriver( - log, - &self.crate_path, + &self.cache, self.mode, )?); Ok(()) @@ -378,13 +384,12 @@ impl Test { Ok(()) } - fn step_get_geckodriver(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> { + fn step_get_geckodriver(&mut self, step: &Step, _log: &Logger) -> Result<(), failure::Error> { PBAR.step(step, "Getting geckodriver..."); assert!(self.firefox && self.geckodriver.is_none()); self.geckodriver = Some(webdriver::get_or_install_geckodriver( - log, - &self.crate_path, + &self.cache, self.mode, )?); Ok(()) @@ -421,11 +426,11 @@ impl Test { Ok(()) } - fn step_get_safaridriver(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> { + fn step_get_safaridriver(&mut self, step: &Step, _log: &Logger) -> Result<(), failure::Error> { PBAR.step(step, "Getting safaridriver..."); assert!(self.safari && self.safaridriver.is_none()); - self.safaridriver = Some(webdriver::get_safaridriver(log, &self.crate_path)?); + self.safaridriver = Some(webdriver::get_safaridriver()?); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index ed2e390..702fafa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,9 +5,11 @@ extern crate cargo_metadata; extern crate console; extern crate curl; +extern crate dirs; #[macro_use] extern crate failure; extern crate flate2; +extern crate hex; extern crate indicatif; #[macro_use] extern crate lazy_static; @@ -15,6 +17,7 @@ extern crate parking_lot; #[macro_use] extern crate serde_derive; extern crate serde_json; +extern crate siphasher; #[macro_use] extern crate structopt; #[macro_use] diff --git a/src/test/webdriver.rs b/src/test/webdriver.rs index cac6d39..4b06314 100644 --- a/src/test/webdriver.rs +++ b/src/test/webdriver.rs @@ -1,40 +1,32 @@ //! Getting WebDriver client binaries. -use binaries::{ - self, bin_path, install_binaries_from_targz_at_url, install_binaries_from_zip_at_url, -}; +use binaries::Cache; use command::build::BuildMode; -use error::Error; use failure; -use slog::Logger; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use target; /// Get the path to an existing `chromedriver`, or install it if no existing /// binary is found. pub fn get_or_install_chromedriver( - log: &Logger, - crate_path: &Path, + cache: &Cache, mode: BuildMode, ) -> Result { - match (mode, bin_path(log, crate_path, "chromedriver")) { - (_, Some(path)) => Ok(path), - (BuildMode::Normal, None) => install_chromedriver(crate_path), - (BuildMode::Force, None) => install_chromedriver(crate_path), - (BuildMode::Noinstall, None) => Err(Error::crate_config( - "No crate-local `chromedriver` binary found, and could not find a global \ - `chromedriver` on the `$PATH`. Not installing `chromedriver` because of noinstall \ - mode.", - ) - .into()), + if let Ok(path) = which::which("chromedriver") { + return Ok(path); } + let installation_allowed = match mode { + BuildMode::Noinstall => false, + _ => true, + }; + install_chromedriver(cache, installation_allowed) } -fn get_local_chromedriver_path(crate_path: &Path) -> PathBuf { - binaries::local_bin_path(crate_path, "chromedriver") -} - -fn get_chromedriver_url() -> Result { +/// Download and install a pre-built `chromedriver` binary. +pub fn install_chromedriver( + cache: &Cache, + installation_allowed: bool, +) -> Result { let target = if target::LINUX && target::x86_64 { "linux64" } else if target::MACOS && target::x86_64 { @@ -42,46 +34,49 @@ fn get_chromedriver_url() -> Result { } else if target::WINDOWS && target::x86 { "win32" } else { - return Err(Error::unsupported( - "geckodriver binaries are unavailable for this target", - )); + bail!("geckodriver binaries are unavailable for this target") }; - Ok(format!( + let url = format!( "https://chromedriver.storage.googleapis.com/2.41/chromedriver_{}.zip", target - )) -} - -/// Download and install a pre-built `chromedriver` binary. -pub fn install_chromedriver(crate_path: &Path) -> Result { - let url = get_chromedriver_url()?; - install_binaries_from_zip_at_url(crate_path, &url, Some("chromedriver"))?; - let chromedriver = get_local_chromedriver_path(crate_path); - assert!(chromedriver.is_file()); - Ok(chromedriver) + ); + match cache.download( + installation_allowed, + "chromedriver", + &["chromedriver"], + &url, + )? { + Some(dl) => Ok(dl.binary("chromedriver")), + None => bail!( + "No cached `chromedriver` binary found, and could not find a global \ + `chromedriver` on the `$PATH`. Not installing `chromedriver` because of noinstall \ + mode." + ), + } } /// Get the path to an existing `geckodriver`, or install it if no existing /// binary is found. pub fn get_or_install_geckodriver( - log: &Logger, - crate_path: &Path, + cache: &Cache, mode: BuildMode, ) -> Result { - match (mode, bin_path(log, crate_path, "geckodriver")) { - (_, Some(path)) => Ok(path), - (BuildMode::Normal, None) => install_geckodriver(crate_path), - (BuildMode::Force, None) => install_geckodriver(crate_path), - (BuildMode::Noinstall, None) => Err(Error::crate_config( - "No crate-local `geckodriver` binary found, and could not find a global `geckodriver` \ - on the `$PATH`. Not installing `geckodriver` because of noinstall mode.", - ) - .into()), + if let Ok(path) = which::which("geckodriver") { + return Ok(path); } + let installation_allowed = match mode { + BuildMode::Noinstall => false, + _ => true, + }; + install_geckodriver(cache, installation_allowed) } -fn get_geckodriver_url() -> Result { +/// Download and install a pre-built `geckodriver` binary. +pub fn install_geckodriver( + cache: &Cache, + installation_allowed: bool, +) -> Result { let (target, ext) = if target::LINUX && target::x86 { ("linux32", "tar.gz") } else if target::LINUX && target::x86_64 { @@ -93,36 +88,21 @@ fn get_geckodriver_url() -> Result { } else if target::WINDOWS && target::x86_64 { ("win64", "zip") } else { - return Err(Error::unsupported( - "geckodriver binaries are unavailable for this target", - )); + bail!("geckodriver binaries are unavailable for this target") }; - Ok(format!( + let url = format!( "https://github.com/mozilla/geckodriver/releases/download/v0.21.0/geckodriver-v0.21.0-{}.{}", target, ext, - )) -} - -fn get_local_geckodriver_path(crate_path: &Path) -> PathBuf { - binaries::local_bin_path(crate_path, "geckodriver") -} - -/// Download and install a pre-built `geckodriver` binary. -pub fn install_geckodriver(crate_path: &Path) -> Result { - let url = get_geckodriver_url()?; - - if url.ends_with("tar.gz") { - install_binaries_from_targz_at_url(crate_path, &url, Some("geckodriver"))?; - } else { - assert!(url.ends_with("zip")); - install_binaries_from_zip_at_url(crate_path, &url, Some("geckodriver"))?; + ); + match cache.download(installation_allowed, "geckodriver", &["geckodriver"], &url)? { + Some(dl) => Ok(dl.binary("geckodriver")), + None => bail!( + "No cached `geckodriver` binary found, and could not find a global `geckodriver` \ + on the `$PATH`. Not installing `geckodriver` because of noinstall mode." + ), } - - let geckodriver = get_local_geckodriver_path(crate_path); - assert!(geckodriver.is_file()); - Ok(geckodriver) } /// Get the path to an existing `safaridriver`. @@ -130,12 +110,9 @@ pub fn install_geckodriver(crate_path: &Path) -> Result /// We can't install `safaridriver` if an existing one is not found because /// Apple does not provide pre-built binaries. However, `safaridriver` *should* /// be present by default. -pub fn get_safaridriver(log: &Logger, crate_path: &Path) -> Result { - if let Some(p) = bin_path(log, crate_path, "safaridriver") { - Ok(p) - } else { - Err(Error::crate_config( - "could not find `safaridriver` on the `$PATH`", - )) +pub fn get_safaridriver() -> Result { + match which::which("safaridriver") { + Ok(p) => Ok(p), + Err(_) => bail!("could not find `safaridriver` on the `$PATH`"), } } diff --git a/tests/all/bindgen.rs b/tests/all/bindgen.rs index 4892019..985a4d0 100644 --- a/tests/all/bindgen.rs +++ b/tests/all/bindgen.rs @@ -1,4 +1,5 @@ use tempfile; +use wasm_pack::binaries::Cache; use wasm_pack::bindgen; #[test] @@ -8,21 +9,11 @@ use wasm_pack::bindgen; all(windows, target_arch = "x86_64"), ))] fn can_download_prebuilt_wasm_bindgen() { - use std::env; let dir = tempfile::TempDir::new().unwrap(); - bindgen::download_prebuilt_wasm_bindgen(dir.path(), "0.2.21").unwrap(); - assert!(dir - .path() - .join("bin") - .join("wasm-bindgen") - .with_extension(env::consts::EXE_EXTENSION) - .is_file()); - assert!(dir - .path() - .join("bin") - .join("wasm-bindgen-test-runner") - .with_extension(env::consts::EXE_EXTENSION) - .is_file()); + let cache = Cache::at(dir.path()); + let dl = bindgen::download_prebuilt_wasm_bindgen(&cache, "0.2.21", true).unwrap(); + assert!(dl.binary("wasm-bindgen").is_file()); + assert!(dl.binary("wasm-bindgen-test-runner").is_file()) } #[test] @@ -34,9 +25,13 @@ fn can_download_prebuilt_wasm_bindgen() { fn downloading_prebuilt_wasm_bindgen_handles_http_errors() { let dir = tempfile::TempDir::new().unwrap(); let bad_version = "0.2.21-some-trailing-version-stuff-that-does-not-exist"; - let result = bindgen::download_prebuilt_wasm_bindgen(dir.path(), bad_version); + let cache = Cache::at(dir.path()); + let result = bindgen::download_prebuilt_wasm_bindgen(&cache, bad_version, true); assert!(result.is_err()); - let error_msg = result.unwrap_err().to_string(); - assert!(error_msg.contains("404")); - assert!(error_msg.contains(bad_version)); + let error = result.err().unwrap(); + + assert!(error.iter_chain().any(|e| e.to_string().contains("404"))); + assert!(error + .iter_chain() + .any(|e| e.to_string().contains(bad_version))); } diff --git a/tests/all/build.rs b/tests/all/build.rs index 240a1a2..295708d 100644 --- a/tests/all/build.rs +++ b/tests/all/build.rs @@ -1,6 +1,6 @@ use structopt::StructOpt; use utils; -use wasm_pack::{command, logger, Cli}; +use wasm_pack::Cli; #[test] fn build_in_non_crate_directory_doesnt_panic() { @@ -11,14 +11,13 @@ fn build_in_non_crate_directory_doesnt_panic() { &fixture.path.display().to_string(), ]) .unwrap(); - let logger = logger::new(&cli.cmd, cli.verbosity).unwrap(); - let result = command::run_wasm_pack(cli.cmd, &logger); + let result = fixture.run(cli.cmd); assert!( result.is_err(), "running wasm-pack in a non-crate directory should fail, but it should not panic" ); - let err_msg = result.unwrap_err().to_string(); - assert!(err_msg.contains("missing a `Cargo.toml`")); + let err = result.unwrap_err(); + assert!(err.iter_chain().any(|e| e.to_string().contains("missing a `Cargo.toml`"))); } #[test] @@ -31,7 +30,5 @@ fn it_should_build_js_hello_world_example() { &fixture.path.display().to_string(), ]) .unwrap(); - let logger = logger::new(&cli.cmd, cli.verbosity).unwrap(); - command::run_wasm_pack(cli.cmd, &logger) - .expect("running wasm-pack in a js-hello-world directory should succeed."); + fixture.run(cli.cmd).unwrap(); } diff --git a/tests/all/test.rs b/tests/all/test.rs index 54d4131..6bad2d6 100644 --- a/tests/all/test.rs +++ b/tests/all/test.rs @@ -1,8 +1,5 @@ use std::env; -use std::fs; -use tempfile; use utils::fixture; -use wasm_pack::binaries; use wasm_pack::command::{self, build, test, Command}; use wasm_pack::logger; @@ -16,8 +13,7 @@ fn it_can_run_node_tests() { mode: build::BuildMode::Noinstall, ..Default::default() }); - let logger = logger::new(&cmd, 3).unwrap(); - command::run_wasm_pack(cmd, &logger).expect("should run test command OK"); + fixture.run(cmd).unwrap(); } #[test] @@ -30,8 +26,7 @@ fn it_can_run_tests_with_different_wbg_test_and_wbg_versions() { mode: build::BuildMode::Noinstall, ..Default::default() }); - let logger = logger::new(&cmd, 3).unwrap(); - command::run_wasm_pack(cmd, &logger).expect("should run test command OK"); + fixture.run(cmd).unwrap(); } #[test] @@ -81,8 +76,7 @@ fn it_can_run_browser_tests() { ..Default::default() }); - let logger = logger::new(&cmd, 3).unwrap(); - command::run_wasm_pack(cmd, &logger).expect("should run test command OK"); + fixture.run(cmd).unwrap(); } #[test] @@ -111,34 +105,35 @@ fn it_can_run_failing_tests() { all(target_os = "windows", target_arch = "x86_64") ))] fn it_can_find_a_webdriver_on_path() { + use std::process::Command; + let fixture = fixture::wbg_test_browser(); - fixture.install_local_wasm_bindgen(); - fixture.install_local_geckodriver(); - - let geckodriver_dir = tempfile::TempDir::new().unwrap(); - let local_geckodriver = binaries::local_bin_path(&fixture.path, "geckodriver"); - fs::copy( - &local_geckodriver, - geckodriver_dir - .path() - .join(local_geckodriver.file_name().unwrap()), - ) - .unwrap(); - fs::remove_file(&local_geckodriver).unwrap(); + let local_geckodriver = fixture.install_local_geckodriver(); + let local_wasm_bindgen = fixture.install_local_wasm_bindgen(); let mut paths: Vec<_> = env::split_paths(&env::var("PATH").unwrap()).collect(); - paths.insert(0, geckodriver_dir.path().into()); - env::set_var("PATH", env::join_paths(paths).unwrap()); - - let cmd = Command::Test(test::TestOptions { - path: Some(fixture.path.clone()), - firefox: true, - headless: true, - mode: build::BuildMode::Noinstall, - ..Default::default() - }); - let logger = logger::new(&cmd, 3).unwrap(); - command::run_wasm_pack(cmd, &logger).expect("should run test command OK"); + paths.insert(0, local_geckodriver.parent().unwrap().to_path_buf()); + paths.insert(0, local_wasm_bindgen.parent().unwrap().to_path_buf()); + let path = env::join_paths(paths).unwrap(); + + let mut me = env::current_exe().unwrap(); + me.pop(); + me.pop(); + me.push("wasm-pack"); + let output = Command::new(&me) + .arg("test") + .arg("--firefox") + .arg("--headless") + .arg("--mode") + .arg("no-install") + .env("PATH", &path) + .arg(&fixture.path) + .output() + .unwrap(); + println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); + println!("stderr: {}", String::from_utf8_lossy(&output.stderr)); + println!("status: {}", output.status); + assert!(output.status.success()); } #[test] diff --git a/tests/all/utils/fixture.rs b/tests/all/utils/fixture.rs index dc47369..64ccbdd 100644 --- a/tests/all/utils/fixture.rs +++ b/tests/all/utils/fixture.rs @@ -1,7 +1,6 @@ use super::logger::null_logger; use std::env; use std::fs; -use std::io; use std::mem::ManuallyDrop; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; @@ -9,12 +8,7 @@ use std::sync::{Once, ONCE_INIT}; use std::thread; use tempfile::TempDir; use wasm_pack; - -fn hard_link_or_copy, P2: AsRef>(from: P1, to: P2) -> io::Result<()> { - let from = from.as_ref(); - let to = to.as_ref(); - fs::hard_link(from, to).or_else(|_| fs::copy(from, to).map(|_| ())) -} +use wasm_pack::binaries::Cache; /// A test fixture in a temporary directory. pub struct Fixture { @@ -134,120 +128,63 @@ impl Fixture { /// /// Takes care not to re-install for every fixture, but only the one time /// for the whole test suite. - pub fn install_local_wasm_bindgen(&self) -> &Self { + pub fn install_local_wasm_bindgen(&self) -> PathBuf { static INSTALL_WASM_BINDGEN: Once = ONCE_INIT; + let cache = self.cache(); + let version = "0.2.21"; + let log = &null_logger(); + + let download = || { + if let Ok(download) = + wasm_pack::bindgen::download_prebuilt_wasm_bindgen(&cache, version, true) + { + return Ok(download); + } - let tests = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests"); - let shared_wasm_bindgen = wasm_pack::binaries::local_bin_path(&tests, "wasm-bindgen"); - let shared_wasm_bindgen_test_runner = - wasm_pack::binaries::local_bin_path(&tests, "wasm-bindgen-test-runner"); + wasm_pack::bindgen::cargo_install_wasm_bindgen(log, &cache, version, true) + }; + // Only one thread can perform the actual download, and then afterwards + // everything will hit the cache so we can run the same path. INSTALL_WASM_BINDGEN.call_once(|| { - if shared_wasm_bindgen.is_file() { - assert!(shared_wasm_bindgen_test_runner.is_file()); - return; - } - - const WASM_BINDGEN_VERSION: &str = "0.2.21"; - wasm_pack::bindgen::download_prebuilt_wasm_bindgen(&tests, WASM_BINDGEN_VERSION) - .or_else(|_| { - wasm_pack::bindgen::cargo_install_wasm_bindgen( - &null_logger(), - &tests, - WASM_BINDGEN_VERSION, - ) - }) - .unwrap(); + download().unwrap(); }); - - assert!(shared_wasm_bindgen.is_file()); - assert!(shared_wasm_bindgen_test_runner.is_file()); - - wasm_pack::binaries::ensure_local_bin_dir(&self.path).unwrap(); - - hard_link_or_copy( - &shared_wasm_bindgen, - wasm_pack::binaries::local_bin_path(&self.path, "wasm-bindgen"), - ) - .expect("could not copy `wasm-bindgen` to fixture directory"); - - hard_link_or_copy( - &shared_wasm_bindgen_test_runner, - wasm_pack::binaries::local_bin_path(&self.path, "wasm-bindgen-test-runner"), - ) - .expect("could not copy `wasm-bindgen-test` to fixture directory"); - - self + download().unwrap().binary("wasm-bindgen") } /// Download `geckodriver` and return its path. /// /// Takes care to ensure that only one `geckodriver` is downloaded for the whole /// test suite. - pub fn install_local_geckodriver(&self) -> &Self { + pub fn install_local_geckodriver(&self) -> PathBuf { static FETCH_GECKODRIVER: Once = ONCE_INIT; + let cache = self.cache(); - let tests = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests"); - - wasm_pack::binaries::ensure_local_bin_dir(&tests) - .expect("could not create fixture's `bin` directory"); - - let geckodriver = wasm_pack::binaries::local_bin_path(&tests, "geckodriver"); - + // like above for synchronization FETCH_GECKODRIVER.call_once(|| { - if geckodriver.is_file() { - return; - } - - wasm_pack::test::webdriver::install_geckodriver(&tests).unwrap(); - assert!(geckodriver.is_file()); + wasm_pack::test::webdriver::install_geckodriver(&cache, true).unwrap(); }); - - wasm_pack::binaries::ensure_local_bin_dir(&self.path) - .expect("could not create fixture's `bin` directory"); - - hard_link_or_copy( - &geckodriver, - wasm_pack::binaries::local_bin_path(&self.path, "geckodriver"), - ) - .expect("could not copy `geckodriver` to fixture directory"); - - self + wasm_pack::test::webdriver::install_geckodriver(&cache, true).unwrap() } /// Download `chromedriver` and return its path. /// /// Takes care to ensure that only one `chromedriver` is downloaded for the whole /// test suite. - pub fn install_local_chromedriver(&self) -> &Self { + pub fn install_local_chromedriver(&self) -> PathBuf { static FETCH_CHROMEDRIVER: Once = ONCE_INIT; + let cache = self.cache(); - let tests = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests"); - - wasm_pack::binaries::ensure_local_bin_dir(&tests) - .expect("could not create fixture's `bin` directory"); - - let chromedriver = wasm_pack::binaries::local_bin_path(&tests, "chromedriver"); - + // like above for synchronization FETCH_CHROMEDRIVER.call_once(|| { - if chromedriver.is_file() { - return; - } - - wasm_pack::test::webdriver::install_chromedriver(&tests).unwrap(); - assert!(chromedriver.is_file()); + wasm_pack::test::webdriver::install_chromedriver(&cache, true).unwrap(); }); + wasm_pack::test::webdriver::install_chromedriver(&cache, true).unwrap() + } - wasm_pack::binaries::ensure_local_bin_dir(&self.path) - .expect("could not create fixture's `bin` directory"); - - hard_link_or_copy( - &chromedriver, - wasm_pack::binaries::local_bin_path(&self.path, "chromedriver"), - ) - .expect("could not copy `chromedriver` to fixture directory"); - - self + pub fn cache(&self) -> Cache { + let target_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("target"); + Cache::at(&target_dir.join("test_cache")) } /// The `step_install_wasm_bindgen` and `step_run_wasm_bindgen` steps only @@ -266,6 +203,23 @@ impl Fixture { .unwrap(); self } + + pub fn run(&self, cmd: wasm_pack::command::Command) -> Result<(), failure::Error> { + let logger = wasm_pack::logger::new(&cmd, 3)?; + match cmd { + wasm_pack::command::Command::Test(cmd) => { + let mut test = wasm_pack::command::test::Test::try_from_opts(cmd)?; + test.set_cache(self.cache()); + test.run(&logger) + } + wasm_pack::command::Command::Build(cmd) => { + let mut build = wasm_pack::command::build::Build::try_from_opts(cmd)?; + build.set_cache(self.cache()); + build.run(&logger) + } + _ => unreachable!(), + } + } } impl Drop for Fixture { diff --git a/tests/all/webdriver.rs b/tests/all/webdriver.rs index 5e3b0cb..f57c275 100644 --- a/tests/all/webdriver.rs +++ b/tests/all/webdriver.rs @@ -1,4 +1,5 @@ use utils::fixture; +use wasm_pack::binaries::Cache; use wasm_pack::test::webdriver; #[test] @@ -9,7 +10,8 @@ use wasm_pack::test::webdriver; ))] fn can_install_chromedriver() { let fixture = fixture::js_hello_world(); - assert!(webdriver::install_chromedriver(&fixture.path).is_ok()); + let cache = Cache::at(&fixture.path); + assert!(webdriver::install_chromedriver(&cache, true).is_ok()); } #[test] @@ -22,5 +24,6 @@ fn can_install_chromedriver() { ))] fn can_install_geckodriver() { let fixture = fixture::js_hello_world(); - assert!(webdriver::install_geckodriver(&fixture.path).is_ok()); + let cache = Cache::at(&fixture.path); + assert!(webdriver::install_geckodriver(&cache, true).is_ok()); }