|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { IApplicationEnvironment } from '../../common/application/types'; |
| 5 | +import { IPersistentState, IPersistentStateFactory } from '../../common/types'; |
| 6 | +import { Common, ToolsExtensions } from '../../common/utils/localize'; |
| 7 | +import { executeCommand } from '../../common/vscodeApis/commandApis'; |
| 8 | +import { isExtensionDisabled, isExtensionEnabled } from '../../common/vscodeApis/extensionsApi'; |
| 9 | +import { showInformationMessage } from '../../common/vscodeApis/windowApis'; |
| 10 | +import { IServiceContainer } from '../../ioc/types'; |
| 11 | +import { sendTelemetryEvent } from '../../telemetry'; |
| 12 | +import { EventName } from '../../telemetry/constants'; |
| 13 | + |
| 14 | +export const ISORT_EXTENSION = 'ms-python.isort'; |
| 15 | +const ISORT_PROMPT_DONOTSHOW_KEY = 'showISortExtensionPrompt'; |
| 16 | + |
| 17 | +function doNotShowPromptState(serviceContainer: IServiceContainer, promptKey: string): IPersistentState<boolean> { |
| 18 | + const persistFactory: IPersistentStateFactory = serviceContainer.get<IPersistentStateFactory>( |
| 19 | + IPersistentStateFactory, |
| 20 | + ); |
| 21 | + return persistFactory.createWorkspacePersistentState<boolean>(promptKey, false); |
| 22 | +} |
| 23 | + |
| 24 | +export class ISortExtensionPrompt { |
| 25 | + private shownThisSession = false; |
| 26 | + |
| 27 | + public constructor(private readonly serviceContainer: IServiceContainer) {} |
| 28 | + |
| 29 | + public async showPrompt(): Promise<boolean> { |
| 30 | + const isEnabled = isExtensionEnabled(ISORT_EXTENSION); |
| 31 | + if (isEnabled || isExtensionDisabled(ISORT_EXTENSION)) { |
| 32 | + sendTelemetryEvent(EventName.TOOLS_EXTENSIONS_ALREADY_INSTALLED, undefined, { |
| 33 | + extensionId: ISORT_EXTENSION, |
| 34 | + isEnabled, |
| 35 | + }); |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + const doNotShow = doNotShowPromptState(this.serviceContainer, ISORT_PROMPT_DONOTSHOW_KEY); |
| 40 | + if (this.shownThisSession || doNotShow.value) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + sendTelemetryEvent(EventName.TOOLS_EXTENSIONS_PROMPT_SHOWN, undefined, { extensionId: ISORT_EXTENSION }); |
| 45 | + this.shownThisSession = true; |
| 46 | + const response = await showInformationMessage( |
| 47 | + ToolsExtensions.isortPromptMessage, |
| 48 | + ToolsExtensions.installISortExtension, |
| 49 | + Common.doNotShowAgain, |
| 50 | + ); |
| 51 | + |
| 52 | + if (response === Common.doNotShowAgain) { |
| 53 | + await doNotShow.updateValue(true); |
| 54 | + sendTelemetryEvent(EventName.TOOLS_EXTENSIONS_PROMPT_DISMISSED, undefined, { |
| 55 | + extensionId: ISORT_EXTENSION, |
| 56 | + dismissType: 'doNotShow', |
| 57 | + }); |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + if (response === ToolsExtensions.installISortExtension) { |
| 62 | + sendTelemetryEvent(EventName.TOOLS_EXTENSIONS_INSTALL_SELECTED, undefined, { |
| 63 | + extensionId: ISORT_EXTENSION, |
| 64 | + }); |
| 65 | + const appEnv: IApplicationEnvironment = this.serviceContainer.get<IApplicationEnvironment>( |
| 66 | + IApplicationEnvironment, |
| 67 | + ); |
| 68 | + await executeCommand('workbench.extensions.installExtension', ISORT_EXTENSION, { |
| 69 | + installPreReleaseVersion: appEnv.extensionChannel === 'insiders', |
| 70 | + }); |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + sendTelemetryEvent(EventName.TOOLS_EXTENSIONS_PROMPT_DISMISSED, undefined, { |
| 75 | + extensionId: ISORT_EXTENSION, |
| 76 | + dismissType: 'close', |
| 77 | + }); |
| 78 | + |
| 79 | + return false; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +let _prompt: ISortExtensionPrompt | undefined; |
| 84 | +export function getOrCreateISortPrompt(serviceContainer: IServiceContainer): ISortExtensionPrompt { |
| 85 | + if (!_prompt) { |
| 86 | + _prompt = new ISortExtensionPrompt(serviceContainer); |
| 87 | + } |
| 88 | + return _prompt; |
| 89 | +} |
0 commit comments