@@ -13,6 +13,7 @@ import org.digma.intellij.plugin.common.Retries
1313import org.digma.intellij.plugin.common.buildVersionRequest
1414import org.digma.intellij.plugin.common.findActiveProject
1515import org.digma.intellij.plugin.common.newerThan
16+ import org.digma.intellij.plugin.common.olderThan
1617import org.digma.intellij.plugin.errorreporting.ErrorReporter
1718import org.digma.intellij.plugin.log.Log
1819import org.digma.intellij.plugin.paths.DigmaPathManager
@@ -21,6 +22,7 @@ import org.digma.intellij.plugin.posthog.ActivityMonitor
2122import org.digma.intellij.plugin.reload.ReloadService
2223import org.digma.intellij.plugin.reload.ReloadSource
2324import org.digma.intellij.plugin.scheduling.disposingPeriodicTask
25+ import org.digma.intellij.plugin.semanticversion.SemanticVersionUtil
2426import org.digma.intellij.plugin.settings.InternalFileSettings
2527import java.io.File
2628import java.net.HttpURLConnection
@@ -114,6 +116,9 @@ class UIVersioningService(val cs: CoroutineScope) : DisposableAdaptor {
114116 }
115117
116118 private fun setCurrentUiVersion (uiVersion : String ) {
119+ // on every change to current version keep also the plugin version it will help to identify a plugin downgrade
120+ val currentPluginVersion = SemanticVersionUtil .getPluginVersionWithoutBuildNumberAndPreRelease(" unknown" )
121+ PersistenceService .getInstance().setLastUiUpdatePluginVersion(currentPluginVersion)
117122 return PersistenceService .getInstance().setCurrentUiVersion(uiVersion)
118123 }
119124
@@ -146,6 +151,36 @@ class UIVersioningService(val cs: CoroutineScope) : DisposableAdaptor {
146151 setCurrentUiVersion(bundledUiVersion)
147152 }
148153
154+ // first check if there was a plugin downgrade since the last time the current ui version was updated if yes
155+ // switch to the bundled UI
156+ val needToUnpackAfterPluginDowngrade = PersistenceService .getInstance().getLastUiUpdatePluginVersion()?.let { lastUiUpdatePluginVersion ->
157+ val currentPluginVersion = SemanticVersionUtil .getPluginVersionWithoutBuildNumberAndPreRelease(" unknown" )
158+ ComparableVersion (currentPluginVersion).olderThan(ComparableVersion (lastUiUpdatePluginVersion))
159+ } ? : false
160+ if (needToUnpackAfterPluginDowngrade) {
161+ Log .log(
162+ logger::info,
163+ " there was a plugin downgrade, using bundled ui. current version: {}, bundled version: {}" ,
164+ getCurrentUiVersion(),
165+ bundledUiVersion
166+ )
167+ if (unpackUiBundle()) {
168+ deleteUiBundle(getCurrentUiVersion())
169+ getLatestDownloadedVersion()?.let {
170+ deleteUiBundle(it)
171+ setLatestDownloadedVersion(null )
172+ }
173+ setCurrentUiVersion(bundledUiVersion)
174+ findActiveProject()?.let {
175+ ActivityMonitor .getInstance(it).setUIVersion(getCurrentUiVersion())
176+ }
177+ } else {
178+ Log .log(logger::warn, " could not unpack bundled ui version {}" , bundledUiVersion)
179+ }
180+ return
181+ }
182+
183+
149184 // Note:always use the methods getCurrentUiVersion() and getLatestDownloadedVersion() and don't assign to local variables
150185 // because values may change concurrently
151186
@@ -160,7 +195,9 @@ class UIVersioningService(val cs: CoroutineScope) : DisposableAdaptor {
160195
161196 // if we have the latest downloaded file, switch to use it and delete the old version
162197 val latestDownloadedUiVersion = getLatestDownloadedVersion()
163- if (latestDownloadedUiVersion != null ) {
198+ if (latestDownloadedUiVersion != null &&
199+ ComparableVersion (latestDownloadedUiVersion).newerThan(ComparableVersion (getCurrentUiVersion()))
200+ ) {
164201 Log .log(
165202 logger::info,
166203 " got latest downloaded ui version on startup {}, trying to update.." , latestDownloadedUiVersion
@@ -207,13 +244,19 @@ class UIVersioningService(val cs: CoroutineScope) : DisposableAdaptor {
207244 " latestDownloadedUiVersion property exists but file does not exist, not updating"
208245 )
209246
247+ setLatestDownloadedVersion(null )
248+ }
249+ } else {
250+ // in any case if we didn't use latest downloaded reset it if it exists
251+ getLatestDownloadedVersion()?.let {
252+ deleteUiBundle(it)
210253 setLatestDownloadedVersion(null )
211254 }
212255 }
213256
214257
215- // maybe user updated the plugin, and the bundled ui is newer then the current ui version .
216- // this is a valid check even if we had a latestDownloadedVersion and we switched to it above
258+ // this is support for plugin update that have a newer ui bundled with it .
259+ // this is a valid check even if we had a latestDownloadedVersion and we switched to it above.
217260 if (ComparableVersion (bundledUiVersion).newerThan(ComparableVersion (getCurrentUiVersion()))) {
218261 Log .log(
219262 logger::info,
0 commit comments