forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
poetryInstaller.ts
109 lines (99 loc) · 4.48 KB
/
poetryInstaller.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import * as path from 'path';
import { Uri } from 'vscode';
import { IInterpreterService } from '../../interpreter/contracts';
import { IServiceContainer } from '../../ioc/types';
import { isPoetryEnvironmentRelatedToFolder } from '../../pythonEnvironments/discovery/locators/services/poetry';
import { EnvironmentType } from '../../pythonEnvironments/info';
import { IWorkspaceService } from '../application/types';
import { inDiscoveryExperiment } from '../experiments/helpers';
import { traceError } from '../logger';
import { IFileSystem } from '../platform/types';
import { IProcessServiceFactory } from '../process/types';
import { ExecutionInfo, IConfigurationService, IExperimentService } from '../types';
import { isResource } from '../utils/misc';
import { ModuleInstaller } from './moduleInstaller';
import { InterpreterUri } from './types';
export const poetryName = 'poetry';
const poetryFile = 'poetry.lock';
@injectable()
export class PoetryInstaller extends ModuleInstaller {
// eslint-disable-next-line class-methods-use-this
public get name(): string {
return 'poetry';
}
// eslint-disable-next-line class-methods-use-this
public get displayName(): string {
return poetryName;
}
// eslint-disable-next-line class-methods-use-this
public get priority(): number {
return 10;
}
constructor(
@inject(IServiceContainer) serviceContainer: IServiceContainer,
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService,
@inject(IConfigurationService) private readonly configurationService: IConfigurationService,
@inject(IFileSystem) private readonly fs: IFileSystem,
@inject(IProcessServiceFactory) private readonly processFactory: IProcessServiceFactory,
) {
super(serviceContainer);
}
public async isSupported(resource?: InterpreterUri): Promise<boolean> {
if (!resource) {
return false;
}
const experimentService = this.serviceContainer.get<IExperimentService>(IExperimentService);
if (await inDiscoveryExperiment(experimentService)) {
if (!isResource(resource)) {
return false;
}
const interpreter = await this.serviceContainer
.get<IInterpreterService>(IInterpreterService)
.getActiveInterpreter(resource);
const workspaceFolder = resource ? this.workspaceService.getWorkspaceFolder(resource) : undefined;
if (!interpreter || !workspaceFolder || interpreter.envType !== EnvironmentType.Poetry) {
return false;
}
// Install using poetry CLI only if the active poetry environment is related to the current folder.
return isPoetryEnvironmentRelatedToFolder(
interpreter.path,
workspaceFolder.uri.fsPath,
this.configurationService.getSettings(resource).poetryPath,
);
}
const workspaceFolder = this.workspaceService.getWorkspaceFolder(isResource(resource) ? resource : undefined);
if (!workspaceFolder) {
return false;
}
if (!(await this.fs.fileExists(path.join(workspaceFolder.uri.fsPath, poetryFile)))) {
return false;
}
return this.isPoetryAvailable(workspaceFolder.uri);
}
protected async isPoetryAvailable(workfolder: Uri): Promise<boolean> {
try {
const processService = await this.processFactory.create(workfolder);
const execPath = this.configurationService.getSettings(workfolder).poetryPath;
const result = await processService.shellExec(`${execPath} env list`, { cwd: workfolder.fsPath });
return result && (result.stderr || '').trim().length === 0;
} catch (error) {
traceError(`${poetryFile} exists but Poetry not found`, error);
return false;
}
}
protected async getExecutionInfo(moduleName: string, resource?: InterpreterUri): Promise<ExecutionInfo> {
const execPath = this.configurationService.getSettings(isResource(resource) ? resource : undefined).poetryPath;
const args = ['add', '--dev', moduleName];
if (moduleName === 'black') {
args.push('--allow-prereleases');
}
return {
args,
execPath,
};
}
}