Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add progress bar to the binary download #419

Merged
merged 5 commits into from
May 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CHANGELOG
### Added

- Support substring matcher for dyn filter, used when the query contains space. ([#411](https://github.com/liuchengxu/vim-clap/pull/411))
- Add progress bar support for the download feature of maple. ([#419](https://github.com/liuchengxu/vim-clap/pull/419))
- Add instructions for building the Rust binary via Docker in case of some users run into the libssl error when using the prebuilt binary, see more info in [INSTALL.md](./INSTALL.md).

### Fixed

Expand Down
75 changes: 73 additions & 2 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/maple_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ serde_json = "1.0"
lazy_static = "1.4.0"
fuzzy-matcher = "0.3.1"
crossbeam-channel = "0.4"
tokio = { version = "0.2.20", features = ["fs"] }
indicatif = "0.14.0"

icon = { path = "../icon" }
printer = { path = "../printer" }
Expand Down
90 changes: 63 additions & 27 deletions crates/maple_cli/src/cmd/check_release/download.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,8 @@
use super::{REPO, USER};
use anyhow::{anyhow, Result};
use anyhow::Result;
use std::fs::File;
use std::io::copy;
use std::path::{Path, PathBuf};

fn get_asset_name() -> Result<String> {
let asset_name = if cfg!(target_os = "macos") {
"maple-x86_64-apple-darwin"
} else if cfg!(target_os = "linux") {
"maple-x86_64-unknown-linux-gnu"
} else if cfg!(target_os = "windows") {
"maple-x86_64-pc-windows-msvc"
} else {
return Err(anyhow!("no-avaliable-prebuilt-binary for this platform"));
};
Ok(asset_name.into())
}

pub fn to_download_url(version: &str) -> Result<String> {
Ok(format!(
"https://github.com/{}/{}/releases/download/{}/{}",
USER,
REPO,
version,
get_asset_name()?
))
}

#[cfg(unix)]
fn set_executable_permission<P: AsRef<Path>>(path: P) -> Result<()> {
use std::os::unix::fs::PermissionsExt;
Expand All @@ -42,7 +18,7 @@ fn set_executable_permission<P: AsRef<Path>>(path: P) -> Result<()> {
///
/// - `version`: "v0.13"
pub fn download_prebuilt_binary_to_a_tempfile(version: &str) -> Result<PathBuf> {
let target = to_download_url(version)?;
let target = crate::cmd::check_release::github::download_url_for(version)?;

let mut response = reqwest::blocking::get(&target)?;

Expand All @@ -67,15 +43,75 @@ pub fn download_prebuilt_binary_to_a_tempfile(version: &str) -> Result<PathBuf>
Ok(temp_file)
}

pub(super) async fn download_prebuilt_binary_to_a_tempfile_async(version: &str) -> Result<PathBuf> {
use crate::cmd::check_release::github::{get_asset_name, retrieve_asset_size};
use indicatif::{ProgressBar, ProgressStyle};
use tokio::{fs, io::AsyncWriteExt};

let asset_name = get_asset_name()?;
let total_size = retrieve_asset_size(&asset_name, version)?;

let client = reqwest::Client::new();
let target = crate::cmd::check_release::github::download_url_for(version)?;
let request = client.get(&target);

let progress_bar = ProgressBar::new(total_size);
progress_bar.set_style(ProgressStyle::default_bar()
.template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})")
.progress_chars("#>-"));

let mut tmp_dir = std::env::temp_dir();
tmp_dir.push(format!("{}-{}", version, asset_name));

if tmp_dir.is_file() {
let metadata = std::fs::metadata(&tmp_dir)?;
if metadata.len() == total_size {
println!("{} has alreay been downloaded", tmp_dir.display());
return Ok(tmp_dir);
} else {
std::fs::remove_file(&tmp_dir)?;
}
}

let mut source = request.send().await?;

let file = tmp_dir.as_path();
let mut dest = fs::OpenOptions::new()
.create(true)
.append(true)
.open(&file)
.await?;

while let Some(chunk) = source.chunk().await? {
dest.write_all(&chunk).await?;
progress_bar.inc(chunk.len() as u64);
}

#[cfg(unix)]
set_executable_permission(&tmp_dir)?;

println!("Download of '{}' has been completed.", tmp_dir.display());

Ok(tmp_dir)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[ignore]
fn test_download_to_a_tempfile() {
let remote_release = crate::cmd::check_release::latest_remote_release().unwrap();
let remote_release = crate::cmd::check_release::github::latest_remote_release().unwrap();
let remote_tag = remote_release.tag_name;
download_prebuilt_binary_to_a_tempfile(&remote_tag).unwrap();
}

#[test]
#[ignore]
fn test_async_download_to_a_tempfile() {
let mut rt = tokio::runtime::Runtime::new().unwrap();
let file = rt.block_on(download_prebuilt_binary_to_a_tempfile_async("v0.14"));
println!("async downloaded file: {:?}", file);
}
}
97 changes: 97 additions & 0 deletions crates/maple_cli/src/cmd/check_release/github.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use super::{REPO, USER};
use anyhow::{anyhow, Result};
use curl::easy::{Easy, List};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct RemoteRelease {
pub tag_name: String,
}

fn retrieve_github_api(api_url: &str) -> Result<Vec<u8>> {
let mut dst = Vec::new();
let mut handle = Easy::new();
handle.url(api_url)?;
let mut headers = List::new();
headers.append(&format!("User-Agent: {}", USER))?;
headers.append("Accept: application/json")?;
handle.http_headers(headers)?;

{
let mut transfer = handle.transfer();
transfer.write_function(|data| {
dst.extend_from_slice(data);
Ok(data.len())
})?;

transfer.perform()?;
}

Ok(dst)
}

fn retrieve_latest_release() -> Result<Vec<u8>> {
retrieve_github_api(&format!(
"https://api.github.com/repos/{}/{}/releases/latest",
USER, REPO
))
}

pub(super) fn retrieve_asset_size(asset_name: &str, tag: &str) -> Result<u64> {
let data = retrieve_github_api(&format!(
"https://api.github.com/repos/{}/{}/releases/tags/{}",
USER, REPO, tag
))?;
let v: serde_json::Value = serde_json::from_slice(&data).unwrap();
if let serde_json::Value::Array(assets) = &v["assets"] {
for asset in assets {
if asset["name"] == asset_name {
return asset["size"].as_u64().ok_or(anyhow!("Couldn't as u64"));
}
}
}
Err(anyhow!("Couldn't retrieve size for {}:{}", asset_name, tag))
}

pub fn latest_remote_release() -> Result<RemoteRelease> {
let data = retrieve_latest_release()?;
let release: RemoteRelease = serde_json::from_slice(&data).unwrap();
Ok(release)
}

pub(super) fn get_asset_name() -> Result<String> {
let asset_name = if cfg!(target_os = "macos") {
"maple-x86_64-apple-darwin"
} else if cfg!(target_os = "linux") {
"maple-x86_64-unknown-linux-gnu"
} else if cfg!(target_os = "windows") {
"maple-x86_64-pc-windows-msvc"
} else {
return Err(anyhow!("no-avaliable-prebuilt-binary for this platform"));
};
Ok(asset_name.into())
}

pub(super) fn download_url_for(version: &str) -> Result<String> {
Ok(format!(
"https://github.com/{}/{}/releases/download/{}/{}",
USER,
REPO,
version,
get_asset_name()?
))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[ignore]
fn test_retrieve_asset_size() {
println!(
"{:?}",
retrieve_asset_size(&get_asset_name().unwrap(), "v0.14")
);
}
}
Loading