|
| 1 | +import debounce = require('p-debounce'); |
| 2 | +import { inject, injectable, postConstruct } from 'inversify'; |
| 3 | +import URI from '@theia/core/lib/common/uri'; |
| 4 | +import { Event, Emitter } from '@theia/core/lib/common/event'; |
| 5 | +import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state'; |
| 6 | +import { DebugConfiguration } from '@theia/debug/lib/common/debug-common'; |
| 7 | +import { DebugConfigurationModel as TheiaDebugConfigurationModel } from '@theia/debug/lib/browser/debug-configuration-model'; |
| 8 | +import { DebugConfigurationManager as TheiaDebugConfigurationManager } from '@theia/debug/lib/browser/debug-configuration-manager'; |
| 9 | +import { SketchesService } from '../../../common/protocol'; |
| 10 | +import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl'; |
| 11 | +import { DebugConfigurationModel } from './debug-configuration-model'; |
| 12 | +import { FileOperationError, FileOperationResult } from '@theia/filesystem/lib/common/files'; |
| 13 | + |
| 14 | +@injectable() |
| 15 | +export class DebugConfigurationManager extends TheiaDebugConfigurationManager { |
| 16 | + |
| 17 | + @inject(SketchesService) |
| 18 | + protected readonly sketchesService: SketchesService; |
| 19 | + |
| 20 | + @inject(SketchesServiceClientImpl) |
| 21 | + protected readonly sketchesServiceClient: SketchesServiceClientImpl; |
| 22 | + |
| 23 | + @inject(FrontendApplicationStateService) |
| 24 | + protected readonly appStateService: FrontendApplicationStateService; |
| 25 | + |
| 26 | + protected onTempContentDidChangeEmitter = new Emitter<TheiaDebugConfigurationModel.JsonContent>(); |
| 27 | + get onTempContentDidChange(): Event<TheiaDebugConfigurationModel.JsonContent> { |
| 28 | + return this.onTempContentDidChangeEmitter.event; |
| 29 | + } |
| 30 | + |
| 31 | + @postConstruct() |
| 32 | + protected async init(): Promise<void> { |
| 33 | + super.init(); |
| 34 | + this.appStateService.reachedState('ready').then(async () => { |
| 35 | + const tempContent = await this.getTempLaunchJsonContent(); |
| 36 | + if (!tempContent) { |
| 37 | + // No active sketch. |
| 38 | + return; |
| 39 | + } |
| 40 | + // Watch the file of the container folder. |
| 41 | + this.fileService.watch(tempContent instanceof URI ? tempContent : tempContent.uri); |
| 42 | + // Use the normalized temp folder name. We cannot compare Theia URIs here. |
| 43 | + // /var/folders/k3/d2fkvv1j16v3_rz93k7f74180000gn/T/arduino-ide2-a0337d47f86b24a51df3dbcf2cc17925/launch.json |
| 44 | + // /private/var/folders/k3/d2fkvv1j16v3_rz93k7f74180000gn/T/arduino-ide2-A0337D47F86B24A51DF3DBCF2CC17925/launch.json |
| 45 | + const tempFolderName = (tempContent instanceof URI ? tempContent : tempContent.uri.parent).path.base.toLowerCase(); |
| 46 | + this.fileService.onDidFilesChange(event => { |
| 47 | + for (const { resource } of event.changes) { |
| 48 | + if (resource.path.base === 'launch.json' && resource.parent.path.base.toLowerCase() === tempFolderName) { |
| 49 | + this.getTempLaunchJsonContent().then(config => { |
| 50 | + if (config && !(config instanceof URI)) { |
| 51 | + this.onTempContentDidChangeEmitter.fire(config); |
| 52 | + } |
| 53 | + }); |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + }); |
| 58 | + this.updateModels(); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + protected updateModels = debounce(async () => { |
| 63 | + await this.appStateService.reachedState('ready'); |
| 64 | + const roots = await this.workspaceService.roots; |
| 65 | + const toDelete = new Set(this.models.keys()); |
| 66 | + for (const rootStat of roots) { |
| 67 | + const key = rootStat.resource.toString(); |
| 68 | + toDelete.delete(key); |
| 69 | + if (!this.models.has(key)) { |
| 70 | + const tempContent = await this.getTempLaunchJsonContent(); |
| 71 | + if (!tempContent) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + const configurations: DebugConfiguration[] = tempContent instanceof URI ? [] : tempContent.configurations; |
| 75 | + const uri = tempContent instanceof URI ? undefined : tempContent.uri; |
| 76 | + const model = new DebugConfigurationModel(key, this.preferences, configurations, uri, this.onTempContentDidChange); |
| 77 | + model.onDidChange(() => this.updateCurrent()); |
| 78 | + model.onDispose(() => this.models.delete(key)); |
| 79 | + this.models.set(key, model); |
| 80 | + } |
| 81 | + } |
| 82 | + for (const uri of toDelete) { |
| 83 | + const model = this.models.get(uri); |
| 84 | + if (model) { |
| 85 | + model.dispose(); |
| 86 | + } |
| 87 | + } |
| 88 | + this.updateCurrent(); |
| 89 | + }, 500); |
| 90 | + |
| 91 | + protected async getTempLaunchJsonContent(): Promise<TheiaDebugConfigurationModel.JsonContent & { uri: URI } | URI | undefined> { |
| 92 | + const sketch = await this.sketchesServiceClient.currentSketch(); |
| 93 | + if (!sketch) { |
| 94 | + return undefined; |
| 95 | + } |
| 96 | + const uri = await this.sketchesService.getIdeTempFolderUri(sketch); |
| 97 | + const tempFolderUri = new URI(uri); |
| 98 | + await this.fileService.createFolder(tempFolderUri); |
| 99 | + try { |
| 100 | + const uri = tempFolderUri.resolve('launch.json'); |
| 101 | + const { value } = await this.fileService.read(uri); |
| 102 | + const configurations = DebugConfigurationModel.parse(JSON.parse(value)); |
| 103 | + return { uri, configurations }; |
| 104 | + } catch (err) { |
| 105 | + if (err instanceof FileOperationError && err.fileOperationResult === FileOperationResult.FILE_NOT_FOUND) { |
| 106 | + return tempFolderUri; |
| 107 | + } |
| 108 | + console.error('Could not load debug configuration from IDE2 temp folder.', err); |
| 109 | + throw err; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments