Skip to content

Commit

Permalink
Merge pull request rustwasm#628 from drager/refactor-version-check
Browse files Browse the repository at this point in the history
refactor: Use PBAR.warn and a struct for versions
  • Loading branch information
ashleygwilliams authored Apr 15, 2019
2 parents c616f6b + e6733d4 commit fc87511
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 59 deletions.
16 changes: 13 additions & 3 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ use PBAR;

pub mod wasm_target;

/// Used when comparing the currently installed
/// wasm-pack version with the latest on crates.io.
pub struct WasmPackVersion {
/// The currently installed wasm-pack version.
pub local: String,
/// The latest version of wasm-pack that's released at
/// crates.io.
pub latest: String,
}

/// Ensure that `rustc` is present and that it is >= 1.30.0
pub fn check_rustc_version() -> Result<String, Error> {
let local_minor_version = rustc_minor_version();
Expand Down Expand Up @@ -50,12 +60,12 @@ fn rustc_minor_version() -> Option<u32> {
}

/// Checks and returns local and latest versions of wasm-pack
pub fn check_wasm_pack_versions() -> Result<(String, String), Error> {
pub fn check_wasm_pack_versions() -> Result<WasmPackVersion, 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()))
Some(latest) => Ok(WasmPackVersion {local, latest}),
None => Ok(WasmPackVersion {local, latest: "".to_string()})
}
},
None => bail!("We can't figure out what your wasm-pack version is, make sure the installation path is correct.")
Expand Down
6 changes: 0 additions & 6 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,6 @@ 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
27 changes: 17 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@ use std::panic;
use std::sync::mpsc;
use std::thread;
use structopt::StructOpt;
use wasm_pack::command::build::Build;
use wasm_pack::{command::run_wasm_pack, Cli};
use wasm_pack::{
build::{self, WasmPackVersion},
command::run_wasm_pack,
Cli, PBAR,
};

mod installer;

fn background_check_for_updates() -> mpsc::Receiver<(String, String)> {
fn background_check_for_updates() -> mpsc::Receiver<WasmPackVersion> {
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();
if let Ok(wasm_pack_version) = build::check_wasm_pack_versions() {
if !wasm_pack_version.local.is_empty()
&& !wasm_pack_version.latest.is_empty()
&& wasm_pack_version.local != wasm_pack_version.latest
{
let _ = sender.send(wasm_pack_version);
}
}
});
Expand All @@ -46,7 +53,7 @@ fn main() {
}

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

// Deprecate `init`
if let Some("init") = env::args().nth(1).as_ref().map(|arg| arg.as_str()) {
Expand All @@ -69,9 +76,9 @@ 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: {}. \
To update, navigate to: https://rustwasm.github.io/wasm-pack/installer/", update_available.1, update_available.0);
if let Ok(wasm_pack_version) = wasm_pack_version.try_recv() {
PBAR.warn(&format!("There's a newer version of wasm-pack available, the new version is: {}, you are using: {}. \
To update, navigate to: https://rustwasm.github.io/wasm-pack/installer/", wasm_pack_version.latest, wasm_pack_version.local));
}

Ok(())
Expand Down
82 changes: 42 additions & 40 deletions src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ use failure::{Error, ResultExt};
use serde::{self, Deserialize};
use serde_json;
use std::collections::BTreeSet;
use std::env;
use std::io::Write;
use strsim::levenshtein;
use toml;
use which;
use PBAR;

const WASM_PACK_METADATA_KEY: &str = "package.metadata.wasm-pack";
Expand Down Expand Up @@ -143,55 +143,57 @@ 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);

Self::return_wasm_pack_file()
.and_then(|contents| {
let last_updated = Self::return_stamp_file_value(&contents, "created")
.and_then(|t| DateTime::parse_from_str(t.as_str(), "%+").ok());

Self::return_stamp_file_value(&contents, "version").and_then(|v| {
last_updated.and_then(|last_updated| {
if current_time.signed_duration_since(last_updated).num_hours() > 24 {
Self::return_api_call_result(current_time)
} else {
Some(v)
}
})
})
})
.map_or(Self::return_api_call_result(current_time), |value| {
Some(value)
})
}

fn return_api_call_result(current_time: DateTime<offset::Local>) -> Option<String> {
Crate::return_latest_wasm_pack_version().and_then(|v| {
Crate::override_stamp_file(current_time, &v);
Self::return_latest_wasm_pack_version().and_then(|v| {
Self::override_stamp_file(current_time, &v).ok();
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 override_stamp_file(
current_time: DateTime<offset::Local>,
version: &String,
) -> Result<(), failure::Error> {
let path = env::current_exe()?;

let mut file = fs::OpenOptions::new()
.read(true)
.write(true)
.append(true)
.create(true)
.open(path.with_extension("stamp"))?;

file.set_len(0)?;

write!(file, "created {:?}\nversion {}", current_time, version)?;

Ok(())
}

/// Return stamp file where metadata is stored.
fn return_wasm_pack_file() -> Option<String> {
if let Ok(path) = which::which("wasm-pack") {
if let Ok(path) = env::current_exe() {
if let Ok(file) = fs::read_to_string(path.with_extension("stamp")) {
return Some(file);
}
Expand All @@ -201,7 +203,7 @@ impl Crate {

/// Returns wasm-pack latest version (if it's received) by executing check_wasm_pack_latest_version function.
fn return_latest_wasm_pack_version() -> Option<String> {
if let Ok(crt) = Crate::check_wasm_pack_latest_version() {
if let Ok(crt) = Self::check_wasm_pack_latest_version() {
return Some(crt.crt.max_version);
}
None
Expand Down

0 comments on commit fc87511

Please sign in to comment.