|
| 1 | +import { DisposableCollection } from '@theia/core/lib/common/disposable'; |
| 2 | +import { isObject } from '@theia/core/lib/common/types'; |
| 3 | +import URI from '@theia/core/lib/common/uri'; |
| 4 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 5 | +import { HostedPluginSupport } from '@theia/plugin-ext/lib/hosted/browser/hosted-plugin'; |
| 6 | +import type { ArduinoState } from 'vscode-arduino-api'; |
| 7 | +import { URI as CodeUri } from 'vscode-uri'; |
| 8 | +import type { Port, Sketch } from '../../common/protocol'; |
| 9 | +import { BoardsService } from '../../common/protocol'; |
| 10 | +import type { BoardsConfig } from '../boards/boards-config'; |
| 11 | +import { BoardsDataStore } from '../boards/boards-data-store'; |
| 12 | +import { BoardsServiceProvider } from '../boards/boards-service-provider'; |
| 13 | +import { CurrentSketch } from '../sketches-service-client-impl'; |
| 14 | +import { SketchContribution } from './contribution'; |
| 15 | + |
| 16 | +interface UpdateWorkspaceStateParams { |
| 17 | + readonly key: keyof ArduinoState; |
| 18 | + readonly value: ArduinoState[keyof ArduinoState]; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Contribution for setting the VS Code [`workspaceState`](https://code.visualstudio.com/api/references/vscode-api#workspace) on Arduino IDE context changes, such as FQBN, selected port, and sketch path changes via commands. |
| 23 | + * See [`vscode-arduino-api`](https://www.npmjs.com/package/vscode-arduino-api) for more details. |
| 24 | + */ |
| 25 | +@injectable() |
| 26 | +export class UpdateArduinoContext extends SketchContribution { |
| 27 | + @inject(BoardsService) |
| 28 | + private readonly boardsService: BoardsService; |
| 29 | + @inject(BoardsServiceProvider) |
| 30 | + private readonly boardsServiceProvider: BoardsServiceProvider; |
| 31 | + @inject(BoardsDataStore) |
| 32 | + private readonly boardsDataStore: BoardsDataStore; |
| 33 | + @inject(HostedPluginSupport) |
| 34 | + private readonly hostedPluginSupport: HostedPluginSupport; |
| 35 | + |
| 36 | + private readonly toDispose = new DisposableCollection(); |
| 37 | + |
| 38 | + override onStart(): void { |
| 39 | + this.toDispose.pushAll([ |
| 40 | + this.boardsServiceProvider.onBoardsConfigChanged((config) => this.updateBoardsConfig(config)), |
| 41 | + this.sketchServiceClient.onCurrentSketchDidChange((sketch) => this.updateSketchPath(sketch)), |
| 42 | + this.configService.onDidChangeDataDirUri((dataDirUri) => this.updateDataDirPath(dataDirUri)), |
| 43 | + this.configService.onDidChangeSketchDirUri((userDirUri) => this.updateUserDirPath(userDirUri)), |
| 44 | + this.commandService.onDidExecuteCommand(({ commandId, args }) => { |
| 45 | + if (commandId === 'arduino.languageserver.notifyBuildDidComplete' && isObject<Partial<{ buildOutputUri: string }>>(args)) { |
| 46 | + const buildOutputUri = args.buildOutputUri; |
| 47 | + if (typeof buildOutputUri === 'string') { |
| 48 | + this.updateBuildPath(new URI(buildOutputUri).path.fsPath()); |
| 49 | + } |
| 50 | + } |
| 51 | + }), |
| 52 | + this.boardsDataStore.onChanged(fqbn => { |
| 53 | + const selectedFqbn = this.boardsServiceProvider.boardsConfig.selectedBoard?.fqbn; |
| 54 | + if (selectedFqbn && fqbn.includes(selectedFqbn)) { |
| 55 | + this.updateBoardDetails(selectedFqbn); |
| 56 | + } |
| 57 | + }) |
| 58 | + ]); |
| 59 | + } |
| 60 | + |
| 61 | + override onReady(): void { |
| 62 | + this.boardsServiceProvider.reconciled.then(() => { |
| 63 | + this.updateBoardsConfig(this.boardsServiceProvider.boardsConfig); |
| 64 | + }); |
| 65 | + const sketch = this.sketchServiceClient.tryGetCurrentSketch(); |
| 66 | + this.updateSketchPath(sketch); |
| 67 | + if (CurrentSketch.isValid(sketch)) { |
| 68 | + this.tryFindBuildPath(sketch).then(buildPath => { |
| 69 | + if (buildPath) { |
| 70 | + this.updateBuildPath(buildPath); |
| 71 | + } |
| 72 | + }) |
| 73 | + } |
| 74 | + this.updateUserDirPath(this.configService.tryGetSketchDirUri()); |
| 75 | + this.updateDataDirPath(this.configService.tryGetDataDirUri()); |
| 76 | + } |
| 77 | + |
| 78 | + onStop(): void { |
| 79 | + this.toDispose.dispose(); |
| 80 | + } |
| 81 | + |
| 82 | + private async tryFindBuildPath(sketch: Sketch): Promise<string | undefined> { |
| 83 | + const buildPaths = await this.sketchesService.tempBuildPath(sketch); |
| 84 | + for (const buildPath of buildPaths) { |
| 85 | + try { |
| 86 | + const uri = new URI(CodeUri.file(buildPath)); |
| 87 | + await this.fileService.access(uri) |
| 88 | + return buildPath; |
| 89 | + } catch {} |
| 90 | + } |
| 91 | + return undefined; |
| 92 | + } |
| 93 | + |
| 94 | + private async updateSketchPath( |
| 95 | + sketch: CurrentSketch | undefined |
| 96 | + ): Promise<void> { |
| 97 | + const sketchPath = CurrentSketch.isValid(sketch) |
| 98 | + ? new URI(sketch.uri).path.fsPath() |
| 99 | + : undefined; |
| 100 | + return this.updateWorkspaceState({ key: 'sketchPath', value: sketchPath }) |
| 101 | + } |
| 102 | + |
| 103 | + private async updateBuildPath( |
| 104 | + buildPath: string | undefined |
| 105 | + ): Promise<void> { |
| 106 | + return this.updateWorkspaceState({ key: 'buildPath', value: buildPath }); |
| 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.updateWorkspaceState({ key: 'fqbn', value: fqbn }); |
| 121 | + } |
| 122 | + |
| 123 | + private async updateBoardDetails(fqbn: string | undefined): Promise<void> { |
| 124 | + const unset = () => this.updateWorkspaceState({ key: 'boardDetails', value: undefined }); |
| 125 | + if (!fqbn) { |
| 126 | + return unset(); |
| 127 | + } |
| 128 | + const [details, persistedData] = await Promise.all([ |
| 129 | + this.boardsService.getBoardDetails({ fqbn }), |
| 130 | + this.boardsDataStore.getData(fqbn) |
| 131 | + ]) |
| 132 | + if (!details) { |
| 133 | + return unset(); |
| 134 | + } |
| 135 | + return this.updateWorkspaceState({ key: 'boardDetails', value: { ...details, configOptions: BoardsDataStore.Data.EMPTY === persistedData ? details.configOptions : persistedData.configOptions.slice() } }); |
| 136 | + } |
| 137 | + |
| 138 | + private async updatePort(port: Port | undefined): Promise<void> { |
| 139 | + return this.updateWorkspaceState({ key: 'port', value: port }); |
| 140 | + } |
| 141 | + |
| 142 | + private async updateUserDirPath(userDirUri: URI | undefined): Promise<void> { |
| 143 | + const userDirPath = userDirUri?.path.fsPath(); |
| 144 | + return this.updateWorkspaceState({ key: 'userDirPath', value: userDirPath }); |
| 145 | + } |
| 146 | + |
| 147 | + private async updateDataDirPath(dataDirUri: URI | undefined): Promise<void> { |
| 148 | + const dataDirPath = dataDirUri?.path.fsPath(); |
| 149 | + return this.updateWorkspaceState({ key: 'dataDirPath', value: dataDirPath }); |
| 150 | + } |
| 151 | + |
| 152 | + private async updateWorkspaceState(params: UpdateWorkspaceStateParams): Promise<void> { |
| 153 | + await this.hostedPluginSupport.didStart; |
| 154 | + return this.commandService.executeCommand('vscodeArduinoAPI.updateState', params); |
| 155 | + } |
| 156 | +} |
0 commit comments