Skip to content

Commit

Permalink
Change some types
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartik Raj committed Aug 17, 2022
1 parent a6bd637 commit 97d4611
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
3 changes: 2 additions & 1 deletion src/client/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ import { sendErrorTelemetry, sendStartupTelemetry } from './startupTelemetry';
import { IStartupDurations } from './types';
import { runAfterActivation } from './common/utils/runAfterActivation';
import { IInterpreterService } from './interpreter/contracts';
import { IExtensionApi, IProposedExtensionAPI } from './apiTypes';
import { IExtensionApi } from './apiTypes';
import { buildProposedApi } from './proposedApi';
import { WorkspaceService } from './common/application/workspace';
import { disposeAll } from './common/utils/resourceLifecycle';
import { IProposedExtensionAPI } from './proposedApiTypes';

durations.codeLoadingTime = stopWatch.elapsedTime;

Expand Down
80 changes: 48 additions & 32 deletions src/client/pythonEnvironments/base/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface EnvironmentDetailsOptions {
/**
* Return details provided by the specific provider, throws error if provider not found.
*/
providerId?: ProviderID;
extensionId?: ExtensionID;
}

export interface EnvironmentDetails {
Expand All @@ -30,13 +30,15 @@ export interface EnvironmentDetails {
bitness?: Architecture;
sysPrefix: string;
};
environment?: {
type: EnvType;
name?: string;
path: string;
project?: string; // Any specific project environment is created for.
source: EnvSource[];
};
environment:
| {
type: EnvType;
name?: string;
path: string;
project?: string; // Any specific project environment is created for.
source: EnvSource[];
}
| undefined;
version: StandardVersionInfo & {
sysVersion?: string;
};
Expand All @@ -51,9 +53,13 @@ export interface EnvironmentDetails {
* Provider is only required to provide the `executable` key, rest are optional. So construct a type using
* `EnvironmentDetails` where `executable` is the only required key.
*/
type EnvironmentDetailsByProvider = Partial<EnvironmentDetails> & Pick<EnvironmentDetails, 'executable'>;
type EnvironmentDetailsByProvider = Partial<EnvironmentDetails> &
Pick<EnvironmentDetails, 'executable'> &
Pick<EnvironmentDetails, 'environment'>;

export interface IInternalEnvironmentProvider extends ILocatorFactoryAPI, IInternalResolverAPI {}
export type IInternalEnvironmentProvider =
| (ILocatorFactoryAPI & IInternalResolverAPI & IInternalIdentifierAPI)
| (ILocatorFactoryAPI & IInternalResolverAPI);

interface ILocatorFactoryAPI {
/**
Expand All @@ -65,38 +71,52 @@ interface ILocatorFactoryAPI {
export type ProposedDetailsAPI = (env: BaseEnvInfo) => Promise<EnvironmentDetailsByProvider | undefined>;
export type InternalDetailsAPI = (env: BasicEnvInfo) => Promise<PythonEnvInfo | undefined>;

export interface IResolverAPI {
/**
* Returns true if provided environment is recognized by the provider.
*/
canIdentifyEnvironment: (path: UniquePathType) => Promise<boolean>;
export interface IDetailsAPI {
/**
* This is only called if this provider can identify the environment.
* This is only called if the provider:
* * Can identify the environment.
* * Iterates out the environment.
* Returns details or `undefined` if it was found if env is invalid.
*/
getEnvironmentDetails: ProposedDetailsAPI;
}

export interface IInternalResolverAPI {
export type IResolverAPI = IDetailsAPI | (IDetailsAPI & IIdentifierAPI);

/**
* Identifier need not be registered
*/
interface IIdentifierAPI {
/**
* Environment source the provider identifies.
*/
readonly envSource: EnvSource;
/**
* Returns true if provided environment is recognized by the provider.
*/
canIdentifyEnvironment: (path: UniquePathType) => Promise<boolean>;
}

export interface IInternalResolverAPI {
getEnvironmentDetails: InternalDetailsAPI;
}

interface IInternalIdentifierAPI {
readonly envKind: PythonEnvKind;
/**
* This is only called if this provider can identify the environment.
* Returns details or `undefined` if it was found if env is invalid.
* Returns true if provided environment is recognized by the provider.
*/
getEnvironmentDetails: InternalDetailsAPI;
canIdentifyEnvironment: (path: UniquePathType) => Promise<boolean>;
}

export type ILocatorFactory = IWorkspaceLocatorFactory | INonWorkspaceLocatorFactory;
export type INonWorkspaceLocatorFactory = () => ILocatorAPI;
export type IWorkspaceLocatorFactory = (root: string) => ILocatorAPI;

export interface IEnvironmentProvider extends ILocatorFactoryAPI, IResolverAPI {}
export type IEnvironmentProvider = ILocatorFactoryAPI & IResolverAPI;
export interface ILocatorAPI {
iterEnvs?(): IPythonEnvsIterator<EnvInfo>;
readonly onChanged?: Event<LocatorEnvsChangedEvent>;
iterEnvs(): IPythonEnvsIterator<EnvInfo>;
readonly onChanged: Event<LocatorEnvsChangedEvent>;
}

export type EnvInfo = BaseEnvInfo & {
Expand All @@ -108,7 +128,7 @@ export type BaseEnvInfo = {
envPath?: string;
};

type ProviderID = string;
type ExtensionID = string;

/**
* These can be used when querying for a particular env.
Expand All @@ -118,11 +138,11 @@ export interface EnvironmentProviderMetadata {
* Details about the environments the locator provides.
* Useful when querying for a particular env.
*/
readonly environments: EnvironmentMetaData;
readonly environments?: EnvironmentMetaData;
/**
* An Identifier for the provider.
* An Identifier for the extension registering the provider.
*/
readonly providerId: ProviderID;
readonly extensionId: ExtensionID;
}

interface InternalEnvironmentMetaData {
Expand All @@ -138,10 +158,7 @@ export interface InternalEnvironmentProviderMetadata {
* Useful when querying for a particular env.
*/
readonly environments: InternalEnvironmentMetaData;
/**
* An Identifier for the provider.
*/
readonly providerId: ProviderID;
readonly extensionId: ExtensionID;
}

interface EnvironmentMetaData {
Expand All @@ -168,7 +185,6 @@ export enum KnownEnvTypes {
VirtualEnv = 'VirtualEnv',
Conda = 'Conda',
Unknown = 'Unknown',
Global = 'Global',
}

export type EnvSource = KnownEnvSourceTypes | string;
Expand Down
5 changes: 3 additions & 2 deletions src/client/pythonEnvironments/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ export function convertProviderAPI(proposed: IEnvironmentProvider): IInternalEnv

export function convertProviderMetaData(proposed: EnvironmentProviderMetadata): InternalEnvironmentProviderMetadata {
return {
providerId: proposed.providerId,
extensionId: proposed.extensionId,
environments: {
envKinds: proposed.environments.envSources?.map((e) => convertKind(e)) ?? [PythonEnvKind.Unknown],
envKinds: proposed.environments?.envSources?.map((e) => convertKind(e)) ?? [PythonEnvKind.Unknown],
},
};
}

function convertResolverAPI(proposed: IResolverAPI): IInternalResolverAPI {
return {
envKind: proposed.envSource ? convertKind(proposed.envSource) : undefined,
canIdentifyEnvironment: proposed.canIdentifyEnvironment,
getEnvironmentDetails: convertDetailsAPI(proposed.getEnvironmentDetails),
};
Expand Down

0 comments on commit 97d4611

Please sign in to comment.