Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mps-sync-plugin-lib): fix inconsistent PluginStatePersister API #110

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading