Skip to content

Commit

Permalink
Add release tag to name of downloaded DAPs
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony-Eid committed Oct 23, 2024
1 parent f5c186a commit fb3375c
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 252 deletions.
44 changes: 15 additions & 29 deletions crates/dap/src/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ use http_client::{github::latest_github_release, HttpClient};
use node_runtime::NodeRuntime;
use serde_json::Value;
use smol::{self, fs::File, process};
use std::{collections::HashMap, ffi::OsString, fmt::Debug, path::Path, sync::Arc};
use std::{
collections::HashMap,
ffi::OsString,
fmt::Debug,
path::{Path, PathBuf},
sync::Arc,
};
use task::DebugAdapterConfig;

pub trait DapDelegate {
Expand Down Expand Up @@ -36,23 +42,14 @@ pub struct DebugAdapterBinary {
pub envs: Option<HashMap<String, String>>,
}

pub enum DebugAdapterDownloadKind {
Github(GithubRepo),
Custom,
}

async fn download_adapter_from_github(
pub async fn download_adapter_from_github(
adapter_name: DebugAdapterName,
github_repo: GithubRepo,
delegate: &dyn DapDelegate,
) -> Result<()> {
) -> Result<PathBuf> {
let adapter_path = paths::debug_adapters_dir().join(&adapter_name);
let fs = delegate.fs();

if fs.is_dir(adapter_path.as_path()).await {
return Ok(());
}

if let Some(http_client) = delegate.http_client() {
if !adapter_path.exists() {
fs.create_dir(&adapter_path.as_path()).await?;
Expand Down Expand Up @@ -91,12 +88,13 @@ async fn download_adapter_from_github(
.ok_or_else(|| anyhow!("Unzipped directory not found"));

let file_name = file_name?;
let downloaded_path = adapter_path
.join(format!("{}_{}", adapter_name, release.tag_name))
.to_owned();

fs.rename(
file_name.as_path(),
adapter_path
.join(format!("{}_{}", adapter_name, release.tag_name))
.as_path(),
downloaded_path.as_path(),
Default::default(),
)
.await?;
Expand All @@ -106,7 +104,7 @@ async fn download_adapter_from_github(
// Err(anyhow!("failed to unzip downloaded dap archive"))?;
// }

return Ok(());
return Ok(downloaded_path);
}
}

Expand All @@ -124,26 +122,14 @@ pub trait DebugAdapter: 'static + Send + Sync {
"".to_string()
}

fn download_kind(&self) -> DebugAdapterDownloadKind;

fn name(&self) -> DebugAdapterName;

fn transport(&self) -> Box<dyn Transport>;

/// Installs the binary for the debug adapter.
/// This method is called when the adapter binary is not found or needs to be updated.
/// It should download and install the necessary files for the debug adapter to function.
async fn install_binary(&self, delegate: &dyn DapDelegate) -> Result<()> {
let adapter_name = self.name();
let download_kind = self.download_kind();

match download_kind {
DebugAdapterDownloadKind::Github(github_repo) => {
download_adapter_from_github(adapter_name, github_repo, delegate).await
}
DebugAdapterDownloadKind::Custom => Ok(()),
}
}
async fn install_binary(&self, delegate: &dyn DapDelegate) -> Result<()>;

async fn fetch_binary(
&self,
Expand Down
4 changes: 0 additions & 4 deletions crates/dap_adapters/src/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ impl DebugAdapter for CustomDebugAdapter {
DebugAdapterName(Self::ADAPTER_NAME.into())
}

fn download_kind(&self) -> DebugAdapterDownloadKind {
DebugAdapterDownloadKind::Custom
}

fn transport(&self) -> Box<dyn Transport> {
match &self.custom_args.connection {
DebugConnectionType::STDIO => Box::new(StdioTransport::new()),
Expand Down
12 changes: 3 additions & 9 deletions crates/dap_adapters/src/dap_adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,18 @@ mod lldb;
mod php;
mod python;

use anyhow::{anyhow, bail, Context, Result};
use anyhow::{anyhow, bail, Result};
use async_trait::async_trait;
use custom::CustomDebugAdapter;
use dap::adapters::{
DapDelegate, DebugAdapter, DebugAdapterBinary, DebugAdapterDownloadKind, DebugAdapterName,
GithubRepo,
self, DapDelegate, DebugAdapter, DebugAdapterBinary, DebugAdapterName, GithubRepo,
};
use http_client::github::latest_github_release;
use javascript::JsDebugAdapter;
use lldb::LldbDebugAdapter;
use php::PhpDebugAdapter;
use python::PythonDebugAdapter;
use serde_json::{json, Value};
use smol::{
fs::{self, File},
process,
};
use std::{fmt::Debug, process::Stdio};
use std::fmt::Debug;
use task::{CustomArgs, DebugAdapterConfig, DebugAdapterKind, DebugConnectionType, TCPHost};

pub fn build_adapter(adapter_config: &DebugAdapterConfig) -> Result<Box<dyn DebugAdapter>> {
Expand Down
123 changes: 24 additions & 99 deletions crates/dap_adapters/src/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ impl DebugAdapter for JsDebugAdapter {
DebugAdapterName(Self::ADAPTER_NAME.into())
}

fn download_kind(&self) -> DebugAdapterDownloadKind {
DebugAdapterDownloadKind::Github(GithubRepo {
repo_name: "vscode-js-debug".into(),
repo_owner: "microsoft".into(),
})
}

fn transport(&self) -> Box<dyn Transport> {
Box::new(TcpTransport::new(TCPHost {
port: Some(8133),
Expand All @@ -45,6 +38,11 @@ impl DebugAdapter for JsDebugAdapter {
.ok_or(anyhow!("Couldn't get npm runtime"))?;

let adapter_path = paths::debug_adapters_dir().join(self.name());
let adapter_path = util::fs::find_file_name_in_dir(adapter_path.as_path(), |file_name| {
file_name.starts_with("vscode-js-debug_")
})
.await
.ok_or_else(|| anyhow!("Couldn't find javascript dap directory"))?;

Ok(DebugAdapterBinary {
command: node_runtime
Expand All @@ -61,102 +59,29 @@ impl DebugAdapter for JsDebugAdapter {
}

async fn install_binary(&self, delegate: &dyn DapDelegate) -> Result<()> {
let adapter_path = paths::debug_adapters_dir().join(self.name());
let fs = delegate.fs();

if fs.is_dir(adapter_path.as_path()).await {
return Ok(());
}

if let Some(http_client) = delegate.http_client() {
if !adapter_path.exists() {
fs.create_dir(&adapter_path.as_path()).await?;
}

let release = latest_github_release(
"microsoft/vscode-js-debug",
false,
false,
http_client.clone(),
)
.await?;

let asset_name = format!("{}-{}", self.name(), release.tag_name);
let zip_path = adapter_path.join(asset_name);

if fs::metadata(&zip_path).await.is_err() {
let mut response = http_client
.get(&release.zipball_url, Default::default(), true)
.await
.context("Error downloading release")?;
let github_repo = GithubRepo {
repo_name: "vscode-js-debug".to_string(),
repo_owner: "microsoft".to_string(),
};

let mut file = File::create(&zip_path).await?;
futures::io::copy(response.body_mut(), &mut file).await?;
let adapter_path =
adapters::download_adapter_from_github(self.name(), github_repo, delegate).await?;

let _unzip_status = process::Command::new("unzip")
.current_dir(&adapter_path)
.arg(&zip_path)
.output()
.await?
.status;

let mut ls = process::Command::new("ls")
.current_dir(&adapter_path)
.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-vscode-js-debug")
.stdin(std)
.output()
.await?
.stdout,
)?;

let file_name = file_name.trim_end();

process::Command::new("sh")
.current_dir(&adapter_path)
.arg("-c")
.arg(format!("mv {file_name}/* ."))
.output()
.await?;

process::Command::new("rm")
.current_dir(&adapter_path)
.arg("-rf")
.arg(file_name)
.arg(zip_path)
.output()
.await?;

let _ = delegate
.node_runtime()
.ok_or(anyhow!("Couldn't get npm runtime"))?
.run_npm_subcommand(&adapter_path, "install", &[])
.await
.ok();

let _ = delegate
.node_runtime()
.ok_or(anyhow!("Couldn't get npm runtime"))?
.run_npm_subcommand(&adapter_path, "run", &["compile"])
.await
.ok();
let _ = delegate
.node_runtime()
.ok_or(anyhow!("Couldn't get npm runtime"))?
.run_npm_subcommand(&adapter_path, "install", &[])
.await
.ok();

return Ok(());
}
}
let _ = delegate
.node_runtime()
.ok_or(anyhow!("Couldn't get npm runtime"))?
.run_npm_subcommand(&adapter_path, "run", &["compile"])
.await
.ok();

bail!("Install or fetch not implemented for Javascript debug adapter (yet)");
return Ok(());
}

fn request_args(&self, config: &DebugAdapterConfig) -> Value {
Expand Down
11 changes: 4 additions & 7 deletions crates/dap_adapters/src/lldb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@ impl DebugAdapter for LldbDebugAdapter {
DebugAdapterName(Self::ADAPTER_NAME.into())
}

fn download_kind(&self) -> DebugAdapterDownloadKind {
DebugAdapterDownloadKind::Github(GithubRepo {
repo_name: "llvm-project".to_string(),
repo_owner: "llvm".to_string(),
})
}

fn transport(&self) -> Box<dyn Transport> {
Box::new(StdioTransport::new())
}

async fn install_binary(&self, _delegate: &dyn DapDelegate) -> Result<()> {
bail!("Install binary is not support for install_binary (yet)")
}

async fn fetch_binary(
&self,
_: &dyn DapDelegate,
Expand Down
Loading

0 comments on commit fb3375c

Please sign in to comment.