PijulPusher.kt
/**
* Dracon - An IntelliJ-Pijul integration.
* Copyright 2021 JonathanxD <jhrldev@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.github.jonathanxd.dracon.push
import com.github.jonathanxd.dracon.context.PijulVcsContext
import com.github.jonathanxd.dracon.dialog.PijulChangesDialog
import com.github.jonathanxd.dracon.i18n.DraconBundle
import com.github.jonathanxd.dracon.pijul.NonZeroExitStatusCode
import com.github.jonathanxd.dracon.pijul.SuccessStatusCode
import com.github.jonathanxd.dracon.pijul.pijul
import com.github.jonathanxd.dracon.repository.PijulRepository
import com.intellij.dvcs.push.PushSpec
import com.intellij.dvcs.push.Pusher
import com.intellij.dvcs.push.VcsPushOptionValue
import com.intellij.notification.Notification
import com.intellij.notification.NotificationGroup
import com.intellij.notification.NotificationType
import com.intellij.notification.Notifications
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import kotlinx.coroutines.runBlocking
class PijulPusher : Pusher<PijulRepository, PijulPushSource, PijulTarget>() {
private val PUSH_GROUP =
NotificationGroup.createIdWithTitle("Push Changes", DraconBundle.message("push.notification.group.id"))
override fun push(
pushSpecs: Map<PijulRepository, PushSpec<PijulPushSource, PijulTarget>>,
vcsPushOptionValue: VcsPushOptionValue?, force: Boolean
) {
for ((repository, pijulSpec) in pushSpecs) {
val destination: PijulTarget = pijulSpec.target
val source: PijulPushSource = pijulSpec.source
val project: Project = repository.project
val ctx = project.service<PijulVcsContext>()
val push = pijul(project).pushInTerminal(
project,
ctx.root,
source.channel,
destination.channel,
destination.target
) {
if (!it.connectAndRetrieveContent()) {
Notifications.Bus.notify(
Notification(
PUSH_GROUP,
DraconBundle.message("editor-server.timeout.title"),
DraconBundle.message("editor-server.timeout.text"),
NotificationType.ERROR,
),
project
)
}
if (it.connected()) {
PijulChangesDialog(project, ctx.root, it, it.content(),
DraconBundle.message("action.Pijul.Push.text"),
DraconBundle.message("push.button.text")).showAndGet()
}
}
if (push.statusCode is SuccessStatusCode) {
Notifications.Bus.notify(
Notification(
PUSH_GROUP,
DraconBundle.message("push.notification.title"),
DraconBundle.message("push.notification.success"),
NotificationType.INFORMATION,
),
project
)
} else {
push.statusCode as NonZeroExitStatusCode
Notifications.Bus.notify(
Notification(
PUSH_GROUP,
DraconBundle.message("push.notification.title"),
DraconBundle.message("push.notification.failure", push.statusCode.exitCode, push.statusCode.message),
NotificationType.ERROR,
),
project
)
}
}
}
}