From 84983fae801a8ba3fc502bd86e9d1ac9cbcc2f0b Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Thu, 23 May 2019 12:48:52 -0500 Subject: [PATCH 01/15] test(generate): add generate with defaults test --- Cargo.lock | 63 +++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +++ tests/wrangler.rs | 16 ++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 tests/wrangler.rs diff --git a/Cargo.lock b/Cargo.lock index f4cee0df5..634001b34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -46,6 +46,17 @@ dependencies = [ "nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "assert_cmd" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "escargot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "predicates 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "predicates-core 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "predicates-tree 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "atty" version = "0.2.11" @@ -339,6 +350,11 @@ dependencies = [ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "difference" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "dirs" version = "1.0.5" @@ -379,6 +395,17 @@ dependencies = [ "termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "escargot" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "failure" version = "0.1.5" @@ -944,6 +971,29 @@ name = "podio" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "predicates" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "predicates-core 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "predicates-core" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "predicates-tree" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "predicates-core 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "treeline 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "proc-macro2" version = "0.4.27" @@ -1556,6 +1606,11 @@ dependencies = [ "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "treeline" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "try-lock" version = "0.2.2" @@ -1715,6 +1770,7 @@ dependencies = [ name = "wrangler" version = "0.1.1" dependencies = [ + "assert_cmd 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)", "binary-install 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", "config 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1776,6 +1832,7 @@ dependencies = [ "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.10 (registry+https://github.com/rust-lang/crates.io-index)" = "92c7fb76bc8826a8b33b4ee5bb07a247a81e76764ab4d55e8f73e3a4d8808c71" +"checksum assert_cmd 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2dc477793bd82ec39799b6f6b3df64938532fdf2ab0d49ef817eac65856a5a1e" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" "checksum autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6d640bee2da49f60a4068a7fae53acde8982514ab7bae8b8cea9e88cbcfd799" "checksum backtrace 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "cd5a90e2b463010cd0e0ce9a11d4a9d5d58d9f41d4a6ba3dcaf9e68b466e88b4" @@ -1807,11 +1864,13 @@ dependencies = [ "checksum crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f8306fcef4a7b563b76b7dd949ca48f52bc1141aa067d2ea09565f3e2652aa5c" "checksum curl 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)" = "bed4741d1d4e1fc1ba6786c1313057c609259785cde2c45e34602acc45fd6ccc" "checksum curl-sys 0.4.17 (registry+https://github.com/rust-lang/crates.io-index)" = "7b8d8e51964f58c8053337fcef48e1c4608c7ee70c6f2e457674a97dda5a5828" +"checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" "checksum dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901" "checksum dtoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6d301140eb411af13d3115f9a562c85cc6b541ade9dfa314132244aaee7489dd" "checksum encode_unicode 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "90b2c9496c001e8cb61827acdefad780795c42264c137744cae6f7d9e3450abd" "checksum encoding_rs 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)" = "4155785c79f2f6701f185eb2e6b4caf0555ec03477cb4c70db67b465311620ed" "checksum env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b61fa891024a945da30a9581546e8cfaf5602c7b3f4c137a2805cf388f92075a" +"checksum escargot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ceb9adbf9874d5d028b5e4c5739d22b71988252b25c9c98fe7cf9738bee84597" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" "checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1" "checksum filetime 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a2df5c1a8c4be27e7707789dc42ae65976e60b394afd293d1419ab915833e646" @@ -1878,6 +1937,9 @@ dependencies = [ "checksum phf_shared 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)" = "234f71a15de2288bcb7e3b6515828d22af7ec8598ee6d24c3b526fa0a80b67a0" "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 predicates 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "53e09015b0d3f5a0ec2d4428f7559bb7b3fff341b4e159fedd1d57fac8b939ff" +"checksum predicates-core 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "06075c3a3e92559ff8929e7a280684489ea27fe44805174c3ebd9328dcb37178" +"checksum predicates-tree 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8e63c4859013b38a76eca2414c64911fba30def9e3202ac461a2d22831220124" "checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" "checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1" @@ -1945,6 +2007,7 @@ dependencies = [ "checksum tokio-timer 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "2910970404ba6fa78c5539126a9ae2045d62e3713041e447f695f41405a120c6" "checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" "checksum toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "87c5890a989fa47ecdc7bcb4c63a77a82c18f306714104b1decfd722db17b39e" +"checksum treeline 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a7f741b240f1a48843f9b8e0444fb55fb2a4ff67293b50a9179dfd5ea67f8d41" "checksum try-lock 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" "checksum unicase 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7f4765f83163b74f957c797ad9253caf97f103fb064d3999aea9568d09fc8a33" diff --git a/Cargo.toml b/Cargo.toml index cf322babd..f2f91eb1b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,5 +25,8 @@ toml = "0.5.0" uuid = "0.7" which = "2.0.1" +[dev-dependencies] +assert_cmd = "0.11.1" + [features] vendored-openssl = ['openssl/vendored'] diff --git a/tests/wrangler.rs b/tests/wrangler.rs new file mode 100644 index 000000000..78c397395 --- /dev/null +++ b/tests/wrangler.rs @@ -0,0 +1,16 @@ +use assert_cmd::prelude::*; + +use std::fs; +use std::path::Path; +use std::process::Command; + +#[test] +fn it_generates() { + let mut wrangler = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); + wrangler.arg("generate").assert(); + + assert_eq!(Path::new("wasm-worker").exists(), true); + assert_eq!(Path::new("wasm-worker/wrangler.toml").exists(), true); + fs::remove_dir_all("wasm-worker").unwrap(); + assert_eq!(Path::new("wasm-worker").exists(), false); +} From 4f4ea126e69eab6ed00739ca8d3fa3c81536bec1 Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Thu, 23 May 2019 12:50:47 -0500 Subject: [PATCH 02/15] fix(generate): default name is worker --- src/main.rs | 4 ++-- tests/wrangler.rs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0c1a137e7..2ab7ac292 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,7 +30,7 @@ fn main() -> Result<(), failure::Error> { )) .arg( Arg::with_name("name") - .help("the name of your worker! defaults to 'wasm-worker'") + .help("the name of your worker! defaults to 'worker'") .index(1), ) .arg( @@ -103,7 +103,7 @@ fn main() -> Result<(), failure::Error> { } if let Some(matches) = matches.subcommand_matches("generate") { - let name = matches.value_of("name").unwrap_or("wasm-worker"); + let name = matches.value_of("name").unwrap_or("worker"); let template = matches .value_of("template") .unwrap_or("https://github.com/cloudflare/worker-template"); diff --git a/tests/wrangler.rs b/tests/wrangler.rs index 78c397395..de7353991 100644 --- a/tests/wrangler.rs +++ b/tests/wrangler.rs @@ -9,8 +9,8 @@ fn it_generates() { let mut wrangler = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); wrangler.arg("generate").assert(); - assert_eq!(Path::new("wasm-worker").exists(), true); - assert_eq!(Path::new("wasm-worker/wrangler.toml").exists(), true); - fs::remove_dir_all("wasm-worker").unwrap(); - assert_eq!(Path::new("wasm-worker").exists(), false); + assert_eq!(Path::new("worker").exists(), true); + assert_eq!(Path::new("worker/wrangler.toml").exists(), true); + fs::remove_dir_all("worker").unwrap(); + assert_eq!(Path::new("worker").exists(), false); } From e86ba80fb214d90e11756a7a55808dacbe8b01e1 Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Thu, 23 May 2019 12:59:45 -0500 Subject: [PATCH 03/15] test(generate): with defaults and with args --- tests/wrangler.rs | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/tests/wrangler.rs b/tests/wrangler.rs index de7353991..d653aba6b 100644 --- a/tests/wrangler.rs +++ b/tests/wrangler.rs @@ -5,12 +5,40 @@ use std::path::Path; use std::process::Command; #[test] -fn it_generates() { +fn it_generates_with_defaults() { + let name = "worker"; + generate(None, None); + + assert_eq!(Path::new(name).exists(), true); + + let wranglertoml_path = format!("{}/wrangler.toml", name); + assert_eq!(Path::new(&wranglertoml_path).exists(), true); + cleanup(name); +} + +#[test] +fn it_generates_with_arguments() { + let name = "example"; + let template = "https://github.com/cloudflare/rustwasm-worker-template"; + generate(Some(name), Some(template)); + + assert_eq!(Path::new(name).exists(), true); + + let wranglertoml_path = format!("{}/wrangler.toml", name); + assert_eq!(Path::new(&wranglertoml_path).exists(), true); + cleanup(name); +} + +fn generate(name: Option<&str>, template: Option<&str>) { let mut wrangler = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); - wrangler.arg("generate").assert(); + if name.is_none() && template.is_none() { + wrangler.arg("generate").assert(); + } else if name.is_some() && template.is_some() { + wrangler.arg("generate").arg(name.unwrap()).arg(template.unwrap()).assert(); + } +} - assert_eq!(Path::new("worker").exists(), true); - assert_eq!(Path::new("worker/wrangler.toml").exists(), true); - fs::remove_dir_all("worker").unwrap(); - assert_eq!(Path::new("worker").exists(), false); +fn cleanup(name: &str) { + fs::remove_dir_all(name).unwrap(); + assert_eq!(Path::new(name).exists(), false); } From 9c15d2833a1b6313a36554f3985bd685c4d08dc3 Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Thu, 23 May 2019 13:00:26 -0500 Subject: [PATCH 04/15] tests: move wrangler -> generate --- tests/{wrangler.rs => generate.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{wrangler.rs => generate.rs} (100%) diff --git a/tests/wrangler.rs b/tests/generate.rs similarity index 100% rename from tests/wrangler.rs rename to tests/generate.rs From 004d2defe684ab497fb2ccb5a4ac457211134c21 Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Thu, 23 May 2019 13:09:37 -0500 Subject: [PATCH 05/15] test(help): basic help --- tests/help.rs | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/help.rs diff --git a/tests/help.rs b/tests/help.rs new file mode 100644 index 000000000..482288b2c --- /dev/null +++ b/tests/help.rs @@ -0,0 +1,9 @@ +use assert_cmd::prelude::*; + +use std::process::Command; + +#[test] +fn it_has_a_help_flag() { + let mut wrangler = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); + wrangler.arg("--help").assert(); +} From 613e0c8d4d04e81cac64a3ac71fbf27d50c4b1a4 Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Thu, 23 May 2019 13:14:51 -0500 Subject: [PATCH 06/15] waa --- tests/generate.rs | 4 ++-- tests/help.rs | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/generate.rs b/tests/generate.rs index d653aba6b..516aafc42 100644 --- a/tests/generate.rs +++ b/tests/generate.rs @@ -32,9 +32,9 @@ fn it_generates_with_arguments() { fn generate(name: Option<&str>, template: Option<&str>) { let mut wrangler = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); if name.is_none() && template.is_none() { - wrangler.arg("generate").assert(); + wrangler.arg("generate").assert().success(); } else if name.is_some() && template.is_some() { - wrangler.arg("generate").arg(name.unwrap()).arg(template.unwrap()).assert(); + wrangler.arg("generate").arg(name.unwrap()).arg(template.unwrap()).assert().success(); } } diff --git a/tests/help.rs b/tests/help.rs index 482288b2c..1542a60f0 100644 --- a/tests/help.rs +++ b/tests/help.rs @@ -2,8 +2,20 @@ use assert_cmd::prelude::*; use std::process::Command; +#[test] +fn it_works() { + let mut wrangler = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); + wrangler.assert().success(); +} + #[test] fn it_has_a_help_flag() { let mut wrangler = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); - wrangler.arg("--help").assert(); + wrangler.arg("--help").assert().success(); +} + +#[test] +fn it_fails_on_bad_command() { + let mut wrangler = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); + wrangler.arg("farts").assert().failure(); } From a7eda8ec906fe0dd6a4f348290b7bc3ca4e1a666 Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Thu, 23 May 2019 13:21:30 -0500 Subject: [PATCH 07/15] fix(cli): match commands, not glob else --- src/main.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 2ab7ac292..7def56268 100644 --- a/src/main.rs +++ b/src/main.rs @@ -113,7 +113,11 @@ fn main() -> Result<(), failure::Error> { ); commands::generate(name, template, &cache)?; } - } else { + } else if matches.subcommand_matches("whoami").is_some() + || matches.subcommand_matches("build").is_some() + || matches.subcommand_matches("preview").is_some() + || matches.subcommand_matches("publish").is_some() + { info!("Getting user and project settings"); let user = User::new()?; From d1e69b4a8ca8ff7af82e5e8e96d2a2cbed77445c Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Thu, 23 May 2019 13:21:47 -0500 Subject: [PATCH 08/15] fix(style): appease cargo fmt --- src/wranglerjs/mod.rs | 2 +- tests/generate.rs | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/wranglerjs/mod.rs b/src/wranglerjs/mod.rs index 0b395051d..a5283a265 100644 --- a/src/wranglerjs/mod.rs +++ b/src/wranglerjs/mod.rs @@ -176,7 +176,7 @@ pub fn run_npm_install() -> Result<(), failure::Error> { fn env_dep_installed(tool: &str) -> Result<(), failure::Error> { if !which::which(tool).is_ok() { - failure::bail!("You need to install {}", tool) + failure::bail!("You need to install {}", tool) } Ok(()) } diff --git a/tests/generate.rs b/tests/generate.rs index 516aafc42..ab3606eef 100644 --- a/tests/generate.rs +++ b/tests/generate.rs @@ -34,7 +34,12 @@ fn generate(name: Option<&str>, template: Option<&str>) { if name.is_none() && template.is_none() { wrangler.arg("generate").assert().success(); } else if name.is_some() && template.is_some() { - wrangler.arg("generate").arg(name.unwrap()).arg(template.unwrap()).assert().success(); + wrangler + .arg("generate") + .arg(name.unwrap()) + .arg(template.unwrap()) + .assert() + .success(); } } From 7098d761242d9848bdacb2a93542136b953f9ab7 Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Thu, 23 May 2019 13:31:11 -0500 Subject: [PATCH 09/15] fix(idioms): appease clippy --- src/commands/config.rs | 2 +- src/wranglerjs/mod.rs | 13 +++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/commands/config.rs b/src/commands/config.rs index 659d02f76..96fef5b6a 100644 --- a/src/commands/config.rs +++ b/src/commands/config.rs @@ -12,7 +12,7 @@ pub fn global_config(email: &str, api_key: &str) -> Result<(), failure::Error> { let toml = toml::to_string(&s)?; - let config_dir = Path::new(&dirs::home_dir().expect(&format!( + let config_dir = Path::new(&dirs::home_dir().unwrap_or_else(|| panic!( "{0} could not determine home directory. {0}", emoji::CONSTRUCTION ))) diff --git a/src/wranglerjs/mod.rs b/src/wranglerjs/mod.rs index a5283a265..7b6ae6bf6 100644 --- a/src/wranglerjs/mod.rs +++ b/src/wranglerjs/mod.rs @@ -44,12 +44,9 @@ impl Bundle { let mut script = create_prologue(); script += &wranglerjs_output.script; - match wranglerjs_output.wasm { - Some(wasm) => { - let mut wasm_file = File::create(self.wasm_path())?; - wasm_file.write_all(wasm.as_bytes())?; - } - None => {} + if let Some(wasm) = wranglerjs_output.wasm { + let mut wasm_file = File::create(self.wasm_path())?; + wasm_file.write_all(wasm.as_bytes())?; } script_file.write_all(script.as_bytes())?; @@ -159,7 +156,7 @@ pub fn run_build( } pub fn run_npm_install() -> Result<(), failure::Error> { - for tool in vec!["node", "npm"] { + for tool in &["node", "npm"] { env_dep_installed(tool)?; } let mut command = Command::new("npm"); @@ -175,7 +172,7 @@ pub fn run_npm_install() -> Result<(), failure::Error> { } fn env_dep_installed(tool: &str) -> Result<(), failure::Error> { - if !which::which(tool).is_ok() { + if which::which(tool).is_err() { failure::bail!("You need to install {}", tool) } Ok(()) From 54d77253a67591c3a47bc6db1fc5df1f1258c5d4 Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Thu, 23 May 2019 15:30:16 -0500 Subject: [PATCH 10/15] fix(ci): install cargo-gen with cargo --- azure-pipelines.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 7352cfd7a..6fda54e60 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -9,6 +9,8 @@ jobs: displayName: "Run wrangler tests, fmt, and clippy" steps: - template: ci/azure-install-rust.yml + - script: cargo install cargo-generate + displayName: "cargo install cargo-generate" - script: cargo test --locked displayName: "cargo test --locked" - script: rustup component add rustfmt From fcefe58c460c60c2f32a161876eed888f96d087f Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Thu, 23 May 2019 15:51:52 -0500 Subject: [PATCH 11/15] fix(style): appease cargo fmt --- src/commands/config.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/commands/config.rs b/src/commands/config.rs index 96fef5b6a..86c815f28 100644 --- a/src/commands/config.rs +++ b/src/commands/config.rs @@ -12,10 +12,12 @@ pub fn global_config(email: &str, api_key: &str) -> Result<(), failure::Error> { let toml = toml::to_string(&s)?; - let config_dir = Path::new(&dirs::home_dir().unwrap_or_else(|| panic!( - "{0} could not determine home directory. {0}", - emoji::CONSTRUCTION - ))) + let config_dir = Path::new(&dirs::home_dir().unwrap_or_else(|| { + panic!( + "{0} could not determine home directory. {0}", + emoji::CONSTRUCTION + ) + })) .join(".wrangler") .join("config"); fs::create_dir_all(&config_dir)?; From 90296e5ed4d37b080ee683965c737e77c2c908f7 Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Fri, 24 May 2019 15:21:24 +0100 Subject: [PATCH 12/15] feat: add build and wranglerjs tests --- Cargo.lock | 7 +++ Cargo.toml | 1 + src/main.rs | 2 +- src/wranglerjs/mod.rs | 69 ++++++++++++++++++-- tests/build.rs | 76 +++++++++++++++++++++++ tests/webpack_simple_js/index.js | 1 + tests/webpack_simple_js/package.json | 1 + tests/webpack_simple_js/webpack.config.js | 1 + 8 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 tests/build.rs create mode 100644 tests/webpack_simple_js/index.js create mode 100644 tests/webpack_simple_js/package.json create mode 100644 tests/webpack_simple_js/webpack.config.js diff --git a/Cargo.lock b/Cargo.lock index d2f52c813..81b3a6b89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -465,6 +465,11 @@ name = "foreign-types-shared" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "fs_extra" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "fuchsia-cprng" version = "0.1.1" @@ -1779,6 +1784,7 @@ dependencies = [ "dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.10.19 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1879,6 +1885,7 @@ dependencies = [ "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" "checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" "checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +"checksum fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5f2a4a2034423744d2cc7ca2068453168dcdb82c438419e639a26bd87839c674" "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" "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" diff --git a/Cargo.toml b/Cargo.toml index bb18f69f0..e676668df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ which = "2.0.1" [dev-dependencies] assert_cmd = "0.11.1" +fs_extra = "1.1.0" [features] vendored-openssl = ['openssl/vendored'] diff --git a/src/main.rs b/src/main.rs index 28008a0e9..226172096 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ mod commands; mod emoji; mod install; mod installer; -mod user; +pub mod user; mod wranglerjs; use user::User; diff --git a/src/wranglerjs/mod.rs b/src/wranglerjs/mod.rs index 6e393627b..26842c007 100644 --- a/src/wranglerjs/mod.rs +++ b/src/wranglerjs/mod.rs @@ -36,6 +36,10 @@ impl Bundle { } pub fn write(&self, wranglerjs_output: WranglerjsOutput) -> Result<(), failure::Error> { + if !Path::new(BUNDLE_OUT).exists() { + fs::create_dir(BUNDLE_OUT)?; + } + let mut metadata_file = File::create(self.metadata_path())?; metadata_file.write_all(create_metadata(self).as_bytes())?; @@ -114,10 +118,6 @@ pub fn run_build( wasm_pack_path: PathBuf, bundle: &Bundle, ) -> Result { - if !Path::new(BUNDLE_OUT).exists() { - fs::create_dir(BUNDLE_OUT)?; - } - let mut command = Command::new(executable_path()); command.env("WASM_PACK_PATH", wasm_pack_path); @@ -239,3 +239,64 @@ fn create_metadata(bundle: &Bundle) -> String { .to_string() } } + +// FIXME(sven): doesn't work because they have a race for the BUNDLE_OUT, +// make it configurable +// #[cfg(test)] +// mod tests { +// use super::*; + +// #[test] +// fn it_writes_the_bundle_metadata() { +// let wranglerjs_output = WranglerjsOutput { +// script: "".to_string(), +// dist_to_clean: None, +// wasm: None, +// }; +// let bundle = Bundle::new(); + +// bundle.write(wranglerjs_output).unwrap(); +// assert!(Path::new(&bundle.metadata_path()).exists()); + +// cleanup(BUNDLE_OUT); +// } + +// #[test] +// fn it_writes_the_bundle_script() { +// let wranglerjs_output = WranglerjsOutput { +// script: "foo".to_string(), +// dist_to_clean: None, +// wasm: None, +// }; +// let bundle = Bundle::new(); + +// bundle.write(wranglerjs_output).unwrap(); +// assert!(Path::new(&bundle.script_path()).exists()); +// assert!(!Path::new(&bundle.wasm_path()).exists()); + +// cleanup(BUNDLE_OUT); +// } + +// #[test] +// fn it_writes_the_bundle_wasm() { +// let wranglerjs_output = WranglerjsOutput { +// script: "".to_string(), +// wasm: Some("abc".to_string()), +// dist_to_clean: None, +// }; +// let bundle = Bundle::new(); + +// bundle.write(wranglerjs_output).unwrap(); +// assert!(Path::new(&bundle.wasm_path()).exists()); +// assert!(bundle.has_wasm()); + +// cleanup(BUNDLE_OUT); +// } + +// fn cleanup(name: &str) { +// let current_dir = env::current_dir().unwrap(); +// let path = Path::new(¤t_dir).join(name); +// println!("p: {:?}", path); +// fs::remove_dir_all(path).unwrap(); +// } +// } diff --git a/tests/build.rs b/tests/build.rs new file mode 100644 index 000000000..6ed27ec0a --- /dev/null +++ b/tests/build.rs @@ -0,0 +1,76 @@ +use assert_cmd::prelude::*; +use fs_extra::dir::{copy, CopyOptions}; +use std::env; +use std::fs; +use std::fs::File; +use std::io::prelude::*; +use std::path::{Path, PathBuf}; +use std::process::Command; + +const BUNDLE_OUT: &str = "./worker"; + +macro_rules! settings { + ( $f:expr, $x:expr ) => { + let file_path = fixture_path($f).join("wrangler.toml"); + let mut file = File::create(file_path).unwrap(); + let content = format!( + r#" + name = "test" + zone_id = "" + {} + "#, + $x + ); + file.write_all(content.as_bytes()).unwrap(); + }; +} + +#[test] +fn it_builds_with_webpack_single_js() { + let fixture = "webpack_simple_js"; + create_temporary_copy(fixture); + + settings! {fixture, r#" + type = "Webpack" + "#}; + + build(fixture); + assert!(fixture_out_path(fixture).join("script.js").exists()); + assert!(fixture_out_path(fixture).join("metadata.json").exists()); + cleanup(fixture); +} + +fn cleanup(fixture: &str) { + let path = fixture_path(fixture); + assert!(path.exists()); + fs::remove_dir_all(path.clone()).unwrap(); +} + +fn build(fixture: &str) { + let mut build = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); + println!("dir: {:?}", fixture_path(fixture)); + build.current_dir(fixture_path(fixture)); + build.arg("build").assert().success(); +} + +fn fixture_path(fixture: &str) -> PathBuf { + let mut dest = env::temp_dir(); + dest.push(fixture); + dest +} + +fn fixture_out_path(fixture: &str) -> PathBuf { + fixture_path(fixture).join(BUNDLE_OUT) +} + +fn create_temporary_copy(fixture: &str) { + let current_dir = env::current_dir().unwrap(); + let src = Path::new(¤t_dir).join("tests").join(fixture); + + let dest = env::temp_dir(); + + fs::create_dir_all(dest.clone()).unwrap(); + let mut options = CopyOptions::new(); + options.overwrite = true; + copy(src, dest, &options).unwrap(); +} diff --git a/tests/webpack_simple_js/index.js b/tests/webpack_simple_js/index.js new file mode 100644 index 000000000..5baefb160 --- /dev/null +++ b/tests/webpack_simple_js/index.js @@ -0,0 +1 @@ +// js diff --git a/tests/webpack_simple_js/package.json b/tests/webpack_simple_js/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/tests/webpack_simple_js/package.json @@ -0,0 +1 @@ +{} diff --git a/tests/webpack_simple_js/webpack.config.js b/tests/webpack_simple_js/webpack.config.js new file mode 100644 index 000000000..f07507ec6 --- /dev/null +++ b/tests/webpack_simple_js/webpack.config.js @@ -0,0 +1 @@ +module.exports = { entry: "./index.js" }; From 849735fd1d981caab124a3d1f4642c3265426564 Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Fri, 24 May 2019 15:26:14 +0100 Subject: [PATCH 13/15] fix: manually remove unwated changes --- Cargo.toml | 1 - docs/_installer/build-installer.rs | 30 ----- docs/_installer/index.html | 47 ------- docs/_installer/init.sh | 190 ----------------------------- docs/_installer/wrangler.js | 92 -------------- docs/index.html | 47 ------- docs/site.webmanifest | 19 --- 7 files changed, 426 deletions(-) delete mode 100644 docs/_installer/build-installer.rs delete mode 100644 docs/_installer/index.html delete mode 100644 docs/_installer/init.sh delete mode 100644 docs/_installer/wrangler.js delete mode 100644 docs/index.html delete mode 100644 docs/site.webmanifest diff --git a/Cargo.toml b/Cargo.toml index e676668df..54bb12efb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ readme = "README.md" categories = ["wasm"] [dependencies] -atty = "0.2.11" binary-install = "0.0.2" clap = "2.32.0" config = "0.9.2" diff --git a/docs/_installer/build-installer.rs b/docs/_installer/build-installer.rs deleted file mode 100644 index 228e10ca3..000000000 --- a/docs/_installer/build-installer.rs +++ /dev/null @@ -1,30 +0,0 @@ -use std::fs; - -fn main() { - fs::create_dir_all("docs/installer").unwrap(); - fs::copy( - "docs/_installer/wrangler.js", - "docs/installer/wrangler.js", - ).unwrap(); - let index = fs::read_to_string("docs/_installer/index.html").unwrap(); - fs::write( - "docs/installer/index.html", - fixup(&index), - ).unwrap(); - - let init = fs::read_to_string("docs/_installer/init.sh").unwrap(); - fs::write( - "docs/installer/init.sh", - fixup(&init), - ).unwrap(); -} - -fn fixup(input: &str) -> String { - let manifest = fs::read_to_string("Cargo.toml").unwrap(); - let version = manifest.lines() - .find(|line| line.starts_with("version =")) - .unwrap(); - let version = &version[version.find('"').unwrap() + 1..version.rfind('"').unwrap()]; - - input.replace("$VERSION", &format!("v{}", version)) -} diff --git a/docs/_installer/index.html b/docs/_installer/index.html deleted file mode 100644 index bd02a27a0..000000000 --- a/docs/_installer/index.html +++ /dev/null @@ -1,47 +0,0 @@ - - - - wrangler - - - - - - -
-
-
- - -
-
- - - - diff --git a/docs/_installer/init.sh b/docs/_installer/init.sh deleted file mode 100644 index ae2b2a236..000000000 --- a/docs/_installer/init.sh +++ /dev/null @@ -1,190 +0,0 @@ -#!/bin/bash -# Copyright 2016 The Rust Project Developers. See the COPYRIGHT -# file at the top-level directory of this distribution and at -# http://rust-lang.org/COPYRIGHT. -# -# Licensed under the Apache License, Version 2.0 or the MIT license -# , at your -# option. This file may not be copied, modified, or distributed -# except according to those terms. - -# This is just a little script that can be downloaded from the internet to -# install wrangler. It just does platform detection, downloads the installer -# and runs it. - -set -u - -UPDATE_ROOT="https://github.com/cloudflare/wrangler/releases/download/$VERSION" - -main() { - downloader --check - need_cmd uname - need_cmd mktemp - need_cmd chmod - need_cmd mkdir - need_cmd rm - need_cmd rmdir - need_cmd tar - need_cmd which - need_cmd dirname - - get_architecture || return 1 - local _arch="$RETVAL" - assert_nz "$_arch" "arch" - - local _ext="" - case "$_arch" in - *windows*) - _ext=".exe" - ;; - esac - - which rustup > /dev/null 2>&1 - need_ok "failed to find Rust installation, is rustup installed?" - local _rustup=`which rustup` - local _tardir="wrangler-$VERSION-${_arch}" - local _url="$UPDATE_ROOT/${_tardir}.tar.gz" - local _dir="$(mktemp -d 2>/dev/null || ensure mktemp -d -t wrangler)" - local _file="$_dir/input.tar.gz" - local _wasmpack="$_dir/wrangler$_ext" - local _wasmpackinit="$_dir/wrangler-init$_ext" - - printf '%s\n' 'info: downloading wrangler' 1>&2 - - ensure mkdir -p "$_dir" - downloader "$_url" "$_file" - if [ $? != 0 ]; then - say "failed to download $_url" - say "this may be a standard network error, but it may also indicate" - say "that wrangler's release process is not working. When in doubt" - say "please feel free to open an issue!" - exit 1 - fi - ensure tar xf "$_file" --strip-components 1 -C "$_dir" - mv "$_wasmpack" "$_wasmpackinit" - - # The installer may want to ask for confirmation on stdin for various - # operations. We were piped through `sh` though so we probably don't have - # access to a tty naturally. If it looks like we're attached to a terminal - # (`-t 1`) then pass the tty down to the installer explicitly. - if [ -t 1 ]; then - "$_wasmpackinit" "$@" < /dev/tty - else - "$_wasmpackinit" "$@" - fi - - local _retval=$? - - ignore rm -rf "$_dir" - - return "$_retval" -} - -get_architecture() { - local _ostype="$(uname -s)" - local _cputype="$(uname -m)" - - if [ "$_ostype" = Darwin -a "$_cputype" = i386 ]; then - # Darwin `uname -s` lies - if sysctl hw.optional.x86_64 | grep -q ': 1'; then - local _cputype=x86_64 - fi - fi - - case "$_ostype" in - Linux) - local _ostype=unknown-linux-musl - ;; - - Darwin) - local _ostype=apple-darwin - ;; - - MINGW* | MSYS* | CYGWIN*) - local _ostype=pc-windows-msvc - ;; - - *) - err "no precompiled binaries available for OS: $_ostype" - ;; - esac - - case "$_cputype" in - x86_64 | x86-64 | x64 | amd64) - local _cputype=x86_64 - ;; - *) - err "no precompiled binaries available for CPU architecture: $_cputype" - - esac - - local _arch="$_cputype-$_ostype" - - RETVAL="$_arch" -} - -say() { - echo "wrangler-init: $1" -} - -err() { - say "$1" >&2 - exit 1 -} - -need_cmd() { - if ! check_cmd "$1" - then err "need '$1' (command not found)" - fi -} - -check_cmd() { - command -v "$1" > /dev/null 2>&1 - return $? -} - -need_ok() { - if [ $? != 0 ]; then err "$1"; fi -} - -assert_nz() { - if [ -z "$1" ]; then err "assert_nz $2"; fi -} - -# Run a command that should never fail. If the command fails execution -# will immediately terminate with an error showing the failing -# command. -ensure() { - "$@" - need_ok "command failed: $*" -} - -# This is just for indicating that commands' results are being -# intentionally ignored. Usually, because it's being executed -# as part of error handling. -ignore() { - "$@" -} - -# This wraps curl or wget. Try curl first, if not installed, -# use wget instead. -downloader() { - if check_cmd curl - then _dld=curl - elif check_cmd wget - then _dld=wget - else _dld='curl or wget' # to be used in error message of need_cmd - fi - - if [ "$1" = --check ] - then need_cmd "$_dld" - elif [ "$_dld" = curl ] - then curl -sSfL "$1" -o "$2" - elif [ "$_dld" = wget ] - then wget "$1" -O "$2" - else err "Unknown downloader" # should not reach here - fi -} - -main "$@" || exit 1 diff --git a/docs/_installer/wrangler.js b/docs/_installer/wrangler.js deleted file mode 100644 index 5d87b703c..000000000 --- a/docs/_installer/wrangler.js +++ /dev/null @@ -1,92 +0,0 @@ -var platforms = ["default", "unknown", "win64", "unix"]; -var platform_override = null; - -function detect_platform() { - "use strict"; - - if (platform_override !== null) { - return platforms[platform_override]; - } - - var os = "unknown"; - - if (navigator.platform == "Linux x86_64") {os = "unix";} - if (navigator.platform == "Linux i686") {os = "unix";} - if (navigator.platform == "Linux i686 on x86_64") {os = "unix";} - if (navigator.platform == "Linux aarch64") {os = "unix";} - if (navigator.platform == "Linux armv6l") {os = "unix";} - if (navigator.platform == "Linux armv7l") {os = "unix";} - if (navigator.platform == "Linux armv8l") {os = "unix";} - if (navigator.platform == "Linux ppc64") {os = "unix";} - if (navigator.platform == "Linux mips") {os = "unix";} - if (navigator.platform == "Linux mips64") {os = "unix";} - if (navigator.platform == "Mac") {os = "unix";} - // if (navigator.platform == "Win32") {os = "win32";} - if (navigator.platform == "Win64" || - navigator.userAgent.indexOf("WOW64") != -1 || - navigator.userAgent.indexOf("Win64") != -1) { os = "win64"; } - if (navigator.platform == "FreeBSD x86_64") {os = "unix";} - if (navigator.platform == "FreeBSD amd64") {os = "unix";} - if (navigator.platform == "NetBSD x86_64") {os = "unix";} - if (navigator.platform == "NetBSD amd64") {os = "unix";} - - // I wish I knew by now, but I don't. Try harder. - if (os == "unknown") { - // if (navigator.appVersion.indexOf("Win")!=-1) {os = "win32";} - if (navigator.appVersion.indexOf("Mac")!=-1) {os = "unix";} - // rust-www/#692 - FreeBSD epiphany! - if (navigator.appVersion.indexOf("FreeBSD")!=-1) {os = "unix";} - } - - // Firefox Quantum likes to hide platform and appVersion but oscpu works - if (navigator.oscpu) { - // if (navigator.oscpu.indexOf("Win32")!=-1) {os = "win32";} - if (navigator.oscpu.indexOf("Win64")!=-1) {os = "win64";} - if (navigator.oscpu.indexOf("Mac")!=-1) {os = "unix";} - if (navigator.oscpu.indexOf("Linux")!=-1) {os = "unix";} - if (navigator.oscpu.indexOf("FreeBSD")!=-1) {os = "unix";} - if (navigator.oscpu.indexOf("NetBSD")!=-1) {os = "unix";} - } - - return os; -} - -function adjust_for_platform() { - "use strict"; - - var platform = detect_platform(); - - platforms.forEach(function (platform_elem) { - var platform_div = document.getElementById("platform-instructions-" + platform_elem); - platform_div.style.display = "none"; - if (platform == platform_elem || - (platform == 'unknown' && platform_elem == 'default')) { - platform_div.style.display = "block"; - } - }); -} - -function go_to_default_platform() { - platform_override = 0; - adjust_for_platform(); -} - -function set_up_default_platform_buttons() { - var defaults_buttons = document.getElementsByClassName('default-platform-button'); - for (var i = 0; i < defaults_buttons.length; i++) { - defaults_buttons[i].onclick = go_to_default_platform; - } -} - -function fill_in_bug_report_values() { - var nav_plat = document.getElementById("nav-plat"); - var nav_app = document.getElementById("nav-app"); - nav_plat.textContent = navigator.platform; - nav_app.textContent = navigator.appVersion; -} - -(function () { - adjust_for_platform(); - set_up_default_platform_buttons(); - fill_in_bug_report_values(); -}()); diff --git a/docs/index.html b/docs/index.html deleted file mode 100644 index bd02a27a0..000000000 --- a/docs/index.html +++ /dev/null @@ -1,47 +0,0 @@ - - - - wrangler - - - - - - -
-
-
- - -
-
- - - - diff --git a/docs/site.webmanifest b/docs/site.webmanifest deleted file mode 100644 index 6c7394624..000000000 --- a/docs/site.webmanifest +++ /dev/null @@ -1,19 +0,0 @@ -{ - "name": "wrangler", - "short_name": "wrangler", - "icons": [ - { - "src": "/android-chrome-192x192.png", - "sizes": "192x192", - "type": "image/png" - }, - { - "src": "/android-chrome-384x384.png", - "sizes": "384x384", - "type": "image/png" - } - ], - "theme_color": "#ffffff", - "background_color": "#ffffff", - "display": "standalone" -} From e6ac24af81ed4c616d604847a6978f6bb5ea9bad Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Fri, 24 May 2019 15:27:10 +0100 Subject: [PATCH 14/15] Revert "fix: manually remove unwated changes" This reverts commit 849735fd1d981caab124a3d1f4642c3265426564. --- Cargo.toml | 1 + docs/_installer/build-installer.rs | 30 +++++ docs/_installer/index.html | 47 +++++++ docs/_installer/init.sh | 190 +++++++++++++++++++++++++++++ docs/_installer/wrangler.js | 92 ++++++++++++++ docs/index.html | 47 +++++++ docs/site.webmanifest | 19 +++ 7 files changed, 426 insertions(+) create mode 100644 docs/_installer/build-installer.rs create mode 100644 docs/_installer/index.html create mode 100644 docs/_installer/init.sh create mode 100644 docs/_installer/wrangler.js create mode 100644 docs/index.html create mode 100644 docs/site.webmanifest diff --git a/Cargo.toml b/Cargo.toml index 54bb12efb..e676668df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ readme = "README.md" categories = ["wasm"] [dependencies] +atty = "0.2.11" binary-install = "0.0.2" clap = "2.32.0" config = "0.9.2" diff --git a/docs/_installer/build-installer.rs b/docs/_installer/build-installer.rs new file mode 100644 index 000000000..228e10ca3 --- /dev/null +++ b/docs/_installer/build-installer.rs @@ -0,0 +1,30 @@ +use std::fs; + +fn main() { + fs::create_dir_all("docs/installer").unwrap(); + fs::copy( + "docs/_installer/wrangler.js", + "docs/installer/wrangler.js", + ).unwrap(); + let index = fs::read_to_string("docs/_installer/index.html").unwrap(); + fs::write( + "docs/installer/index.html", + fixup(&index), + ).unwrap(); + + let init = fs::read_to_string("docs/_installer/init.sh").unwrap(); + fs::write( + "docs/installer/init.sh", + fixup(&init), + ).unwrap(); +} + +fn fixup(input: &str) -> String { + let manifest = fs::read_to_string("Cargo.toml").unwrap(); + let version = manifest.lines() + .find(|line| line.starts_with("version =")) + .unwrap(); + let version = &version[version.find('"').unwrap() + 1..version.rfind('"').unwrap()]; + + input.replace("$VERSION", &format!("v{}", version)) +} diff --git a/docs/_installer/index.html b/docs/_installer/index.html new file mode 100644 index 000000000..bd02a27a0 --- /dev/null +++ b/docs/_installer/index.html @@ -0,0 +1,47 @@ + + + + wrangler + + + + + + +
+
+
+ + +
+
+ + + + diff --git a/docs/_installer/init.sh b/docs/_installer/init.sh new file mode 100644 index 000000000..ae2b2a236 --- /dev/null +++ b/docs/_installer/init.sh @@ -0,0 +1,190 @@ +#!/bin/bash +# Copyright 2016 The Rust Project Developers. See the COPYRIGHT +# file at the top-level directory of this distribution and at +# http://rust-lang.org/COPYRIGHT. +# +# Licensed under the Apache License, Version 2.0 or the MIT license +# , at your +# option. This file may not be copied, modified, or distributed +# except according to those terms. + +# This is just a little script that can be downloaded from the internet to +# install wrangler. It just does platform detection, downloads the installer +# and runs it. + +set -u + +UPDATE_ROOT="https://github.com/cloudflare/wrangler/releases/download/$VERSION" + +main() { + downloader --check + need_cmd uname + need_cmd mktemp + need_cmd chmod + need_cmd mkdir + need_cmd rm + need_cmd rmdir + need_cmd tar + need_cmd which + need_cmd dirname + + get_architecture || return 1 + local _arch="$RETVAL" + assert_nz "$_arch" "arch" + + local _ext="" + case "$_arch" in + *windows*) + _ext=".exe" + ;; + esac + + which rustup > /dev/null 2>&1 + need_ok "failed to find Rust installation, is rustup installed?" + local _rustup=`which rustup` + local _tardir="wrangler-$VERSION-${_arch}" + local _url="$UPDATE_ROOT/${_tardir}.tar.gz" + local _dir="$(mktemp -d 2>/dev/null || ensure mktemp -d -t wrangler)" + local _file="$_dir/input.tar.gz" + local _wasmpack="$_dir/wrangler$_ext" + local _wasmpackinit="$_dir/wrangler-init$_ext" + + printf '%s\n' 'info: downloading wrangler' 1>&2 + + ensure mkdir -p "$_dir" + downloader "$_url" "$_file" + if [ $? != 0 ]; then + say "failed to download $_url" + say "this may be a standard network error, but it may also indicate" + say "that wrangler's release process is not working. When in doubt" + say "please feel free to open an issue!" + exit 1 + fi + ensure tar xf "$_file" --strip-components 1 -C "$_dir" + mv "$_wasmpack" "$_wasmpackinit" + + # The installer may want to ask for confirmation on stdin for various + # operations. We were piped through `sh` though so we probably don't have + # access to a tty naturally. If it looks like we're attached to a terminal + # (`-t 1`) then pass the tty down to the installer explicitly. + if [ -t 1 ]; then + "$_wasmpackinit" "$@" < /dev/tty + else + "$_wasmpackinit" "$@" + fi + + local _retval=$? + + ignore rm -rf "$_dir" + + return "$_retval" +} + +get_architecture() { + local _ostype="$(uname -s)" + local _cputype="$(uname -m)" + + if [ "$_ostype" = Darwin -a "$_cputype" = i386 ]; then + # Darwin `uname -s` lies + if sysctl hw.optional.x86_64 | grep -q ': 1'; then + local _cputype=x86_64 + fi + fi + + case "$_ostype" in + Linux) + local _ostype=unknown-linux-musl + ;; + + Darwin) + local _ostype=apple-darwin + ;; + + MINGW* | MSYS* | CYGWIN*) + local _ostype=pc-windows-msvc + ;; + + *) + err "no precompiled binaries available for OS: $_ostype" + ;; + esac + + case "$_cputype" in + x86_64 | x86-64 | x64 | amd64) + local _cputype=x86_64 + ;; + *) + err "no precompiled binaries available for CPU architecture: $_cputype" + + esac + + local _arch="$_cputype-$_ostype" + + RETVAL="$_arch" +} + +say() { + echo "wrangler-init: $1" +} + +err() { + say "$1" >&2 + exit 1 +} + +need_cmd() { + if ! check_cmd "$1" + then err "need '$1' (command not found)" + fi +} + +check_cmd() { + command -v "$1" > /dev/null 2>&1 + return $? +} + +need_ok() { + if [ $? != 0 ]; then err "$1"; fi +} + +assert_nz() { + if [ -z "$1" ]; then err "assert_nz $2"; fi +} + +# Run a command that should never fail. If the command fails execution +# will immediately terminate with an error showing the failing +# command. +ensure() { + "$@" + need_ok "command failed: $*" +} + +# This is just for indicating that commands' results are being +# intentionally ignored. Usually, because it's being executed +# as part of error handling. +ignore() { + "$@" +} + +# This wraps curl or wget. Try curl first, if not installed, +# use wget instead. +downloader() { + if check_cmd curl + then _dld=curl + elif check_cmd wget + then _dld=wget + else _dld='curl or wget' # to be used in error message of need_cmd + fi + + if [ "$1" = --check ] + then need_cmd "$_dld" + elif [ "$_dld" = curl ] + then curl -sSfL "$1" -o "$2" + elif [ "$_dld" = wget ] + then wget "$1" -O "$2" + else err "Unknown downloader" # should not reach here + fi +} + +main "$@" || exit 1 diff --git a/docs/_installer/wrangler.js b/docs/_installer/wrangler.js new file mode 100644 index 000000000..5d87b703c --- /dev/null +++ b/docs/_installer/wrangler.js @@ -0,0 +1,92 @@ +var platforms = ["default", "unknown", "win64", "unix"]; +var platform_override = null; + +function detect_platform() { + "use strict"; + + if (platform_override !== null) { + return platforms[platform_override]; + } + + var os = "unknown"; + + if (navigator.platform == "Linux x86_64") {os = "unix";} + if (navigator.platform == "Linux i686") {os = "unix";} + if (navigator.platform == "Linux i686 on x86_64") {os = "unix";} + if (navigator.platform == "Linux aarch64") {os = "unix";} + if (navigator.platform == "Linux armv6l") {os = "unix";} + if (navigator.platform == "Linux armv7l") {os = "unix";} + if (navigator.platform == "Linux armv8l") {os = "unix";} + if (navigator.platform == "Linux ppc64") {os = "unix";} + if (navigator.platform == "Linux mips") {os = "unix";} + if (navigator.platform == "Linux mips64") {os = "unix";} + if (navigator.platform == "Mac") {os = "unix";} + // if (navigator.platform == "Win32") {os = "win32";} + if (navigator.platform == "Win64" || + navigator.userAgent.indexOf("WOW64") != -1 || + navigator.userAgent.indexOf("Win64") != -1) { os = "win64"; } + if (navigator.platform == "FreeBSD x86_64") {os = "unix";} + if (navigator.platform == "FreeBSD amd64") {os = "unix";} + if (navigator.platform == "NetBSD x86_64") {os = "unix";} + if (navigator.platform == "NetBSD amd64") {os = "unix";} + + // I wish I knew by now, but I don't. Try harder. + if (os == "unknown") { + // if (navigator.appVersion.indexOf("Win")!=-1) {os = "win32";} + if (navigator.appVersion.indexOf("Mac")!=-1) {os = "unix";} + // rust-www/#692 - FreeBSD epiphany! + if (navigator.appVersion.indexOf("FreeBSD")!=-1) {os = "unix";} + } + + // Firefox Quantum likes to hide platform and appVersion but oscpu works + if (navigator.oscpu) { + // if (navigator.oscpu.indexOf("Win32")!=-1) {os = "win32";} + if (navigator.oscpu.indexOf("Win64")!=-1) {os = "win64";} + if (navigator.oscpu.indexOf("Mac")!=-1) {os = "unix";} + if (navigator.oscpu.indexOf("Linux")!=-1) {os = "unix";} + if (navigator.oscpu.indexOf("FreeBSD")!=-1) {os = "unix";} + if (navigator.oscpu.indexOf("NetBSD")!=-1) {os = "unix";} + } + + return os; +} + +function adjust_for_platform() { + "use strict"; + + var platform = detect_platform(); + + platforms.forEach(function (platform_elem) { + var platform_div = document.getElementById("platform-instructions-" + platform_elem); + platform_div.style.display = "none"; + if (platform == platform_elem || + (platform == 'unknown' && platform_elem == 'default')) { + platform_div.style.display = "block"; + } + }); +} + +function go_to_default_platform() { + platform_override = 0; + adjust_for_platform(); +} + +function set_up_default_platform_buttons() { + var defaults_buttons = document.getElementsByClassName('default-platform-button'); + for (var i = 0; i < defaults_buttons.length; i++) { + defaults_buttons[i].onclick = go_to_default_platform; + } +} + +function fill_in_bug_report_values() { + var nav_plat = document.getElementById("nav-plat"); + var nav_app = document.getElementById("nav-app"); + nav_plat.textContent = navigator.platform; + nav_app.textContent = navigator.appVersion; +} + +(function () { + adjust_for_platform(); + set_up_default_platform_buttons(); + fill_in_bug_report_values(); +}()); diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 000000000..bd02a27a0 --- /dev/null +++ b/docs/index.html @@ -0,0 +1,47 @@ + + + + wrangler + + + + + + +
+
+
+ + +
+
+ + + + diff --git a/docs/site.webmanifest b/docs/site.webmanifest new file mode 100644 index 000000000..6c7394624 --- /dev/null +++ b/docs/site.webmanifest @@ -0,0 +1,19 @@ +{ + "name": "wrangler", + "short_name": "wrangler", + "icons": [ + { + "src": "/android-chrome-192x192.png", + "sizes": "192x192", + "type": "image/png" + }, + { + "src": "/android-chrome-384x384.png", + "sizes": "384x384", + "type": "image/png" + } + ], + "theme_color": "#ffffff", + "background_color": "#ffffff", + "display": "standalone" +} From a1500792408a7a9dfc5f83da3eeed4b48d134e37 Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Fri, 24 May 2019 16:17:28 +0100 Subject: [PATCH 15/15] fix: remove unneeded pub --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 226172096..28008a0e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ mod commands; mod emoji; mod install; mod installer; -pub mod user; +mod user; mod wranglerjs; use user::User;