From 8674f1013f3fe71bc6534e6e2f44324a678f5b96 Mon Sep 17 00:00:00 2001 From: Anthony Eid Date: Sun, 20 Oct 2024 03:47:18 -0400 Subject: [PATCH] Get debugpy version to work --- Cargo.lock | 1 + crates/dap_adapters/Cargo.toml | 1 + crates/dap_adapters/src/python.rs | 65 ++++++++++++++----------------- crates/util/src/fs.rs | 23 ++++++++++- 4 files changed, 53 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9973b2ece813d..6c917f9df1e47 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3388,6 +3388,7 @@ dependencies = [ "serde_json", "smol", "task", + "util", ] [[package]] diff --git a/crates/dap_adapters/Cargo.toml b/crates/dap_adapters/Cargo.toml index d6515c69b8a41..44f516763c484 100644 --- a/crates/dap_adapters/Cargo.toml +++ b/crates/dap_adapters/Cargo.toml @@ -21,6 +21,7 @@ fs.workspace = true futures.workspace = true gpui.workspace = true http_client.workspace = true +util.workspace = true task.workspace = true smol.workspace = true serde.workspace = true diff --git a/crates/dap_adapters/src/python.rs b/crates/dap_adapters/src/python.rs index 12b16280f0de0..9a4b9ddf76f7e 100644 --- a/crates/dap_adapters/src/python.rs +++ b/crates/dap_adapters/src/python.rs @@ -33,9 +33,15 @@ impl DebugAdapter for PythonDebugAdapter { ) -> Result { let adapter_path = paths::debug_adapters_dir().join(self.name()); + let debugpy_dir = util::fs::find_file_name_in_dir(adapter_path.as_path(), |file_name| { + file_name.starts_with("debugpy_") + }) + .await + .ok_or_else(|| anyhow!("Debugpy directory not found"))?; + Ok(DebugAdapterBinary { command: "python3".to_string(), - arguments: Some(vec![adapter_path.join(Self::ADAPTER_PATH).into()]), + arguments: Some(vec![debugpy_dir.join(Self::ADAPTER_PATH).into()]), envs: None, }) } @@ -58,7 +64,7 @@ impl DebugAdapter for PythonDebugAdapter { let release = latest_github_release("microsoft/debugpy", false, false, http_client.clone()) .await?; - let asset_name = format!("{}.zip", release.tag_name); + let asset_name = format!("{}_{}.zip", self.name(), release.tag_name); let zip_path = debugpy_dir.join(asset_name); @@ -78,42 +84,29 @@ impl DebugAdapter for PythonDebugAdapter { .await? .status; - let mut ls = process::Command::new("ls") - .current_dir(&debugpy_dir) - .stdout(Stdio::piped()) - .spawn()?; - - let std = ls - .stdout - .take() - .ok_or(anyhow!("Failed to list directories"))? - .into_stdio() - .await?; - - let file_name = String::from_utf8( - process::Command::new("grep") - .arg("microsoft-debugpy") - .stdin(std) - .output() - .await? - .stdout, - )?; - - let file_name = file_name.trim_end(); - process::Command::new("sh") - .current_dir(&debugpy_dir) - .arg("-c") - .arg(format!("mv {file_name}/* .")) - .output() + let file_name = + util::fs::find_file_name_in_dir(&debugpy_dir.as_path(), |file_name| { + file_name.starts_with("microsoft-debugpy-") + }) + .await + .ok_or_else(|| anyhow!("Debugpy unzipped directory not found"))?; + + fs.rename( + file_name.as_path(), + debugpy_dir + .join(format!("{}_{}", self.name(), release.tag_name)) + .as_path(), + Default::default(), + ) + .await?; + + fs.remove_file(&zip_path.as_path(), Default::default()) .await?; - process::Command::new("rm") - .current_dir(&debugpy_dir) - .arg("-rf") - .arg(file_name) - .arg(zip_path) - .output() - .await?; + // if !unzip_status.success() { + // dbg!(unzip_status); + // Err(anyhow!("failed to unzip debugpy archive"))?; + // } return Ok(()); } diff --git a/crates/util/src/fs.rs b/crates/util/src/fs.rs index f235753e8b2af..93340729df2b4 100644 --- a/crates/util/src/fs.rs +++ b/crates/util/src/fs.rs @@ -1,4 +1,4 @@ -use std::path::Path; +use std::path::{Path, PathBuf}; use crate::ResultExt; use async_fs as fs; @@ -26,3 +26,24 @@ where } } } + +pub async fn find_file_name_in_dir(dir: &Path, predicate: F) -> Option +where + F: Fn(&str) -> bool, +{ + if let Some(mut entries) = fs::read_dir(dir).await.log_err() { + while let Some(entry) = entries.next().await { + if let Some(entry) = entry.log_err() { + let entry_path = entry.path(); + + if let Some(file_name) = entry_path.file_name() { + if predicate(file_name.to_str().unwrap_or("")) { + return Some(entry_path); + } + } + } + } + } + + None +}