|
| 1 | +import { inject, injectable } from 'inversify'; |
| 2 | +import { Event, Emitter } from '@theia/core/lib/common/event'; |
| 3 | +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 | +import { Board, BoardsService, ExecutableService } from '../../common/protocol'; |
| 7 | +import { BoardsServiceProvider } from '../boards/boards-service-provider'; |
| 8 | +import { URI, Command, CommandRegistry, SketchContribution, TabBarToolbarRegistry } from './contribution'; |
| 9 | + |
| 10 | +@injectable() |
| 11 | +export class Debug extends SketchContribution { |
| 12 | + |
| 13 | + @inject(HostedPluginSupport) |
| 14 | + protected hostedPluginSupport: HostedPluginSupport; |
| 15 | + |
| 16 | + @inject(NotificationCenter) |
| 17 | + protected readonly notificationCenter: NotificationCenter; |
| 18 | + |
| 19 | + @inject(ExecutableService) |
| 20 | + protected readonly executableService: ExecutableService; |
| 21 | + |
| 22 | + @inject(BoardsService) |
| 23 | + protected readonly boardService: BoardsService; |
| 24 | + |
| 25 | + @inject(BoardsServiceProvider) |
| 26 | + protected readonly boardsServiceProvider: BoardsServiceProvider; |
| 27 | + |
| 28 | + /** |
| 29 | + * If `undefined`, debugging is enabled. Otherwise, the reason why it's disabled. |
| 30 | + */ |
| 31 | + protected _disabledMessages?: string = 'No board selected'; // Initial pessimism. |
| 32 | + protected disabledMessageDidChangeEmitter = new Emitter<string | undefined>(); |
| 33 | + protected onDisabledMessageDidChange = this.disabledMessageDidChangeEmitter.event; |
| 34 | + |
| 35 | + protected get disabledMessage(): string | undefined { |
| 36 | + return this._disabledMessages; |
| 37 | + } |
| 38 | + protected set disabledMessage(message: string | undefined) { |
| 39 | + this._disabledMessages = message; |
| 40 | + this.disabledMessageDidChangeEmitter.fire(this._disabledMessages); |
| 41 | + } |
| 42 | + |
| 43 | + protected readonly debugToolbarItem = { |
| 44 | + id: Debug.Commands.START_DEBUGGING.id, |
| 45 | + command: Debug.Commands.START_DEBUGGING.id, |
| 46 | + tooltip: `${this.disabledMessage ? `Debug - ${this.disabledMessage}` : 'Start Debugging'}`, |
| 47 | + priority: 3, |
| 48 | + onDidChange: this.onDisabledMessageDidChange as Event<void> |
| 49 | + }; |
| 50 | + |
| 51 | + onStart(): void { |
| 52 | + this.onDisabledMessageDidChange(() => this.debugToolbarItem.tooltip = `${this.disabledMessage ? `Debug - ${this.disabledMessage}` : 'Start Debugging'}`); |
| 53 | + const refreshState = async (board: Board | undefined = this.boardsServiceProvider.boardsConfig.selectedBoard) => { |
| 54 | + if (!board) { |
| 55 | + this.disabledMessage = 'No board selected'; |
| 56 | + return; |
| 57 | + } |
| 58 | + const fqbn = board.fqbn; |
| 59 | + if (!fqbn) { |
| 60 | + this.disabledMessage = `Platform is not installed for '${board.name}'`; |
| 61 | + return; |
| 62 | + } |
| 63 | + const details = await this.boardService.getBoardDetails({ fqbn }); |
| 64 | + if (!details) { |
| 65 | + this.disabledMessage = `Platform is not installed for '${board.name}'`; |
| 66 | + return; |
| 67 | + } |
| 68 | + const { debuggingSupported } = details; |
| 69 | + if (!debuggingSupported) { |
| 70 | + this.disabledMessage = `Debugging is not supported by '${board.name}'`; |
| 71 | + } else { |
| 72 | + this.disabledMessage = undefined; |
| 73 | + } |
| 74 | + } |
| 75 | + this.boardsServiceProvider.onBoardsConfigChanged(({ selectedBoard }) => refreshState(selectedBoard)); |
| 76 | + this.notificationCenter.onPlatformInstalled(() => refreshState()); |
| 77 | + this.notificationCenter.onPlatformUninstalled(() => refreshState()); |
| 78 | + refreshState(); |
| 79 | + } |
| 80 | + |
| 81 | + registerCommands(registry: CommandRegistry): void { |
| 82 | + registry.registerCommand(Debug.Commands.START_DEBUGGING, { |
| 83 | + execute: () => this.startDebug(), |
| 84 | + isVisible: widget => ArduinoToolbar.is(widget) && widget.side === 'left', |
| 85 | + isEnabled: () => !this.disabledMessage |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + registerToolbarItems(registry: TabBarToolbarRegistry): void { |
| 90 | + registry.registerItem(this.debugToolbarItem); |
| 91 | + } |
| 92 | + |
| 93 | + protected async startDebug(board: Board | undefined = this.boardsServiceProvider.boardsConfig.selectedBoard): Promise<void> { |
| 94 | + if (!board) { |
| 95 | + return; |
| 96 | + } |
| 97 | + const { name, fqbn } = board; |
| 98 | + if (!fqbn) { |
| 99 | + return; |
| 100 | + } |
| 101 | + await this.hostedPluginSupport.didStart; |
| 102 | + const [sketch, executables] = await Promise.all([ |
| 103 | + this.sketchServiceClient.currentSketch(), |
| 104 | + this.executableService.list() |
| 105 | + ]); |
| 106 | + if (!sketch) { |
| 107 | + return; |
| 108 | + } |
| 109 | + const [cliPath, sketchPath] = await Promise.all([ |
| 110 | + this.fileService.fsPath(new URI(executables.cliUri)), |
| 111 | + this.fileService.fsPath(new URI(sketch.uri)) |
| 112 | + ]) |
| 113 | + const config = { |
| 114 | + cliPath, |
| 115 | + board: { |
| 116 | + fqbn, |
| 117 | + name |
| 118 | + }, |
| 119 | + sketchPath |
| 120 | + }; |
| 121 | + return this.commandService.executeCommand('arduino.debug.start', config); |
| 122 | + } |
| 123 | + |
| 124 | +} |
| 125 | + |
| 126 | +export namespace Debug { |
| 127 | + export namespace Commands { |
| 128 | + export const START_DEBUGGING: Command = { |
| 129 | + id: 'arduino-start-debug', |
| 130 | + label: 'Start Debugging', |
| 131 | + category: 'Arduino' |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments