|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { inject, injectable } from 'inversify'; |
| 5 | +import { FileSystemWatcher, RelativePattern, WorkspaceFolder, WorkspaceFoldersChangeEvent } from 'vscode'; |
| 6 | +import { IExtensionSingleActivationService } from '../activation/types'; |
| 7 | +import { IWorkspaceService } from '../common/application/types'; |
| 8 | +import { NativeTensorBoard } from '../common/experiments/groups'; |
| 9 | +import { traceError } from '../common/logger'; |
| 10 | +import { IDisposableRegistry, IExperimentService } from '../common/types'; |
| 11 | +import { TensorBoardPrompt } from './tensorBoardPrompt'; |
| 12 | + |
| 13 | +@injectable() |
| 14 | +export class TensorBoardFileWatcher implements IExtensionSingleActivationService { |
| 15 | + private fileSystemWatchers = new Map<WorkspaceFolder, FileSystemWatcher[]>(); |
| 16 | + private globPattern1 = '*tfevents*'; |
| 17 | + private globPattern2 = '*/*tfevents*'; |
| 18 | + |
| 19 | + constructor( |
| 20 | + @inject(IWorkspaceService) private workspaceService: IWorkspaceService, |
| 21 | + @inject(TensorBoardPrompt) private tensorBoardPrompt: TensorBoardPrompt, |
| 22 | + @inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry, |
| 23 | + @inject(IExperimentService) private experimentService: IExperimentService |
| 24 | + ) {} |
| 25 | + |
| 26 | + public async activate() { |
| 27 | + if (!(await this.experimentService.inExperiment(NativeTensorBoard.experiment))) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + const folders = this.workspaceService.workspaceFolders; |
| 32 | + if (!folders) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + // Look for pre-existing tfevent files, as the file watchers will only pick up files |
| 37 | + // created or changed after they have been registered and hooked up. Just one will do. |
| 38 | + await this.promptIfWorkspaceHasPreexistingFiles(); |
| 39 | + |
| 40 | + // If the user creates or changes tfevent files, listen for those too |
| 41 | + for (const folder of folders) { |
| 42 | + this.createFileSystemWatcher(folder); |
| 43 | + } |
| 44 | + |
| 45 | + // If workspace folders change, ensure we update our FileSystemWatchers |
| 46 | + this.disposables.push( |
| 47 | + this.workspaceService.onDidChangeWorkspaceFolders((e) => this.updateFileSystemWatchers(e)) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + private async promptIfWorkspaceHasPreexistingFiles() { |
| 52 | + try { |
| 53 | + for (const pattern of [this.globPattern1, this.globPattern2]) { |
| 54 | + const matches = await this.workspaceService.findFiles(pattern, undefined, 1); |
| 55 | + if (matches.length > 0) { |
| 56 | + await this.tensorBoardPrompt.showNativeTensorBoardPrompt(); |
| 57 | + return; |
| 58 | + } |
| 59 | + } |
| 60 | + } catch (e) { |
| 61 | + traceError( |
| 62 | + `Failed to prompt to launch TensorBoard session based on preexisting tfevent files in workspace: ${e}` |
| 63 | + ); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private async updateFileSystemWatchers(event: WorkspaceFoldersChangeEvent) { |
| 68 | + for (const added of event.added) { |
| 69 | + this.createFileSystemWatcher(added); |
| 70 | + } |
| 71 | + for (const removed of event.removed) { |
| 72 | + const fileSystemWatchers = this.fileSystemWatchers.get(removed); |
| 73 | + if (fileSystemWatchers) { |
| 74 | + fileSystemWatchers.forEach((fileWatcher) => fileWatcher.dispose()); |
| 75 | + this.fileSystemWatchers.delete(removed); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private createFileSystemWatcher(folder: WorkspaceFolder) { |
| 81 | + const fileWatchers = []; |
| 82 | + for (const pattern of [this.globPattern1, this.globPattern2]) { |
| 83 | + const relativePattern = new RelativePattern(folder, pattern); |
| 84 | + const fileSystemWatcher = this.workspaceService.createFileSystemWatcher(relativePattern); |
| 85 | + |
| 86 | + // When a file is created or changed that matches `this.globPattern`, try to show our prompt |
| 87 | + this.disposables.push( |
| 88 | + fileSystemWatcher.onDidCreate((_uri) => this.tensorBoardPrompt.showNativeTensorBoardPrompt()) |
| 89 | + ); |
| 90 | + this.disposables.push( |
| 91 | + fileSystemWatcher.onDidChange((_uri) => this.tensorBoardPrompt.showNativeTensorBoardPrompt()) |
| 92 | + ); |
| 93 | + this.disposables.push(fileSystemWatcher); |
| 94 | + fileWatchers.push(fileSystemWatcher); |
| 95 | + } |
| 96 | + this.fileSystemWatchers.set(folder, fileWatchers); |
| 97 | + } |
| 98 | +} |
0 commit comments