larc r18

69 lines ยท 2.6 KB Raw
1 package org.iwakura.larcjb.actions
2
3 import com.intellij.notification.NotificationGroupManager
4 import com.intellij.notification.NotificationType
5 import com.intellij.openapi.actionSystem.ActionUpdateThread
6 import com.intellij.openapi.actionSystem.AnAction
7 import com.intellij.openapi.actionSystem.AnActionEvent
8 import com.intellij.openapi.progress.ProgressIndicator
9 import com.intellij.openapi.progress.ProgressManager
10 import com.intellij.openapi.progress.Task
11 import com.intellij.openapi.project.DumbAware
12 import com.intellij.openapi.project.Project
13 import com.intellij.openapi.vcs.ProjectLevelVcsManager
14 import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager
15 import com.intellij.openapi.vfs.VirtualFile
16 import org.iwakura.larcjb.LarcExecutable
17 import org.iwakura.larcjb.LarcVcs
18
19 /**
20 * Action to pull changes from remote.
21 */
22 class LarcPullAction : AnAction(), DumbAware {
23
24 override fun actionPerformed(e: AnActionEvent) {
25 val project = e.project ?: return
26
27 ProgressManager.getInstance().run(object : Task.Backgroundable(project, "Pulling from remote...", true) {
28 override fun run(indicator: ProgressIndicator) {
29 val executable = LarcExecutable(project)
30 val roots = getLarcRoots(project)
31
32 for (root in roots) {
33 indicator.text = "Pulling ${root.name}..."
34
35 val result = executable.execute(root, "pull")
36
37 if (result.isSuccess) {
38 notify(project, "Pull successful", result.stdout, NotificationType.INFORMATION)
39 VcsDirtyScopeManager.getInstance(project).dirDirtyRecursively(root)
40 } else {
41 notify(project, "Pull failed", result.stderr, NotificationType.ERROR)
42 }
43 }
44 }
45 })
46 }
47
48 override fun update(e: AnActionEvent) {
49 val project = e.project
50 e.presentation.isEnabledAndVisible = project != null && getLarcRoots(project).isNotEmpty()
51 }
52
53 override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT
54
55 private fun getLarcRoots(project: Project): List<VirtualFile> {
56 return ProjectLevelVcsManager.getInstance(project)
57 .allVcsRoots
58 .filter { it.vcs?.name == LarcVcs.NAME }
59 .mapNotNull { it.path }
60 }
61
62 private fun notify(project: Project, title: String, content: String, type: NotificationType) {
63 NotificationGroupManager.getInstance()
64 .getNotificationGroup("Larc")
65 .createNotification(title, content, type)
66 .notify(project)
67 }
68 }
69