forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
70 lines (58 loc) · 2.26 KB
/
api.ts
File metadata and controls
70 lines (58 loc) · 2.26 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { StopWatch } from '../common/utils/stopWatch';
import { sendTelemetryEvent } from '../telemetry';
import { EventName } from '../telemetry/constants';
import { getEnvPath } from './base/info/env';
import { IDiscoveryAPI, PythonLocatorQuery } from './base/locator';
export type GetLocatorFunc = () => Promise<IDiscoveryAPI>;
/**
* The public API for the Python environments component.
*
* Note that this is composed of sub-components.
*/
class PythonEnvironments implements IDiscoveryAPI {
private locator!: IDiscoveryAPI;
constructor(
// These are factories for the sub-components the full component is composed of:
private readonly getLocator: GetLocatorFunc,
) {}
public async activate(): Promise<void> {
this.locator = await this.getLocator();
}
public get onRefreshStart() {
return this.locator.onRefreshStart;
}
public get refreshPromise() {
return this.locator.refreshPromise;
}
public get onChanged() {
return this.locator.onChanged;
}
public getEnvs(query?: PythonLocatorQuery) {
return this.locator.getEnvs(query);
}
public async resolveEnv(env: string) {
return this.locator.resolveEnv(env);
}
public async triggerRefresh(query?: PythonLocatorQuery, trigger?: 'auto' | 'ui') {
const stopWatch = new StopWatch();
await this.locator.triggerRefresh(query);
if (!query) {
// Intent is to capture time taken for all of discovery to complete, so make sure
// all interpreters are queried for.
sendTelemetryEvent(EventName.PYTHON_INTERPRETER_DISCOVERY, stopWatch.elapsedTime, {
interpreters: this.getEnvs().length,
environmentsWithoutPython: this.getEnvs().filter(
(e) => getEnvPath(e.executable.filename, e.location).pathType === 'envFolderPath',
).length,
trigger: trigger ?? 'auto',
});
}
}
}
export async function createPythonEnvironments(getLocator: GetLocatorFunc): Promise<IDiscoveryAPI> {
const api = new PythonEnvironments(getLocator);
await api.activate();
return api;
}