|
| 1 | +import * as path from 'path'; |
| 2 | +import * as fsapi from 'fs-extra'; |
| 3 | +import { Uri, Event, EventEmitter, FileChangeType } from 'vscode'; |
| 4 | +import { DidChangeEnvironmentVariablesEventArgs, PythonEnvironmentVariablesApi } from '../../api'; |
| 5 | +import { Disposable } from 'vscode-jsonrpc'; |
| 6 | +import { createFileSystemWatcher, getConfiguration } from '../../common/workspace.apis'; |
| 7 | +import { PythonProjectManager } from '../../internal.api'; |
| 8 | +import { mergeEnvVariables, parseEnvFile } from './envVarUtils'; |
| 9 | +import { resolveVariables } from '../../common/utils/internalVariables'; |
| 10 | + |
| 11 | +export interface EnvVarManager extends PythonEnvironmentVariablesApi, Disposable {} |
| 12 | + |
| 13 | +export class PythonEnvVariableManager implements EnvVarManager { |
| 14 | + private disposables: Disposable[] = []; |
| 15 | + |
| 16 | + private _onDidChangeEnvironmentVariables; |
| 17 | + private watcher; |
| 18 | + |
| 19 | + constructor(private pm: PythonProjectManager) { |
| 20 | + this._onDidChangeEnvironmentVariables = new EventEmitter<DidChangeEnvironmentVariablesEventArgs>(); |
| 21 | + this.onDidChangeEnvironmentVariables = this._onDidChangeEnvironmentVariables.event; |
| 22 | + |
| 23 | + this.watcher = createFileSystemWatcher('**/.env'); |
| 24 | + this.disposables.push( |
| 25 | + this._onDidChangeEnvironmentVariables, |
| 26 | + this.watcher, |
| 27 | + this.watcher.onDidCreate((e) => |
| 28 | + this._onDidChangeEnvironmentVariables.fire({ uri: e, changeTye: FileChangeType.Created }), |
| 29 | + ), |
| 30 | + this.watcher.onDidChange((e) => |
| 31 | + this._onDidChangeEnvironmentVariables.fire({ uri: e, changeTye: FileChangeType.Changed }), |
| 32 | + ), |
| 33 | + this.watcher.onDidDelete((e) => |
| 34 | + this._onDidChangeEnvironmentVariables.fire({ uri: e, changeTye: FileChangeType.Deleted }), |
| 35 | + ), |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + async getEnvironmentVariables( |
| 40 | + uri: Uri, |
| 41 | + overrides?: ({ [key: string]: string | undefined } | Uri)[], |
| 42 | + baseEnvVar?: { [key: string]: string | undefined }, |
| 43 | + ): Promise<{ [key: string]: string | undefined }> { |
| 44 | + const project = this.pm.get(uri); |
| 45 | + |
| 46 | + const base = baseEnvVar || { ...process.env }; |
| 47 | + let env = base; |
| 48 | + |
| 49 | + const config = getConfiguration('python', project?.uri ?? uri); |
| 50 | + let envFilePath = config.get<string>('envFile'); |
| 51 | + envFilePath = envFilePath ? path.normalize(resolveVariables(envFilePath)) : undefined; |
| 52 | + |
| 53 | + if (envFilePath && (await fsapi.pathExists(envFilePath))) { |
| 54 | + const other = await parseEnvFile(Uri.file(envFilePath)); |
| 55 | + env = mergeEnvVariables(env, other); |
| 56 | + } |
| 57 | + |
| 58 | + let projectEnvFilePath = project ? path.normalize(path.join(project.uri.fsPath, '.env')) : undefined; |
| 59 | + if ( |
| 60 | + projectEnvFilePath && |
| 61 | + projectEnvFilePath?.toLowerCase() !== envFilePath?.toLowerCase() && |
| 62 | + (await fsapi.pathExists(projectEnvFilePath)) |
| 63 | + ) { |
| 64 | + const other = await parseEnvFile(Uri.file(projectEnvFilePath)); |
| 65 | + env = mergeEnvVariables(env, other); |
| 66 | + } |
| 67 | + |
| 68 | + if (overrides) { |
| 69 | + for (const override of overrides) { |
| 70 | + const other = override instanceof Uri ? await parseEnvFile(override) : override; |
| 71 | + env = mergeEnvVariables(env, other); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return env; |
| 76 | + } |
| 77 | + |
| 78 | + onDidChangeEnvironmentVariables: Event<DidChangeEnvironmentVariablesEventArgs>; |
| 79 | + |
| 80 | + dispose(): void { |
| 81 | + this.disposables.forEach((disposable) => disposable.dispose()); |
| 82 | + } |
| 83 | +} |
0 commit comments