Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update flow of various quickpicks
  • Loading branch information
karthiknadig committed Aug 24, 2023
commit 3a127fb27595f339dade639a7ae5d1ec4187f8e8
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ import { sendTelemetryEvent } from '../../../telemetry';
import { EventName } from '../../../telemetry/constants';
import { VenvProgressAndTelemetry, VENV_CREATED_MARKER, VENV_EXISTING_MARKER } from './venvProgressAndTelemetry';
import { showErrorMessageWithLogs } from '../common/commonUtils';
import { IPackageInstallSelection, pickExistingVenvAction, pickPackagesToInstall } from './venvUtils';
import {
ExistingVenvAction,
IPackageInstallSelection,
deleteEnvironment,
pickExistingVenvAction,
pickPackagesToInstall,
} from './venvUtils';
import { InputFlowAction } from '../../../common/utils/multiStepInput';
import {
CreateEnvironmentProvider,
Expand Down Expand Up @@ -150,9 +156,32 @@ export class VenvCreationProvider implements CreateEnvironmentProvider {
undefined,
);

let existingVenvAction: ExistingVenvAction | undefined;
const existingEnvStep = new MultiStepNode(
workspaceStep,
async (context?: MultiStepAction) => {
if (workspace && context === MultiStepAction.Continue) {
try {
existingVenvAction = await pickExistingVenvAction(workspace);
return MultiStepAction.Continue;
} catch (ex) {
if (ex === MultiStepAction.Back || ex === MultiStepAction.Cancel) {
return ex;
}
throw ex;
}
} else if (context === MultiStepAction.Back) {
return MultiStepAction.Back;
}
return MultiStepAction.Continue;
},
undefined,
);
workspaceStep.next = existingEnvStep;

let interpreter: string | undefined;
const interpreterStep = new MultiStepNode(
workspaceStep,
existingEnvStep,
async () => {
if (workspace) {
try {
Expand Down Expand Up @@ -189,14 +218,7 @@ export class VenvCreationProvider implements CreateEnvironmentProvider {
},
undefined,
);
workspaceStep.next = interpreterStep;

const existingEnvStep = new MultiStepNode(
interpreterStep,
async (context?: MultiStepAction) => pickExistingVenvAction(workspace, interpreter, context),
undefined,
);
interpreterStep.next = existingEnvStep;
existingEnvStep.next = interpreterStep;

let addGitIgnore = true;
let installPackages = true;
Expand All @@ -206,7 +228,7 @@ export class VenvCreationProvider implements CreateEnvironmentProvider {
}
let installInfo: IPackageInstallSelection[] | undefined;
const packagesStep = new MultiStepNode(
existingEnvStep,
interpreterStep,
async () => {
if (workspace && installPackages) {
try {
Expand All @@ -227,13 +249,38 @@ export class VenvCreationProvider implements CreateEnvironmentProvider {
},
undefined,
);
existingEnvStep.next = packagesStep;
interpreterStep.next = packagesStep;

const action = await MultiStepNode.run(workspaceStep);
if (action === MultiStepAction.Back || action === MultiStepAction.Cancel) {
throw action;
}

if (workspace) {
if (existingVenvAction === ExistingVenvAction.Recreate) {
sendTelemetryEvent(EventName.ENVIRONMENT_DELETE, undefined, {
environmentType: 'venv',
status: 'triggered',
});
if (await deleteEnvironment(workspace, interpreter)) {
sendTelemetryEvent(EventName.ENVIRONMENT_DELETE, undefined, {
environmentType: 'venv',
status: 'deleted',
});
} else {
sendTelemetryEvent(EventName.ENVIRONMENT_DELETE, undefined, {
environmentType: 'venv',
status: 'failed',
});
throw MultiStepAction.Cancel;
}
} else if (existingVenvAction === ExistingVenvAction.UseExisting) {
sendTelemetryEvent(EventName.ENVIRONMENT_REUSE, undefined, {
environmentType: 'venv',
});
}
}

const args = generateCommandArgs(installInfo, addGitIgnore);

return withProgress(
Expand Down
77 changes: 31 additions & 46 deletions src/client/pythonEnvironments/creation/provider/venvUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import { Commands } from '../../../common/constants';
import { isWindows } from '../../../common/platform/platformService';
import { getVenvPath, hasVenv } from '../common/commonUtils';
import { deleteEnvironmentNonWindows, deleteEnvironmentWindows } from './venvDeleteUtils';
import { sendTelemetryEvent } from '../../../telemetry';
import { EventName } from '../../../telemetry/constants';

const exclude = '**/{.venv*,.git,.nox,.tox,.conda,site-packages,__pypackages__}/**';
async function getPipRequirementsFiles(
Expand Down Expand Up @@ -238,7 +236,10 @@ export async function pickPackagesToInstall(
return packages;
}

async function deleteEnvironment(workspaceFolder: WorkspaceFolder, interpreter: string | undefined): Promise<boolean> {
export async function deleteEnvironment(
workspaceFolder: WorkspaceFolder,
interpreter: string | undefined,
): Promise<boolean> {
const venvPath = getVenvPath(workspaceFolder);
return withProgress<boolean>(
{
Expand All @@ -255,61 +256,45 @@ async function deleteEnvironment(workspaceFolder: WorkspaceFolder, interpreter:
);
}

export enum ExistingVenvAction {
Recreate,
UseExisting,
Create,
}

export async function pickExistingVenvAction(
workspaceFolder: WorkspaceFolder | undefined,
interpreter: string | undefined,
context?: MultiStepAction,
): Promise<MultiStepAction> {
if (workspaceFolder && (await hasVenv(workspaceFolder)) && context === MultiStepAction.Continue) {
const items: QuickPickItem[] = [
{ label: CreateEnv.Venv.recreate, description: CreateEnv.Venv.recreateDescription },
{
label: CreateEnv.Venv.useExisting,
description: CreateEnv.Venv.useExistingDescription,
},
];

let selection: QuickPickItem | undefined;
try {
selection = (await showQuickPickWithBack(
): Promise<ExistingVenvAction> {
if (workspaceFolder) {
if (await hasVenv(workspaceFolder)) {
const items: QuickPickItem[] = [
{ label: CreateEnv.Venv.recreate, description: CreateEnv.Venv.recreateDescription },
{
label: CreateEnv.Venv.useExisting,
description: CreateEnv.Venv.useExistingDescription,
},
];

const selection = (await showQuickPickWithBack(
items,
{
placeHolder: CreateEnv.Venv.existingVenvQuickPickPlaceholder,
ignoreFocusOut: true,
},
undefined,
)) as QuickPickItem | undefined;
} catch (ex) {
return MultiStepAction.Back;
}

if (selection === undefined) {
return MultiStepAction.Cancel;
}

if (selection.label === CreateEnv.Venv.recreate) {
if (await deleteEnvironment(workspaceFolder, interpreter)) {
sendTelemetryEvent(EventName.ENVIRONMENT_DELETE, undefined, {
environmentType: 'venv',
status: 'deleted',
});
return MultiStepAction.Continue;
if (selection?.label === CreateEnv.Venv.recreate) {
return ExistingVenvAction.Recreate;
}
sendTelemetryEvent(EventName.ENVIRONMENT_DELETE, undefined, {
environmentType: 'venv',
status: 'failed',
});
return MultiStepAction.Cancel;
}

if (selection.label === CreateEnv.Venv.useExisting) {
sendTelemetryEvent(EventName.ENVIRONMENT_REUSE, undefined, {
environmentType: 'venv',
});
return MultiStepAction.Continue;
if (selection?.label === CreateEnv.Venv.useExisting) {
return ExistingVenvAction.UseExisting;
}
} else {
return ExistingVenvAction.Create;
}
} else if (context === MultiStepAction.Back) {
return MultiStepAction.Back;
}
return MultiStepAction.Continue;

throw MultiStepAction.Cancel;
}
2 changes: 1 addition & 1 deletion src/client/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2131,7 +2131,7 @@ export interface IEventNamePropertyMapping {
*/
[EventName.ENVIRONMENT_DELETE]: {
environmentType: 'venv' | 'conda';
status: 'deleted' | 'failed';
status: 'triggered' | 'deleted' | 'failed';
};
/**
* Telemetry event if user selected to re-use the existing environment.
Expand Down
Loading