|
| 1 | +import { DisposableCollection } from '@theia/core/lib/common/disposable'; |
| 2 | +import URI from '@theia/core/lib/common/uri'; |
| 3 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 4 | +import { HostedPluginSupport } from '@theia/plugin-ext/lib/hosted/browser/hosted-plugin'; |
| 5 | +import type { ArduinoState } from 'vscode-arduino-api'; |
| 6 | +import { |
| 7 | + BoardsService, |
| 8 | + CompileSummary, |
| 9 | + Port, |
| 10 | + isCompileSummary, |
| 11 | +} from '../../common/protocol'; |
| 12 | +import { |
| 13 | + toApiBoardDetails, |
| 14 | + toApiCompileSummary, |
| 15 | + toApiPort, |
| 16 | +} from '../../common/protocol/arduino-context-mapper'; |
| 17 | +import type { BoardsConfig } from '../boards/boards-config'; |
| 18 | +import { BoardsDataStore } from '../boards/boards-data-store'; |
| 19 | +import { BoardsServiceProvider } from '../boards/boards-service-provider'; |
| 20 | +import { CurrentSketch } from '../sketches-service-client-impl'; |
| 21 | +import { SketchContribution } from './contribution'; |
| 22 | + |
| 23 | +interface UpdateStateParams<T extends ArduinoState> { |
| 24 | + readonly key: keyof T; |
| 25 | + readonly value: T[keyof T]; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Contribution for updating the Arduino state, such as the FQBN, selected port, and sketch path changes via commands, so other VS Code extensions can access it. |
| 30 | + * See [`vscode-arduino-api`](https://github.com/dankeboy36/vscode-arduino-api#api) for more details. |
| 31 | + */ |
| 32 | +@injectable() |
| 33 | +export class UpdateArduinoState extends SketchContribution { |
| 34 | + @inject(BoardsService) |
| 35 | + private readonly boardsService: BoardsService; |
| 36 | + @inject(BoardsServiceProvider) |
| 37 | + private readonly boardsServiceProvider: BoardsServiceProvider; |
| 38 | + @inject(BoardsDataStore) |
| 39 | + private readonly boardsDataStore: BoardsDataStore; |
| 40 | + @inject(HostedPluginSupport) |
| 41 | + private readonly hostedPluginSupport: HostedPluginSupport; |
| 42 | + |
| 43 | + private readonly toDispose = new DisposableCollection(); |
| 44 | + |
| 45 | + override onStart(): void { |
| 46 | + this.toDispose.pushAll([ |
| 47 | + this.boardsServiceProvider.onBoardsConfigChanged((config) => |
| 48 | + this.updateBoardsConfig(config) |
| 49 | + ), |
| 50 | + this.sketchServiceClient.onCurrentSketchDidChange((sketch) => |
| 51 | + this.updateSketchPath(sketch) |
| 52 | + ), |
| 53 | + this.configService.onDidChangeDataDirUri((dataDirUri) => |
| 54 | + this.updateDataDirPath(dataDirUri) |
| 55 | + ), |
| 56 | + this.configService.onDidChangeSketchDirUri((userDirUri) => |
| 57 | + this.updateUserDirPath(userDirUri) |
| 58 | + ), |
| 59 | + this.commandService.onDidExecuteCommand(({ commandId, args }) => { |
| 60 | + if ( |
| 61 | + commandId === 'arduino.languageserver.notifyBuildDidComplete' && |
| 62 | + isCompileSummary(args[0]) |
| 63 | + ) { |
| 64 | + this.updateCompileSummary(args[0]); |
| 65 | + } |
| 66 | + }), |
| 67 | + this.boardsDataStore.onChanged((fqbn) => { |
| 68 | + const selectedFqbn = |
| 69 | + this.boardsServiceProvider.boardsConfig.selectedBoard?.fqbn; |
| 70 | + if (selectedFqbn && fqbn.includes(selectedFqbn)) { |
| 71 | + this.updateBoardDetails(selectedFqbn); |
| 72 | + } |
| 73 | + }), |
| 74 | + ]); |
| 75 | + } |
| 76 | + |
| 77 | + override onReady(): void { |
| 78 | + this.boardsServiceProvider.reconciled.then(() => { |
| 79 | + this.updateBoardsConfig(this.boardsServiceProvider.boardsConfig); |
| 80 | + }); |
| 81 | + this.updateSketchPath(this.sketchServiceClient.tryGetCurrentSketch()); |
| 82 | + this.updateUserDirPath(this.configService.tryGetSketchDirUri()); |
| 83 | + this.updateDataDirPath(this.configService.tryGetDataDirUri()); |
| 84 | + } |
| 85 | + |
| 86 | + onStop(): void { |
| 87 | + this.toDispose.dispose(); |
| 88 | + } |
| 89 | + |
| 90 | + private async updateSketchPath( |
| 91 | + sketch: CurrentSketch | undefined |
| 92 | + ): Promise<void> { |
| 93 | + const sketchPath = CurrentSketch.isValid(sketch) |
| 94 | + ? new URI(sketch.uri).path.fsPath() |
| 95 | + : undefined; |
| 96 | + return this.updateState({ key: 'sketchPath', value: sketchPath }); |
| 97 | + } |
| 98 | + |
| 99 | + private async updateCompileSummary( |
| 100 | + compileSummary: CompileSummary |
| 101 | + ): Promise<void> { |
| 102 | + const apiCompileSummary = toApiCompileSummary(compileSummary); |
| 103 | + return this.updateState({ |
| 104 | + key: 'compileSummary', |
| 105 | + value: apiCompileSummary, |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + private async updateBoardsConfig( |
| 110 | + boardsConfig: BoardsConfig.Config |
| 111 | + ): Promise<void> { |
| 112 | + const fqbn = boardsConfig.selectedBoard?.fqbn; |
| 113 | + const port = boardsConfig.selectedPort; |
| 114 | + await this.updateFqbn(fqbn); |
| 115 | + await this.updateBoardDetails(fqbn); |
| 116 | + await this.updatePort(port); |
| 117 | + } |
| 118 | + |
| 119 | + private async updateFqbn(fqbn: string | undefined): Promise<void> { |
| 120 | + await this.updateState({ key: 'fqbn', value: fqbn }); |
| 121 | + } |
| 122 | + |
| 123 | + private async updateBoardDetails(fqbn: string | undefined): Promise<void> { |
| 124 | + const unset = () => |
| 125 | + this.updateState({ key: 'boardDetails', value: undefined }); |
| 126 | + if (!fqbn) { |
| 127 | + return unset(); |
| 128 | + } |
| 129 | + const [details, persistedData] = await Promise.all([ |
| 130 | + this.boardsService.getBoardDetails({ fqbn }), |
| 131 | + this.boardsDataStore.getData(fqbn), |
| 132 | + ]); |
| 133 | + if (!details) { |
| 134 | + return unset(); |
| 135 | + } |
| 136 | + const apiBoardDetails = toApiBoardDetails({ |
| 137 | + ...details, |
| 138 | + configOptions: |
| 139 | + BoardsDataStore.Data.EMPTY === persistedData |
| 140 | + ? details.configOptions |
| 141 | + : persistedData.configOptions.slice(), |
| 142 | + }); |
| 143 | + return this.updateState({ |
| 144 | + key: 'boardDetails', |
| 145 | + value: apiBoardDetails, |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + private async updatePort(port: Port | undefined): Promise<void> { |
| 150 | + const apiPort = port && toApiPort(port); |
| 151 | + return this.updateState({ key: 'port', value: apiPort }); |
| 152 | + } |
| 153 | + |
| 154 | + private async updateUserDirPath(userDirUri: URI | undefined): Promise<void> { |
| 155 | + const userDirPath = userDirUri?.path.fsPath(); |
| 156 | + return this.updateState({ |
| 157 | + key: 'userDirPath', |
| 158 | + value: userDirPath, |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + private async updateDataDirPath(dataDirUri: URI | undefined): Promise<void> { |
| 163 | + const dataDirPath = dataDirUri?.path.fsPath(); |
| 164 | + return this.updateState({ |
| 165 | + key: 'dataDirPath', |
| 166 | + value: dataDirPath, |
| 167 | + }); |
| 168 | + } |
| 169 | + |
| 170 | + private async updateState<T extends ArduinoState>( |
| 171 | + params: UpdateStateParams<T> |
| 172 | + ): Promise<void> { |
| 173 | + await this.hostedPluginSupport.didStart; |
| 174 | + return this.commandService.executeCommand( |
| 175 | + 'arduinoAPI.updateState', |
| 176 | + params |
| 177 | + ); |
| 178 | + } |
| 179 | +} |
0 commit comments