Skip to content

Commit

Permalink
Implementing a download progress bar for dune upgrade (#236)
Browse files Browse the repository at this point in the history
* Adding progress-bar when downloading the next version

* Getting ready for v0.9.1
  • Loading branch information
aalykiot authored Nov 2, 2024
1 parent 030cede commit 6159a28
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 6 deletions.
47 changes: 46 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,6 +1,6 @@
[package]
name = "dune"
version = "0.9.0"
version = "0.9.1"
authors = ["Alex Alikiotis <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down Expand Up @@ -63,6 +63,7 @@ axum = { version = "0.7.7", features = ["ws"] }
uuid = { version = "1.11.0", features = ["v4", "fast-rng"] }
base64 = "0.22.1"
swc_ecma_transforms = "3.0.0"
indicatif = "0.17.8"

[target.'cfg(unix)'.dependencies]
nix = { version = "0.29.0", features = ["signal"] }
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dune",
"version": "0.9.0",
"version": "0.9.1",
"description": "A hobby runtime for JavaScript and TypeScript 🚀",
"homepage": "https://github.com/aalykiot/dune#readme",
"keywords": [],
Expand Down
38 changes: 35 additions & 3 deletions src/tools/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anyhow::Result;
use indicatif::ProgressBar;
use indicatif::ProgressStyle;
use std::fs;
use std::io::Read;
use std::process::Command;
Expand Down Expand Up @@ -33,14 +35,44 @@ pub fn run_upgrade() -> Result<()> {
// Download the new binary.
let response = ureq::get(&download_url).call()?;

let mut binary = vec![];
response.into_reader().read_to_end(&mut binary)?;
let total_bytes = response
.header("content-length")
.and_then(|len| len.parse::<u64>().ok())
.unwrap_or(0);

// Create a buffer and read in chunks.
let mut reader = response.into_reader();
let mut buffer = [0; 8192];
let mut downloaded_data = vec![];
let mut downloaded: u64 = 0;

// Display a progress bar when downloading for better UX.
let pb = ProgressBar::new(total_bytes);
let pb_template =
"{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes}";

pb.set_style(
ProgressStyle::with_template(pb_template)
.unwrap()
.progress_chars("#>-"),
);

while let Ok(bytes_read) = reader.read(&mut buffer) {
if bytes_read == 0 {
break;
}
downloaded_data.extend_from_slice(&buffer);
downloaded += bytes_read as u64;
pb.set_position(downloaded);
}

pb.finish_and_clear();

// Get handles to temp and home directories.
let temp_dir = TempDir::new("dune_zip_binary")?;

// Write binary to disk.
fs::write(temp_dir.path().join(&archive), binary)?;
fs::write(temp_dir.path().join(&archive), downloaded_data)?;

let exe_name = "dune";
let exe_extension = if cfg!(windows) { "exe" } else { "" };
Expand Down

0 comments on commit 6159a28

Please sign in to comment.