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
142 lines (124 loc) · 5.96 KB
/
index.ts
File metadata and controls
142 lines (124 loc) · 5.96 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
// 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 { 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 { 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 { registerLegacyDiscoveryForIOC, registerNewDiscoveryForIOC } from './legacyIOC';
import { EnvironmentsSecurity, IEnvironmentsSecurity } from './security';
/**
* Set up the Python environments component (during extension activation).'
*/
export function initialize(ext: ExtensionState): 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.
// Deal with legacy IOC.
registerLegacyDiscoveryForIOC(ext.legacyIOC.serviceManager);
initializeLegacyExternalDependencies(ext.legacyIOC.serviceContainer);
registerNewDiscoveryForIOC(ext.legacyIOC.serviceManager, api, environmentsSecurity, ext.disposables);
return api;
}
/**
* Make use of the component (e.g. register with VS Code).
*/
export async function activate(api: PythonEnvironments): Promise<ActivationResult> {
// Force an initial background refresh of the environments.
getEnvs(api.iterEnvs()).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, environmentsSecurity: IEnvironmentsSecurity): Promise<ILocator> {
// Create the low-level locators.
let locators: ILocator = new ExtensionLocators(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, envInfoService, environmentsSecurity.isEnvSafe);
const caching = await createCachingLocator(ext, envInfoService, locators);
ext.disposables.push(caching);
locators = caching;
return locators;
}
function createNonWorkspaceLocators(ext: ExtensionState): ILocator[] {
let locators: (ILocator & Partial<IDisposable>)[];
if (getOSType() === OSType.Windows) {
// Windows specific locators go here
locators = [new WindowsRegistryLocator(), new WindowsStoreLocator()];
} else {
// Linux/Mac locators go here
locators = [new PosixKnownPathsLocator()];
}
locators.push(new GlobalVirtualEnvironmentLocator(), new PyenvLocator(), new CustomVirtualEnvironmentLocator());
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)],
// Add an ILocator factory func here for each kind of workspace-rooted locator.
]);
ext.disposables.push(locators);
return locators;
}
async function createCachingLocator(
ext: ExtensionState,
envInfoService: EnvironmentInfoService,
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),
},
(env: PythonEnvInfo) => envInfoService.isInfoProvided(env.executable.filename), // "isComplete"
);
return new CachingLocator(cache, locators);
}