22// Licensed under the MIT License.
33
44import * as vscode from 'vscode' ;
5- import { Uri } from 'vscode' ;
6- import { cloneDeep } from 'lodash' ;
7- import { getGlobalStorage , IPersistentStorage } from '../common/persistentState' ;
8- import { getOSType , OSType } from '../common/utils/platform' ;
5+ import { getGlobalStorage } from '../common/persistentState' ;
96import { ActivationResult , ExtensionState } from '../components' ;
107import { PythonEnvInfo } from './base/info' ;
11- import { BasicEnvInfo , IDiscoveryAPI , ILocator } from './base/locator' ;
12- import { PythonEnvsReducer } from './base/locators/composite/envsReducer' ;
13- import { PythonEnvsResolver } from './base/locators/composite/envsResolver' ;
14- import { WindowsPathEnvVarLocator } from './base/locators/lowLevel/windowsKnownPathsLocator' ;
15- import { WorkspaceVirtualEnvironmentLocator } from './base/locators/lowLevel/workspaceVirtualEnvLocator' ;
8+ import { IDiscoveryAPI } from './base/locator' ;
169import {
1710 initializeExternalDependencies as initializeLegacyExternalDependencies ,
1811 normCasePath ,
1912} from './common/externalDependencies' ;
20- import { ExtensionLocators , WatchRootsArgs , WorkspaceLocators } from './base/locators/wrappers' ;
21- import { CustomVirtualEnvironmentLocator } from './base/locators/lowLevel/customVirtualEnvLocator' ;
22- import { CondaEnvironmentLocator } from './base/locators/lowLevel/condaLocator' ;
23- import { GlobalVirtualEnvironmentLocator } from './base/locators/lowLevel/globalVirtualEnvronmentLocator' ;
24- import { PosixKnownPathsLocator } from './base/locators/lowLevel/posixKnownPathsLocator' ;
25- import { PyenvLocator } from './base/locators/lowLevel/pyenvLocator' ;
26- import { WindowsRegistryLocator } from './base/locators/lowLevel/windowsRegistryLocator' ;
27- import { MicrosoftStoreLocator } from './base/locators/lowLevel/microsoftStoreLocator' ;
28- import { getEnvironmentInfoService } from './base/info/environmentInfoService' ;
2913import { registerNewDiscoveryForIOC } from './legacyIOC' ;
30- import { PoetryLocator } from './base/locators/lowLevel/poetryLocator' ;
31- import { HatchLocator } from './base/locators/lowLevel/hatchLocator' ;
32- import { createPythonEnvironments } from './api' ;
33- import {
34- createCollectionCache as createCache ,
35- IEnvsCollectionCache ,
36- } from './base/locators/composite/envsCollectionCache' ;
37- import { EnvsCollectionService } from './base/locators/composite/envsCollectionService' ;
38- import { IDisposable } from '../common/types' ;
39- import { traceError } from '../logging' ;
40- import { ActiveStateLocator } from './base/locators/lowLevel/activeStateLocator' ;
41- import { CustomWorkspaceLocator } from './base/locators/lowLevel/customWorkspaceLocator' ;
42- import { PixiLocator } from './base/locators/lowLevel/pixiLocator' ;
43- import { getConfiguration } from '../common/vscodeApis/workspaceApis' ;
4414import { getNativePythonFinder } from './base/locators/common/nativePythonFinder' ;
4515import { createNativeEnvironmentsApi } from './nativeAPI' ;
4616import { useEnvExtension } from '../envExt/api.internal' ;
4717import { createEnvExtApi } from '../envExt/envExtApi' ;
4818
4919const PYTHON_ENV_INFO_CACHE_KEY = 'PYTHON_ENV_INFO_CACHEv2' ;
5020
51- export function shouldUseNativeLocator ( ) : boolean {
52- const config = getConfiguration ( 'python' ) ;
53- return config . get < string > ( 'locator' , 'js' ) === 'native' ;
54- }
55-
5621/**
5722 * Set up the Python environments component (during extension activation).'
5823 */
@@ -70,19 +35,9 @@ export async function initialize(ext: ExtensionState): Promise<IDiscoveryAPI> {
7035 return api ;
7136 }
7237
73- if ( shouldUseNativeLocator ( ) ) {
74- const finder = getNativePythonFinder ( ext . context ) ;
75- const api = createNativeEnvironmentsApi ( finder ) ;
76- ext . disposables . push ( api ) ;
77- registerNewDiscoveryForIOC (
78- // These are what get wrapped in the legacy adapter.
79- ext . legacyIOC . serviceManager ,
80- api ,
81- ) ;
82- return api ;
83- }
84-
85- const api = await createPythonEnvironments ( ( ) => createLocator ( ext ) ) ;
38+ const finder = getNativePythonFinder ( ext . context ) ;
39+ const api = createNativeEnvironmentsApi ( finder ) ;
40+ ext . disposables . push ( api ) ;
8641 registerNewDiscoveryForIOC (
8742 // These are what get wrapped in the legacy adapter.
8843 ext . legacyIOC . serviceManager ,
@@ -136,138 +91,3 @@ export async function activate(api: IDiscoveryAPI, ext: ExtensionState): Promise
13691 fullyReady : Promise . resolve ( ) ,
13792 } ;
13893}
139-
140- /**
141- * Get the locator to use in the component.
142- */
143- async function createLocator (
144- ext : ExtensionState ,
145- // This is shared.
146- ) : Promise < IDiscoveryAPI > {
147- // Create the low-level locators.
148- const locators : ILocator < BasicEnvInfo > = new ExtensionLocators < BasicEnvInfo > (
149- // Here we pull the locators together.
150- createNonWorkspaceLocators ( ext ) ,
151- createWorkspaceLocator ( ext ) ,
152- ) ;
153-
154- // Create the env info service used by ResolvingLocator and CachingLocator.
155- const envInfoService = getEnvironmentInfoService ( ext . disposables ) ;
156-
157- // Build the stack of composite locators.
158- const reducer = new PythonEnvsReducer ( locators ) ;
159- const resolvingLocator = new PythonEnvsResolver (
160- reducer ,
161- // These are shared.
162- envInfoService ,
163- ) ;
164- const caching = new EnvsCollectionService (
165- await createCollectionCache ( ext ) ,
166- // This is shared.
167- resolvingLocator ,
168- shouldUseNativeLocator ( ) ,
169- ) ;
170- return caching ;
171- }
172-
173- function createNonWorkspaceLocators ( ext : ExtensionState ) : ILocator < BasicEnvInfo > [ ] {
174- const locators : ( ILocator < BasicEnvInfo > & Partial < IDisposable > ) [ ] = [ ] ;
175- locators . push (
176- // OS-independent locators go here.
177- new PyenvLocator ( ) ,
178- new CondaEnvironmentLocator ( ) ,
179- new ActiveStateLocator ( ) ,
180- new GlobalVirtualEnvironmentLocator ( ) ,
181- new CustomVirtualEnvironmentLocator ( ) ,
182- ) ;
183-
184- if ( getOSType ( ) === OSType . Windows ) {
185- locators . push (
186- // Windows specific locators go here.
187- new WindowsRegistryLocator ( ) ,
188- new MicrosoftStoreLocator ( ) ,
189- new WindowsPathEnvVarLocator ( ) ,
190- ) ;
191- } else {
192- locators . push (
193- // Linux/Mac locators go here.
194- new PosixKnownPathsLocator ( ) ,
195- ) ;
196- }
197-
198- const disposables = locators . filter ( ( d ) => d . dispose !== undefined ) as IDisposable [ ] ;
199- ext . disposables . push ( ...disposables ) ;
200- return locators ;
201- }
202-
203- function watchRoots ( args : WatchRootsArgs ) : IDisposable {
204- const { initRoot, addRoot, removeRoot } = args ;
205-
206- const folders = vscode . workspace . workspaceFolders ;
207- if ( folders ) {
208- folders . map ( ( f ) => f . uri ) . forEach ( initRoot ) ;
209- }
210-
211- return vscode . workspace . onDidChangeWorkspaceFolders ( ( event ) => {
212- for ( const root of event . removed ) {
213- removeRoot ( root . uri ) ;
214- }
215- for ( const root of event . added ) {
216- addRoot ( root . uri ) ;
217- }
218- } ) ;
219- }
220-
221- function createWorkspaceLocator ( ext : ExtensionState ) : WorkspaceLocators {
222- const locators = new WorkspaceLocators ( watchRoots , [
223- ( root : vscode . Uri ) => [
224- new WorkspaceVirtualEnvironmentLocator ( root . fsPath ) ,
225- new PoetryLocator ( root . fsPath ) ,
226- new HatchLocator ( root . fsPath ) ,
227- new PixiLocator ( root . fsPath ) ,
228- new CustomWorkspaceLocator ( root . fsPath ) ,
229- ] ,
230- // Add an ILocator factory func here for each kind of workspace-rooted locator.
231- ] ) ;
232- ext . disposables . push ( locators ) ;
233- return locators ;
234- }
235-
236- function getFromStorage ( storage : IPersistentStorage < PythonEnvInfo [ ] > ) : PythonEnvInfo [ ] {
237- return storage . get ( ) . map ( ( e ) => {
238- if ( e . searchLocation ) {
239- if ( typeof e . searchLocation === 'string' ) {
240- e . searchLocation = Uri . parse ( e . searchLocation ) ;
241- } else if ( 'scheme' in e . searchLocation && 'path' in e . searchLocation ) {
242- e . searchLocation = Uri . parse ( `${ e . searchLocation . scheme } ://${ e . searchLocation . path } ` ) ;
243- } else {
244- traceError ( 'Unexpected search location' , JSON . stringify ( e . searchLocation ) ) ;
245- }
246- }
247- return e ;
248- } ) ;
249- }
250-
251- function putIntoStorage ( storage : IPersistentStorage < PythonEnvInfo [ ] > , envs : PythonEnvInfo [ ] ) : Promise < void > {
252- storage . set (
253- // We have to `cloneDeep()` here so that we don't overwrite the original `PythonEnvInfo` objects.
254- cloneDeep ( envs ) . map ( ( e ) => {
255- if ( e . searchLocation ) {
256- // Make TS believe it is string. This is temporary. We need to serialize this in
257- // a custom way.
258- e . searchLocation = ( e . searchLocation . toString ( ) as unknown ) as Uri ;
259- }
260- return e ;
261- } ) ,
262- ) ;
263- return Promise . resolve ( ) ;
264- }
265-
266- async function createCollectionCache ( ext : ExtensionState ) : Promise < IEnvsCollectionCache > {
267- const storage = getGlobalStorage < PythonEnvInfo [ ] > ( ext . context , PYTHON_ENV_INFO_CACHE_KEY , [ ] ) ;
268- const cache = await createCache ( {
269- get : ( ) => getFromStorage ( storage ) ,
270- store : async ( e ) => putIntoStorage ( storage , e ) ,
271- } ) ;
272- return cache ;
273- }
0 commit comments