Skip to content

Commit

Permalink
fix(mps-sync-plugin-lib): fix inconsistent PluginStatePersister API (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
benedekh authored Jun 13, 2024
1 parent 89f5bd2 commit b137c81
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ import java.io.File
/**
* This is a utility class that saves, loads and restores a [PersistableState] to and from an XML file.
*
* @param providedFile the File in which the class will be saved. If it's a directory, then a fill will be created in it with name [defaultFileName].
* @param defaultFileName the file name to be used if [providedFile] is a directory
* @param providedFile the File in which the class will be saved. If it's a directory, then a fill will be created in it with name defaultFileName.
* @param defaultFileName (optional) the file name to be used if providedFile is a directory. Defaults to [PluginStatePersister.DEFAULT_FILE_NAME].
*/
@UnstableModelixFeature(reason = "The new modelix MPS plugin is under construction", intendedFinalization = "2024.1")
class PluginStatePersister(providedFile: File, defaultFileName: String = "syncState.xml") {
class PluginStatePersister(providedFile: File, defaultFileName: String? = null) {

companion object {
const val DEFAULT_FILE_NAME = "modelixSyncPluginState.xml"
}

private val logger = KotlinLogging.logger {}

private val targetFile: File = if (providedFile.isDirectory) {
providedFile.resolve(defaultFileName)
providedFile.resolve(defaultFileName ?: DEFAULT_FILE_NAME)
} else {
providedFile
}
Expand All @@ -39,11 +43,9 @@ class PluginStatePersister(providedFile: File, defaultFileName: String = "syncSt
/**
* Loads the state from the XML file.
*
* @param baseDirectory the folder from which we will load the serialized [PersistableState]
*
* @return the deserialized [PersistableState]
*/
fun load(baseDirectory: File): PersistableState? {
fun load(): PersistableState? {
return try {
XmlMapper().readValue(targetFile, PersistableState::class.java)
} catch (t: Throwable) {
Expand All @@ -56,13 +58,12 @@ class PluginStatePersister(providedFile: File, defaultFileName: String = "syncSt
* Loads the state from the XML file and then initializes the internal state of the modelix sync lib via
* [PersistableState.restoreState].
*
* @param baseDirectory the folder from which we will load the serialized [PersistableState]
* @param syncService see [PersistableState.restoreState]
*
* @return see [PersistableState.restoreState]
*/
fun restore(baseDirectory: File, syncService: IRebindModulesSyncService): RestoredStateContext? {
val state = load(baseDirectory) ?: return null
fun restore(syncService: IRebindModulesSyncService): RestoredStateContext? {
val state = load() ?: return null
return state.restoreState(syncService)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.service
import org.modelix.kotlin.utils.UnstableModelixFeature
import org.modelix.mps.sync.persistence.PersistableState
import org.modelix.mps.sync.persistence.PluginStatePersister
import org.modelix.mps.sync.persistence.RestoredStateContext
import org.modelix.mps.sync.plugin.ModelSyncService

@UnstableModelixFeature(reason = "The new modelix MPS plugin is under construction", intendedFinalization = "This feature is finalized when the new sync plugin is ready for release.")
@UnstableModelixFeature(
reason = "The new modelix MPS plugin is under construction",
intendedFinalization = "This feature is finalized when the new sync plugin is ready for release.",
)
@Service(Service.Level.PROJECT)
@State(
name = "ModelixSyncPluginState",
reloadable = true,
storages = [Storage("modelixSyncPluginState.xml", roamingType = RoamingType.DISABLED)],
storages = [Storage(PluginStatePersister.DEFAULT_FILE_NAME, roamingType = RoamingType.DISABLED)],
)
class SyncPluginState : PersistentStateComponent<PersistableState> {

Expand Down

0 comments on commit b137c81

Please sign in to comment.