| 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.vfs.VirtualFile |
| 15 |
import org.iwakura.larcjb.LarcExecutable |
| 16 |
import org.iwakura.larcjb.LarcVcs |
| 17 |
|
| 18 |
/** |
| 19 |
* Action to push changes to remote. |
| 20 |
*/ |
| 21 |
class LarcPushAction : AnAction(), DumbAware { |
| 22 |
|
| 23 |
override fun actionPerformed(e: AnActionEvent) { |
| 24 |
val project = e.project ?: return |
| 25 |
|
| 26 |
ProgressManager.getInstance().run(object : Task.Backgroundable(project, "Pushing to remote...", true) { |
| 27 |
override fun run(indicator: ProgressIndicator) { |
| 28 |
val executable = LarcExecutable(project) |
| 29 |
val roots = getLarcRoots(project) |
| 30 |
|
| 31 |
for (root in roots) { |
| 32 |
indicator.text = "Pushing ${root.name}..." |
| 33 |
|
| 34 |
val result = executable.execute(root, "push") |
| 35 |
|
| 36 |
if (result.isSuccess) { |
| 37 |
notify(project, "Push successful", result.stdout, NotificationType.INFORMATION) |
| 38 |
} else { |
| 39 |
notify(project, "Push failed", result.stderr, NotificationType.ERROR) |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
}) |
| 44 |
} |
| 45 |
|
| 46 |
override fun update(e: AnActionEvent) { |
| 47 |
val project = e.project |
| 48 |
e.presentation.isEnabledAndVisible = project != null && getLarcRoots(project).isNotEmpty() |
| 49 |
} |
| 50 |
|
| 51 |
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT |
| 52 |
|
| 53 |
private fun getLarcRoots(project: Project): List<VirtualFile> { |
| 54 |
return ProjectLevelVcsManager.getInstance(project) |
| 55 |
.allVcsRoots |
| 56 |
.filter { it.vcs?.name == LarcVcs.NAME } |
| 57 |
.mapNotNull { it.path } |
| 58 |
} |
| 59 |
|
| 60 |
private fun notify(project: Project, title: String, content: String, type: NotificationType) { |
| 61 |
NotificationGroupManager.getInstance() |
| 62 |
.getNotificationGroup("Larc") |
| 63 |
.createNotification(title, content, type) |
| 64 |
.notify(project) |
| 65 |
} |
| 66 |
} |
| 67 |
|