1- import { existsSync , mkdirSync } from "node:fs" ;
2- import { join } from "node:path" ;
1+ import { existsSync } from "node:fs" ;
32import { z } from "zod" ;
43import { defineCommand } from "../cli/command.ts" ;
4+ import { getAdapterInstaller , listAdapterNames } from "../dap/adapters/index.ts" ;
55import { getManagedAdaptersDir } from "../dap/session.ts" ;
66
7- const LLVM_VERSION = "19.1.7" ;
8-
9- function getPlatformArch ( ) : { os : string ; arch : string } {
10- const os = process . platform ; // "darwin", "linux", "win32"
11- const arch = process . arch ; // "arm64", "x64"
12- return { os, arch } ;
13- }
14-
15- function getLlvmDownloadUrl ( version : string , os : string , arch : string ) : string | null {
16- if ( os === "darwin" ) {
17- if ( arch === "arm64" ) {
18- return `https://github.com/llvm/llvm-project/releases/download/llvmorg-${ version } /LLVM-${ version } -macOS-ARM64.tar.xz` ;
19- }
20- if ( arch === "x64" ) {
21- return `https://github.com/llvm/llvm-project/releases/download/llvmorg-${ version } /LLVM-${ version } -macOS-X64.tar.xz` ;
22- }
23- }
24- if ( os === "linux" ) {
25- if ( arch === "x64" ) {
26- return `https://github.com/llvm/llvm-project/releases/download/llvmorg-${ version } /LLVM-${ version } -Linux-X64.tar.xz` ;
27- }
28- if ( arch === "arm64" ) {
29- return `https://github.com/llvm/llvm-project/releases/download/llvmorg-${ version } /LLVM-${ version } -Linux-AArch64.tar.xz` ;
30- }
31- }
32- return null ;
33- }
34-
357defineCommand ( {
368 name : "install" ,
379 description : "Download managed adapter binary" ,
@@ -44,6 +16,7 @@ defineCommand({
4416 } ) ,
4517 handler : async ( ctx ) => {
4618 const adapter = ctx . positional || undefined ;
19+ const supported = listAdapterNames ( ) ;
4720
4821 if ( ctx . flags . list ) {
4922 const dir = getManagedAdaptersDir ( ) ;
@@ -65,124 +38,32 @@ defineCommand({
6538
6639 if ( ! adapter ) {
6740 console . error ( "Usage: dbg install <adapter>" ) ;
68- console . error ( " Supported adapters: lldb" ) ;
41+ console . error ( ` Supported adapters: ${ supported . join ( ", " ) } ` ) ;
6942 console . error ( " Options: --list (show installed adapters)" ) ;
7043 return 1 ;
7144 }
7245
73- if ( adapter !== "lldb" ) {
46+ const installer = getAdapterInstaller ( adapter ) ;
47+ if ( ! installer ) {
7448 console . error ( `Unknown adapter: ${ adapter } ` ) ;
75- console . error ( " Supported adapters: lldb" ) ;
49+ console . error ( ` Supported adapters: ${ supported . join ( ", " ) } ` ) ;
7650 return 1 ;
7751 }
7852
79- const { os, arch } = getPlatformArch ( ) ;
80- const url = getLlvmDownloadUrl ( LLVM_VERSION , os , arch ) ;
81-
82- if ( ! url ) {
83- console . error ( `Unsupported platform: ${ os } -${ arch } ` ) ;
84- console . error ( " Supported: darwin-arm64, darwin-x64, linux-x64, linux-arm64" ) ;
85- return 1 ;
86- }
87-
88- const adaptersDir = getManagedAdaptersDir ( ) ;
89- const targetPath = join ( adaptersDir , "lldb-dap" ) ;
90-
91- if ( existsSync ( targetPath ) ) {
92- console . log ( `lldb-dap already installed at ${ targetPath } ` ) ;
93- console . log ( ` To reinstall, remove it first: rm ${ targetPath } ` ) ;
53+ if ( installer . isInstalled ( ) ) {
54+ console . log ( `${ installer . name } is already installed.` ) ;
55+ console . log ( " To reinstall, remove ~/.debug-that/adapters/ entry first." ) ;
9456 return 0 ;
9557 }
9658
97- console . log ( `Downloading LLVM ${ LLVM_VERSION } for ${ os } -${ arch } ...` ) ;
98- console . log ( ` From: ${ url } ` ) ;
99-
100- // Download the tarball
101- const response = await fetch ( url , { redirect : "follow" } ) ;
102- if ( ! response . ok ) {
103- console . error ( `Download failed: HTTP ${ response . status } ` ) ;
104- console . error ( " -> Check your internet connection or try again later" ) ;
105- return 1 ;
106- }
107-
108- const tarball = await response . arrayBuffer ( ) ;
109- console . log ( `Downloaded ${ ( tarball . byteLength / 1024 / 1024 ) . toFixed ( 1 ) } MB` ) ;
110-
111- // Extract lldb-dap from the tarball using tar
112- mkdirSync ( adaptersDir , { recursive : true } ) ;
113-
114- const tmpTar = join ( adaptersDir , "llvm-download.tar.xz" ) ;
115- await Bun . write ( tmpTar , tarball ) ;
116-
117- // Find lldb-dap inside the archive and extract just that binary
118- console . log ( "Extracting lldb-dap..." ) ;
119- const listResult = Bun . spawnSync ( [ "tar" , "-tf" , tmpTar ] , {
120- stdout : "pipe" ,
121- } ) ;
122- const files = listResult . stdout . toString ( ) . split ( "\n" ) ;
123- const lldbDapEntry = files . find ( ( f ) => f . endsWith ( "/bin/lldb-dap" ) || f === "bin/lldb-dap" ) ;
124-
125- if ( ! lldbDapEntry ) {
126- Bun . spawnSync ( [ "rm" , tmpTar ] ) ;
127- console . error ( "Could not find lldb-dap in the LLVM archive" ) ;
128- console . error ( ` Archive entries searched: ${ files . length } ` ) ;
129- console . error ( " -> Try installing manually: brew install llvm" ) ;
130- return 1 ;
131- }
132-
133- // Extract just the lldb-dap binary
134- const extractResult = Bun . spawnSync (
135- [
136- "tar" ,
137- "-xf" ,
138- tmpTar ,
139- "-C" ,
140- adaptersDir ,
141- "--strip-components" ,
142- String ( lldbDapEntry . split ( "/" ) . length - 1 ) ,
143- lldbDapEntry ,
144- ] ,
145- { stdout : "pipe" , stderr : "pipe" } ,
146- ) ;
147-
148- // Clean up tarball
149- Bun . spawnSync ( [ "rm" , tmpTar ] ) ;
150-
151- if ( extractResult . exitCode !== 0 ) {
152- console . error ( `Extraction failed: ${ extractResult . stderr . toString ( ) } ` ) ;
153- return 1 ;
154- }
155-
156- // Also extract liblldb if present (needed on some platforms)
157- const liblldbEntries = files . filter (
158- ( f ) => f . includes ( "liblldb" ) && ( f . endsWith ( ".so" ) || f . endsWith ( ".dylib" ) ) ,
159- ) ;
160- for ( const libEntry of liblldbEntries ) {
161- Bun . spawnSync (
162- [
163- "tar" ,
164- "-xf" ,
165- tmpTar ,
166- "-C" ,
167- adaptersDir ,
168- "--strip-components" ,
169- String ( libEntry . split ( "/" ) . length - 1 ) ,
170- libEntry ,
171- ] ,
172- { stdout : "pipe" , stderr : "pipe" } ,
173- ) ;
174- }
175-
176- // Make executable
177- Bun . spawnSync ( [ "chmod" , "+x" , targetPath ] ) ;
178-
179- if ( existsSync ( targetPath ) ) {
180- console . log ( `Installed lldb-dap to ${ targetPath } ` ) ;
59+ try {
60+ console . log ( `Installing ${ installer . name } ...` ) ;
61+ await installer . install ( ( msg ) => console . log ( msg ) ) ;
62+ console . log ( `${ installer . name } installed successfully.` ) ;
18163 return 0 ;
64+ } catch ( e ) {
65+ console . error ( `Installation failed: ${ ( e as Error ) . message } ` ) ;
66+ return 1 ;
18267 }
183-
184- console . error ( "Installation failed — lldb-dap not found after extraction" ) ;
185- console . error ( " -> Try installing manually: brew install llvm" ) ;
186- return 1 ;
18768 } ,
18869} ) ;
0 commit comments