PijulAddToIgnore.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.actions
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.wrapInHml
import com.github.jonathanxd.dracon.vcs.DraconVcsUtil
import com.github.jonathanxd.dracon.vcs.NotificationIds
import com.github.jonathanxd.dracon.vfs.DraconVfsUtil
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.fileChooser.FileChooser
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.Task
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.vcs.ProjectLevelVcsManager
import com.intellij.openapi.vcs.VcsNotifier
import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager
import com.intellij.vcsUtil.VcsUtil
class PijulAddToIgnore: DumbAwareAction() {
override fun actionPerformed(e: AnActionEvent) {
val project = e.getData(CommonDataKeys.PROJECT) ?: ProjectManager.getInstance().defaultProject
val fcd = FileChooserDescriptorFactory.createSingleFileDescriptor()
fcd.isShowFileSystemRoots = true
fcd.title = DraconBundle.Init.title
fcd.description = DraconBundle.Init.description
fcd.isHideIgnored = false
val baseDir = e.getData(CommonDataKeys.VIRTUAL_FILE)?.let { if (it.isDirectory) null else it } ?: project.baseDir
FileChooser.chooseFile(fcd, project, baseDir) { root ->
if (Pijul.isUnderPijul(root)) {
val dialog = Messages.showYesNoCancelDialog(
project,
DraconBundle.Init.alreadyUnderPijulForHtml(root.presentableUrl),
DraconBundle.Init.title,
Messages.getWarningIcon()
)
if (dialog != Messages.YES) {
return@chooseFile
}
}
object : Task.Backgroundable(project, DraconBundle.Dracon.refreshing) {
override fun run(indicator: ProgressIndicator) {
indicator.isIndeterminate = true
val init = pijul(project).init(project, root)
if (init.statusCode is NonZeroExitStatusCode) {
VcsNotifier
.getInstance(this.project)
.notifyError(
NotificationIds.FAILED_TO_INIT,
DraconBundle.Init.error,
init.statusCode.message.wrapInHml(),
true
)
}
if (project.isDefault) {
return
}
// Refresh VCS?
DraconVcsUtil.refreshAfterInit(project, root)
}
}.queue()
}
}
}