Skip to content

Commit

Permalink
Add version check to IntelliJ Extension (metalbear-co#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
infiniteregrets authored and tamasfe committed Oct 10, 2022
1 parent 8cbf1bd commit ecc8d86
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
- Support impersonated deployments, closes [[#293](https://github.com/metalbear-co/mirrord/issues/293)]
- Shorter way to select which deployment/pod/container to impersonate through `--target` or `MIRRORD_IMPERSONATED_TARGET`, closes [[#392](https://github.com/metalbear-co/mirrord/issues/392)]
- mirrord-layer: Support config from file alongside environment variables.
- intellij-ext: Add version check, closes [[#289](https://github.com/metalbear-co/mirrord/issues/289)]
- intellij-ext: better support for Windows with WSL.

### Deprecated
Expand Down
1 change: 1 addition & 0 deletions intellij-ext/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies {
exclude(group = "org.slf4j", module = "slf4j-api")
exclude(group = "org.yaml", module = "snakeyaml")
}
implementation("com.github.zafarkhaja:java-semver:0.9.0")
}

// Configure Gradle IntelliJ Plugin - read more: https://github.com/JetBrains/gradle-intellij-plugin
Expand Down
2 changes: 1 addition & 1 deletion intellij-ext/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ gradleVersion = 7.5.1
# Opt-out flag for bundling Kotlin standard library.
# See https://plugins.jetbrains.com/docs/intellij/kotlin.html#kotlin-standard-library for details.
# suppress inspection "UnusedProperty"
kotlin.stdlib.default.dependency = false
kotlin.stdlib.default.dependency = false
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.metalbear.mirrord

import com.intellij.notification.Notification
import com.intellij.notification.NotificationGroup
import com.intellij.notification.NotificationGroupManager
import com.intellij.notification.NotificationType
Expand All @@ -22,9 +23,11 @@ class MirrordEnabler : ToggleAction() {
.notify(project)
}
}
}


fun notifier(message: String, type: NotificationType): Notification {
return notificationManager.createNotification("mirrord", message, type)
}
}
override fun isSelected(e: AnActionEvent): Boolean {
return MirrordListener.enabled
}
Expand Down
62 changes: 62 additions & 0 deletions intellij-ext/src/main/kotlin/com/metalbear/mirrord/VersionCheck.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.metalbear.mirrord

import com.github.zafarkhaja.semver.Version
import com.intellij.ide.plugins.PluginManagerConfigurable
import com.intellij.ide.plugins.PluginManagerCore
import com.intellij.ide.util.PropertiesComponent
import com.intellij.notification.Notification
import com.intellij.notification.NotificationAction
import com.intellij.notification.NotificationType
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.extensions.PluginId
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManagerListener
import java.net.URL


class VersionCheck : ProjectManagerListener {
private val pluginId = PluginId.getId("com.metalbear.mirrord")
private val version: String? = PluginManagerCore.getPlugin(pluginId)?.version
private val versionCheckEndpoint: String =
"https://version.mirrord.dev/get-latest-version?source=3&version=$version"
private var versionCheckDisabled
get() = PropertiesComponent.getInstance().getBoolean("versionCheckDisabled", false)
set(value) {
PropertiesComponent.getInstance().setValue("versionCheckDisabled", value)
}

override fun projectOpened(project: Project) {
if (!versionCheckDisabled) {
checkVersion(project)
}
super.projectOpened(project)
}

private fun checkVersion(project: Project) {
val remoteVersion = Version.valueOf(URL(versionCheckEndpoint).readText())
val localVersion = Version.valueOf(version)
if (localVersion.lessThan(remoteVersion)) {
MirrordEnabler.notifier(
"Your version of mirrord is outdated, you should update.",
NotificationType.INFORMATION
)
.addAction(object : NotificationAction("Update") {
override fun actionPerformed(e: AnActionEvent, notification: Notification) {
try {
PluginManagerConfigurable.showPluginConfigurable(project, listOf(pluginId))
} catch (e: Exception) {
notification.expire()
}
notification.expire()
}
})
.addAction(object : NotificationAction("Don't show again") {
override fun actionPerformed(e: AnActionEvent, notification: Notification) {
versionCheckDisabled = true
notification.expire()
}
}).notify(project)
}
return
}
}
2 changes: 2 additions & 0 deletions intellij-ext/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<projectListeners>
<listener class="com.metalbear.mirrord.MirrordListener"
topic="com.intellij.execution.ExecutionListener"/>
<listener class="com.metalbear.mirrord.VersionCheck"
topic="com.intellij.openapi.project.ProjectManagerListener"/>
</projectListeners>

<actions>
Expand Down

0 comments on commit ecc8d86

Please sign in to comment.