|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE.md in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { callWithTelemetryAndErrorHandling, IActionContext } from '@microsoft/vscode-azext-utils'; |
| 7 | +import * as vscode from 'vscode'; |
| 8 | +import { ext } from '../extensionVariables'; |
| 9 | + |
| 10 | +const dontShowAgainKey = 'vscode-docker.migrateToContainerTools.dontShowAgain'; |
| 11 | + |
| 12 | +const reminderIntervalMs = 60 * 60 * 60 * 1000; // 60 hours (2.5 days) in milliseconds |
| 13 | +const lastShownKey = 'vscode-docker.migrateToContainerTools.lastShown'; |
| 14 | + |
| 15 | +export async function migrateToContainerTools(): Promise<void> { |
| 16 | + const shouldInstall = await callWithTelemetryAndErrorHandling<boolean>('vscode-docker.migrateToContainerTools', async (context: IActionContext) => { |
| 17 | + context.telemetry.properties.isActivationEvent = 'true'; |
| 18 | + |
| 19 | + // Check if the user has already opted out of the notification |
| 20 | + const dontShowAgain = ext.context.globalState.get<boolean>(dontShowAgainKey, false); |
| 21 | + |
| 22 | + if (dontShowAgain) { |
| 23 | + context.telemetry.suppressAll = true; |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + // Check if the user has already seen the notification within the reminder interval |
| 28 | + const lastShown = ext.context.globalState.get<number>(lastShownKey, 0); |
| 29 | + const now = Date.now(); |
| 30 | + if (lastShown && now - lastShown < reminderIntervalMs) { |
| 31 | + context.telemetry.suppressAll = true; |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + // Update the last shown time to now |
| 36 | + await ext.context.globalState.update(lastShownKey, now); |
| 37 | + |
| 38 | + const message = vscode.l10n.t('The Docker extension is becoming the Container Tools extension, which you can install and try now. Installing the Container Tools extension will uninstall the Docker extension and the window will be reloaded. Make sure to save your changes before proceeding. [Learn More](https://aka.ms/vscode-container-tools-learn-more)'); |
| 39 | + |
| 40 | + const tryNowButton = vscode.l10n.t('Install and Reload'); |
| 41 | + const remindMeButton = vscode.l10n.t('Remind Me'); |
| 42 | + const dontShowAgainButton = vscode.l10n.t('Don\'t Show Again'); |
| 43 | + |
| 44 | + // Show the notification |
| 45 | + const result = await vscode.window.showInformationMessage(message, tryNowButton, remindMeButton, dontShowAgainButton); |
| 46 | + |
| 47 | + if (result === tryNowButton) { |
| 48 | + context.telemetry.properties.choice = 'install'; |
| 49 | + return true; |
| 50 | + } else if (result === remindMeButton) { |
| 51 | + context.telemetry.properties.choice = 'remindMe'; |
| 52 | + return false; |
| 53 | + } else if (result === dontShowAgainButton) { |
| 54 | + context.telemetry.properties.choice = 'dontShowAgain'; |
| 55 | + await ext.context.globalState.update(dontShowAgainKey, true); |
| 56 | + return false; |
| 57 | + } else { |
| 58 | + // User closed the notification |
| 59 | + context.telemetry.properties.choice = 'closed'; |
| 60 | + return false; |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + if (shouldInstall) { |
| 65 | + // Install the Container Tools extension |
| 66 | + await vscode.commands.executeCommand('workbench.extensions.installExtension', 'ms-azuretools.vscode-containers'); |
| 67 | + |
| 68 | + // Uninstall the Docker extension |
| 69 | + await vscode.commands.executeCommand('workbench.extensions.uninstallExtension', 'ms-azuretools.vscode-docker'); |
| 70 | + |
| 71 | + // Reload the window to apply changes |
| 72 | + await vscode.commands.executeCommand('workbench.action.reloadWindow'); |
| 73 | + } |
| 74 | +} |
0 commit comments