Skip to content

Commit

Permalink
fix: Detect Vaadin projects if dependencies are updated (#97)
Browse files Browse the repository at this point in the history
* Use ModuleRootListener to detect Vaadin project

* Use single Vaadin detection
  • Loading branch information
MarcinVaadin authored Sep 25, 2024
1 parent 6fe7726 commit 13df6d9
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 53 deletions.
33 changes: 33 additions & 0 deletions src/main/kotlin/com/vaadin/plugin/VaadinProjectDetector.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.vaadin.plugin

import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ModuleRootEvent
import com.intellij.openapi.roots.ModuleRootListener
import com.intellij.openapi.startup.ProjectActivity
import com.vaadin.plugin.listeners.VaadinProjectListener
import com.vaadin.plugin.utils.VaadinProjectUtil.Companion.isVaadinProject

class VaadinProjectDetector : ModuleRootListener, ProjectActivity {

private val LOG: Logger = Logger.getInstance(VaadinProjectDetector::class.java)

override fun rootsChanged(event: ModuleRootEvent) {
if (isVaadinProject(event.project)) {
doNotifyAboutVaadinProject(event.project)
LOG.info("Vaadin detected in dependencies of " + event.project.name)
}
}

override suspend fun execute(project: Project) {
if (isVaadinProject(project)) {
doNotifyAboutVaadinProject(project)
LOG.info("Vaadin detected during startup of " + project.name)
}
}

private fun doNotifyAboutVaadinProject(project: Project) {
val publisher: VaadinProjectListener = project.messageBus.syncPublisher(VaadinProjectListener.TOPIC)
publisher.vaadinProjectDetected(project)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.vaadin.plugin.copilot.listeners

import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.project.ProjectManagerListener
import com.vaadin.plugin.copilot.CopilotPluginUtil
import com.vaadin.plugin.listeners.VaadinProjectListener

class CopilotVaadinProjectListener : VaadinProjectListener {

private var triggered = false

override fun vaadinProjectDetected(project: Project) {
if (!triggered) {
triggered = true
createDotFile(project)
removeDotFileOnExit(project)
}
}

private fun createDotFile(project: Project) {
val dotFileDirectory = CopilotPluginUtil.getDotFileDirectory(project)
if (dotFileDirectory == null) {
CopilotPluginUtil.createIdeaDirectoryIfMissing(project)
}
CopilotPluginUtil.saveDotFile(project)
}

private fun removeDotFileOnExit(project: Project) {
ProjectManager.getInstance()
.addProjectManagerListener(
project,
object : ProjectManagerListener {
override fun projectClosing(project: Project) {
CopilotPluginUtil.removeDotFile(project)
}
},
)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.vaadin.plugin.activity
package com.vaadin.plugin.listeners

import com.intellij.debugger.JavaDebuggerBundle
import com.intellij.debugger.settings.DebuggerSettings
Expand All @@ -11,7 +11,6 @@ import com.intellij.notification.Notifications
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.options.ShowSettingsUtil
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
import com.intellij.openapi.vcs.VcsBundle
import com.intellij.openapi.vcs.VcsConfiguration
import com.intellij.openapi.vcs.VcsShowConfirmationOption
Expand All @@ -20,7 +19,7 @@ import com.vaadin.plugin.copilot.CopilotPluginUtil
import com.vaadin.plugin.utils.VaadinHomeUtil
import com.vaadin.plugin.utils.VaadinIcons

class ConfigurationCheckPostStartupProjectActivity : ProjectActivity {
class ConfigurationCheckVaadinProjectListener : VaadinProjectListener {

companion object {
const val NOTIFICATION_GROUP = "Vaadin Configuration Check"
Expand All @@ -38,11 +37,16 @@ class ConfigurationCheckPostStartupProjectActivity : ProjectActivity {
val MESSAGE_DONT_ASK_AGAIN = IdeCoreBundle.message("dialog.options.do.not.ask")
}

override suspend fun execute(project: Project) {
checkReloadClassesSetting(project)
checkVcsAddConfirmationSetting(project)
RunOnceUtil.runOnceForApp("hotswap-version-check-" + CopilotPluginUtil.getPluginVersion()) {
VaadinHomeUtil.updateOrInstallHotSwapJar()
private var triggered = false

override fun vaadinProjectDetected(project: Project) {
if (!triggered) {
triggered = true
checkReloadClassesSetting(project)
checkVcsAddConfirmationSetting(project)
RunOnceUtil.runOnceForApp("hotswap-version-check-" + CopilotPluginUtil.getPluginVersion()) {
VaadinHomeUtil.updateOrInstallHotSwapJar()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.vaadin.plugin.listeners

import com.intellij.openapi.project.Project
import com.intellij.util.messages.Topic
import com.intellij.util.messages.Topic.ProjectLevel
import java.util.*

interface VaadinProjectListener : EventListener {

companion object {
@ProjectLevel
val TOPIC: Topic<VaadinProjectListener> =
Topic.create("Vaadin project detected", VaadinProjectListener::class.java)
}

fun vaadinProjectDetected(project: Project) {}
}
14 changes: 8 additions & 6 deletions src/main/kotlin/com/vaadin/plugin/utils/VaadinProjectUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,16 @@ class VaadinProjectUtil {
}

fun isVaadinProject(project: Project): Boolean {
return ModuleManager.getInstance(project).modules.any { isVaadinModule(it) }
}

fun isVaadinModule(module: com.intellij.openapi.module.Module): Boolean {
var hasVaadin = false
ModuleManager.getInstance(project).modules.forEach { module ->
ModuleRootManager.getInstance(module).orderEntries().forEachLibrary { library: Library ->
if (library.name?.contains(VAADIN_LIB_PREFIX) == true) {
hasVaadin = true
}
true
ModuleRootManager.getInstance(module).orderEntries().forEachLibrary { library: Library ->
if (library.name?.contains(VAADIN_LIB_PREFIX) == true) {
hasVaadin = true
}
true
}
return hasVaadin
}
Expand Down
20 changes: 16 additions & 4 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
<!-- Extension points defined by the plugin.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -->
<extensions defaultExtensionNs="com.intellij">

<backgroundPostStartupActivity
implementation="com.vaadin.plugin.VaadinProjectDetector"/>

<!-- Vaadin Copilot -->
<notificationGroup id="Vaadin Copilot" displayType="BALLOON"/>
<backgroundPostStartupActivity
implementation="com.vaadin.plugin.copilot.activity.CopilotPostStartupProjectActivity"/>
<errorHandler implementation="com.vaadin.plugin.copilot.CopilotErrorHandler"/>
<statusBarWidgetFactory implementation="com.vaadin.plugin.copilot.CopilotStatusBarWidgetFactory"
id="CopilotStatusBarWidgetFactory" order="last"/>
Expand All @@ -62,8 +64,6 @@
<executor implementation="com.vaadin.plugin.hotswapagent.HotswapAgentExecutor"/>

<!-- Configuration check -->
<backgroundPostStartupActivity
implementation="com.vaadin.plugin.activity.ConfigurationCheckPostStartupProjectActivity"/>
<notificationGroup id="Vaadin Configuration Check" displayType="STICKY_BALLOON"/>
</extensions>

Expand All @@ -85,4 +85,16 @@
</action>
</actions>

<projectListeners>
<listener
class="com.vaadin.plugin.VaadinProjectDetector"
topic="com.intellij.openapi.roots.ModuleRootListener"/>
<listener
class="com.vaadin.plugin.copilot.listeners.CopilotVaadinProjectListener"
topic="com.vaadin.plugin.listeners.VaadinProjectListener"/>
<listener
class="com.vaadin.plugin.listeners.ConfigurationCheckVaadinProjectListener"
topic="com.vaadin.plugin.listeners.VaadinProjectListener" />
</projectListeners>

</idea-plugin>

0 comments on commit 13df6d9

Please sign in to comment.