From f12291400b0402e2fc380f4e15eccec2dd6bc087 Mon Sep 17 00:00:00 2001 From: Anthony Eid Date: Wed, 23 Oct 2024 08:55:39 -0400 Subject: [PATCH] Clean up download adapter from github function --- crates/dap/src/adapters.rs | 109 +++++++++++++++++++------------------ crates/util/src/fs.rs | 3 +- 2 files changed, 57 insertions(+), 55 deletions(-) diff --git a/crates/dap/src/adapters.rs b/crates/dap/src/adapters.rs index a4b64381e9c1c..564f9261e9dce 100644 --- a/crates/dap/src/adapters.rs +++ b/crates/dap/src/adapters.rs @@ -91,66 +91,69 @@ pub async fn download_adapter_from_github( let asset_name = format!("{}_{}.zip", &adapter_name, github_version.tag_name); let zip_path = adapter_path.join(&asset_name); - - if smol::fs::metadata(&zip_path).await.is_err() { - let mut response = http_client - .get(&github_version.url, Default::default(), true) - .await - .context("Error downloading release")?; - - let mut file = File::create(&zip_path).await?; - futures::io::copy(response.body_mut(), &mut file).await?; - - let old_files: HashSet<_> = - util::fs::collect_matching(&adapter_path.as_path(), |file_path| { - file_path != zip_path.as_path() - }) - .await - .into_iter() - .filter_map(|file_path| { - file_path - .file_name() - .and_then(|f| f.to_str()) - .map(|f| f.to_string()) - }) - .collect(); - - let _unzip_status = process::Command::new("unzip") - .current_dir(&adapter_path) - .arg(&zip_path) - .output() - .await? - .status; - - let file_name = util::fs::find_file_name_in_dir(&adapter_path.as_path(), |file_name| { - !file_name.ends_with(".zip") && !old_files.contains(file_name) - }) + fs.remove_file( + zip_path.as_path(), + fs::RemoveOptions { + recursive: true, + ignore_if_not_exists: true, + }, + ) + .await?; + + let mut response = http_client + .get(&github_version.url, Default::default(), true) .await - .ok_or_else(|| anyhow!("Unzipped directory not found")); + .context("Error downloading release")?; + + let mut file = File::create(&zip_path).await?; + futures::io::copy(response.body_mut(), &mut file).await?; + + let old_files: HashSet<_> = util::fs::collect_matching(&adapter_path.as_path(), |file_path| { + file_path != zip_path.as_path() + }) + .await + .into_iter() + .filter_map(|file_path| { + file_path + .file_name() + .and_then(|f| f.to_str()) + .map(|f| f.to_string()) + }) + .collect(); + + let _unzip_status = process::Command::new("unzip") + .current_dir(&adapter_path) + .arg(&zip_path) + .output() + .await? + .status; - let file_name = file_name?; - let downloaded_path = adapter_path - .join(format!("{}_{}", adapter_name, github_version.tag_name)) - .to_owned(); + let file_name = util::fs::find_file_name_in_dir(&adapter_path.as_path(), |file_name| { + !file_name.ends_with(".zip") && !old_files.contains(file_name) + }) + .await + .ok_or_else(|| anyhow!("Unzipped directory not found")); - fs.rename( - file_name.as_path(), - downloaded_path.as_path(), - Default::default(), - ) - .await?; + let file_name = file_name?; + let downloaded_path = adapter_path + .join(format!("{}_{}", adapter_name, github_version.tag_name)) + .to_owned(); - util::fs::remove_matching(&adapter_path, |entry| entry != version_dir).await; + fs.rename( + file_name.as_path(), + downloaded_path.as_path(), + Default::default(), + ) + .await?; - // if !unzip_status.success() { - // dbg!(unzip_status); - // Err(anyhow!("failed to unzip downloaded dap archive"))?; - // } + util::fs::remove_matching(&adapter_path, |entry| entry != version_dir).await; - return Ok(downloaded_path); - } + // if !unzip_status.success() { + // dbg!(unzip_status); + // Err(anyhow!("failed to unzip downloaded dap archive"))?; + // } - bail!("Install failed to download & counldn't preinstalled dap") + Ok(downloaded_path) } pub async fn fetch_latest_adapter_version_from_github( diff --git a/crates/util/src/fs.rs b/crates/util/src/fs.rs index 92ca4e60aa1ce..5fe9b055ab382 100644 --- a/crates/util/src/fs.rs +++ b/crates/util/src/fs.rs @@ -1,8 +1,7 @@ -use std::path::{Path, PathBuf}; - use crate::ResultExt; use async_fs as fs; use futures_lite::StreamExt; +use std::path::{Path, PathBuf}; /// Removes all files and directories matching the given predicate pub async fn remove_matching(dir: &Path, predicate: F)