Skip to content

Commit

Permalink
Use ModuleRootListener to detect Vaadin project
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinVaadin committed Sep 24, 2024
1 parent 6fe7726 commit 7bd831e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,10 @@ import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.project.ProjectManagerListener
import com.intellij.openapi.startup.ProjectActivity
import com.vaadin.plugin.copilot.CopilotPluginUtil
import com.vaadin.plugin.utils.VaadinProjectUtil
import org.jetbrains.ide.BuiltInServerManager

class CopilotPostStartupProjectActivity : ProjectActivity {

override suspend fun execute(project: Project) {

BuiltInServerManager.getInstance().waitForStart()

if (VaadinProjectUtil.isVaadinProject(project)) {
val dotFileDirectory = CopilotPluginUtil.getDotFileDirectory(project)
if (dotFileDirectory == null) {
CopilotPluginUtil.createIdeaDirectoryIfMissing(project)
}
CopilotPluginUtil.saveDotFile(project)
}

ProjectManager.getInstance()
.addProjectManagerListener(
project,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.vaadin.plugin.copilot.listeners

import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ModuleRootEvent
import com.intellij.openapi.roots.ModuleRootListener
import com.vaadin.plugin.copilot.CopilotPluginUtil
import com.vaadin.plugin.utils.VaadinProjectUtil.Companion.isVaadinProject

class CopilotModuleRootListener : ModuleRootListener {

override fun rootsChanged(event: ModuleRootEvent) {
if (isVaadinProject(event.project)) {
createDotFile(event.project)
}
}

private fun createDotFile(project: Project) {
val dotFileDirectory = CopilotPluginUtil.getDotFileDirectory(project)
if (dotFileDirectory == null) {
CopilotPluginUtil.createIdeaDirectoryIfMissing(project)
}
CopilotPluginUtil.saveDotFile(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
8 changes: 8 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
<backgroundPostStartupActivity
implementation="com.vaadin.plugin.activity.ConfigurationCheckPostStartupProjectActivity"/>
<notificationGroup id="Vaadin Configuration Check" displayType="STICKY_BALLOON"/>

<dependencySupport kind="java" coordinate="com.vaadin:vaadin" displayName="Vaadin"/>
</extensions>

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

<projectListeners>
<listener
class="com.vaadin.plugin.copilot.listeners.CopilotModuleRootListener"
topic="com.intellij.openapi.roots.ModuleRootListener"/>
</projectListeners>

</idea-plugin>

0 comments on commit 7bd831e

Please sign in to comment.