package org.iwakura.larcjb.actions import com.intellij.notification.NotificationGroupManager import com.intellij.notification.NotificationType import com.intellij.openapi.actionSystem.ActionUpdateThread import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.progress.ProgressIndicator import com.intellij.openapi.progress.ProgressManager import com.intellij.openapi.progress.Task import com.intellij.openapi.project.DumbAware import com.intellij.openapi.project.Project import com.intellij.openapi.vcs.ProjectLevelVcsManager import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager import com.intellij.openapi.vfs.VirtualFile import org.iwakura.larcjb.LarcExecutable import org.iwakura.larcjb.LarcVcs /** * Action to pull changes from remote. */ class LarcPullAction : AnAction(), DumbAware { override fun actionPerformed(e: AnActionEvent) { val project = e.project ?: return ProgressManager.getInstance().run(object : Task.Backgroundable(project, "Pulling from remote...", true) { override fun run(indicator: ProgressIndicator) { val executable = LarcExecutable(project) val roots = getLarcRoots(project) for (root in roots) { indicator.text = "Pulling ${root.name}..." val result = executable.execute(root, "pull") if (result.isSuccess) { notify(project, "Pull successful", result.stdout, NotificationType.INFORMATION) VcsDirtyScopeManager.getInstance(project).dirDirtyRecursively(root) } else { notify(project, "Pull failed", result.stderr, NotificationType.ERROR) } } } }) } override fun update(e: AnActionEvent) { val project = e.project e.presentation.isEnabledAndVisible = project != null && getLarcRoots(project).isNotEmpty() } override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT private fun getLarcRoots(project: Project): List { return ProjectLevelVcsManager.getInstance(project) .allVcsRoots .filter { it.vcs?.name == LarcVcs.NAME } .mapNotNull { it.path } } private fun notify(project: Project, title: String, content: String, type: NotificationType) { NotificationGroupManager.getInstance() .getNotificationGroup("Larc") .createNotification(title, content, type) .notify(project) } }