Skip to content

Commit

Permalink
feat(pbar): Add global progress bar to write to
Browse files Browse the repository at this point in the history
This commit allows us to have a global progress bar to write data to
giving us the following benefits:

- Consistent ways to handle types of messages such as errors and
  warnings
- Easy interface to add progress bars of various types
- Easy to maintain, add new types of bars, or more while encapsulating
  the login in a single module
  • Loading branch information
mgattozzi committed Apr 4, 2018
1 parent 40c9886 commit aa629b3
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 66 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
toml = "0.4"
lazy_static = "1.0.0"
20 changes: 3 additions & 17 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use PBAR;
use console::style;
use emoji;
use progressbar;
use std::process::Command;

pub fn cargo_install_wasm_bindgen() {
Expand All @@ -9,22 +9,13 @@ pub fn cargo_install_wasm_bindgen() {
style("[6/7]").bold().dim(),
emoji::DOWN_ARROW
);
let pb = progressbar::new(step);
let pb = PBAR.message(&step);
let _output = Command::new("cargo")
.arg("install")
.arg("wasm-bindgen")
.output()
.unwrap_or_else(|e| panic!("{} failed to execute process: {}", emoji::ERROR, e));
pb.finish();
//if !output.status.success() {
// let s = String::from_utf8_lossy(&output.stderr);

// print!(
// "{} cargo_install_wasm_bindgen failed and stderr was:\n{}",
// emoji::ERROR,
// s
// );
// }
}

pub fn wasm_bindgen_build(path: &str, name: &str) {
Expand All @@ -33,7 +24,7 @@ pub fn wasm_bindgen_build(path: &str, name: &str) {
style("[7/7]").bold().dim(),
emoji::RUNNER
);
let pb = progressbar::new(step);
let pb = PBAR.message(&step);
let binary_name = name.replace("-", "_");
let wasm_path = format!("target/wasm32-unknown-unknown/release/{}.wasm", binary_name);
let _output = Command::new("wasm-bindgen")
Expand All @@ -44,9 +35,4 @@ pub fn wasm_bindgen_build(path: &str, name: &str) {
.output()
.unwrap_or_else(|e| panic!("{} failed to execute process: {}", emoji::ERROR, e));
pb.finish();
//if !output.status.success() {
// let s = String::from_utf8_lossy(&output.stderr);

// print!(" wasm_bindgen_build failed and stderr was:\n{}", emoji::ERROR, s);
//}
}
6 changes: 3 additions & 3 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use PBAR;
use console::style;
use emoji;
use progressbar;
use std::process::Command;

pub fn rustup_add_wasm_target() {
Expand All @@ -9,7 +9,7 @@ pub fn rustup_add_wasm_target() {
style("[1/7]").bold().dim(),
emoji::TARGET
);
let pb = progressbar::new(step);
let pb = PBAR.message(&step);
let output = Command::new("rustup")
.arg("target")
.arg("add")
Expand All @@ -34,7 +34,7 @@ pub fn cargo_build_wasm(path: &str) {
style("[2/7]").bold().dim(),
emoji::CYCLONE
);
let pb = progressbar::new(step);
let pb = PBAR.message(&step);
let output = Command::new("cargo")
.current_dir(path)
.arg("build")
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ extern crate console;
extern crate failure;
extern crate indicatif;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate toml;
Expand All @@ -18,14 +20,19 @@ use std::fs;

use console::style;
use failure::Error;
use progressbar::ProgressOutput;

lazy_static! {
pub static ref PBAR: ProgressOutput = { ProgressOutput::new() };
}

pub fn create_pkg_dir(path: &str) -> Result<(), Error> {
let step = format!(
"{} {}Creating a pkg directory...",
style("[3/7]").bold().dim(),
emoji::FOLDER
);
let pb = progressbar::new(step);
let pb = PBAR.message(&step);
let pkg_dir_path = format!("{}/pkg", path);
fs::create_dir_all(pkg_dir_path)?;
pb.finish();
Expand Down
11 changes: 6 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::time::Instant;

use indicatif::HumanDuration;
use quicli::prelude::*;
use wasm_pack::{bindgen, build, emoji, manifest, npm, readme};
use wasm_pack::{bindgen, build, emoji, manifest, npm, readme, PBAR};

/// 📦 ✨ pack and publish your wasm!
#[derive(Debug, StructOpt)]
Expand Down Expand Up @@ -54,16 +54,17 @@ main!(|args: Cli, log_level: verbosity| match args.cmd {
bindgen::cargo_install_wasm_bindgen();
let name = manifest::get_crate_name(&crate_path)?;
bindgen::wasm_bindgen_build(&crate_path, &name);
println!(
PBAR.one_off_message(&format!(
"{} Done in {}",
emoji::SPARKLE,
HumanDuration(started.elapsed())
);
println!(
));
PBAR.one_off_message(&format!(
"{} Your WASM pkg is ready to publish at {}/pkg",
emoji::PACKAGE,
&crate_path
)
));
PBAR.done()?;
}
Command::Pack { path } => {
let crate_path = match path {
Expand Down
23 changes: 7 additions & 16 deletions src/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::fs::File;
use std::io::prelude::*;

use PBAR;
use console::style;
use emoji;
use failure::Error;
use indicatif::MultiProgress;
use progressbar;
use serde_json;
use toml;

Expand Down Expand Up @@ -82,40 +81,32 @@ pub fn write_package_json(path: &str, scope: Option<String>) -> Result<(), Error
emoji::MEMO
);

let warn = |field| {
let warn_fmt = |field| {
format!(
"{} {}: Field {} is missing from Cargo.toml. It is not necessary, but recommended",
emoji::WARN,
style("[WARN]").bold().dim(),
"Field {} is missing from Cargo.toml. It is not necessary, but recommended",
field
)
};

let m = MultiProgress::new();
let pb = m.add(progressbar::new(step));

let pb = PBAR.message(&step);
let pkg_file_path = format!("{}/pkg/package.json", path);
let mut pkg_file = File::create(pkg_file_path)?;
let crate_data = read_cargo_toml(path)?;
let npm_data = crate_data.into_npm(scope);

if npm_data.description.is_none() {
let warn_pb = m.add(progressbar::new(warn("description")));
warn_pb.finish();
PBAR.warn(&warn_fmt("description"));
}
if npm_data.repository.is_none() {
let warn_pb = m.add(progressbar::new(warn("repository")));
warn_pb.finish();
PBAR.warn(&warn_fmt("repository"));
}
if npm_data.license.is_none() {
let warn_pb = m.add(progressbar::new(warn("license")));
warn_pb.finish();
PBAR.warn(&warn_fmt("license"));
}

let npm_json = serde_json::to_string_pretty(&npm_data)?;
pkg_file.write_all(npm_json.as_bytes())?;
pb.finish();
m.join_and_clear()?;
Ok(())
}

Expand Down
74 changes: 62 additions & 12 deletions src/progressbar.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,63 @@
use indicatif::{ProgressBar, ProgressStyle};

pub fn new(msg: String) -> ProgressBar {
let pb = ProgressBar::new_spinner();
pb.enable_steady_tick(200);
pb.set_style(
ProgressStyle::default_spinner()
.tick_chars("/|\\- ")
.template("{spinner:.dim.bold} {wide_msg}"),
);
pb.set_message(&msg);
pb
use console::style;
use emoji;
use failure::Error;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};

pub struct ProgressOutput {
bar: MultiProgress,
}

impl ProgressOutput {
pub fn new() -> Self {
Self {
bar: MultiProgress::new(),
}
}

pub fn message(&self, message: &str) -> ProgressBar {
self.bar.add(Self::progressbar(message))
}

pub fn one_off_message(&self, message: &str) {
let bar = self.bar.add(Self::progressbar(message));
bar.finish();
}

pub fn warn(&self, message: &str) {
let warn = format!(
"{} {}: {}",
style("[WARN]").bold().dim(),
emoji::WARN,
message
);
let bar = self.bar.add(Self::progressbar(&warn));
bar.finish();
}

pub fn error(&self, message: &str) {
let err = format!(
"{} {}: {}",
emoji::ERROR,
style("[Error]").bold().dim(),
message
);
let bar = self.bar.add(Self::progressbar(&err));
bar.finish();
}

fn progressbar(msg: &str) -> ProgressBar {
let pb = ProgressBar::new_spinner();
pb.enable_steady_tick(200);
pb.set_style(
ProgressStyle::default_spinner()
.tick_chars("/|\\- ")
.template("{spinner:.dim.bold} {wide_msg}"),
);
pb.set_message(&msg);
pb
}

pub fn done(&self) -> Result<(), Error> {
self.bar.join_and_clear().map_err(|e| Error::from(e))
}
}
15 changes: 3 additions & 12 deletions src/readme.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
use console::style;
use failure::Error;
use indicatif::MultiProgress;
use std::fs;

use PBAR;
use emoji;
use progressbar;

pub fn copy_from_crate(path: &str) -> Result<(), Error> {
let m = MultiProgress::new();
let step = format!(
"{} {}Copying over your README...",
style("[5/7]").bold().dim(),
emoji::DANCERS
);
let pb = m.add(progressbar::new(step));
let pb = PBAR.message(&step);
let crate_readme_path = format!("{}/README.md", path);
let new_readme_path = format!("{}/pkg/README.md", path);
if let Err(_) = fs::copy(&crate_readme_path, &new_readme_path) {
let warn = format!(
"{} {}: origin crate has no README",
emoji::WARN,
style("[WARN]").bold().dim()
);
let warn_pb = m.add(progressbar::new(warn));
warn_pb.finish();
PBAR.warn("origin crate has no README");
};
pb.finish();
m.join_and_clear()?;
Ok(())
}

0 comments on commit aa629b3

Please sign in to comment.