Skip to content

Commit

Permalink
Tracking active config across moves (#61)
Browse files Browse the repository at this point in the history
* Tracking active config across moves

* Doc
  • Loading branch information
Razz4780 authored Jul 21, 2023
1 parent 5ba0bee commit 5bc859d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
1 change: 1 addition & 0 deletions changelog.d/60.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Active config is now updated when the file is moved to another location.
Original file line number Diff line number Diff line change
@@ -1,56 +1,69 @@
package com.metalbear.mirrord

import com.intellij.notification.NotificationType
import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.vfs.AsyncFileListener
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.openapi.vfs.newvfs.events.VFileDeleteEvent
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
import com.intellij.openapi.vfs.newvfs.events.VFileMoveEvent

class MirrordActiveConfigWatch(private val service: MirrordProjectService) : AsyncFileListener {
/**
* @param url null means that the file has been removed
*/
class ConfigMovedTo(val url: String?)

/**
* Searches for deletions and renames of the active config file (or any of its parent directories).
* When the change has been applied to the VFS, a warning notification is displayed to the user
* and the active config file reference is updated.
*/
override fun prepareChange(events: MutableList<out VFileEvent>): AsyncFileListener.ChangeApplier? {
val notification = service
val result: ConfigMovedTo? = service
.activeConfig
?.url
?.let { activeUrl ->
events
.filter { it.file?.url == activeUrl }
.filter { it.file?.let { file -> activeUrl.startsWith(file.url) } ?: false }
.firstNotNullOfOrNull {
when (it) {
is VFileDeleteEvent -> service.notifier.notification(
"mirrord active config has been removed",
NotificationType.WARNING
)
.withDontShowAgain(MirrordSettingsState.NotificationId.ACTIVE_CONFIG_REMOVED)

is VFileMoveEvent -> service.notifier.notification(
"mirrord active config has been moved to ${it.newParent.presentableUrl}",
NotificationType.WARNING
)
.withDontShowAgain(MirrordSettingsState.NotificationId.ACTIVE_CONFIG_REMOVED)

is VFileDeleteEvent -> ConfigMovedTo(null)
is VFileMoveEvent -> ConfigMovedTo(activeUrl.replace(it.oldParent.url, it.newParent.url))
else -> null
}
}
}

return object : AsyncFileListener.ChangeApplier {
override fun afterVfsChange() {
notification?.let {
service.activeConfig = null
it.fire()
return
}
return result?.let {
object : AsyncFileListener.ChangeApplier {
override fun afterVfsChange() {
val newActiveConfig = it.url?.let { url ->
ReadAction.compute<VirtualFile, Exception> {
VirtualFileManager.getInstance().findFileByUrl(url)
}
}

service.activeConfig = newActiveConfig

service.activeConfig?.let {
if (!it.isValid) {
service.activeConfig = null
val notification = if (newActiveConfig == null) {
service.notifier.notification(
"directory containing mirrord active config has been moved or removed",
"mirrord active config has been removed",
NotificationType.WARNING
)
.withDontShowAgain(MirrordSettingsState.NotificationId.ACTIVE_CONFIG_REMOVED)
.fire()
} else {
service.notifier.notification(
"mirrord active config has been moved to ${newActiveConfig.presentableUrl}",
NotificationType.WARNING
)
.withDontShowAgain(MirrordSettingsState.NotificationId.ACTIVE_CONFIG_MOVED)
}

notification.fire()

return
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ open class MirrordSettingsState : PersistentStateComponent<MirrordSettingsState.

enum class NotificationId(val presentableName: String) {
RUNNING_TARGETLESS("mirrord running targetless"),
ACTIVE_CONFIG_REMOVED("active mirrord config is removed or moved to another directory"),
ACTIVE_CONFIG_REMOVED("active mirrord config is removed"),
ACTIVE_CONFIG_USED("active mirrord config is used"),
DEFAULT_CONFIG_USED("default mirrord config is used"),
DEFAULT_CONFIG_CREATED("default mirrord config is created"),
POSSIBLY_OUTDATED_BINARY_USED("possibly outdated mirrord binary is used")
POSSIBLY_OUTDATED_BINARY_USED("possibly outdated mirrord binary is used"),
ACTIVE_CONFIG_MOVED("active mirrord config is moved")
}

class MirrordState {
Expand Down

0 comments on commit 5bc859d

Please sign in to comment.