Fork channel

Create a new channel as a copy of main.

Rename channel

Rename main to:

Delete channel

Delete main? This cannot be undone.

PijulPostStartupActivity.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.activity

import com.github.jonathanxd.dracon.exec.execInTerminal
import com.github.jonathanxd.dracon.i18n.BUNDLE
import com.github.jonathanxd.dracon.i18n.DraconBundle
import com.github.jonathanxd.dracon.pijul.NonZeroExitStatusCode
import com.github.jonathanxd.dracon.pijul.Pijul
import com.github.jonathanxd.dracon.pijul.pijul
import com.github.jonathanxd.dracon.util.*
import com.github.jonathanxd.dracon.vcs.DraconVcsUtil
import com.github.jonathanxd.dracon.vcs.NotificationIds
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.util.ExecUtil
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.Task
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.StartupActivity
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.util.SystemInfo
import com.intellij.openapi.vcs.VcsNotifier
import com.intellij.util.download.DownloadableFileService
import com.intellij.util.download.FileDownloader
import com.intellij.util.io.IdeUtilIoBundle
import com.intellij.vcsUtil.VcsUtil
import org.jetbrains.annotations.PropertyKey
import java.nio.file.Paths
import java.util.concurrent.CompletableFuture

class PijulPostStartupActivity : StartupActivity {
    override fun runActivity(project: Project) {
        val path = project.basePath?.let { Paths.get(it) } ?: return

        if (Pijul.isUnderPijul(path)) {
            val hasPijul = findPijulOrNull() != null
            val hasEditorServer = findEditorServerOrNull() != null

            if (!hasPijul) {
                val hasCargo = findBinaryOrNull("cargo") != null

                if (!hasCargo) {
                    this.askForCargoInstallation(project, "pijul").handle { t, u ->
                        if (t != null && checkForCargo(project)) {
                            cargoInstallPijul(project)
                        }

                        if (u != null) {
                            warnDraconWillNotWork(project)
                            u.printStackTrace()
                        }
                    }
                } else {
                    cargoInstallPijul(project)
                }
            }

            if (!hasEditorServer) {
                val hasCargo = findBinaryOrNull("cargo") != null

                if (!hasCargo) {
                    this.askForCargoInstallation(project, "editor-server").handle { t, u ->
                        if (t != null && checkForCargo(project)) {
                            cargoInstallEditor(project)
                        }

                        if (u != null) {
                            warnDraconWillNotWork(project)
                            u.printStackTrace()
                        }
                    }
                } else {
                    cargoInstallEditor(project)
                }
            }
        }
    }

    fun cargoInstallPijul(project: Project) {
        val dialog = Messages.showYesNoDialog(
            project,
            DraconBundle.message("install.pijul.text").wrapInHml(),
            DraconBundle.message("install.pijul.title2"),
            Messages.getWarningIcon()
        )

        if (dialog == Messages.YES) {
            object : Task.Backgroundable(project, DraconBundle.message("install.pijul.title")) {
                override fun run(indicator: ProgressIndicator) {
                    indicator.isIndeterminate = true
                    indicator.text = DraconBundle.message("install.pijul.title")
                    indicator.text2 = DraconBundle.message("install.cargo.running.text")

                    execInTerminal("install.pijul.title", "cargo install pijul --version '~1.0.0-alpha'")
                }
            }.queue()
        } else {
            warnDraconWillNotWork(project)
        }

    }

    fun cargoInstallEditor(project: Project) {
        val dialog = Messages.showYesNoDialog(
            project,
            DraconBundle.message("install.editor.server.text").wrapInHml(),
            DraconBundle.message("install.editor.server.title2"),
            Messages.getWarningIcon()
        )

        if (dialog == Messages.YES) {
            object : Task.Backgroundable(project, DraconBundle.message("install.editor.server.title")) {
                override fun run(indicator: ProgressIndicator) {
                    indicator.isIndeterminate = true
                    indicator.text = DraconBundle.message("install.editor.server.title")
                    indicator.text2 = DraconBundle.message("install.cargo.running.text")

                    execInTerminal("install.editor.server.title", "cargo install editor-server")
                }
            }.queue()

        } else {
            warnDraconWillNotWork(project)
        }
    }

    fun checkForCargo(project: Project): Boolean {
        if (findBinaryOrNull("cargo") == null) {
            warnDraconWillNotWork(project)
            return false
        }

        return true
    }

    fun warnDraconWillNotWork(project: Project) {
        Messages.showWarningDialog(
            project,
            DraconBundle.message("install.pijul.warning.text").wrapInHml(),
            DraconBundle.message("install.pijul.title")
        )
    }

    fun askForCargoInstallation(project: Project, sub: String): CompletableFuture<Unit> {
        val dialog = Messages.showYesNoDialog(
            project,
            DraconBundle.message("install.cargo.text", sub.escapeXml()).wrapInHml(),
            DraconBundle.message("install.cargo.title"),
            Messages.getWarningIcon()
        )

        if (dialog == Messages.YES) {
            val download = if (SystemInfo.isWindows) {
                DownloadableFileService.getInstance().createDownloader(listOf(
                    DownloadableFileService.getInstance()
                        .createFileDescription(
                            "https://win.rustup.rs/x86_64",
                            "rustup-init.exe"
                        )
                ), "Rustup")
            } else {
                DownloadableFileService.getInstance().createDownloader(listOf(
                    DownloadableFileService.getInstance()
                        .createFileDescription(
                            "https://sh.rustup.rs",
                            "rustup-init.sh"
                        )
                ), "Rustup")
            }

            return download.downloadWithBackgroundProgress(null, project)
                .handle { t, u ->
                    if (t != null && t.isNotEmpty()) {
                        val file = Paths.get(VcsUtil.getFilePath(t.single().first).path).toAbsolutePath()
                        if (SystemInfo.isWindows) {
                            execInTerminal("install.cargo.title", file.toString())
                        } else {
                            execInTerminal("install.cargo.title", "sh $file")
                        }
                    }

                    u?.printStackTrace()
                }
        } else {
            return CompletableFuture.failedFuture(IllegalStateException("User not accepted cargo installation."))
        }
    }
}