Skip to content
This repository has been archived by the owner on Nov 8, 2021. It is now read-only.

Commit

Permalink
Updated with the current master
Browse files Browse the repository at this point in the history
  • Loading branch information
daubaris committed Mar 19, 2019
1 parent 88f45fe commit 9934171
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 2 deletions.
25 changes: 24 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "wasm-pack"
description = "📦✨ your favorite rust -> wasm workflow tool!"
version = "0.7.0"
version = "0.6.0"
authors = ["Ashley Williams <[email protected]>"]
repository = "https://github.com/ashleygwilliams/wasm-pack.git"
license = "MIT/Apache-2.0"
Expand Down Expand Up @@ -34,6 +34,7 @@ toml = "0.4"
which = "2.0.0"
binary-install = { version = "0.0.2", path = "./binary-install" }
walkdir = "2"
chrono = "0.4.6"

[dev-dependencies]
assert_cmd = "0.10.2"
Expand Down
37 changes: 37 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use log::info;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str;
use manifest::Crate;
use PBAR;

/// Ensure that `rustc` is present and that it is >= 1.30.0
Expand Down Expand Up @@ -47,6 +48,42 @@ fn rustc_minor_version() -> Option<u32> {
otry!(pieces.next()).parse().ok()
}

/// Checks and returns local and latest versions of wasm-pack
pub fn check_wasm_pack_versions() -> Result<(String, String), Error> {
match wasm_pack_local_version() {
Some(local) => {
match Crate::return_wasm_pack_latest_version() {
Some(latest) => Ok((local, latest)),
None => Ok((local, "".to_string()))
}
},
None => bail!("We can't figure out what your wasm-pack version is, make sure the installation path is correct.")
}
}

fn wasm_pack_local_version() -> Option<String> {
macro_rules! otry {
($e:expr) => {
match $e {
Some(e) => e,
None => return None,
}
};
}

let output = otry!(Command::new("wasm-pack").arg("--version").output().ok());
let version = otry!(str::from_utf8(&output.stdout).ok());
let mut pieces = version.split(' ');
if pieces.next() != Some("wasm-pack") {
return None;
}
otry!(pieces.next())
.to_string()
.trim()
.parse::<String>()
.ok()
}

/// Get rustc's sysroot as a PathBuf
fn get_rustc_sysroot() -> Result<PathBuf, Error> {
let command = Command::new("rustc")
Expand Down
6 changes: 6 additions & 0 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ impl Build {
Ok(())
}

/// Returns local and latest wasm-pack versions.
pub fn return_wasm_pack_versions() -> Result<(String, String), Error> {
let (local, latest) = build::check_wasm_pack_versions()?;
Ok((local, latest))
}

fn get_process_steps(mode: BuildMode) -> Vec<(&'static str, BuildStep)> {
macro_rules! steps {
($($name:ident),+) => {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ extern crate dialoguer;
extern crate log;
extern crate toml;
extern crate walkdir;
extern crate chrono;
extern crate curl;

pub mod bindgen;
pub mod build;
Expand Down
24 changes: 24 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,30 @@ extern crate human_panic;
extern crate structopt;
extern crate wasm_pack;
extern crate which;
extern crate log;

use std::env;
use structopt::StructOpt;
use wasm_pack::{command::run_wasm_pack, Cli};
use wasm_pack::command::build::{Build};
use std::sync::mpsc;
use std::thread;

mod installer;

fn background_check_for_updates() -> mpsc::Receiver<(String, String)> {
let (sender, receiver) = mpsc::channel();
let _detached_thread = thread::spawn(move || {
if let Ok((local, latest)) = Build::return_wasm_pack_versions() {
if !local.is_empty() && !latest.is_empty() && local != latest {
sender.send((local, latest)).unwrap();
}
}
});

receiver
}

fn main() {
env_logger::init();
setup_panic!();
Expand All @@ -27,6 +44,8 @@ fn main() {
}

fn run() -> Result<(), failure::Error> {
let update_available = background_check_for_updates();

// Deprecate `init`
if let Some("init") = env::args().nth(1).as_ref().map(|arg| arg.as_str()) {
println!("wasm-pack init is deprecated, consider using wasm-pack build");
Expand All @@ -47,5 +66,10 @@ fn run() -> Result<(), failure::Error> {

let args = Cli::from_args();
run_wasm_pack(args.cmd)?;

if let Ok(update_available) = update_available.try_recv() {
println!("There's a newer version of wasm-pack available, the new version is: {}, you are using: {}", update_available.1, update_available.0);
}

Ok(())
}
116 changes: 116 additions & 0 deletions src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ use command::build::{BuildProfile, Target};
use failure::{Error, ResultExt};
use serde::{self, Deserialize};
use serde_json;
use chrono::offset;
use chrono::DateTime;
use curl::easy;
use std::io::Write;
use std::collections::BTreeSet;
use strsim::levenshtein;
use which;
use toml;
use PBAR;

Expand Down Expand Up @@ -112,6 +117,117 @@ struct CargoWasmPackProfileWasmBindgen {
dwarf_debug_info: Option<bool>,
}


struct Collector(Vec<u8>);

impl easy::Handler for Collector {
fn write(&mut self, data: &[u8]) -> Result<usize, easy::WriteError> {
self.0.extend_from_slice(data);
Ok(data.len())
}
}

/// Struct for storing information received from crates.io
#[derive(Deserialize, Debug)]
pub struct Crate {
#[serde(rename = "crate")]
crt: CrateInformation,
}

#[derive(Deserialize, Debug)]
struct CrateInformation {
max_version: String,
}

impl Crate {
/// Returns latest wasm-pack version
pub fn return_wasm_pack_latest_version() -> Option<String> {
let current_time = chrono::offset::Local::now();
Crate::return_wasm_pack_file().and_then(|contents| {
let last_updated = Crate::return_stamp_file_value(&contents, "created")
.and_then(|t| Some(DateTime::parse_from_str(t.as_str(), "%+").unwrap()));
let version = Crate::return_stamp_file_value(&contents, "version").and_then(|v| {
if current_time
.signed_duration_since(last_updated.unwrap())
.num_hours()
> 24
{
return Crate::return_api_call_result(current_time);
} else {
return Some(v);
}
});
version
});
return Crate::return_api_call_result(current_time);
}

fn return_api_call_result(current_time: DateTime<offset::Local>) -> Option<String> {
Crate::call_for_wasm_pack_version().and_then(|v| {
Crate::override_stamp_file(current_time, &v);
Some(v)
})
}

fn override_stamp_file(current_time: DateTime<offset::Local>, version: &String) {
if let Ok(path) = which::which("wasm-pack") {
let file = fs::OpenOptions::new()
.read(true)
.write(true)
.append(true)
.create(true)
.open(path.with_extension("stamp"));

if let Ok(()) = file.as_ref().unwrap().set_len(0) {
if let Err(_) = write!(
file.unwrap(),
"created {:?}\nversion {}",
current_time,
version
) {}
}
}
}

fn return_wasm_pack_file() -> Option<String> {
if let Ok(path) = which::which("wasm-pack") {
if let Ok(file) = fs::read_to_string(path.with_extension("stamp")) {
return Some(file);
}
}
None
}

fn call_for_wasm_pack_version() -> Option<String> {
if let Ok(crt) = Crate::check_wasm_pack_latest_version() {
return Some(crt.crt.max_version);
}
None
}

fn return_stamp_file_value(file: &String, word: &str) -> Option<String> {
let created = file
.lines()
.find(|line| line.starts_with(word))
.and_then(|l| l.split_whitespace().nth(1));

let value = created.map(|s| s.to_string());

value
}

/// Call to the crates.io api and return the latest version of `wasm-pack`
fn check_wasm_pack_latest_version() -> Result<Crate, Error> {
let mut easy = easy::Easy2::new(Collector(Vec::new()));
easy.get(true)?;
easy.url("https://crates.io/api/v1/crates/wasm-pack")?;
easy.perform()?;
let contents = easy.get_ref();
let result = String::from_utf8_lossy(&contents.0);
Ok(serde_json::from_str(result.into_owned().as_str())?)
}
}

impl CargoWasmPackProfile {
fn default_dev() -> Self {
CargoWasmPackProfile {
Expand Down

0 comments on commit 9934171

Please sign in to comment.