1
+ import { Emitter , Event } from '@theia/core/lib/common/event' ;
2
+ import { MenuModelRegistry } from '@theia/core/lib/common/menu/menu-model-registry' ;
3
+ import { nls } from '@theia/core/lib/common/nls' ;
1
4
import { inject , injectable } from '@theia/core/shared/inversify' ;
2
- import { Event , Emitter } from '@theia/core/lib/common/event' ;
3
5
import { HostedPluginSupport } from '@theia/plugin-ext/lib/hosted/browser/hosted-plugin' ;
4
- import { ArduinoToolbar } from '../toolbar/arduino-toolbar' ;
5
- import { NotificationCenter } from '../notification-center' ;
6
6
import {
7
7
Board ,
8
8
BoardIdentifier ,
9
9
BoardsService ,
10
10
ExecutableService ,
11
+ SketchRef ,
11
12
isBoardIdentifierChangeEvent ,
12
- Sketch ,
13
13
} from '../../common/protocol' ;
14
+ import { BoardsDataStore } from '../boards/boards-data-store' ;
14
15
import { BoardsServiceProvider } from '../boards/boards-service-provider' ;
16
+ import { ArduinoMenus } from '../menu/arduino-menus' ;
17
+ import { NotificationCenter } from '../notification-center' ;
18
+ import { CurrentSketch } from '../sketches-service-client-impl' ;
19
+ import { ArduinoToolbar } from '../toolbar/arduino-toolbar' ;
15
20
import {
16
- URI ,
17
21
Command ,
18
22
CommandRegistry ,
19
23
SketchContribution ,
20
24
TabBarToolbarRegistry ,
25
+ URI ,
21
26
} from './contribution' ;
22
- import { MenuModelRegistry , nls } from '@theia/core/lib/common' ;
23
- import { CurrentSketch } from '../sketches-service-client-impl' ;
24
- import { ArduinoMenus } from '../menu/arduino-menus' ;
25
27
26
28
const COMPILE_FOR_DEBUG_KEY = 'arduino-compile-for-debug' ;
27
29
30
+ interface StartDebugParams {
31
+ /**
32
+ * Absolute filesystem path to the Arduino CLI executable.
33
+ */
34
+ readonly cliPath : string ;
35
+ /**
36
+ * The the board to debug.
37
+ */
38
+ readonly board : Readonly < { fqbn : string ; name ?: string } > ;
39
+ /**
40
+ * Absolute filesystem path of the sketch to debug.
41
+ */
42
+ readonly sketchPath : string ;
43
+ /**
44
+ * Location where the `launch.json` will be created on the fly before starting every debug session.
45
+ * If not defined, it falls back to `sketchPath/.vscode/launch.json`.
46
+ */
47
+ readonly launchConfigPath ?: string ;
48
+ /**
49
+ * Absolute path to the `arduino-cli.yaml` file. If not specified, it falls back to `~/.arduinoIDE/arduino-cli.yaml`.
50
+ */
51
+ readonly cliConfigPath ?: string ;
52
+ /**
53
+ * Programmer for the debugging.
54
+ */
55
+ readonly programmer ?: string ;
56
+ }
57
+ type StartDebugResult = boolean ;
58
+
28
59
@injectable ( )
29
60
export class Debug extends SketchContribution {
30
61
@inject ( HostedPluginSupport )
31
62
private readonly hostedPluginSupport : HostedPluginSupport ;
32
-
33
63
@inject ( NotificationCenter )
34
64
private readonly notificationCenter : NotificationCenter ;
35
-
36
65
@inject ( ExecutableService )
37
66
private readonly executableService : ExecutableService ;
38
-
39
67
@inject ( BoardsService )
40
68
private readonly boardService : BoardsService ;
41
-
42
69
@inject ( BoardsServiceProvider )
43
70
private readonly boardsServiceProvider : BoardsServiceProvider ;
71
+ @inject ( BoardsDataStore )
72
+ private readonly boardsDataStore : BoardsDataStore ;
44
73
45
74
/**
46
75
* If `undefined`, debugging is enabled. Otherwise, the reason why it's disabled.
@@ -175,42 +204,18 @@ export class Debug extends SketchContribution {
175
204
private async startDebug (
176
205
board : BoardIdentifier | undefined = this . boardsServiceProvider . boardsConfig
177
206
. selectedBoard
178
- ) : Promise < void > {
179
- if ( ! board ) {
180
- return ;
181
- }
182
- const { name, fqbn } = board ;
183
- if ( ! fqbn ) {
184
- return ;
207
+ ) : Promise < StartDebugResult > {
208
+ const params = await this . createStartDebugParams ( board ) ;
209
+ if ( ! params ) {
210
+ return false ;
185
211
}
186
212
await this . hostedPluginSupport . didStart ;
187
- const [ sketch , executables ] = await Promise . all ( [
188
- this . sketchServiceClient . currentSketch ( ) ,
189
- this . executableService . list ( ) ,
190
- ] ) ;
191
- if ( ! CurrentSketch . isValid ( sketch ) ) {
192
- return ;
193
- }
194
- const ideTempFolderUri = await this . sketchesService . getIdeTempFolderUri (
195
- sketch
196
- ) ;
197
- const [ cliPath , sketchPath , configPath ] = await Promise . all ( [
198
- this . fileService . fsPath ( new URI ( executables . cliUri ) ) ,
199
- this . fileService . fsPath ( new URI ( sketch . uri ) ) ,
200
- this . fileService . fsPath ( new URI ( ideTempFolderUri ) ) ,
201
- ] ) ;
202
- const config = {
203
- cliPath,
204
- board : {
205
- fqbn,
206
- name,
207
- } ,
208
- sketchPath,
209
- configPath,
210
- } ;
211
213
try {
212
- await this . commandService . executeCommand ( 'arduino.debug.start' , config ) ;
214
+ const result = await this . debug ( params ) ;
215
+ return Boolean ( result ) ;
213
216
} catch ( err ) {
217
+ const sketchUri = await this . fileSystemExt . getUri ( params . sketchPath ) ;
218
+ const sketch = SketchRef . fromUri ( sketchUri ) ;
214
219
if ( await this . isSketchNotVerifiedError ( err , sketch ) ) {
215
220
const yes = nls . localize ( 'vscode/extensionsUtils/yes' , 'Yes' ) ;
216
221
const answer = await this . messageService . error (
@@ -230,6 +235,16 @@ export class Debug extends SketchContribution {
230
235
) ;
231
236
}
232
237
}
238
+ return false ;
239
+ }
240
+
241
+ private async debug (
242
+ params : StartDebugParams
243
+ ) : Promise < StartDebugResult | undefined > {
244
+ return this . commandService . executeCommand < StartDebugResult > (
245
+ 'arduino.debug.start' ,
246
+ params
247
+ ) ;
233
248
}
234
249
235
250
get compileForDebug ( ) : boolean {
@@ -246,7 +261,7 @@ export class Debug extends SketchContribution {
246
261
247
262
private async isSketchNotVerifiedError (
248
263
err : unknown ,
249
- sketch : Sketch
264
+ sketch : SketchRef
250
265
) : Promise < boolean > {
251
266
if ( err instanceof Error ) {
252
267
try {
@@ -260,6 +275,37 @@ export class Debug extends SketchContribution {
260
275
}
261
276
return false ;
262
277
}
278
+
279
+ private async createStartDebugParams (
280
+ board : BoardIdentifier | undefined
281
+ ) : Promise < StartDebugParams | undefined > {
282
+ if ( ! board || ! board . fqbn ) {
283
+ return undefined ;
284
+ }
285
+ const [ sketch , executables , boardsData ] = await Promise . all ( [
286
+ this . sketchServiceClient . currentSketch ( ) ,
287
+ this . executableService . list ( ) ,
288
+ this . boardsDataStore . getData ( board . fqbn ) ,
289
+ ] ) ;
290
+ if ( ! CurrentSketch . isValid ( sketch ) ) {
291
+ return ;
292
+ }
293
+ const ideTempFolderUri = await this . sketchesService . getIdeTempFolderUri (
294
+ sketch
295
+ ) ;
296
+ const [ cliPath , sketchPath , launchConfigPath ] = await Promise . all ( [
297
+ this . fileService . fsPath ( new URI ( executables . cliUri ) ) ,
298
+ this . fileService . fsPath ( new URI ( sketch . uri ) ) ,
299
+ this . fileService . fsPath ( new URI ( ideTempFolderUri ) ) ,
300
+ ] ) ;
301
+ return {
302
+ board : { fqbn : board . fqbn , name : board . name } ,
303
+ cliPath,
304
+ sketchPath,
305
+ launchConfigPath,
306
+ programmer : boardsData . selectedProgrammer ?. id ,
307
+ } ;
308
+ }
263
309
}
264
310
export namespace Debug {
265
311
export namespace Commands {
0 commit comments