|
| 1 | +// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package software.aws.toolkits.jetbrains.core.plugins |
| 5 | + |
| 6 | +import com.intellij.ide.plugins.IdeaPluginDescriptor |
| 7 | +import com.intellij.ide.plugins.InstalledPluginsState |
| 8 | +import com.intellij.notification.NotificationAction |
| 9 | +import com.intellij.openapi.application.ApplicationManager |
| 10 | +import com.intellij.openapi.application.runInEdt |
| 11 | +import com.intellij.openapi.components.service |
| 12 | +import com.intellij.openapi.extensions.PluginId |
| 13 | +import com.intellij.openapi.options.ShowSettingsUtil |
| 14 | +import com.intellij.openapi.progress.ProgressIndicator |
| 15 | +import com.intellij.openapi.progress.ProgressManager |
| 16 | +import com.intellij.openapi.progress.Task |
| 17 | +import com.intellij.openapi.updateSettings.impl.PluginDownloader |
| 18 | +import com.intellij.openapi.updateSettings.impl.PluginDownloader.compareVersionsSkipBrokenAndIncompatible |
| 19 | +import com.intellij.openapi.updateSettings.impl.UpdateChecker |
| 20 | +import com.intellij.util.Alarm |
| 21 | +import com.intellij.util.concurrency.annotations.RequiresBackgroundThread |
| 22 | +import org.jetbrains.annotations.VisibleForTesting |
| 23 | +import software.aws.toolkits.core.utils.debug |
| 24 | +import software.aws.toolkits.core.utils.getLogger |
| 25 | +import software.aws.toolkits.jetbrains.AwsToolkit |
| 26 | +import software.aws.toolkits.jetbrains.settings.AwsSettings |
| 27 | +import software.aws.toolkits.jetbrains.settings.AwsSettingsConfigurable |
| 28 | +import software.aws.toolkits.jetbrains.utils.notifyInfo |
| 29 | +import software.aws.toolkits.resources.message |
| 30 | +import software.aws.toolkits.telemetry.Component |
| 31 | +import software.aws.toolkits.telemetry.Result |
| 32 | +import software.aws.toolkits.telemetry.ToolkitTelemetry |
| 33 | + |
| 34 | +class ToolkitUpdateManager { |
| 35 | + private val alarm = Alarm(Alarm.ThreadToUse.SWING_THREAD) |
| 36 | + |
| 37 | + init { |
| 38 | + runTask() |
| 39 | + } |
| 40 | + |
| 41 | + @VisibleForTesting |
| 42 | + internal fun runTask() { |
| 43 | + if (alarm.isDisposed) return |
| 44 | + scheduleUpdateTask() |
| 45 | + |
| 46 | + val enabled = AwsSettings.getInstance().isAutoUpdateEnabled |
| 47 | + LOG.debug { "AWS Toolkit checking for new updates. Auto update enabled: $enabled" } |
| 48 | + |
| 49 | + if (!enabled) return |
| 50 | + |
| 51 | + runInEdt { |
| 52 | + ProgressManager.getInstance().run( |
| 53 | + object : Task.Backgroundable(null, message("aws.settings.auto_update.progress.message")) { |
| 54 | + override fun run(indicator: ProgressIndicator) { |
| 55 | + checkForUpdates(indicator) |
| 56 | + } |
| 57 | + } |
| 58 | + ) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private fun scheduleUpdateTask() { |
| 63 | + alarm.addRequest({ runTask() }, UPDATE_CHECK_INTERVAL_IN_MS) |
| 64 | + } |
| 65 | + |
| 66 | + @RequiresBackgroundThread |
| 67 | + fun checkForUpdates(progressIndicator: ProgressIndicator) { |
| 68 | + // Note: This will need to handle exceptions and ensure thread-safety |
| 69 | + try { |
| 70 | + // wasUpdatedWithRestart means that, it was an update and it needs to restart to apply |
| 71 | + if (InstalledPluginsState.getInstance().wasUpdatedWithRestart(PluginId.getId(AwsToolkit.PLUGIN_ID))) { |
| 72 | + LOG.debug { "AWS Toolkit was recently updated and needed restart, not performing auto-update again" } |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + val toolkitPlugin = AwsToolkit.DESCRIPTOR as IdeaPluginDescriptor? ?: return |
| 77 | + if (!toolkitPlugin.isEnabled) { |
| 78 | + LOG.debug { "AWS Toolkit is disabled, not performing auto-update" } |
| 79 | + return |
| 80 | + } |
| 81 | + LOG.debug { "Current version: ${toolkitPlugin.version}" } |
| 82 | + val latestToolkitPluginDownloader = getUpdate(toolkitPlugin) |
| 83 | + if (latestToolkitPluginDownloader == null) { |
| 84 | + LOG.debug { "No newer version found, not performing auto-update" } |
| 85 | + return |
| 86 | + } else { |
| 87 | + LOG.debug { "Found newer version: ${latestToolkitPluginDownloader.pluginVersion}" } |
| 88 | + } |
| 89 | + |
| 90 | + if (!latestToolkitPluginDownloader.prepareToInstall(progressIndicator)) return |
| 91 | + latestToolkitPluginDownloader.install() |
| 92 | + ToolkitTelemetry.showAction( |
| 93 | + project = null, |
| 94 | + success = true, |
| 95 | + id = SOURCE_AUTO_UPDATE_FINISH_NOTIFY, |
| 96 | + source = SOURCE_AUTO_UPDATE_FINISH_NOTIFY, |
| 97 | + component = Component.Filesystem |
| 98 | + ) |
| 99 | + } catch (e: Exception) { |
| 100 | + LOG.debug(e) { "Unable to update AWS Toolkit" } |
| 101 | + ToolkitTelemetry.showAction( |
| 102 | + project = null, |
| 103 | + success = false, |
| 104 | + id = SOURCE_AUTO_UPDATE_FINISH_NOTIFY, |
| 105 | + source = SOURCE_AUTO_UPDATE_FINISH_NOTIFY, |
| 106 | + component = Component.Filesystem, |
| 107 | + reason = e.message |
| 108 | + ) |
| 109 | + return |
| 110 | + } catch (e: Error) { |
| 111 | + // Handle cases like NoSuchMethodError when the API is not available in certain versions |
| 112 | + LOG.debug(e) { "Unable to update AWS Toolkit" } |
| 113 | + ToolkitTelemetry.showAction( |
| 114 | + project = null, |
| 115 | + success = false, |
| 116 | + id = SOURCE_AUTO_UPDATE_FINISH_NOTIFY, |
| 117 | + source = SOURCE_AUTO_UPDATE_FINISH_NOTIFY, |
| 118 | + component = Component.Filesystem, |
| 119 | + reason = e.message |
| 120 | + ) |
| 121 | + return |
| 122 | + } |
| 123 | + if (!AwsSettings.getInstance().isAutoUpdateNotificationEnabled) return |
| 124 | + notifyInfo( |
| 125 | + title = message("aws.notification.auto_update.title"), |
| 126 | + content = message("aws.settings.auto_update.notification.message"), |
| 127 | + project = null, |
| 128 | + notificationActions = listOf( |
| 129 | + NotificationAction.createSimpleExpiring(message("aws.settings.auto_update.notification.yes")) { |
| 130 | + ToolkitTelemetry.invokeAction( |
| 131 | + project = null, |
| 132 | + result = Result.Succeeded, |
| 133 | + id = "autoUpdateActionRestart", |
| 134 | + source = SOURCE_AUTO_UPDATE_FINISH_NOTIFY, |
| 135 | + component = Component.Filesystem |
| 136 | + ) |
| 137 | + ApplicationManager.getApplication().restart() |
| 138 | + }, |
| 139 | + NotificationAction.createSimpleExpiring(message("aws.settings.auto_update.notification.no")) { |
| 140 | + ToolkitTelemetry.invokeAction( |
| 141 | + project = null, |
| 142 | + result = Result.Succeeded, |
| 143 | + id = "autoUpdateActionNotNow", |
| 144 | + source = SOURCE_AUTO_UPDATE_FINISH_NOTIFY, |
| 145 | + component = Component.Filesystem |
| 146 | + ) |
| 147 | + }, |
| 148 | + NotificationAction.createSimple(message("aws.notification.auto_update.settings.title")) { |
| 149 | + ToolkitTelemetry.invokeAction( |
| 150 | + project = null, |
| 151 | + result = Result.Succeeded, |
| 152 | + id = ID_ACTION_AUTO_UPDATE_SETTINGS, |
| 153 | + source = SOURCE_AUTO_UPDATE_FINISH_NOTIFY, |
| 154 | + component = Component.Filesystem |
| 155 | + ) |
| 156 | + ShowSettingsUtil.getInstance().showSettingsDialog(null, AwsSettingsConfigurable::class.java) |
| 157 | + } |
| 158 | + ) |
| 159 | + ) |
| 160 | + } |
| 161 | + |
| 162 | + @VisibleForTesting |
| 163 | + internal fun getUpdate(pluginDescriptor: IdeaPluginDescriptor): PluginDownloader? = |
| 164 | + getUpdateInfo().firstOrNull { |
| 165 | + it.id == pluginDescriptor.pluginId && |
| 166 | + compareVersionsSkipBrokenAndIncompatible(it.pluginVersion, pluginDescriptor) > 0 |
| 167 | + } |
| 168 | + |
| 169 | + // TODO: Optimize this to only search the result for AWS Toolkit |
| 170 | + @VisibleForTesting |
| 171 | + internal fun getUpdateInfo(): Collection<PluginDownloader> = UpdateChecker.getPluginUpdates() ?: emptyList() |
| 172 | + |
| 173 | + companion object { |
| 174 | + fun getInstance(): ToolkitUpdateManager = service() |
| 175 | + private val LOG = getLogger<ToolkitUpdateManager>() |
| 176 | + private const val UPDATE_CHECK_INTERVAL_IN_MS = 4 * 60 * 60 * 1000 // 4 hours |
| 177 | + private const val SOURCE_AUTO_UPDATE_FINISH_NOTIFY = "autoUpdateFinishNotification" |
| 178 | + const val SOURCE_AUTO_UPDATE_FEATURE_INTRO_NOTIFY = "autoUpdateFeatureIntroNotification" |
| 179 | + const val ID_ACTION_AUTO_UPDATE_SETTINGS = "autoUpdateActionSettings" |
| 180 | + } |
| 181 | +} |
0 commit comments