Skip to content

Commit

Permalink
Revert to older download method and version bump -> 0.1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
harshdoesdev committed Oct 6, 2023
1 parent eefeb73 commit d2da8c1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "vercel-cache-helper"
version = "0.1.5"
version = "0.1.6"
edition = "2021"
description = "Hello world"

Expand Down
48 changes: 12 additions & 36 deletions src/commands/download.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use indicatif::{ProgressBar, ProgressStyle};
use std::io::{Seek, Write};

pub async fn download(
Expand All @@ -11,79 +10,56 @@ pub async fn download(
std::env::current_dir()?
};
let cache_dir = if let Some(cache_dir) = vercel_cache_helper::utils::get_cache_dir() {
println!("Cache dir found: '{:?}'", cache_dir);
println!("Cache dir found: {:?}", cache_dir);
cache_dir
} else {
println!("Cache dir not found.");
println!("Cache dir not found");
return Ok(());
};
let output_dir = tempfile::tempdir()?;

let pb = ProgressBar::new_spinner();
pb.enable_steady_tick(std::time::Duration::new(0, 500));
pb.set_style(
ProgressStyle::default_spinner()
.template("[{spinner}] {prefix} {wide_msg}")
.unwrap()
.tick_chars("/|\\- "),
);
pb.set_message("Looking for artifacts...");
println!("Looking for artifacts...");

let mut output_exists_req = remote_client.exists(
vercel_cache_helper::vercel::constants::FASTN_VERCEL_REMOTE_CACHE_HASH.to_string(),
None,
)?;
let output_artifact_exists = output_exists_req.send().await?;

if output_artifact_exists {
pb.finish_with_message("Remote artifact found");
println!("Build artifacts found");
} else {
pb.finish_with_message("No remote artifact found");
println!("Build artifacts not found");
return Ok(());
}

pb.reset();
pb.set_message("Downloading artifacts");
println!("Downloading build artifacts");

let mut output_dir_archive = tempfile::tempfile()?;
let mut output_get_req = remote_client.get(
vercel_cache_helper::vercel::constants::FASTN_VERCEL_REMOTE_CACHE_HASH.to_string(),
None,
)?;

let mut output_get_res = output_get_req.get().await?;
let download_size = output_get_res.content_length().unwrap_or(0);

let mut downloaded_bytes: u64 = 0;

while let Some(chunk) = output_get_res.chunk().await? {
output_dir_archive.write_all(&chunk)?;
let output_get_res = output_get_req.get().await?;

downloaded_bytes += chunk.len() as u64;
println!("build artifacts downloaded");

if download_size > 0 {
pb.set_position(downloaded_bytes);
pb.set_length(download_size);
}
}

pb.finish_with_message("Remote artifacts downloaded");
output_dir_archive.write_all(&output_get_res.bytes().await?.to_vec())?;

output_dir_archive
.seek(std::io::SeekFrom::Start(0))
.unwrap();

vercel_cache_helper::utils::extract_tar_zst(
output_dir_archive,
&output_dir.path().to_path_buf(),
)?;
vercel_cache_helper::utils::extract_tar_zst(output_dir_archive, &output_dir.path().to_path_buf())?;

let temp_build_dir = output_dir.path().join(".build");
let temp_cache_dir = output_dir.path().join("cache");

vercel_cache_helper::utils::copy_recursively(temp_build_dir, project_dir.join(".build"))?;
vercel_cache_helper::utils::copy_recursively(temp_cache_dir, cache_dir)?;

println!("Cached artifacts downloaded and copied successfully.");
println!("Remote cache retrieval completed successfully.");

Ok(())
}

0 comments on commit d2da8c1

Please sign in to comment.