|
| 1 | +import { inject, injectable } from 'inversify'; |
| 2 | +import { CoreService } from '../../common/protocol'; |
| 3 | +import { MonitorConnection } from '../monitor/monitor-connection'; |
| 4 | +import { BoardsConfigStore } from '../boards/boards-config-store'; |
| 5 | +import { BoardsServiceClientImpl } from '../boards/boards-service-client-impl'; |
| 6 | +import { ArduinoMenus } from '../menu/arduino-menus'; |
| 7 | +import { ArduinoToolbar } from '../toolbar/arduino-toolbar'; |
| 8 | +import { SketchContribution, Command, CommandRegistry, MenuModelRegistry, KeybindingRegistry, TabBarToolbarRegistry } from './contribution'; |
| 9 | + |
| 10 | +@injectable() |
| 11 | +export class UploadSketch extends SketchContribution { |
| 12 | + |
| 13 | + @inject(CoreService) |
| 14 | + protected readonly coreService: CoreService; |
| 15 | + |
| 16 | + @inject(MonitorConnection) |
| 17 | + protected readonly monitorConnection: MonitorConnection; |
| 18 | + |
| 19 | + @inject(BoardsConfigStore) |
| 20 | + protected readonly boardsConfigStore: BoardsConfigStore; |
| 21 | + |
| 22 | + @inject(BoardsServiceClientImpl) |
| 23 | + protected readonly boardsServiceClientImpl: BoardsServiceClientImpl; |
| 24 | + |
| 25 | + registerCommands(registry: CommandRegistry): void { |
| 26 | + registry.registerCommand(UploadSketch.Commands.UPLOAD_SKETCH, { |
| 27 | + execute: () => this.uploadSketch() |
| 28 | + }); |
| 29 | + registry.registerCommand(UploadSketch.Commands.UPLOAD_SKETCH_TOOLBAR, { |
| 30 | + isVisible: widget => ArduinoToolbar.is(widget) && widget.side === 'left', |
| 31 | + execute: () => registry.executeCommand(UploadSketch.Commands.UPLOAD_SKETCH.id) |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + registerMenus(registry: MenuModelRegistry): void { |
| 36 | + registry.registerMenuAction(ArduinoMenus.SKETCH__MAIN_GROUP, { |
| 37 | + commandId: UploadSketch.Commands.UPLOAD_SKETCH.id, |
| 38 | + label: 'Verify', |
| 39 | + order: '0' |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + registerKeybindings(registry: KeybindingRegistry): void { |
| 44 | + registry.registerKeybinding({ |
| 45 | + command: UploadSketch.Commands.UPLOAD_SKETCH.id, |
| 46 | + keybinding: 'CtrlCmd+U' |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + registerToolbarItems(registry: TabBarToolbarRegistry): void { |
| 51 | + registry.registerItem({ |
| 52 | + id: UploadSketch.Commands.UPLOAD_SKETCH_TOOLBAR.id, |
| 53 | + command: UploadSketch.Commands.UPLOAD_SKETCH_TOOLBAR.id, |
| 54 | + tooltip: 'Upload', |
| 55 | + priority: 1 |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + async uploadSketch(): Promise<void> { |
| 60 | + const sketch = await this.getCurrentSketch(); |
| 61 | + if (!sketch) { |
| 62 | + return; |
| 63 | + } |
| 64 | + const monitorConfig = this.monitorConnection.monitorConfig; |
| 65 | + if (monitorConfig) { |
| 66 | + await this.monitorConnection.disconnect(); |
| 67 | + } |
| 68 | + try { |
| 69 | + const { boardsConfig } = this.boardsServiceClientImpl; |
| 70 | + if (!boardsConfig || !boardsConfig.selectedBoard) { |
| 71 | + throw new Error('No boards selected. Please select a board.'); |
| 72 | + } |
| 73 | + const { selectedPort } = boardsConfig; |
| 74 | + if (!selectedPort) { |
| 75 | + throw new Error('No ports selected. Please select a port.'); |
| 76 | + } |
| 77 | + if (!boardsConfig.selectedBoard.fqbn) { |
| 78 | + throw new Error(`No core is installed for the '${boardsConfig.selectedBoard.name}' board. Please install the core.`); |
| 79 | + } |
| 80 | + const fqbn = await this.boardsConfigStore.appendConfigToFqbn(boardsConfig.selectedBoard.fqbn); |
| 81 | + await this.coreService.upload({ |
| 82 | + sketchUri: sketch.uri, |
| 83 | + fqbn, |
| 84 | + port: selectedPort.address, |
| 85 | + optimizeForDebug: this.editorMode.compileForDebug |
| 86 | + }); |
| 87 | + } catch (e) { |
| 88 | + await this.messageService.error(e.toString()); |
| 89 | + } finally { |
| 90 | + if (monitorConfig) { |
| 91 | + await this.monitorConnection.connect(monitorConfig); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +export namespace UploadSketch { |
| 99 | + export namespace Commands { |
| 100 | + export const UPLOAD_SKETCH: Command = { |
| 101 | + id: 'arduino-upload-sketch' |
| 102 | + }; |
| 103 | + export const UPLOAD_SKETCH_TOOLBAR: Command = { |
| 104 | + id: 'arduino-upload-sketch--toolbar' |
| 105 | + }; |
| 106 | + } |
| 107 | +} |
0 commit comments