|
| 1 | +package com.gmail.blueboxware.libgdxplugin |
| 2 | + |
| 3 | +import com.intellij.ide.highlighter.JavaClassFileType |
| 4 | +import com.intellij.notification.Notification |
| 5 | +import com.intellij.notification.NotificationDisplayType |
| 6 | +import com.intellij.notification.NotificationGroup |
| 7 | +import com.intellij.notification.NotificationType |
| 8 | +import com.intellij.openapi.application.ApplicationManager |
| 9 | +import com.intellij.openapi.project.Project |
| 10 | +import com.intellij.openapi.roots.ProjectFileIndex |
| 11 | +import com.intellij.openapi.vfs.VfsUtilCore |
| 12 | +import com.intellij.openapi.vfs.VirtualFile |
| 13 | +import com.intellij.openapi.vfs.newvfs.BulkFileListener |
| 14 | +import com.intellij.openapi.vfs.newvfs.events.VFileEvent |
| 15 | + |
| 16 | + |
| 17 | +/* |
| 18 | + * Copyright 2021 Blue Box Ware |
| 19 | + * |
| 20 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | + * you may not use this file except in compliance with the License. |
| 22 | + * You may obtain a copy of the License at |
| 23 | + * |
| 24 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | + * |
| 26 | + * Unless required by applicable law or agreed to in writing, software |
| 27 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | + * See the License for the specific language governing permissions and |
| 30 | + * limitations under the License. |
| 31 | + */ |
| 32 | +class GwtOutdatedListener(val project: Project): |
| 33 | + BulkFileListener { |
| 34 | + |
| 35 | + private var balloon: Notification? = null |
| 36 | + |
| 37 | + private var gwtFile: VirtualFile? = null |
| 38 | + |
| 39 | + override fun after(events: MutableList<out VFileEvent>) { |
| 40 | + |
| 41 | + var doCheck = false |
| 42 | + |
| 43 | + for (event in events) { |
| 44 | + event.file?.let { file -> |
| 45 | + if (file.name.endsWith(".gwt.xml")) { |
| 46 | + VfsUtilCore.findContainingDirectory(file, "html")?.let { html -> |
| 47 | + if (ProjectFileIndex.getInstance(project).isInContent(html)) { |
| 48 | + if (file.timeStamp >= gwtFile?.timeStamp ?: 0) { |
| 49 | + gwtFile = file |
| 50 | + doCheck = true |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (doCheck || balloon?.isExpired == false) { |
| 59 | + ApplicationManager.getApplication().invokeLater { |
| 60 | + gwtFile?.let { gwtFile -> |
| 61 | + VfsUtilCore.findContainingDirectory(gwtFile, "html")?.let { html -> |
| 62 | + var outDated = false |
| 63 | + html.findChild("build")?.let { buildDir -> |
| 64 | + VfsUtilCore.processFilesRecursively(buildDir) { file -> |
| 65 | + if (file.fileType == JavaClassFileType.INSTANCE) { |
| 66 | + if (file.timeStamp < gwtFile.timeStamp) { |
| 67 | + outDated = true |
| 68 | + return@processFilesRecursively false |
| 69 | + } |
| 70 | + } |
| 71 | + true |
| 72 | + } |
| 73 | + } |
| 74 | + if (!outDated) { |
| 75 | + html.findChild("war")?.let { warDir -> |
| 76 | + VfsUtilCore.processFilesRecursively(warDir) { file -> |
| 77 | + if (!file.isDirectory && file.timeStamp < gwtFile.timeStamp) { |
| 78 | + outDated = true |
| 79 | + return@processFilesRecursively false |
| 80 | + } |
| 81 | + true |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + if (balloon?.isExpired == false) { |
| 86 | + if (!outDated) { |
| 87 | + balloon?.hideBalloon() |
| 88 | + balloon = null |
| 89 | + } |
| 90 | + } else { |
| 91 | + if (outDated) { |
| 92 | + balloon = GROUP.createNotification( |
| 93 | + message("gwt.build.outdated.title"), |
| 94 | + message("gwt.build.outdated.body"), |
| 95 | + NotificationType.WARNING) |
| 96 | + balloon?.notify(project) |
| 97 | + } |
| 98 | + } |
| 99 | + return@invokeLater |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + balloon?.hideBalloon() |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + companion object { |
| 111 | + val GROUP = NotificationGroup( |
| 112 | + "Outdated GWT", |
| 113 | + NotificationDisplayType.STICKY_BALLOON, |
| 114 | + false |
| 115 | + ) |
| 116 | + } |
| 117 | + |
| 118 | +} |
| 119 | + |
0 commit comments