|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { inject, injectable } from 'inversify'; |
| 5 | +import { Uri } from 'vscode'; |
| 6 | +import { IApplicationEnvironment, IApplicationShell } from '../../common/application/types'; |
| 7 | +import { IBrowserService, IDisposableRegistry, IExperimentService, IPersistentStateFactory } from '../../common/types'; |
| 8 | +import { Common, Interpreters } from '../../common/utils/localize'; |
| 9 | +import { IExtensionSingleActivationService } from '../../activation/types'; |
| 10 | +import { inTerminalEnvVarExperiment } from '../../common/experiments/helpers'; |
| 11 | +import { IInterpreterService } from '../../interpreter/contracts'; |
| 12 | +import { PythonEnvType } from '../../pythonEnvironments/base/info'; |
| 13 | +import { identifyShellFromShellPath } from '../../common/terminal/shellDetectors/baseShellDetector'; |
| 14 | +import { TerminalShellType } from '../../common/terminal/types'; |
| 15 | +import { sendTelemetryEvent } from '../../telemetry'; |
| 16 | +import { EventName } from '../../telemetry/constants'; |
| 17 | + |
| 18 | +export const terminalDeactivationPromptKey = 'TERMINAL_DEACTIVATION_PROMPT_KEY'; |
| 19 | + |
| 20 | +@injectable() |
| 21 | +export class TerminalDeactivateLimitationPrompt implements IExtensionSingleActivationService { |
| 22 | + public readonly supportedWorkspaceTypes = { untrustedWorkspace: false, virtualWorkspace: false }; |
| 23 | + |
| 24 | + constructor( |
| 25 | + @inject(IApplicationShell) private readonly appShell: IApplicationShell, |
| 26 | + @inject(IPersistentStateFactory) private readonly persistentStateFactory: IPersistentStateFactory, |
| 27 | + @inject(IDisposableRegistry) private readonly disposableRegistry: IDisposableRegistry, |
| 28 | + @inject(IInterpreterService) private readonly interpreterService: IInterpreterService, |
| 29 | + @inject(IBrowserService) private readonly browserService: IBrowserService, |
| 30 | + @inject(IApplicationEnvironment) private readonly appEnvironment: IApplicationEnvironment, |
| 31 | + @inject(IExperimentService) private readonly experimentService: IExperimentService, |
| 32 | + ) {} |
| 33 | + |
| 34 | + public async activate(): Promise<void> { |
| 35 | + if (!inTerminalEnvVarExperiment(this.experimentService)) { |
| 36 | + return; |
| 37 | + } |
| 38 | + this.disposableRegistry.push( |
| 39 | + this.appShell.onDidWriteTerminalData(async (e) => { |
| 40 | + if (!e.data.includes('deactivate')) { |
| 41 | + return; |
| 42 | + } |
| 43 | + const shellType = identifyShellFromShellPath(this.appEnvironment.shell); |
| 44 | + if (shellType === TerminalShellType.commandPrompt) { |
| 45 | + return; |
| 46 | + } |
| 47 | + const { terminal } = e; |
| 48 | + const cwd = |
| 49 | + 'cwd' in terminal.creationOptions && terminal.creationOptions.cwd |
| 50 | + ? terminal.creationOptions.cwd |
| 51 | + : undefined; |
| 52 | + const resource = typeof cwd === 'string' ? Uri.file(cwd) : cwd; |
| 53 | + const interpreter = await this.interpreterService.getActiveInterpreter(resource); |
| 54 | + if (interpreter?.type !== PythonEnvType.Virtual) { |
| 55 | + return; |
| 56 | + } |
| 57 | + await this.notifyUsers(); |
| 58 | + }), |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + private async notifyUsers(): Promise<void> { |
| 63 | + const notificationPromptEnabled = this.persistentStateFactory.createGlobalPersistentState( |
| 64 | + terminalDeactivationPromptKey, |
| 65 | + true, |
| 66 | + ); |
| 67 | + if (!notificationPromptEnabled.value) { |
| 68 | + return; |
| 69 | + } |
| 70 | + const prompts = [Common.seeInstructions, Interpreters.deactivateDoneButton, Common.doNotShowAgain]; |
| 71 | + const telemetrySelections: ['See Instructions', 'Done, it works', "Don't show again"] = [ |
| 72 | + 'See Instructions', |
| 73 | + 'Done, it works', |
| 74 | + "Don't show again", |
| 75 | + ]; |
| 76 | + const selection = await this.appShell.showWarningMessage(Interpreters.terminalDeactivatePrompt, ...prompts); |
| 77 | + if (!selection) { |
| 78 | + return; |
| 79 | + } |
| 80 | + sendTelemetryEvent(EventName.TERMINAL_DEACTIVATE_PROMPT, undefined, { |
| 81 | + selection: selection ? telemetrySelections[prompts.indexOf(selection)] : undefined, |
| 82 | + }); |
| 83 | + if (selection === prompts[0]) { |
| 84 | + const url = `https://aka.ms/AAmx2ft`; |
| 85 | + this.browserService.launch(url); |
| 86 | + } |
| 87 | + if (selection === prompts[1] || selection === prompts[2]) { |
| 88 | + await notificationPromptEnabled.updateValue(false); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments