forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathproposedApi.ts
112 lines (105 loc) · 4.72 KB
/
proposedApi.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
110
111
112
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { ConfigurationTarget, EventEmitter } from 'vscode';
import {
ActiveEnvironmentChangedParams,
EnvironmentDetails,
EnvironmentDetailsOptions,
EnvironmentsChangedParams,
IProposedExtensionAPI,
RefreshEnvironmentsOptions,
} from './apiTypes';
import { arePathsSame } from './common/platform/fs-paths';
import { IInterpreterPathService, Resource } from './common/types';
import { IInterpreterService } from './interpreter/contracts';
import { IServiceContainer } from './ioc/types';
import { PythonEnvInfo } from './pythonEnvironments/base/info';
import { getEnvPath } from './pythonEnvironments/base/info/env';
import { IDiscoveryAPI } from './pythonEnvironments/base/locator';
const onDidInterpretersChangedEvent = new EventEmitter<EnvironmentsChangedParams[]>();
export function reportInterpretersChanged(e: EnvironmentsChangedParams[]): void {
onDidInterpretersChangedEvent.fire(e);
}
const onDidActiveInterpreterChangedEvent = new EventEmitter<ActiveEnvironmentChangedParams>();
export function reportActiveInterpreterChanged(e: ActiveEnvironmentChangedParams): void {
onDidActiveInterpreterChangedEvent.fire(e);
}
function getVersionString(env: PythonEnvInfo): string[] {
const ver = [`${env.version.major}`, `${env.version.minor}`, `${env.version.micro}`];
if (env.version.release) {
ver.push(`${env.version.release}`);
if (env.version.sysVersion) {
ver.push(`${env.version.release}`);
}
}
return ver;
}
/**
* Returns whether the path provided matches the environment.
* @param path Path to environment folder or path to interpreter that uniquely identifies an environment.
* @param env Environment to match with.
*/
function isEnvSame(path: string, env: PythonEnvInfo) {
return arePathsSame(path, env.location) || arePathsSame(path, env.executable.filename);
}
export function buildProposedApi(
discoveryApi: IDiscoveryAPI,
serviceContainer: IServiceContainer,
): IProposedExtensionAPI {
const interpreterPathService = serviceContainer.get<IInterpreterPathService>(IInterpreterPathService);
const interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
const proposed: IProposedExtensionAPI = {
environment: {
async getActiveEnvironmentPath(resource?: Resource) {
const env = await interpreterService.getActiveInterpreter(resource);
if (!env) {
return undefined;
}
return getEnvPath(env.path, env.envPath);
},
async getEnvironmentDetails(
path: string,
options?: EnvironmentDetailsOptions,
): Promise<EnvironmentDetails | undefined> {
let env: PythonEnvInfo | undefined;
if (options?.useCache) {
env = discoveryApi.getEnvs().find((v) => isEnvSame(path, v));
}
if (!env) {
env = await discoveryApi.resolveEnv(path);
if (!env) {
return undefined;
}
}
return {
interpreterPath: env.executable.filename,
envFolderPath: env.location.length ? env.location : undefined,
version: getVersionString(env),
environmentType: [env.kind],
metadata: {
sysPrefix: env.executable.sysPrefix,
bitness: env.arch,
},
};
},
getEnvironmentPaths() {
const paths = discoveryApi.getEnvs().map((e) => getEnvPath(e.executable.filename, e.location));
return Promise.resolve(paths);
},
setActiveEnvironment(path: string, resource?: Resource): Promise<void> {
return interpreterPathService.update(resource, ConfigurationTarget.WorkspaceFolder, path);
},
async refreshEnvironment(options?: RefreshEnvironmentsOptions) {
await discoveryApi.triggerRefresh(options ? { clearCache: options.clearCache } : undefined);
const paths = discoveryApi.getEnvs().map((e) => getEnvPath(e.executable.filename, e.location));
return Promise.resolve(paths);
},
getRefreshPromise(): Promise<void> | undefined {
return discoveryApi.refreshPromise;
},
onDidEnvironmentsChanged: onDidInterpretersChangedEvent.event,
onDidActiveEnvironmentChanged: onDidActiveInterpreterChangedEvent.event,
},
};
return proposed;
}