forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
188 lines (167 loc) · 7.13 KB
/
index.ts
File metadata and controls
188 lines (167 loc) · 7.13 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as vscode from 'vscode';
import { getGlobalStorage } from '../common/persistentState';
import { getOSType, OSType } from '../common/utils/platform';
import { IDisposable } from '../common/utils/resourceLifecycle';
import { ActivationResult, ExtensionState } from '../components';
import { PythonEnvironments } from './api';
import { getPersistentCache } from './base/envsCache';
import { PythonEnvInfo } from './base/info';
import { ILocator } from './base/locator';
import { CachingLocator } from './base/locators/composite/cachingLocator';
import { PythonEnvsReducer } from './base/locators/composite/environmentsReducer';
import { PythonEnvsResolver } from './base/locators/composite/environmentsResolver';
import { WindowsPathEnvVarLocator } from './base/locators/lowLevel/windowsKnownPathsLocator';
import { WorkspaceVirtualEnvironmentLocator } from './base/locators/lowLevel/workspaceVirtualEnvLocator';
import { getEnvs } from './base/locatorUtils';
import { initializeExternalDependencies as initializeLegacyExternalDependencies } from './common/externalDependencies';
import { ExtensionLocators, WatchRootsArgs, WorkspaceLocators } from './discovery/locators';
import { CustomVirtualEnvironmentLocator } from './discovery/locators/services/customVirtualEnvLocator';
import { CondaEnvironmentLocator } from './discovery/locators/services/condaLocator';
import { GlobalVirtualEnvironmentLocator } from './discovery/locators/services/globalVirtualEnvronmentLocator';
import { PosixKnownPathsLocator } from './discovery/locators/services/posixKnownPathsLocator';
import { PyenvLocator } from './discovery/locators/services/pyenvLocator';
import { WindowsRegistryLocator } from './discovery/locators/services/windowsRegistryLocator';
import { WindowsStoreLocator } from './discovery/locators/services/windowsStoreLocator';
import { EnvironmentInfoService } from './info/environmentInfoService';
import { isComponentEnabled, registerLegacyDiscoveryForIOC, registerNewDiscoveryForIOC } from './legacyIOC';
import { EnvironmentsSecurity, IEnvironmentsSecurity } from './security';
import { PoetryLocator } from './discovery/locators/services/poetryLocator';
/**
* Set up the Python environments component (during extension activation).'
*/
export async function initialize(ext: ExtensionState): Promise<PythonEnvironments> {
const environmentsSecurity = new EnvironmentsSecurity();
const api = new PythonEnvironments(
() => createLocators(ext, environmentsSecurity),
// Other sub-commonents (e.g. config, "current" env) will go here.
);
ext.disposables.push(api);
// Any other initialization goes here.
initializeLegacyExternalDependencies(ext.legacyIOC.serviceContainer);
registerNewDiscoveryForIOC(
// These are what get wrapped in the legacy adapter.
ext.legacyIOC.serviceManager,
api,
environmentsSecurity,
);
// Deal with legacy IOC.
await registerLegacyDiscoveryForIOC(ext.legacyIOC.serviceManager);
return api;
}
/**
* Make use of the component (e.g. register with VS Code).
*/
export async function activate(api: PythonEnvironments): Promise<ActivationResult> {
if (!(await isComponentEnabled())) {
return {
fullyReady: Promise.resolve(),
};
}
// Force an initial background refresh of the environments.
getEnvs(api.iterEnvs())
// Don't wait for it to finish.
.ignoreErrors();
// Registration with VS Code will go here.
return {
fullyReady: Promise.resolve(),
};
}
/**
* Get the set of locators to use in the component.
*/
async function createLocators(
ext: ExtensionState,
// This is shared.
environmentsSecurity: IEnvironmentsSecurity,
): Promise<ILocator> {
// Create the low-level locators.
let locators: ILocator = new ExtensionLocators(
// Here we pull the locators together.
createNonWorkspaceLocators(ext),
createWorkspaceLocator(ext),
);
// Create the env info service used by ResolvingLocator and CachingLocator.
const envInfoService = new EnvironmentInfoService();
ext.disposables.push(envInfoService);
// Build the stack of composite locators.
locators = new PythonEnvsReducer(locators);
locators = new PythonEnvsResolver(
locators,
// These are shared.
envInfoService,
// Class methods may depend on other properties which belong to the class, so bind the correct context.
environmentsSecurity.isEnvSafe.bind(environmentsSecurity),
);
const caching = await createCachingLocator(
ext,
// This is shared.
locators,
);
ext.disposables.push(caching);
locators = caching;
return locators;
}
function createNonWorkspaceLocators(ext: ExtensionState): ILocator[] {
const locators: (ILocator & Partial<IDisposable>)[] = [];
locators.push(
// OS-independent locators go here.
new PyenvLocator(),
new CondaEnvironmentLocator(),
new GlobalVirtualEnvironmentLocator(),
new CustomVirtualEnvironmentLocator(),
);
if (getOSType() === OSType.Windows) {
locators.push(
// Windows specific locators go here.
new WindowsRegistryLocator(),
new WindowsStoreLocator(),
new WindowsPathEnvVarLocator(),
);
} else {
locators.push(
// Linux/Mac locators go here.
new PosixKnownPathsLocator(),
);
}
const disposables = locators.filter((d) => d.dispose !== undefined) as IDisposable[];
ext.disposables.push(...disposables);
return locators;
}
function watchRoots(args: WatchRootsArgs): IDisposable {
const { initRoot, addRoot, removeRoot } = args;
const folders = vscode.workspace.workspaceFolders;
if (folders) {
folders.map((f) => f.uri).forEach(initRoot);
}
return vscode.workspace.onDidChangeWorkspaceFolders((event) => {
for (const root of event.removed) {
removeRoot(root.uri);
}
for (const root of event.added) {
addRoot(root.uri);
}
});
}
function createWorkspaceLocator(ext: ExtensionState): WorkspaceLocators {
const locators = new WorkspaceLocators(watchRoots, [
(root: vscode.Uri) => [new WorkspaceVirtualEnvironmentLocator(root.fsPath), new PoetryLocator(root.fsPath)],
// Add an ILocator factory func here for each kind of workspace-rooted locator.
]);
ext.disposables.push(locators);
return locators;
}
async function createCachingLocator(ext: ExtensionState, locators: ILocator): Promise<CachingLocator> {
const storage = getGlobalStorage<PythonEnvInfo[]>(ext.context, 'PYTHON_ENV_INFO_CACHE');
const cache = await getPersistentCache(
{
load: async () => storage.get(),
store: async (e) => storage.set(e),
},
// For now we assume that if when iteration is complete, the env is as complete as it's going to get.
// So no further check for complete environments is needed.
() => true, // "isComplete"
);
return new CachingLocator(cache, locators);
}