Skip to content

Commit

Permalink
bring back RunOnce, add action to manually install or update hotswap-…
Browse files Browse the repository at this point in the history
…agent.jar in case of issues (#72)
  • Loading branch information
MarcinVaadin authored Sep 6, 2024
1 parent f312ca3 commit 915672d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.vaadin.plugin.actions

import com.intellij.notification.NotificationType
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.vaadin.plugin.copilot.CopilotPluginUtil
import com.vaadin.plugin.utils.VaadinHomeUtil

class InstallOrUpdateHotSwapAction : AnAction() {

override fun actionPerformed(p0: AnActionEvent) {
val version = VaadinHomeUtil.updateOrInstallHotSwapJar()
if (version != null) {
CopilotPluginUtil.notify("hotswap-agent.jar:$version installed", NotificationType.INFORMATION, p0.project)
} else {
CopilotPluginUtil.notify(
"Installation of hotswap-agent.jar failed, see logs for details",
NotificationType.ERROR,
p0.project
)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.vaadin.plugin.activity
import com.intellij.debugger.JavaDebuggerBundle
import com.intellij.debugger.settings.DebuggerSettings
import com.intellij.ide.IdeCoreBundle
import com.intellij.ide.util.RunOnceUtil
import com.intellij.notification.Notification
import com.intellij.notification.NotificationAction
import com.intellij.notification.NotificationType
Expand All @@ -15,6 +16,7 @@ import com.intellij.openapi.vcs.VcsBundle
import com.intellij.openapi.vcs.VcsConfiguration
import com.intellij.openapi.vcs.VcsShowConfirmationOption
import com.intellij.openapi.vcs.impl.ProjectLevelVcsManagerImpl
import com.vaadin.plugin.copilot.CopilotPluginUtil
import com.vaadin.plugin.utils.VaadinHomeUtil
import com.vaadin.plugin.utils.VaadinIcons

Expand All @@ -39,7 +41,9 @@ class ConfigurationCheckPostStartupProjectActivity : ProjectActivity {
override suspend fun execute(project: Project) {
checkReloadClassesSetting(project)
checkVcsAddConfirmationSetting(project)
VaadinHomeUtil.updateOrInstallHotSwapJar()
RunOnceUtil.runOnceForApp("hotswap-version-check-" + CopilotPluginUtil.getPluginVersion()) {
VaadinHomeUtil.updateOrInstallHotSwapJar()
}
}

private fun checkReloadClassesSetting(project: Project) {
Expand Down
19 changes: 13 additions & 6 deletions src/main/kotlin/com/vaadin/plugin/utils/VaadinHomeUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ object VaadinHomeUtil {

/**
* Gets the hotswap-agent.jar location in ~/.vaadin.
* If the file does not exist, copies the bundled version.
*
* @return the hotswap-agent.jar file
*/
fun getHotSwapAgentJar(): File {
// might only happen if user removes hotswap-agent.jar manually after plugin is already installed
if (!hotSwapAgentJarFile.exists()) {
throw IllegalStateException(
"hotswap-agent.jar not found in " + intellijFolder.path +
", please restart IntelliJ to fix the issue"
"hotswap-agent.jar is not present, run \"HotSwap: Install or update\" action to install"
)
}
return hotSwapAgentJarFile
}

/**
* Installs or updates hotswap-agent.jar in ~/.vaadin
*
* @return version of installed hotswap-agent.jar or null in case of error
*/
fun updateOrInstallHotSwapJar() {
fun updateOrInstallHotSwapJar(): String? {
try {
val bundledHotswap = this.javaClass.classLoader.getResource(HOTSWAP_AGENT_JAR_FILE_NAME)
?: throw IllegalStateException("The plugin package is broken: no hotswap-agent.jar found")
Expand All @@ -55,7 +55,9 @@ object VaadinHomeUtil {
FileUtil.createParentDirs(hotSwapAgentJarFile)
) { "Unable to create directory for hotswap-agent.jar" }
FileUtil.copy(bundledHotswap.openStream(), hotSwapAgentJarFile.outputStream())
LOG.info("Installed hotswap-agent.jar version: " + getHotswapAgentVersion(hotSwapAgentJarFile))
val version = getHotswapAgentVersion(hotSwapAgentJarFile)
LOG.info("Installed hotswap-agent.jar version: $version")
return version
} catch (e: IOException) {
throw IllegalStateException(
"Unable to copy hotswap-agent.jar to " + hotSwapAgentJarFile.absolutePath,
Expand All @@ -65,18 +67,23 @@ object VaadinHomeUtil {
} else if (isBundledVersionNewer()) {
try {
FileUtil.copy(bundledHotswap.openStream(), hotSwapAgentJarFile.outputStream())
LOG.info("Updated hotswap-agent.jar to version " + getHotswapAgentVersion(hotSwapAgentJarFile))
val version = getHotswapAgentVersion(hotSwapAgentJarFile)
LOG.info("Updated hotswap-agent.jar to version $version")
return version
} catch (e: IOException) {
throw IllegalStateException(
"Unable to update hotswap-agent.jar",
e
)
}
} else {
val version = getHotswapAgentVersion(hotSwapAgentJarFile)
LOG.info("Using existing hotswap-agent.jar version " + getHotswapAgentVersion(hotSwapAgentJarFile))
return version
}
} catch (e: Exception) {
LOG.error(e.message, e)
return null
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,13 @@
<notificationGroup id="Vaadin Configuration Check" displayType="STICKY_BALLOON"/>
</extensions>

<actions>
<action
id="HotSwap.installOrUpdate"
class="com.vaadin.plugin.actions.InstallOrUpdateHotSwapAction"
text="HotSwap: Install or update"
description="Installs or updates hotswap-agent.jar">
</action>
</actions>

</idea-plugin>

0 comments on commit 915672d

Please sign in to comment.