forked from DonJayamanne/vscode-python-manager
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreateEnvironment.ts
More file actions
203 lines (185 loc) · 6.93 KB
/
Copy pathcreateEnvironment.ts
File metadata and controls
203 lines (185 loc) · 6.93 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License
import { Event, EventEmitter, QuickInputButtons, QuickPickItem } from 'vscode';
import { CreateEnv } from '../../common/utils/localize';
import {
MultiStepAction,
MultiStepNode,
showQuickPick,
showQuickPickWithBack,
} from '../../common/vscodeApis/windowApis';
import { traceError, traceVerbose } from '../../logging';
import {
CreateEnvironmentOptions,
CreateEnvironmentResult,
CreateEnvironmentProvider,
EnvironmentWillCreateEvent,
EnvironmentDidCreateEvent,
} from './proposed.createEnvApis';
const onCreateEnvironmentStartedEvent = new EventEmitter<EnvironmentWillCreateEvent>();
const onCreateEnvironmentExitedEvent = new EventEmitter<EnvironmentDidCreateEvent>();
let startedEventCount = 0;
function isBusyCreatingEnvironment(): boolean {
return startedEventCount > 0;
}
function fireStartedEvent(options?: CreateEnvironmentOptions): void {
onCreateEnvironmentStartedEvent.fire({ options });
startedEventCount += 1;
}
function fireExitedEvent(result?: CreateEnvironmentResult, options?: CreateEnvironmentOptions, error?: Error): void {
startedEventCount -= 1;
if (result) {
onCreateEnvironmentExitedEvent.fire({ options, ...result });
} else if (error) {
onCreateEnvironmentExitedEvent.fire({ options, error });
}
}
export function getCreationEvents(): {
onCreateEnvironmentStarted: Event<EnvironmentWillCreateEvent>;
onCreateEnvironmentExited: Event<EnvironmentDidCreateEvent>;
isCreatingEnvironment: () => boolean;
} {
return {
onCreateEnvironmentStarted: onCreateEnvironmentStartedEvent.event,
onCreateEnvironmentExited: onCreateEnvironmentExitedEvent.event,
isCreatingEnvironment: isBusyCreatingEnvironment,
};
}
async function createEnvironment(
provider: CreateEnvironmentProvider,
options: CreateEnvironmentOptions,
): Promise<CreateEnvironmentResult | undefined> {
let result: CreateEnvironmentResult | undefined;
let err: Error | undefined;
try {
fireStartedEvent(options);
result = await provider.createEnvironment(options);
} catch (ex) {
if (ex === QuickInputButtons.Back) {
traceVerbose('Create Env: User clicked back button during environment creation');
if (!options.showBackButton) {
return undefined;
}
}
err = ex as Error;
throw err;
} finally {
fireExitedEvent(result, options, err);
}
return result;
}
interface CreateEnvironmentProviderQuickPickItem extends QuickPickItem {
id: string;
}
async function showCreateEnvironmentQuickPick(
providers: readonly CreateEnvironmentProvider[],
options?: CreateEnvironmentOptions,
): Promise<CreateEnvironmentProvider | undefined> {
const items: CreateEnvironmentProviderQuickPickItem[] = providers.map((p) => ({
label: p.name,
description: p.description,
id: p.id,
}));
let selectedItem: CreateEnvironmentProviderQuickPickItem | CreateEnvironmentProviderQuickPickItem[] | undefined;
if (options?.showBackButton) {
selectedItem = await showQuickPickWithBack(items, {
placeHolder: CreateEnv.providersQuickPickPlaceholder,
matchOnDescription: true,
ignoreFocusOut: true,
});
} else {
selectedItem = await showQuickPick(items, {
placeHolder: CreateEnv.providersQuickPickPlaceholder,
matchOnDescription: true,
ignoreFocusOut: true,
});
}
if (selectedItem) {
const selected = Array.isArray(selectedItem) ? selectedItem[0] : selectedItem;
if (selected) {
const selections = providers.filter((p) => p.id === selected.id);
if (selections.length > 0) {
return selections[0];
}
}
}
return undefined;
}
function getOptionsWithDefaults(options?: CreateEnvironmentOptions): CreateEnvironmentOptions {
return {
installPackages: true,
ignoreSourceControl: true,
showBackButton: false,
selectEnvironment: true,
...options,
};
}
export async function handleCreateEnvironmentCommand(
providers: readonly CreateEnvironmentProvider[],
options?: CreateEnvironmentOptions,
): Promise<CreateEnvironmentResult | undefined> {
const optionsWithDefaults = getOptionsWithDefaults(options);
if (!optionsWithDefaults.showBackButton && providers.length === 1) {
optionsWithDefaults.showBackButton = false;
}
let selectedProvider: CreateEnvironmentProvider | undefined = providers.length === 1 ? providers[0] : undefined;
const envTypeStep = new MultiStepNode(
undefined,
async (context?: MultiStepAction) => {
if (providers.length > 0) {
try {
selectedProvider = await showCreateEnvironmentQuickPick(providers, optionsWithDefaults);
} catch (ex) {
if (ex === MultiStepAction.Back || ex === MultiStepAction.Cancel) {
return ex;
}
throw ex;
}
if (!selectedProvider) {
return MultiStepAction.Cancel;
}
} else {
traceError('No Environment Creation providers were registered.');
if (context === MultiStepAction.Back) {
// There are no providers to select, so just step back.
return MultiStepAction.Back;
}
}
return MultiStepAction.Continue;
},
undefined,
);
let result: CreateEnvironmentResult | undefined;
const createStep = new MultiStepNode(
providers.length === 1 ? undefined : envTypeStep,
async (context?: MultiStepAction) => {
if (context === MultiStepAction.Back) {
// This step is to trigger creation, which can go into other extension.
return MultiStepAction.Back;
}
if (selectedProvider) {
try {
result = await createEnvironment(selectedProvider, optionsWithDefaults);
} catch (ex) {
if (ex === MultiStepAction.Back || ex === MultiStepAction.Cancel) {
return ex;
}
throw ex;
}
}
return MultiStepAction.Continue;
},
undefined,
);
envTypeStep.next = createStep;
const action = await MultiStepNode.run(providers.length === 1 ? createStep : envTypeStep);
if (options?.showBackButton) {
if (action === MultiStepAction.Back || action === MultiStepAction.Cancel) {
result = { action, workspaceFolder: undefined, path: undefined, error: undefined };
}
}
if (result) {
return Object.freeze(result);
}
return undefined;
}