|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { Disposable, Event, EventEmitter, GlobPattern, RelativePattern, Uri, WorkspaceFolder } from 'vscode'; |
| 5 | +import { createFileSystemWatcher, getWorkspaceFolder } from '../../../../common/vscodeApis/workspaceApis'; |
| 6 | +import { isWindows } from '../../../../common/platform/platformService'; |
| 7 | +import { arePathsSame } from '../../../common/externalDependencies'; |
| 8 | +import { FileChangeType } from '../../../../common/platform/fileSystemWatcher'; |
| 9 | + |
| 10 | +export interface PythonWorkspaceEnvEvent { |
| 11 | + type: FileChangeType; |
| 12 | + workspaceFolder: WorkspaceFolder; |
| 13 | + executable: string; |
| 14 | +} |
| 15 | + |
| 16 | +export interface PythonGlobalEnvEvent { |
| 17 | + type: FileChangeType; |
| 18 | + uri: Uri; |
| 19 | +} |
| 20 | + |
| 21 | +export interface PythonWatcher extends Disposable { |
| 22 | + watchWorkspace(wf: WorkspaceFolder): void; |
| 23 | + unwatchWorkspace(wf: WorkspaceFolder): void; |
| 24 | + onDidWorkspaceEnvChanged: Event<PythonWorkspaceEnvEvent>; |
| 25 | + |
| 26 | + watchPath(uri: Uri, pattern?: string): void; |
| 27 | + unwatchPath(uri: Uri): void; |
| 28 | + onDidGlobalEnvChanged: Event<PythonGlobalEnvEvent>; |
| 29 | +} |
| 30 | + |
| 31 | +/* |
| 32 | + * The pattern to search for python executables in the workspace. |
| 33 | + * project |
| 34 | + * ├── python or python.exe <--- This is what we are looking for. |
| 35 | + * ├── .conda |
| 36 | + * │ └── python or python.exe <--- This is what we are looking for. |
| 37 | + * └── .venv |
| 38 | + * │ └── Scripts or bin |
| 39 | + * │ └── python or python.exe <--- This is what we are looking for. |
| 40 | + */ |
| 41 | +const WORKSPACE_PATTERN = isWindows() ? '**/python.exe' : '**/python'; |
| 42 | + |
| 43 | +class PythonWatcherImpl implements PythonWatcher { |
| 44 | + private disposables: Disposable[] = []; |
| 45 | + |
| 46 | + private readonly _onDidWorkspaceEnvChanged = new EventEmitter<PythonWorkspaceEnvEvent>(); |
| 47 | + |
| 48 | + private readonly _onDidGlobalEnvChanged = new EventEmitter<PythonGlobalEnvEvent>(); |
| 49 | + |
| 50 | + private readonly _disposeMap: Map<string, Disposable> = new Map<string, Disposable>(); |
| 51 | + |
| 52 | + constructor() { |
| 53 | + this.disposables.push(this._onDidWorkspaceEnvChanged, this._onDidGlobalEnvChanged); |
| 54 | + } |
| 55 | + |
| 56 | + onDidGlobalEnvChanged: Event<PythonGlobalEnvEvent> = this._onDidGlobalEnvChanged.event; |
| 57 | + |
| 58 | + onDidWorkspaceEnvChanged: Event<PythonWorkspaceEnvEvent> = this._onDidWorkspaceEnvChanged.event; |
| 59 | + |
| 60 | + watchWorkspace(wf: WorkspaceFolder): void { |
| 61 | + if (this._disposeMap.has(wf.uri.fsPath)) { |
| 62 | + const disposer = this._disposeMap.get(wf.uri.fsPath); |
| 63 | + disposer?.dispose(); |
| 64 | + } |
| 65 | + |
| 66 | + const disposables: Disposable[] = []; |
| 67 | + const watcher = createFileSystemWatcher(new RelativePattern(wf, WORKSPACE_PATTERN)); |
| 68 | + disposables.push( |
| 69 | + watcher, |
| 70 | + watcher.onDidChange((uri) => { |
| 71 | + this.fireWorkspaceEvent(FileChangeType.Changed, wf, uri); |
| 72 | + }), |
| 73 | + watcher.onDidCreate((uri) => { |
| 74 | + this.fireWorkspaceEvent(FileChangeType.Created, wf, uri); |
| 75 | + }), |
| 76 | + watcher.onDidDelete((uri) => { |
| 77 | + this.fireWorkspaceEvent(FileChangeType.Deleted, wf, uri); |
| 78 | + }), |
| 79 | + ); |
| 80 | + |
| 81 | + const disposable = { |
| 82 | + dispose: () => { |
| 83 | + disposables.forEach((d) => d.dispose()); |
| 84 | + this._disposeMap.delete(wf.uri.fsPath); |
| 85 | + }, |
| 86 | + }; |
| 87 | + this._disposeMap.set(wf.uri.fsPath, disposable); |
| 88 | + } |
| 89 | + |
| 90 | + unwatchWorkspace(wf: WorkspaceFolder): void { |
| 91 | + const disposable = this._disposeMap.get(wf.uri.fsPath); |
| 92 | + disposable?.dispose(); |
| 93 | + } |
| 94 | + |
| 95 | + private fireWorkspaceEvent(type: FileChangeType, wf: WorkspaceFolder, uri: Uri) { |
| 96 | + const uriWorkspace = getWorkspaceFolder(uri); |
| 97 | + if (uriWorkspace && arePathsSame(uriWorkspace.uri.fsPath, wf.uri.fsPath)) { |
| 98 | + this._onDidWorkspaceEnvChanged.fire({ type, workspaceFolder: wf, executable: uri.fsPath }); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + watchPath(uri: Uri, pattern?: string): void { |
| 103 | + if (this._disposeMap.has(uri.fsPath)) { |
| 104 | + const disposer = this._disposeMap.get(uri.fsPath); |
| 105 | + disposer?.dispose(); |
| 106 | + } |
| 107 | + |
| 108 | + const glob: GlobPattern = pattern ? new RelativePattern(uri, pattern) : uri.fsPath; |
| 109 | + const disposables: Disposable[] = []; |
| 110 | + const watcher = createFileSystemWatcher(glob); |
| 111 | + disposables.push( |
| 112 | + watcher, |
| 113 | + watcher.onDidChange(() => { |
| 114 | + this._onDidGlobalEnvChanged.fire({ type: FileChangeType.Changed, uri }); |
| 115 | + }), |
| 116 | + watcher.onDidCreate(() => { |
| 117 | + this._onDidGlobalEnvChanged.fire({ type: FileChangeType.Created, uri }); |
| 118 | + }), |
| 119 | + watcher.onDidDelete(() => { |
| 120 | + this._onDidGlobalEnvChanged.fire({ type: FileChangeType.Deleted, uri }); |
| 121 | + }), |
| 122 | + ); |
| 123 | + |
| 124 | + const disposable = { |
| 125 | + dispose: () => { |
| 126 | + disposables.forEach((d) => d.dispose()); |
| 127 | + this._disposeMap.delete(uri.fsPath); |
| 128 | + }, |
| 129 | + }; |
| 130 | + this._disposeMap.set(uri.fsPath, disposable); |
| 131 | + } |
| 132 | + |
| 133 | + unwatchPath(uri: Uri): void { |
| 134 | + const disposable = this._disposeMap.get(uri.fsPath); |
| 135 | + disposable?.dispose(); |
| 136 | + } |
| 137 | + |
| 138 | + dispose() { |
| 139 | + this.disposables.forEach((d) => d.dispose()); |
| 140 | + this._disposeMap.forEach((d) => d.dispose()); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +export function createPythonWatcher(): PythonWatcher { |
| 145 | + return new PythonWatcherImpl(); |
| 146 | +} |
0 commit comments