Skip to content

Commit

Permalink
Clean up download adapter from github function
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony-Eid committed Oct 23, 2024
1 parent 2edf771 commit f122914
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 55 deletions.
109 changes: 56 additions & 53 deletions crates/dap/src/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions crates/util/src/fs.rs
Original file line number Diff line number Diff line change
@@ -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<F>(dir: &Path, predicate: F)
Expand Down

0 comments on commit f122914

Please sign in to comment.