forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateEnvApi.ts
More file actions
134 lines (123 loc) · 5.62 KB
/
createEnvApi.ts
File metadata and controls
134 lines (123 loc) · 5.62 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { ConfigurationTarget, Disposable, QuickInputButtons } from 'vscode';
import { Commands } from '../../common/constants';
import { IDisposableRegistry, IInterpreterPathService, IPathUtils } from '../../common/types';
import { executeCommand, registerCommand } from '../../common/vscodeApis/commandApis';
import { IInterpreterQuickPick } from '../../interpreter/configuration/types';
import { getCreationEvents, handleCreateEnvironmentCommand } from './createEnvironment';
import { condaCreationProvider } from './provider/condaCreationProvider';
import { VenvCreationProvider } from './provider/venvCreationProvider';
import { showInformationMessage } from '../../common/vscodeApis/windowApis';
import { CreateEnv } from '../../common/utils/localize';
import {
CreateEnvironmentProvider,
CreateEnvironmentOptions,
CreateEnvironmentResult,
ProposedCreateEnvironmentAPI,
EnvironmentDidCreateEvent,
} from './proposed.createEnvApis';
import { sendTelemetryEvent } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { CreateEnvironmentOptionsInternal } from './types';
import { useEnvExtension } from '../../envExt/api.internal';
import { PythonEnvironment } from '../../envExt/types';
class CreateEnvironmentProviders {
private _createEnvProviders: CreateEnvironmentProvider[] = [];
constructor() {
this._createEnvProviders = [];
}
public add(provider: CreateEnvironmentProvider) {
if (this._createEnvProviders.filter((p) => p.id === provider.id).length > 0) {
throw new Error(`Create Environment provider with id ${provider.id} already registered`);
}
this._createEnvProviders.push(provider);
}
public remove(provider: CreateEnvironmentProvider) {
this._createEnvProviders = this._createEnvProviders.filter((p) => p !== provider);
}
public getAll(): readonly CreateEnvironmentProvider[] {
return this._createEnvProviders;
}
}
const _createEnvironmentProviders: CreateEnvironmentProviders = new CreateEnvironmentProviders();
export function registerCreateEnvironmentProvider(provider: CreateEnvironmentProvider): Disposable {
_createEnvironmentProviders.add(provider);
return new Disposable(() => {
_createEnvironmentProviders.remove(provider);
});
}
export const { onCreateEnvironmentStarted, onCreateEnvironmentExited, isCreatingEnvironment } = getCreationEvents();
export function registerCreateEnvironmentFeatures(
disposables: IDisposableRegistry,
interpreterQuickPick: IInterpreterQuickPick,
interpreterPathService: IInterpreterPathService,
pathUtils: IPathUtils,
): void {
disposables.push(
registerCommand(
Commands.Create_Environment,
async (
options?: CreateEnvironmentOptions & CreateEnvironmentOptionsInternal,
): Promise<CreateEnvironmentResult | undefined> => {
if (useEnvExtension()) {
try {
const result = await executeCommand<PythonEnvironment | undefined>(
'python-envs.createAny',
options,
);
if (result) {
return { path: result.environmentPath.path };
}
} catch (err) {
if (err === QuickInputButtons.Back) {
return { workspaceFolder: undefined, action: 'Back' };
}
throw err;
}
} else {
const providers = _createEnvironmentProviders.getAll();
return handleCreateEnvironmentCommand(providers, options);
}
return undefined;
},
),
registerCommand(
Commands.Create_Environment_Button,
async (): Promise<void> => {
sendTelemetryEvent(EventName.ENVIRONMENT_BUTTON, undefined, undefined);
await executeCommand(Commands.Create_Environment);
},
),
registerCreateEnvironmentProvider(new VenvCreationProvider(interpreterQuickPick)),
registerCreateEnvironmentProvider(condaCreationProvider()),
onCreateEnvironmentExited(async (e: EnvironmentDidCreateEvent) => {
if (e.path && e.options?.selectEnvironment) {
await interpreterPathService.update(
e.workspaceFolder?.uri,
ConfigurationTarget.WorkspaceFolder,
e.path,
);
showInformationMessage(`${CreateEnv.informEnvCreation} ${pathUtils.getDisplayName(e.path)}`);
}
}),
);
}
export function buildEnvironmentCreationApi(): ProposedCreateEnvironmentAPI {
return {
onWillCreateEnvironment: onCreateEnvironmentStarted,
onDidCreateEnvironment: onCreateEnvironmentExited,
createEnvironment: async (
options?: CreateEnvironmentOptions | undefined,
): Promise<CreateEnvironmentResult | undefined> => {
const providers = _createEnvironmentProviders.getAll();
try {
return await handleCreateEnvironmentCommand(providers, options);
} catch (err) {
return { path: undefined, workspaceFolder: undefined, action: undefined, error: err as Error };
}
},
registerCreateEnvironmentProvider: (provider: CreateEnvironmentProvider) =>
registerCreateEnvironmentProvider(provider),
};
}