|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { inject, injectable, optional } from 'inversify'; |
| 5 | +import { ConfigurationTarget, Uri } from 'vscode'; |
| 6 | +import { IExtensionActivationService } from '../../activation/types'; |
| 7 | +import { IApplicationShell, IWorkspaceService } from '../../common/application/types'; |
| 8 | +import { traceDecorators, traceError } from '../../common/logger'; |
| 9 | +import { IPersistentStateFactory } from '../../common/types'; |
| 10 | +import { InteractiveShiftEnterBanner, Interpreters } from '../../common/utils/localize'; |
| 11 | +import { sendTelemetryEvent } from '../../telemetry'; |
| 12 | +import { EventName } from '../../telemetry/constants'; |
| 13 | +import { IInterpreterService, InterpreterType } from '../contracts'; |
| 14 | + |
| 15 | +export const condaInheritEnvPromptKey = 'CONDA_INHERIT_ENV_PROMPT_KEY'; |
| 16 | + |
| 17 | +@injectable() |
| 18 | +export class CondaInheritEnvPrompt implements IExtensionActivationService { |
| 19 | + constructor( |
| 20 | + @inject(IInterpreterService) private readonly interpreterService: IInterpreterService, |
| 21 | + @inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService, |
| 22 | + @inject(IApplicationShell) private readonly appShell: IApplicationShell, |
| 23 | + @inject(IPersistentStateFactory) private readonly persistentStateFactory: IPersistentStateFactory, |
| 24 | + @optional() public hasPromptBeenShownInCurrentSession: boolean = false |
| 25 | + ) { } |
| 26 | + |
| 27 | + public async activate(resource: Uri): Promise<void> { |
| 28 | + this.initializeInBackground(resource).ignoreErrors(); |
| 29 | + } |
| 30 | + |
| 31 | + @traceDecorators.error('Failed to intialize conda inherit env prompt') |
| 32 | + public async initializeInBackground(resource: Uri): Promise<void> { |
| 33 | + const show = await this.shouldShowPrompt(resource); |
| 34 | + if (!show) { |
| 35 | + return; |
| 36 | + } |
| 37 | + await this.promptAndUpdate(); |
| 38 | + } |
| 39 | + |
| 40 | + @traceDecorators.error('Failed to display conda inherit env prompt') |
| 41 | + public async promptAndUpdate() { |
| 42 | + const notificationPromptEnabled = this.persistentStateFactory.createGlobalPersistentState(condaInheritEnvPromptKey, true); |
| 43 | + if (!notificationPromptEnabled.value) { |
| 44 | + return; |
| 45 | + } |
| 46 | + const prompts = [InteractiveShiftEnterBanner.bannerLabelYes(), InteractiveShiftEnterBanner.bannerLabelNo()]; |
| 47 | + const telemetrySelections: ['Yes', 'No'] = ['Yes', 'No']; |
| 48 | + const selection = await this.appShell.showInformationMessage(Interpreters.condaInheritEnvMessage(), ...prompts); |
| 49 | + sendTelemetryEvent(EventName.CONDA_INHERIT_ENV_PROMPT, undefined, { selection: selection ? telemetrySelections[prompts.indexOf(selection)] : undefined }); |
| 50 | + if (!selection) { |
| 51 | + return; |
| 52 | + } |
| 53 | + if (selection === prompts[0]) { |
| 54 | + await this.workspaceService.getConfiguration('terminal').update('integrated.inheritEnv', false, ConfigurationTarget.Global); |
| 55 | + } else if (selection === prompts[1]) { |
| 56 | + await notificationPromptEnabled.updateValue(false); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @traceDecorators.error('Failed to check whether to display prompt for conda inherit env setting') |
| 61 | + public async shouldShowPrompt(resource: Uri): Promise<boolean> { |
| 62 | + if (this.hasPromptBeenShownInCurrentSession) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + const interpreter = await this.interpreterService.getActiveInterpreter(resource); |
| 66 | + if (!interpreter || interpreter.type !== InterpreterType.Conda) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + const setting = this.workspaceService.getConfiguration('terminal', resource).inspect<boolean>('integrated.inheritEnv'); |
| 70 | + if (!setting) { |
| 71 | + traceError('WorkspaceConfiguration.inspect returns `undefined` for setting `terminal.integrated.inheritEnv`'); |
| 72 | + return false; |
| 73 | + } |
| 74 | + if (setting.globalValue !== undefined || setting.workspaceValue !== undefined || setting.workspaceFolderValue !== undefined) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + this.hasPromptBeenShownInCurrentSession = true; |
| 78 | + return true; |
| 79 | + } |
| 80 | +} |
0 commit comments