|
| 1 | +package org.elixir_lang.mix |
| 2 | + |
| 3 | +import com.intellij.openapi.Disposable |
| 4 | +import com.intellij.openapi.application.ApplicationManager |
| 5 | +import com.intellij.openapi.components.Service |
| 6 | +import com.intellij.openapi.diagnostic.logger |
| 7 | +import com.intellij.openapi.module.ModuleManager |
| 8 | +import com.intellij.openapi.project.Project |
| 9 | +import com.intellij.openapi.projectRoots.Sdk |
| 10 | +import com.intellij.openapi.roots.ModuleRootManager |
| 11 | +import com.intellij.openapi.roots.ProjectRootManager |
| 12 | +import com.intellij.openapi.util.io.FileUtil |
| 13 | +import com.intellij.openapi.vfs.VirtualFile |
| 14 | +import com.intellij.openapi.vfs.VirtualFileManager |
| 15 | +import com.intellij.openapi.vfs.newvfs.BulkFileListener |
| 16 | +import com.intellij.openapi.vfs.newvfs.events.VFileEvent |
| 17 | +import com.intellij.util.Alarm |
| 18 | +import org.elixir_lang.notification.setup_sdk.Notifier |
| 19 | +import org.elixir_lang.package_manager.DepsStatusResult |
| 20 | +import org.elixir_lang.package_manager.virtualFile |
| 21 | +import org.elixir_lang.sdk.elixir.Type as ElixirSdkType |
| 22 | +import java.util.concurrent.atomic.AtomicBoolean |
| 23 | + |
| 24 | +@Service(Service.Level.PROJECT) |
| 25 | +class DepsCheckerService(private val project: Project) : Disposable { |
| 26 | + private val alarm = Alarm(Alarm.ThreadToUse.POOLED_THREAD, this) |
| 27 | + private val checkInProgress = AtomicBoolean(false) |
| 28 | + @Volatile |
| 29 | + private var checkPending = false |
| 30 | + |
| 31 | + init { |
| 32 | + project.messageBus.connect(this).subscribe( |
| 33 | + VirtualFileManager.VFS_CHANGES, |
| 34 | + object : BulkFileListener { |
| 35 | + override fun after(events: List<VFileEvent>) { |
| 36 | + if (events.any { isDepsChange(it) }) { |
| 37 | + scheduleCheck("deps change") |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + fun scheduleInitialCheck() { |
| 45 | + scheduleCheck("startup", 0) |
| 46 | + } |
| 47 | + |
| 48 | + fun scheduleCheckNow(reason: String) { |
| 49 | + scheduleCheck(reason, 0) |
| 50 | + } |
| 51 | + |
| 52 | + private fun scheduleCheck(reason: String, delayMs: Int = DEPS_CHECK_DEBOUNCE_MS) { |
| 53 | + if (project.isDisposed) { |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + if (checkInProgress.get()) { |
| 58 | + checkPending = true |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + alarm.cancelAllRequests() |
| 63 | + alarm.addRequest({ runCheck(reason) }, delayMs) |
| 64 | + } |
| 65 | + |
| 66 | + private fun runCheck(reason: String) { |
| 67 | + if (!checkInProgress.compareAndSet(false, true)) { |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + try { |
| 72 | + if (project.isDisposed) { |
| 73 | + return |
| 74 | + } |
| 75 | + LOG.debug("DepsCheckerService: Checking Mix deps ($reason)") |
| 76 | + val sdk = findElixirSdk(project) |
| 77 | + val projectRoots = selectTopLevelMixRoots(ProjectRootManager.getInstance(project).contentRootsFromAllModules) |
| 78 | + |
| 79 | + var sawSupported = false |
| 80 | + var sawNonOk = false |
| 81 | + |
| 82 | + for (root in projectRoots) { |
| 83 | + when (val statusResult = depsStatusResult(project, root, sdk)) { |
| 84 | + is DepsStatusResult.Available -> { |
| 85 | + sawSupported = true |
| 86 | + if (statusResult.status.hasNonOk) { |
| 87 | + sawNonOk = true |
| 88 | + } |
| 89 | + } |
| 90 | + is DepsStatusResult.Error -> { |
| 91 | + notifyOnEdt { Notifier.mixDepsCheckFailed(project, statusResult.message) } |
| 92 | + return |
| 93 | + } |
| 94 | + DepsStatusResult.Unsupported -> Unit |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (!sawSupported) { |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + if (sawNonOk) { |
| 103 | + notifyOnEdt { Notifier.mixDepsOutdated(project) } |
| 104 | + } else { |
| 105 | + notifyOnEdt { Notifier.clearMixDepsOutdated(project) } |
| 106 | + } |
| 107 | + } finally { |
| 108 | + checkInProgress.set(false) |
| 109 | + if (checkPending) { |
| 110 | + checkPending = false |
| 111 | + scheduleCheck("pending") |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private fun depsStatusResult(project: Project, projectRoot: VirtualFile, sdk: Sdk?): DepsStatusResult { |
| 117 | + val packageManagerVirtualFile = virtualFile(projectRoot) |
| 118 | + ?: return DepsStatusResult.Unsupported |
| 119 | + val (packageManager, packageVirtualFile) = packageManagerVirtualFile |
| 120 | + return packageManager.depsStatus(project, packageVirtualFile, sdk) |
| 121 | + } |
| 122 | + |
| 123 | + private fun findElixirSdk(project: Project): Sdk? { |
| 124 | + val elixirSdkType = ElixirSdkType.instance |
| 125 | + |
| 126 | + val projectSdk = ProjectRootManager.getInstance(project).projectSdk |
| 127 | + if (projectSdk?.sdkType === elixirSdkType) { |
| 128 | + return projectSdk |
| 129 | + } |
| 130 | + |
| 131 | + ModuleManager.getInstance(project).modules.forEach { module -> |
| 132 | + val moduleSdk = ModuleRootManager.getInstance(module).sdk |
| 133 | + if (moduleSdk?.sdkType === elixirSdkType) { |
| 134 | + return moduleSdk |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return null |
| 139 | + } |
| 140 | + |
| 141 | + private fun isDepsChange(event: VFileEvent): Boolean { |
| 142 | + val eventPath = FileUtil.toSystemIndependentName(event.path) |
| 143 | + val contentRoots = selectTopLevelMixRoots(ProjectRootManager.getInstance(project).contentRootsFromAllModules) |
| 144 | + |
| 145 | + return contentRoots.any { root -> |
| 146 | + val depsPath = FileUtil.toSystemIndependentName(root.path) + "/deps" |
| 147 | + FileUtil.isAncestor(depsPath, eventPath, false) |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + internal fun selectTopLevelMixRoots(roots: List<VirtualFile>): List<VirtualFile> { |
| 152 | + val mixRoots = roots.filter { virtualFile(it) != null } |
| 153 | + if (mixRoots.size <= 1) { |
| 154 | + return mixRoots |
| 155 | + } |
| 156 | + |
| 157 | + val mixRootPaths = mixRoots.map { FileUtil.toSystemIndependentName(it.path) } |
| 158 | + |
| 159 | + return mixRoots.filter { root -> |
| 160 | + val rootPath = FileUtil.toSystemIndependentName(root.path) |
| 161 | + mixRootPaths.none { otherPath -> |
| 162 | + otherPath != rootPath && FileUtil.isAncestor(otherPath, rootPath, false) |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + internal fun selectTopLevelMixRoots(roots: Array<out VirtualFile>): List<VirtualFile> = |
| 168 | + selectTopLevelMixRoots(roots.asList()) |
| 169 | + |
| 170 | + private fun notifyOnEdt(action: () -> Unit) { |
| 171 | + ApplicationManager.getApplication().invokeLater { |
| 172 | + if (!project.isDisposed) { |
| 173 | + action() |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + override fun dispose() {} |
| 179 | + |
| 180 | + companion object { |
| 181 | + private const val DEPS_CHECK_DEBOUNCE_MS: Int = 1500 |
| 182 | + private val LOG = logger<DepsCheckerService>() |
| 183 | + } |
| 184 | +} |
0 commit comments