From 095588ae124d546d0cc13bc2fdf9fa1060f32517 Mon Sep 17 00:00:00 2001 From: marcin Date: Fri, 12 Apr 2024 09:12:54 +0200 Subject: [PATCH] detect Vaadin based on pom and gradle --- .../plugin/copilot/CopilotPluginUtil.kt | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt b/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt index 5c21fca..1c14962 100644 --- a/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt +++ b/src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt @@ -13,10 +13,7 @@ import com.intellij.openapi.components.service import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.extensions.PluginId import com.intellij.openapi.fileTypes.FileTypeManager -import com.intellij.openapi.module.ModuleManager import com.intellij.openapi.project.Project -import com.intellij.openapi.roots.ModuleRootManager -import com.intellij.openapi.roots.libraries.Library import com.intellij.openapi.util.IconLoader import com.intellij.openapi.vfs.VfsUtil import com.intellij.psi.PsiDirectory @@ -30,7 +27,9 @@ import com.vaadin.plugin.copilot.service.CopilotServerService import java.io.BufferedWriter import java.io.File import java.io.StringWriter +import java.nio.file.Files import java.util.* +import kotlin.io.path.Path class CopilotPluginUtil { @@ -51,7 +50,7 @@ class CopilotPluginUtil { private val COPILOT_ICON = IconLoader.getIcon("/icons/copilot.svg", CopilotPluginUtil::class.java) - private var isVaadinProject = false + private var isVaadinProject: Boolean? = null private enum class HANDLERS(val command: String) { WRITE("write"), @@ -63,19 +62,34 @@ class CopilotPluginUtil { private val pluginVersion = PluginManagerCore.getPlugin(PluginId.getId("com.vaadin.intellij-plugin"))?.version fun isVaadinProject(project: Project): Boolean { - // after first opening project, when libs are not analyzed yet this will return false - if (!isVaadinProject) { - for (module in ModuleManager.getInstance(project).modules) { - ModuleRootManager.getInstance(module).orderEntries().forEachLibrary { library: Library -> - if (library.name?.contains(VAADIN_LIB_PREFIX) == true) { - isVaadinProject = true - return@forEachLibrary true - } - true - } - } + if (project.basePath == null) { + return false + } + + if (isVaadinProject != null) { + return isVaadinProject!! } - return isVaadinProject + + val containsVaadinDeps = fun(file: String): Boolean { + return Files.readString(Path(project.basePath!!, file)).contains(VAADIN_LIB_PREFIX) + } + + // Maven projects + if (File(project.basePath, "pom.xml").exists()) { + isVaadinProject = containsVaadinDeps("pom.xml") + } + + // Gradle projects + if (File(project.basePath, "build.gradle").exists()) { + isVaadinProject = containsVaadinDeps("build.gradle") + } + + // Gradle Kotlin projects + if (File(project.basePath, "build.gradle.kts").exists()) { + isVaadinProject = containsVaadinDeps("build.gradle.kts") + } + + return isVaadinProject ?: false } fun notify(content: String, type: NotificationType) { @@ -83,8 +97,10 @@ class CopilotPluginUtil { } fun notify(content: String, type: NotificationType, project: Project?) { - Notifications.Bus.notify(Notification(NOTIFICATION_GROUP, content, type) - .setIcon(COPILOT_ICON), project) + Notifications.Bus.notify( + Notification(NOTIFICATION_GROUP, content, type) + .setIcon(COPILOT_ICON), project + ) } fun isServerRunning(project: Project): Boolean {