| 19 | } |
| 20 | |
| 21 | export class PythonEnvironment { |
| 22 | private provider: Promise<InterpreterProvider | undefined>; |
| 23 | private listeners: (() => void)[] = []; |
| 24 | private listenerDisposables: vscode.Disposable[] = []; |
| 25 | private context: vscode.ExtensionContext; |
| 26 | |
| 27 | constructor(context: vscode.ExtensionContext) { |
| 28 | this.context = context; |
| 29 | this.provider = this.tryResolveProvider().then(provider => { |
| 30 | if (!provider) { |
| 31 | this.showInstallWarning(); |
| 32 | } |
| 33 | return provider; |
| 34 | }); |
| 35 | this.watchExtensionChanges(); |
| 36 | } |
| 37 | |
| 38 | private async tryResolveProvider(): Promise<InterpreterProvider | undefined> { |
| 39 | // Prefer vscode-python-environments if available |
| 40 | try { |
| 41 | const api = await PythonEnvironments.api(); |
| 42 | return { |
| 43 | async getPath(uri?: vscode.Uri) { |
| 44 | const env = await api.getEnvironment(uri); |
| 45 | return env?.execInfo?.run?.executable; |
| 46 | }, |
| 47 | onDidChange(callback: () => void) { |
| 48 | return api.onDidChangeEnvironment(() => callback()); |
| 49 | }, |
| 50 | }; |
| 51 | } catch {} |
| 52 | |
| 53 | // Fall back to ms-python.python |
| 54 | try { |
| 55 | const ext = await PythonExtension.api(); |
| 56 | return { |
| 57 | async getPath(uri?: vscode.Uri) { |
| 58 | const envPath = |
| 59 | await ext.environments.getActiveEnvironmentPath(uri); |
| 60 | return envPath.path.length > 0 ? envPath.path : undefined; |
| 61 | }, |
| 62 | onDidChange(callback: () => void) { |
| 63 | return ext.environments.onDidChangeActiveEnvironmentPath(callback); |
| 64 | }, |
| 65 | }; |
| 66 | } catch { |
| 67 | return undefined; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private showInstallWarning() { |
| 72 | if (this.context.globalState.get(DISMISSED_KEY)) { |
| 73 | return; |
| 74 | } |
| 75 | const install = 'Install'; |
| 76 | const dismiss = "Don't Show Again"; |
| 77 | vscode.window |
| 78 | .showInformationMessage( |
nothing calls this directly
no outgoing calls
no test coverage detected