|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { inject, injectable } from 'inversify'; |
| 7 | +import { Uri } from 'vscode'; |
| 8 | +import { IExtensionSingleActivationService } from '../../../../activation/types'; |
| 9 | +import { Commands } from '../../../../common/constants'; |
| 10 | +import { IDisposable, IDisposableRegistry } from '../../../../common/types'; |
| 11 | +import { registerCommand } from '../../../../common/vscodeApis/commandApis'; |
| 12 | +import { IInterpreterService } from '../../../../interpreter/contracts'; |
| 13 | + |
| 14 | +@injectable() |
| 15 | +export class InterpreterPathCommand implements IExtensionSingleActivationService { |
| 16 | + public readonly supportedWorkspaceTypes = { untrustedWorkspace: false, virtualWorkspace: false }; |
| 17 | + |
| 18 | + constructor( |
| 19 | + @inject(IInterpreterService) private readonly interpreterService: IInterpreterService, |
| 20 | + @inject(IDisposableRegistry) private readonly disposables: IDisposable[], |
| 21 | + ) {} |
| 22 | + |
| 23 | + public async activate(): Promise<void> { |
| 24 | + this.disposables.push( |
| 25 | + registerCommand(Commands.GetSelectedInterpreterPath, (args) => this._getSelectedInterpreterPath(args)), |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + public async _getSelectedInterpreterPath(args: { workspaceFolder: string } | string[]): Promise<string> { |
| 30 | + // If `launch.json` is launching this command, `args.workspaceFolder` carries the workspaceFolder |
| 31 | + // If `tasks.json` is launching this command, `args[1]` carries the workspaceFolder |
| 32 | + let workspaceFolder; |
| 33 | + if ('workspaceFolder' in args) { |
| 34 | + workspaceFolder = args.workspaceFolder; |
| 35 | + } else if (args[1]) { |
| 36 | + const [, second] = args; |
| 37 | + workspaceFolder = second; |
| 38 | + } else { |
| 39 | + workspaceFolder = undefined; |
| 40 | + } |
| 41 | + |
| 42 | + let workspaceFolderUri; |
| 43 | + try { |
| 44 | + workspaceFolderUri = workspaceFolder ? Uri.file(workspaceFolder) : undefined; |
| 45 | + } catch (ex) { |
| 46 | + workspaceFolderUri = undefined; |
| 47 | + } |
| 48 | + |
| 49 | + return (await this.interpreterService.getActiveInterpreter(workspaceFolderUri))?.path ?? 'python'; |
| 50 | + } |
| 51 | +} |
0 commit comments