From 756aeb507574e0d84c8c83b2817d8f299ecb3c3e Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Wed, 22 Feb 2023 17:20:27 -0800 Subject: [PATCH] Add option to control if environment is selected after creation. --- .../creation/createEnvApi.ts | 15 ++++-- .../creation/createEnvironment.ts | 52 ++++++++++++------- .../pythonEnvironments/creation/types.ts | 19 +++++++ 3 files changed, 63 insertions(+), 23 deletions(-) diff --git a/src/client/pythonEnvironments/creation/createEnvApi.ts b/src/client/pythonEnvironments/creation/createEnvApi.ts index b05670feae02a..cfbf81909d59e 100644 --- a/src/client/pythonEnvironments/creation/createEnvApi.ts +++ b/src/client/pythonEnvironments/creation/createEnvApi.ts @@ -9,7 +9,12 @@ import { IInterpreterQuickPick } from '../../interpreter/configuration/types'; import { getCreationEvents, handleCreateEnvironmentCommand } from './createEnvironment'; import { condaCreationProvider } from './provider/condaCreationProvider'; import { VenvCreationProvider } from './provider/venvCreationProvider'; -import { CreateEnvironmentOptions, CreateEnvironmentProvider, CreateEnvironmentResult } from './types'; +import { + CreateEnvironmentExitedEventArgs, + CreateEnvironmentOptions, + CreateEnvironmentProvider, + CreateEnvironmentResult, +} from './types'; import { showInformationMessage } from '../../common/vscodeApis/windowApis'; import { CreateEnv } from '../../common/utils/localize'; @@ -62,10 +67,10 @@ export function registerCreateEnvironmentFeatures( disposables.push(registerCreateEnvironmentProvider(new VenvCreationProvider(interpreterQuickPick))); disposables.push(registerCreateEnvironmentProvider(condaCreationProvider())); disposables.push( - onCreateEnvironmentExited(async (e: CreateEnvironmentResult | undefined) => { - if (e && e.path) { - await interpreterPathService.update(e.uri, ConfigurationTarget.WorkspaceFolder, e.path); - showInformationMessage(`${CreateEnv.informEnvCreation} ${pathUtils.getDisplayName(e.path)}`); + onCreateEnvironmentExited(async (e: CreateEnvironmentExitedEventArgs) => { + if (e.result?.path && e.options?.selectEnvironment) { + await interpreterPathService.update(e.result.uri, ConfigurationTarget.WorkspaceFolder, e.result.path); + showInformationMessage(`${CreateEnv.informEnvCreation} ${pathUtils.getDisplayName(e.result.path)}`); } }), ); diff --git a/src/client/pythonEnvironments/creation/createEnvironment.ts b/src/client/pythonEnvironments/creation/createEnvironment.ts index 2f21d787d336a..bdeaf89ba82d4 100644 --- a/src/client/pythonEnvironments/creation/createEnvironment.ts +++ b/src/client/pythonEnvironments/creation/createEnvironment.ts @@ -10,10 +10,16 @@ import { showQuickPickWithBack, } from '../../common/vscodeApis/windowApis'; import { traceError, traceVerbose } from '../../logging'; -import { CreateEnvironmentOptions, CreateEnvironmentProvider, CreateEnvironmentResult } from './types'; +import { + CreateEnvironmentExitedEventArgs, + CreateEnvironmentOptions, + CreateEnvironmentProvider, + CreateEnvironmentResult, + CreateEnvironmentStartedEventArgs, +} from './types'; -const onCreateEnvironmentStartedEvent = new EventEmitter(); -const onCreateEnvironmentExitedEvent = new EventEmitter(); +const onCreateEnvironmentStartedEvent = new EventEmitter(); +const onCreateEnvironmentExitedEvent = new EventEmitter(); let startedEventCount = 0; @@ -21,19 +27,19 @@ function isBusyCreatingEnvironment(): boolean { return startedEventCount > 0; } -function fireStartedEvent(): void { - onCreateEnvironmentStartedEvent.fire(); +function fireStartedEvent(options?: CreateEnvironmentOptions): void { + onCreateEnvironmentStartedEvent.fire({ options }); startedEventCount += 1; } -function fireExitedEvent(result: CreateEnvironmentResult | undefined): void { - onCreateEnvironmentExitedEvent.fire(result); +function fireExitedEvent(result?: CreateEnvironmentResult, options?: CreateEnvironmentOptions, error?: unknown): void { + onCreateEnvironmentExitedEvent.fire({ result, options, error }); startedEventCount -= 1; } export function getCreationEvents(): { - onCreateEnvironmentStarted: Event; - onCreateEnvironmentExited: Event; + onCreateEnvironmentStarted: Event; + onCreateEnvironmentExited: Event; isCreatingEnvironment: () => boolean; } { return { @@ -45,14 +51,12 @@ export function getCreationEvents(): { async function createEnvironment( provider: CreateEnvironmentProvider, - options: CreateEnvironmentOptions = { - ignoreSourceControl: true, - installPackages: true, - }, + options: CreateEnvironmentOptions, ): Promise { let result: CreateEnvironmentResult | undefined; + let err: unknown | undefined; try { - fireStartedEvent(); + fireStartedEvent(options); result = await provider.createEnvironment(options); } catch (ex) { if (ex === QuickInputButtons.Back) { @@ -61,9 +65,10 @@ async function createEnvironment( return undefined; } } - throw ex; + err = ex; + throw err; } finally { - fireExitedEvent(result); + fireExitedEvent(result, options, err); } return result; } @@ -110,17 +115,28 @@ async function showCreateEnvironmentQuickPick( return undefined; } +function getOptionsWithDefaults(options?: CreateEnvironmentOptions): CreateEnvironmentOptions { + return { + installPackages: true, + ignoreSourceControl: true, + showBackButton: false, + selectEnvironment: true, + ...options, + }; +} + export async function handleCreateEnvironmentCommand( providers: readonly CreateEnvironmentProvider[], options?: CreateEnvironmentOptions, ): Promise { + const optionsWithDefaults = getOptionsWithDefaults(options); let selectedProvider: CreateEnvironmentProvider | undefined; const envTypeStep = new MultiStepNode( undefined, async (context?: MultiStepAction) => { if (providers.length > 0) { try { - selectedProvider = await showCreateEnvironmentQuickPick(providers, options); + selectedProvider = await showCreateEnvironmentQuickPick(providers, optionsWithDefaults); } catch (ex) { if (ex === MultiStepAction.Back || ex === MultiStepAction.Cancel) { return ex; @@ -152,7 +168,7 @@ export async function handleCreateEnvironmentCommand( } if (selectedProvider) { try { - result = await createEnvironment(selectedProvider, options); + result = await createEnvironment(selectedProvider, optionsWithDefaults); } catch (ex) { if (ex === MultiStepAction.Back || ex === MultiStepAction.Cancel) { return ex; diff --git a/src/client/pythonEnvironments/creation/types.ts b/src/client/pythonEnvironments/creation/types.ts index c18249a2bd720..7f49256d455e6 100644 --- a/src/client/pythonEnvironments/creation/types.ts +++ b/src/client/pythonEnvironments/creation/types.ts @@ -6,9 +6,18 @@ import { Progress, Uri } from 'vscode'; export interface CreateEnvironmentProgress extends Progress<{ message?: string; increment?: number }> {} export interface CreateEnvironmentOptions { + // Default `true`. If `true`, the environment creation handler is expected to install packages. installPackages?: boolean; + + // Default `true`. If `true`, the environment creation provider is expected to add the environment to ignore list + // for the source control. ignoreSourceControl?: boolean; + + // Default `false`. If `true` the creation provider should show back button when showing QuickPick or QuickInput. showBackButton?: boolean; + + // Default `true`. If `true`, the environment will be selected as the environment to be used for the workspace. + selectEnvironment?: boolean; } export interface CreateEnvironmentResult { @@ -17,6 +26,16 @@ export interface CreateEnvironmentResult { action?: 'Back' | 'Cancel'; } +export interface CreateEnvironmentStartedEventArgs { + options: CreateEnvironmentOptions | undefined; +} + +export interface CreateEnvironmentExitedEventArgs { + result: CreateEnvironmentResult | undefined; + error?: unknown; + options: CreateEnvironmentOptions | undefined; +} + export interface CreateEnvironmentProvider { createEnvironment(options?: CreateEnvironmentOptions): Promise; name: string;