addition of stamp file and additional thread for running version checks

master
daubaris 7 years ago
parent fc9c69e136
commit 75b85e58c7
  1. 1
      Cargo.toml
  2. 20
      src/build.rs
  3. 16
      src/command/build.rs
  4. 21
      src/command/mod.rs
  5. 1
      src/lib.rs
  6. 150
      src/manifest/mod.rs

@ -35,6 +35,7 @@ tar = "0.4.16"
toml = "0.4"
which = "2.0.0"
zip = "0.4.2"
chrono = "0.4.6"
[dev-dependencies]
tempfile = "3"

@ -33,21 +33,15 @@ pub fn check_rustc_version(step: &Step) -> Result<String, Error> {
}
/// Checks if `wasm-pack` is updated to the latest version
pub fn check_wasm_pack_version(step: &Step) -> Result<(), failure::Error> {
let msg = format!("{}Checking `wasm-pack` version...", emoji::PACKAGE);
PBAR.step(step, &msg);
let wasm_pack_local_version = wasm_pack_local_version();
let wasm_pack_latest_version = Crate::return_wasm_pack_latest_version()?;
match wasm_pack_local_version {
Some(lv) => {
if !(lv == wasm_pack_latest_version) {
Ok(PBAR.info(&format!("There's a newer version of wasm-pack available, the new version is: {}, you are using: {}", wasm_pack_latest_version, lv)))
} else {
Ok(())
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."),
None => bail!("We can't figure out what your wasm-pack version is, make sure the installation path is correct.")
}
}

@ -203,7 +203,6 @@ impl Build {
match &mode {
BuildMode::Normal => steps![
step_check_rustc_version,
step_check_wasm_pack_version,
step_check_crate_config,
step_add_wasm_target,
step_build_wasm,
@ -215,7 +214,6 @@ impl Build {
],
BuildMode::Noinstall => steps![
step_check_rustc_version,
step_check_wasm_pack_version,
step_check_crate_config,
step_build_wasm,
step_create_dir,
@ -233,6 +231,12 @@ impl Build {
}
}
/// Returns local and latest versions of wasm-pack
pub fn return_wasm_pack_versions() -> Result<(String, String), Error> {
let (local, latest) = build::check_wasm_pack_versions()?;
Ok((local, latest))
}
fn step_check_rustc_version(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Checking rustc version...");
let version = build::check_rustc_version(step)?;
@ -241,14 +245,6 @@ impl Build {
Ok(())
}
fn step_check_wasm_pack_version(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Checking wasm-pack version...");
let version = build::check_wasm_pack_version(step)?;
let msg = format!("wasm-pack version is: {:?}.", version);
info!(&log, "{}", &msg);
Ok(())
}
fn step_check_crate_config(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Checking crate configuration...");
self.crate_data.check_crate_config(step)?;

@ -13,11 +13,11 @@ use self::login::login;
use self::pack::pack;
use self::publish::{access::Access, publish};
use self::test::{Test, TestOptions};
use std::sync::mpsc;
use failure::Error;
use slog::Logger;
use std::path::PathBuf;
use std::result;
use std::sync::mpsc;
use std::thread;
/// The various kinds of commands that `wasm-pack` can execute.
@ -84,23 +84,30 @@ pub enum Command {
Test(TestOptions),
}
/// Spawn a thread for wasm-pack version
/// Spawn a thread for wasm-pack version check
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
}
/// Run a command with the given logger!
pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error> {
// Run the correct command based off input and store the result of it so that we can clear
// the progress bar then return it
let update_available = background_check_for_updates();
let status = match command {
Command::Build(build_opts) => {
info!(&log, "Running build command...");
// Add the background_check_for_updates() here
Build::try_from_opts(build_opts).and_then(|mut b| b.run(&log))
}
Command::Pack { path } => {
@ -136,6 +143,10 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error
}
};
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);
}
// Return the actual status of the program to the main function
status
}

@ -23,6 +23,7 @@ extern crate siphasher;
extern crate structopt;
#[macro_use]
extern crate slog;
extern crate chrono;
extern crate slog_async;
extern crate slog_term;
extern crate tar;

@ -9,6 +9,8 @@ use self::npm::{
repository::Repository, CommonJSPackage, ESModulesPackage, NoModulesPackage, NpmPackage,
};
use cargo_metadata::Metadata;
use chrono::offset;
use chrono::DateTime;
use command::build::BuildProfile;
use curl::easy;
use emoji;
@ -16,7 +18,9 @@ use failure::{Error, ResultExt};
use progressbar::Step;
use serde::{self, Deserialize};
use serde_json;
use std::io::Write;
use toml;
use which;
use PBAR;
/// Store for metadata learned about a crate
@ -26,7 +30,6 @@ pub struct CrateData {
manifest: CargoManifest,
}
#[derive(Deserialize)]
struct Collector(Vec<u8>);
impl easy::Handler for Collector {
@ -36,7 +39,7 @@ impl easy::Handler for Collector {
}
}
#[derive(Debug, Deserialize)]
#[derive(Deserialize)]
struct CargoManifest {
package: CargoPackage,
}
@ -115,6 +118,117 @@ struct CargoWasmPackProfileWasmBindgen {
dwarf_debug_info: Option<bool>,
}
/// Struct for crate which is 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();
if let Some(contents) = Crate::return_wasm_pack_file() {
let (created, version) = Crate::return_stamp_file_values(&contents);
if current_time.signed_duration_since(created).num_hours() > 24 {
return Crate::return_api_call_result(current_time);
} else {
return Some(version);
}
} else {
return Crate::return_api_call_result(current_time);
}
}
fn return_api_call_result(current_time: DateTime<offset::Local>) -> Option<String> {
match Crate::call_for_wasm_pack_version() {
Some(version) => {
Crate::override_stamp_file(current_time, &version);
return Some(version);
}
None => return None,
}
}
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);
}
return None;
}
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_values(file: &String) -> (DateTime<offset::FixedOffset>, String) {
let created = file
.lines()
.find(|line| line.starts_with("created"))
.unwrap()
.split_whitespace()
.nth(1)
.unwrap();
let last_updated = DateTime::parse_from_str(created, "%+").unwrap();
let version = file
.lines()
.find(|line| line.starts_with("version"))
.unwrap()
.split_whitespace()
.nth(1)
.unwrap()
.to_string();
(last_updated, version)
}
/// 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 {
@ -207,38 +321,6 @@ struct NpmData {
main: String,
}
/// Struct for crates.io api, currently checking wasm-pack latest version
#[derive(Deserialize, Debug)]
pub struct Crate {
#[serde(rename = "crate")]
crt: CrateInformation,
}
#[derive(Deserialize, Debug)]
struct CrateInformation {
max_version: String,
}
impl Crate {
/// Call to the crates.io api and return the latest version of `wasm-pack`
pub fn return_wasm_pack_latest_version() -> Result<String, Error> {
let crt = Crate::check_wasm_pack_latest_version()?;
Ok(crt.crt.max_version)
}
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 CrateData {
/// Reads all metadata for the crate whose manifest is inside the directory
/// specified by `path`.

Loading…
Cancel
Save