forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
pipEnvActivationProvider.ts
56 lines (49 loc) · 2.5 KB
/
pipEnvActivationProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable, named } from 'inversify';
import { Uri } from 'vscode';
import '../../extensions';
import { IInterpreterService } from '../../../interpreter/contracts';
import { isPipenvEnvironmentRelatedToFolder } from '../../../pythonEnvironments/common/environmentManagers/pipenv';
import { EnvironmentType } from '../../../pythonEnvironments/info';
import { IWorkspaceService } from '../../application/types';
import { IToolExecutionPath, ToolExecutionPath } from '../../types';
import { ITerminalActivationCommandProvider } from '../types';
@injectable()
export class PipEnvActivationCommandProvider implements ITerminalActivationCommandProvider {
constructor(
@inject(IInterpreterService) private readonly interpreterService: IInterpreterService,
@inject(IToolExecutionPath)
@named(ToolExecutionPath.pipenv)
private readonly pipEnvExecution: IToolExecutionPath,
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService,
) {}
// eslint-disable-next-line class-methods-use-this
public isShellSupported(): boolean {
return false;
}
public async getActivationCommands(resource: Uri | undefined): Promise<string[] | undefined> {
const interpreter = await this.interpreterService.getActiveInterpreter(resource);
if (!interpreter || interpreter.envType !== EnvironmentType.Pipenv) {
return undefined;
}
// Activate using `pipenv shell` only if the current folder relates pipenv environment.
const workspaceFolder = resource ? this.workspaceService.getWorkspaceFolder(resource) : undefined;
if (workspaceFolder) {
if (!(await isPipenvEnvironmentRelatedToFolder(interpreter.path, workspaceFolder?.uri.fsPath))) {
return undefined;
}
}
const execName = this.pipEnvExecution.executable;
return [`${execName.fileToCommandArgumentForPythonExt()} shell`];
}
public async getActivationCommandsForInterpreter(pythonPath: string): Promise<string[] | undefined> {
const interpreter = await this.interpreterService.getInterpreterDetails(pythonPath);
if (!interpreter || interpreter.envType !== EnvironmentType.Pipenv) {
return undefined;
}
const execName = this.pipEnvExecution.executable;
return [`${execName.fileToCommandArgumentForPythonExt()} shell`];
}
}