Skip to content

Commit

Permalink
detect Vaadin based on pom and gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinVaadin committed Apr 12, 2024
1 parent 4671d9a commit 095588a
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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"),
Expand All @@ -63,28 +62,45 @@ 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) {
notify(content, type, null)
}

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 {
Expand Down

0 comments on commit 095588a

Please sign in to comment.