Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: detect Vaadin based on pom and gradle #30

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 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,8 +50,6 @@ class CopilotPluginUtil {

private val COPILOT_ICON = IconLoader.getIcon("/icons/copilot.svg", CopilotPluginUtil::class.java)

private var isVaadinProject = false

private enum class HANDLERS(val command: String) {
WRITE("write"),
UNDO("undo"),
Expand All @@ -63,28 +60,41 @@ 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) {
MarcinVaadin marked this conversation as resolved.
Show resolved Hide resolved
return false
}

val containsVaadinDeps = fun(file: String): Boolean {
return Files.readString(Path(project.basePath!!, file)).contains(VAADIN_LIB_PREFIX)
MarcinVaadin marked this conversation as resolved.
Show resolved Hide resolved
}

// Maven projects
if (File(project.basePath, "pom.xml").exists()) {
return containsVaadinDeps("pom.xml")
}

// Gradle projects
if (File(project.basePath, "build.gradle").exists()) {
return containsVaadinDeps("build.gradle")
}
return isVaadinProject

// Gradle Kotlin projects
if (File(project.basePath, "build.gradle.kts").exists()) {
return containsVaadinDeps("build.gradle.kts")
}

return 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
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ class CopilotStatusBarPanel(project: Project) : EditorBasedStatusBarPopup(projec
}

override fun getWidgetState(file: VirtualFile?): WidgetState {
if (!CopilotPluginUtil.isVaadinProject(project)) {
return WidgetState.HIDDEN
}

val state = WidgetState("Vaadin", null, true)
state.icon = IconManager.getInstance().getIcon("/icons/vaadin.svg", javaClass)
return state
Expand Down