-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathinternal.api.ts
More file actions
409 lines (359 loc) · 14.1 KB
/
internal.api.ts
File metadata and controls
409 lines (359 loc) · 14.1 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import { CancellationError, Disposable, Event, LogOutputChannel, MarkdownString, Uri } from 'vscode';
import {
CreateEnvironmentOptions,
CreateEnvironmentScope,
DidChangeEnvironmentEventArgs,
DidChangeEnvironmentsEventArgs,
DidChangePackagesEventArgs,
EnvironmentGroupInfo,
EnvironmentManager,
GetEnvironmentScope,
GetEnvironmentsScope,
IconPath,
Package,
PackageChangeKind,
PackageId,
PackageInfo,
PackageManagementOptions,
PackageManager,
PythonEnvironment,
PythonEnvironmentExecutionInfo,
PythonEnvironmentId,
PythonEnvironmentInfo,
PythonProject,
PythonProjectCreator,
QuickCreateConfig,
RefreshEnvironmentsScope,
ResolveEnvironmentContext,
SetEnvironmentScope,
} from './api';
import { CreateEnvironmentNotSupported, RemoveEnvironmentNotSupported } from './common/errors/NotSupportedError';
import { EventNames } from './common/telemetry/constants';
import { sendTelemetryEvent } from './common/telemetry/sender';
export type EnvironmentManagerScope = undefined | string | Uri | PythonEnvironment;
export type PackageManagerScope = undefined | string | Uri | PythonEnvironment | Package;
export interface PackageEventArg {
package: Package;
manager: InternalPackageManager;
environment: PythonEnvironment;
}
export type PackageCommandOptions =
| {
uri: Uri;
packages?: string[];
}
| {
packageManager: PackageManager;
environment: PythonEnvironment;
packages?: string[];
};
export interface DidChangeEnvironmentManagerEventArgs {
kind: 'registered' | 'unregistered';
manager: InternalEnvironmentManager;
}
export interface DidChangePackageManagerEventArgs {
kind: 'registered' | 'unregistered';
manager: InternalPackageManager;
}
export interface InternalDidChangePackagesEventArgs {
environment: PythonEnvironment;
manager: InternalPackageManager;
changes: { kind: PackageChangeKind; pkg: Package }[];
}
export interface InternalDidChangeEnvironmentsEventArgs {
manager: InternalEnvironmentManager;
changes: DidChangeEnvironmentsEventArgs;
}
export interface EnvironmentManagers extends Disposable {
registerEnvironmentManager(manager: EnvironmentManager): Disposable;
registerPackageManager(manager: PackageManager): Disposable;
/**
* This event is fired when any environment manager changes its collection of environments.
* This can be any environment manager even if it is not the one selected by the user for the workspace.
*/
onDidChangeEnvironments: Event<InternalDidChangeEnvironmentsEventArgs>;
/**
* This event is fired when an environment manager changes the environment for
* a particular scope (global, uri, workspace, etc). This can be any environment manager even if it is not the
* one selected by the user for the workspace. It is also fired if the change
* involves unselected to selected or selected to unselected.
*/
onDidChangeEnvironment: Event<DidChangeEnvironmentEventArgs>;
/**
* This event is fired when a selected environment manager changes the environment
* for a particular scope (global, uri, workspace, etc). This is also only fired if
* the previous and current environments are different. It is also fired if the change
* involves unselected to selected or selected to unselected.
*/
onDidChangeEnvironmentFiltered: Event<DidChangeEnvironmentEventArgs>;
onDidChangePackages: Event<InternalDidChangePackagesEventArgs>;
onDidChangeEnvironmentManager: Event<DidChangeEnvironmentManagerEventArgs>;
onDidChangePackageManager: Event<DidChangePackageManagerEventArgs>;
getEnvironmentManager(scope: EnvironmentManagerScope): InternalEnvironmentManager | undefined;
getPackageManager(scope: PackageManagerScope): InternalPackageManager | undefined;
managers: InternalEnvironmentManager[];
packageManagers: InternalPackageManager[];
clearCache(scope: EnvironmentManagerScope): Promise<void>;
/**
* Sets the environment for a scope.
* @param scope - The scope to set the environment for
* @param environment - The environment to set (optional)
* @param shouldPersistSettings - Whether to persist to settings.json (default: true)
*/
setEnvironment(
scope: SetEnvironmentScope,
environment?: PythonEnvironment,
shouldPersistSettings?: boolean,
): Promise<void>;
/**
* Sets environments for multiple scopes.
* @param scope - Array of URIs or 'global'
* @param environment - The environment to set (optional)
* @param shouldPersistSettings - Whether to persist to settings.json (default: true)
*/
setEnvironments(
scope: Uri[] | string,
environment?: PythonEnvironment,
shouldPersistSettings?: boolean,
): Promise<void>;
setEnvironmentsIfUnset(scope: Uri[] | string, environment?: PythonEnvironment): Promise<void>;
getEnvironment(scope: GetEnvironmentScope): Promise<PythonEnvironment | undefined>;
getProjectEnvManagers(uris: Uri[]): InternalEnvironmentManager[];
}
export class InternalEnvironmentManager implements EnvironmentManager {
public constructor(
public readonly id: string,
private readonly manager: EnvironmentManager,
) {}
public get name(): string {
return this.manager.name;
}
public get displayName(): string {
return this.manager.displayName ?? this.name;
}
public get preferredPackageManagerId(): string {
return this.manager.preferredPackageManagerId;
}
public get description(): string | undefined {
return this.manager.description;
}
public get tooltip(): string | MarkdownString | undefined {
return this.manager.tooltip;
}
public get iconPath(): IconPath | undefined {
return this.manager.iconPath;
}
public get log(): LogOutputChannel | undefined {
return this.manager.log;
}
public get supportsCreate(): boolean {
return this.manager.create !== undefined;
}
create(
scope: CreateEnvironmentScope,
options: CreateEnvironmentOptions | undefined,
): Promise<PythonEnvironment | undefined> {
if (this.manager.create) {
return this.manager.create(scope, options);
}
return Promise.reject(new CreateEnvironmentNotSupported(`Create Environment not supported by: ${this.id}`));
}
public get supportsQuickCreate(): boolean {
return this.manager.quickCreateConfig !== undefined && this.manager.create !== undefined;
}
quickCreateConfig(): QuickCreateConfig | undefined {
if (this.manager.quickCreateConfig && this.manager.create) {
return this.manager.quickCreateConfig();
}
throw new CreateEnvironmentNotSupported(`Quick Create Environment not supported by: ${this.id}`);
}
public get supportsRemove(): boolean {
return this.manager.remove !== undefined;
}
remove(scope: PythonEnvironment): Promise<void> {
return this.manager.remove
? this.manager.remove(scope)
: Promise.reject(new RemoveEnvironmentNotSupported(`Remove Environment not supported by: ${this.id}`));
}
refresh(options: RefreshEnvironmentsScope): Promise<void> {
return this.manager.refresh(options);
}
getEnvironments(options: GetEnvironmentsScope): Promise<PythonEnvironment[]> {
return this.manager.getEnvironments(options);
}
onDidChangeEnvironments(handler: (e: DidChangeEnvironmentsEventArgs) => void): Disposable {
return this.manager.onDidChangeEnvironments
? this.manager.onDidChangeEnvironments(handler)
: new Disposable(() => {});
}
set(scope: SetEnvironmentScope, environment?: PythonEnvironment): Promise<void> {
return this.manager.set(scope, environment);
}
get(scope: GetEnvironmentScope): Promise<PythonEnvironment | undefined> {
return this.manager.get(scope);
}
onDidChangeEnvironment(handler: (e: DidChangeEnvironmentEventArgs) => void): Disposable {
return this.manager.onDidChangeEnvironment
? this.manager.onDidChangeEnvironment(handler)
: new Disposable(() => {});
}
resolve(context: ResolveEnvironmentContext): Promise<PythonEnvironment | undefined> {
return this.manager.resolve(context);
}
public equals(other: EnvironmentManager): boolean {
return this.manager === other;
}
public supportsClearCache(): boolean {
return this.manager.clearCache !== undefined;
}
public clearCache(): Promise<void> {
return this.manager.clearCache ? this.manager.clearCache() : Promise.resolve();
}
}
export class InternalPackageManager implements PackageManager {
public constructor(
public readonly id: string,
private readonly manager: PackageManager,
) {}
public get name(): string {
return this.manager.name;
}
public get displayName(): string {
return this.manager.displayName ?? this.name;
}
public get description(): string | undefined {
return this.manager.description;
}
public get tooltip(): string | MarkdownString | undefined {
return this.manager.tooltip;
}
public get iconPath(): IconPath | undefined {
return this.manager.iconPath;
}
public get log(): LogOutputChannel | undefined {
return this.manager.log;
}
async manage(environment: PythonEnvironment, options: PackageManagementOptions): Promise<void> {
try {
await this.manager.manage(environment, options);
sendTelemetryEvent(EventNames.PACKAGE_MANAGEMENT, undefined, { managerId: this.id, result: 'success' });
} catch (error) {
if (error instanceof CancellationError) {
sendTelemetryEvent(EventNames.PACKAGE_MANAGEMENT, undefined, {
managerId: this.id,
result: 'cancelled',
});
throw error;
}
sendTelemetryEvent(EventNames.PACKAGE_MANAGEMENT, undefined, { managerId: this.id, result: 'error' });
throw error;
}
}
refresh(environment: PythonEnvironment): Promise<void> {
return this.manager.refresh(environment);
}
getPackages(environment: PythonEnvironment): Promise<Package[] | undefined> {
return this.manager.getPackages(environment);
}
onDidChangePackages(handler: (e: DidChangePackagesEventArgs) => void): Disposable {
return this.manager.onDidChangePackages ? this.manager.onDidChangePackages(handler) : new Disposable(() => {});
}
equals(other: PackageManager): boolean {
return this.manager === other;
}
}
export interface PythonProjectManager extends Disposable {
initialize(): void;
create(
name: string,
uri: Uri,
options?: { description?: string; tooltip?: string | MarkdownString; iconPath?: IconPath },
): PythonProject;
add(pyWorkspace: PythonProject | PythonProject[]): Promise<void>;
remove(pyWorkspace: PythonProject | PythonProject[]): void;
getProjects(uris?: Uri[]): ReadonlyArray<PythonProject>;
get(uri: Uri): PythonProject | undefined;
onDidChangeProjects: Event<PythonProject[] | undefined>;
}
export interface PythonProjectSettings {
path: string;
envManager: string;
packageManager: string;
workspace?: string;
}
export class PythonEnvironmentImpl implements PythonEnvironment {
public readonly name: string;
public readonly displayName: string;
public readonly shortDisplayName?: string;
public readonly displayPath: string;
public readonly version: string;
public readonly environmentPath: Uri;
public readonly description?: string;
public readonly tooltip?: string | MarkdownString;
public readonly iconPath?: IconPath;
public readonly execInfo: PythonEnvironmentExecutionInfo;
public readonly sysPrefix: string;
public readonly group?: string | EnvironmentGroupInfo;
public readonly error?: string;
constructor(
public readonly envId: PythonEnvironmentId,
info: PythonEnvironmentInfo,
) {
this.name = info.name;
this.displayName = info.displayName ?? this.name;
this.shortDisplayName = info.shortDisplayName;
this.displayPath = info.displayPath;
this.version = info.version;
this.environmentPath = info.environmentPath;
this.description = info.description;
this.tooltip = info.tooltip;
this.iconPath = info.iconPath;
this.execInfo = info.execInfo;
this.sysPrefix = info.sysPrefix;
this.group = info.group;
this.error = info.error;
}
}
export class PythonPackageImpl implements Package {
public readonly name: string;
public readonly displayName: string;
public readonly version?: string;
public readonly description?: string;
public readonly tooltip?: string | MarkdownString;
public readonly iconPath?: IconPath;
public readonly uris?: readonly Uri[];
constructor(
public readonly pkgId: PackageId,
info: PackageInfo,
) {
this.name = info.name;
this.displayName = info.displayName ?? this.name;
this.version = info.version;
this.description = info.description;
this.tooltip = info.tooltip;
this.iconPath = info.iconPath;
this.uris = info.uris;
}
}
export class PythonProjectsImpl implements PythonProject {
name: string;
uri: Uri;
description?: string;
tooltip?: string | MarkdownString;
iconPath?: IconPath;
constructor(
name: string,
uri: Uri,
options?: { description?: string; tooltip?: string | MarkdownString; iconPath?: IconPath },
) {
this.name = name;
this.uri = uri;
this.description = options?.description ?? uri.fsPath;
this.tooltip = options?.tooltip ?? uri.fsPath;
this.iconPath = options?.iconPath;
}
}
export interface ProjectCreators extends Disposable {
registerPythonProjectCreator(creator: PythonProjectCreator): Disposable;
getProjectCreators(): PythonProjectCreator[];
}