Skip to content

Commit 3016ad8

Browse files
author
Eric Snow
authored
PyEnvs: Register proxies with the DI framework to replace @Injectable usage. (#12795)
With each class in the component that has the @Injectable decorator: * add a compatible proxy class to src/client/pythonEnvironments/legacyIOC.ts * register the proxy with the DI framework rather than the component-internal class * drop the @Injectable decorator from the component-internal class Note that this change does the absolute minimum. No classes are refactored. No code is pulled up into the proxies, no matter how basic. Also note that the @Inject decorators are left behind to make it easier to identify where separate refactoring needs to happen (per our plans).
1 parent fdbc380 commit 3016ad8

35 files changed

Lines changed: 544 additions & 267 deletions

src/client/common/process/pythonExecutionFactory.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { ICondaService, IInterpreterService } from '../../interpreter/contracts'
1010
import { IWindowsStoreInterpreter } from '../../interpreter/locators/types';
1111
import { IServiceContainer } from '../../ioc/types';
1212
import { CondaEnvironmentInfo } from '../../pythonEnvironments/discovery/locators/services/conda';
13-
import { WindowsStoreInterpreter } from '../../pythonEnvironments/discovery/locators/services/windowsStoreInterpreter';
1413
import { sendTelemetryEvent } from '../../telemetry';
1514
import { EventName } from '../../telemetry/constants';
1615
import { traceError } from '../logger';
@@ -51,7 +50,7 @@ export class PythonExecutionFactory implements IPythonExecutionFactory {
5150
@inject(IConfigurationService) private readonly configService: IConfigurationService,
5251
@inject(ICondaService) private readonly condaService: ICondaService,
5352
@inject(IBufferDecoder) private readonly decoder: IBufferDecoder,
54-
@inject(WindowsStoreInterpreter) private readonly windowsStoreInterpreter: IWindowsStoreInterpreter,
53+
@inject(IWindowsStoreInterpreter) private readonly windowsStoreInterpreter: IWindowsStoreInterpreter,
5554
@inject(IPlatformService) private readonly platformService: IPlatformService
5655
) {
5756
// Acquire other objects here so that if we are called during dispose they are available.

src/client/common/terminal/environmentActivationProviders/pipEnvActivationProvider.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33

44
'use strict';
55

6-
import { inject, injectable, named } from 'inversify';
6+
import { inject, injectable } from 'inversify';
77
import { Uri } from 'vscode';
88
import '../../../common/extensions';
9-
import {
10-
IInterpreterLocatorService,
11-
IInterpreterService,
12-
IPipEnvService,
13-
PIPENV_SERVICE
14-
} from '../../../interpreter/contracts';
9+
import { IInterpreterService, IPipEnvService } from '../../../interpreter/contracts';
1510
import { InterpreterType } from '../../../pythonEnvironments/info';
1611
import { IWorkspaceService } from '../../application/types';
1712
import { IFileSystem } from '../../platform/types';
@@ -21,9 +16,7 @@ import { ITerminalActivationCommandProvider, TerminalShellType } from '../types'
2116
export class PipEnvActivationCommandProvider implements ITerminalActivationCommandProvider {
2217
constructor(
2318
@inject(IInterpreterService) private readonly interpreterService: IInterpreterService,
24-
@inject(IInterpreterLocatorService)
25-
@named(PIPENV_SERVICE)
26-
private readonly pipenvService: IPipEnvService,
19+
@inject(IPipEnvService) private readonly pipenvService: IPipEnvService,
2720
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService,
2821
@inject(IFileSystem) private readonly fs: IFileSystem
2922
) {}

src/client/interpreter/contracts.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export interface IInterpreterHelper {
8989
}
9090

9191
export const IPipEnvService = Symbol('IPipEnvService');
92-
export interface IPipEnvService extends IInterpreterLocatorService {
92+
export interface IPipEnvService {
9393
executable: string;
9494
isRelatedPipEnvironment(dir: string, pythonPath: string): Promise<boolean>;
9595
}
@@ -99,11 +99,15 @@ export interface IInterpreterLocatorHelper {
9999
mergeInterpreters(interpreters: PythonInterpreter[]): Promise<PythonInterpreter[]>;
100100
}
101101

102-
export const IInterpreterWatcher = Symbol('IInterpreterWatcher');
103102
export interface IInterpreterWatcher {
104103
onDidCreate: Event<Resource>;
105104
}
106105

106+
export const IInterpreterWatcherRegistry = Symbol('IInterpreterWatcherRegistry');
107+
export interface IInterpreterWatcherRegistry extends IInterpreterWatcher {
108+
register(resource: Resource): Promise<void>;
109+
}
110+
107111
export const IInterpreterWatcherBuilder = Symbol('IInterpreterWatcherBuilder');
108112
export interface IInterpreterWatcherBuilder {
109113
getWorkspaceVirtualEnvInterpreterWatcher(resource: Resource): Promise<IInterpreterWatcher>;

src/client/interpreter/helpers.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { IPythonExecutionFactory } from '../common/process/types';
77
import { IPersistentStateFactory, Resource } from '../common/types';
88
import { IServiceContainer } from '../ioc/types';
99
import { isMacDefaultPythonPath } from '../pythonEnvironments/discovery';
10-
import { InterpeterHashProviderFactory } from '../pythonEnvironments/discovery/locators/services/hashProviderFactory';
1110
import {
1211
getInterpreterTypeName,
1312
InterpreterInformation,
@@ -49,7 +48,7 @@ export class InterpreterHelper implements IInterpreterHelper {
4948
private readonly persistentFactory: IPersistentStateFactory;
5049
constructor(
5150
@inject(IServiceContainer) private serviceContainer: IServiceContainer,
52-
@inject(InterpeterHashProviderFactory) private readonly hashProviderFactory: IInterpreterHashProviderFactory
51+
@inject(IInterpreterHashProviderFactory) private readonly hashProviderFactory: IInterpreterHashProviderFactory
5352
) {
5453
this.persistentFactory = this.serviceContainer.get<IPersistentStateFactory>(IPersistentStateFactory);
5554
}

src/client/interpreter/interpreterService.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
} from '../common/types';
2121
import { sleep } from '../common/utils/async';
2222
import { IServiceContainer } from '../ioc/types';
23-
import { InterpeterHashProviderFactory } from '../pythonEnvironments/discovery/locators/services/hashProviderFactory';
2423
import { InterpreterType, PythonInterpreter } from '../pythonEnvironments/info';
2524
import { captureTelemetry } from '../telemetry';
2625
import { EventName } from '../telemetry/constants';
@@ -70,7 +69,7 @@ export class InterpreterService implements Disposable, IInterpreterService {
7069

7170
constructor(
7271
@inject(IServiceContainer) private serviceContainer: IServiceContainer,
73-
@inject(InterpeterHashProviderFactory) private readonly hashProviderFactory: IInterpreterHashProviderFactory
72+
@inject(IInterpreterHashProviderFactory) private readonly hashProviderFactory: IInterpreterHashProviderFactory
7473
) {
7574
this.locator = serviceContainer.get<IInterpreterLocatorService>(
7675
IInterpreterLocatorService,

src/client/interpreter/locators/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface IPipEnvServiceHelper {
1515
trackWorkspaceFolder(pythonPath: string, workspaceFolder: Uri): Promise<void>;
1616
}
1717

18+
export const IInterpreterHashProviderFactory = Symbol('IInterpreterHashProviderFactory');
1819
/**
1920
* Factory to create a hash provider.
2021
* Getting the hash of an interpreter can vary based on the type of the interpreter.
@@ -26,6 +27,7 @@ export interface IInterpreterHashProviderFactory {
2627
create(options: { pythonPath: string } | { resource: Uri }): Promise<IInterpreterHashProvider>;
2728
}
2829

30+
export const IInterpreterHashProvider = Symbol('IInterpreterHashProvider');
2931
/**
3032
* Provides the ability to get the has of a given interpreter.
3133
*
@@ -44,6 +46,10 @@ export interface IInterpreterHashProvider {
4446
getInterpreterHash(pythonPath: string): Promise<string>;
4547
}
4648

49+
export const IWindowsStoreHashProvider = Symbol('IWindowStoreHashProvider');
50+
export interface IWindowsStoreHashProvider extends IInterpreterHashProvider {}
51+
52+
export const IWindowsStoreInterpreter = Symbol('IWindowsStoreInterpreter');
4753
export interface IWindowsStoreInterpreter {
4854
/**
4955
* Whether this is a Windows Store/App Interpreter.

src/client/interpreter/virtualEnvs/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { IServiceContainer } from '../../ioc/types';
1414
import * as globalenvs from '../../pythonEnvironments/discovery/globalenv';
1515
import * as subenvs from '../../pythonEnvironments/discovery/subenv';
1616
import { InterpreterType } from '../../pythonEnvironments/info';
17-
import { IInterpreterLocatorService, IPipEnvService, PIPENV_SERVICE } from '../contracts';
17+
import { IPipEnvService } from '../contracts';
1818
import { IVirtualEnvironmentManager } from './types';
1919

2020
@injectable()
@@ -26,10 +26,7 @@ export class VirtualEnvironmentManager implements IVirtualEnvironmentManager {
2626
constructor(@inject(IServiceContainer) private readonly serviceContainer: IServiceContainer) {
2727
this.processServiceFactory = serviceContainer.get<IProcessServiceFactory>(IProcessServiceFactory);
2828
this.fs = serviceContainer.get<IFileSystem>(IFileSystem);
29-
this.pipEnvService = serviceContainer.get<IInterpreterLocatorService>(
30-
IInterpreterLocatorService,
31-
PIPENV_SERVICE
32-
) as IPipEnvService;
29+
this.pipEnvService = serviceContainer.get<IPipEnvService>(IPipEnvService);
3330
this.workspaceService = serviceContainer.get<IWorkspaceService>(IWorkspaceService);
3431
}
3532

src/client/pythonEnvironments/discovery/locators/helpers.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import * as fsapi from 'fs-extra';
2-
import { inject, injectable } from 'inversify';
2+
import { inject } from 'inversify';
33
import * as path from 'path';
44
import { traceError } from '../../../common/logger';
55
import { IS_WINDOWS } from '../../../common/platform/constants';
66
import { IFileSystem } from '../../../common/platform/types';
7-
import { IInterpreterLocatorHelper } from '../../../interpreter/contracts';
87
import { IPipEnvServiceHelper } from '../../../interpreter/locators/types';
98
import { InterpreterType, PythonInterpreter } from '../../info';
109

@@ -26,8 +25,7 @@ export async function lookForInterpretersInDirectory(pathToCheck: string, _: IFi
2625
}
2726
}
2827

29-
@injectable()
30-
export class InterpreterLocatorHelper implements IInterpreterLocatorHelper {
28+
export class InterpreterLocatorHelper {
3129
constructor(
3230
@inject(IFileSystem) private readonly fs: IFileSystem,
3331
@inject(IPipEnvServiceHelper) private readonly pipEnvServiceHelper: IPipEnvServiceHelper

src/client/pythonEnvironments/discovery/locators/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { inject, injectable } from 'inversify';
1+
import { inject } from 'inversify';
22
import { Disposable, Event, EventEmitter, Uri } from 'vscode';
33
import { traceDecorators } from '../../../common/logger';
44
import { IPlatformService } from '../../../common/platform/types';
5-
import { IDisposableRegistry } from '../../../common/types';
65
import { createDeferred, Deferred } from '../../../common/utils/async';
76
import { OSType } from '../../../common/utils/platform';
87
import {
@@ -28,8 +27,7 @@ const flatten = require('lodash/flatten') as typeof import('lodash/flatten');
2827
/**
2928
* Facilitates locating Python interpreters.
3029
*/
31-
@injectable()
32-
export class PythonInterpreterLocatorService implements IInterpreterLocatorService {
30+
export class PythonInterpreterLocatorService {
3331
public didTriggerInterpreterSuggestions: boolean;
3432

3533
private readonly disposables: Disposable[] = [];
@@ -39,7 +37,6 @@ export class PythonInterpreterLocatorService implements IInterpreterLocatorServi
3937

4038
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer) {
4139
this._hasInterpreters = createDeferred<boolean>();
42-
serviceContainer.get<Disposable[]>(IDisposableRegistry).push(this);
4340
this.platform = serviceContainer.get<IPlatformService>(IPlatformService);
4441
this.interpreterLocatorHelper = serviceContainer.get<IInterpreterLocatorHelper>(IInterpreterLocatorHelper);
4542
this.didTriggerInterpreterSuggestions = false;

src/client/pythonEnvironments/discovery/locators/progressService.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33

44
'use strict';
55

6-
import { inject, injectable } from 'inversify';
6+
import { inject } from 'inversify';
77
import { Disposable, Event, EventEmitter } from 'vscode';
88
import { traceDecorators } from '../../../common/logger';
99
import { IDisposableRegistry } from '../../../common/types';
1010
import { createDeferredFrom, Deferred } from '../../../common/utils/async';
1111
import { noop } from '../../../common/utils/misc';
12-
import { IInterpreterLocatorProgressService, IInterpreterLocatorService } from '../../../interpreter/contracts';
12+
import { IInterpreterLocatorService } from '../../../interpreter/contracts';
1313
import { IServiceContainer } from '../../../ioc/types';
1414
import { PythonInterpreter } from '../../info';
1515

16-
@injectable()
17-
export class InterpreterLocatorProgressService implements IInterpreterLocatorProgressService {
16+
export class InterpreterLocatorProgressService {
1817
private deferreds: Deferred<PythonInterpreter[]>[] = [];
1918
private readonly refreshing = new EventEmitter<void>();
2019
private readonly refreshed = new EventEmitter<void>();

0 commit comments

Comments
 (0)