Saving custom Library globally

Answered

Hello,

I'm strugling with Custom Library - global at least.

I've managed to create and use project level Library by:

val table = LibraryTablesRegistrar.getInstance().getLibraryTable(project)

val newLib = table.createLibrary(name)
val libModel = newLib.modifiableModel
libModel.addRoot(virtualFile!!.url, type)
libModel.commit()

val module = ModuleManager.getInstance(project).modules.first()
val rootModel = ModuleRootManager.getInstance(module).modifiableModel
rootModel.addLibraryEntry(newLib)
rootModel.commit()

This works and creates and index that Library, problem is that I'm trying to improving into something like GoLand offers for it's SDKs:

I wanted to try an approach to creating Global Libraries which will then be visible in that select box for all projects. After selecting any of them I'd then call rootModel.addLibraryEntry(lib) to link it into a project.

Problem is, that I'm not able to save/list those Libraries. I'm trying very similar approach, but instead of .getLibraryTable(project), getting what might be a Global one?

val modifier = LibraryTablesRegistrar.getInstance().libraryTable.modifiableModel
val library = modifier.createLibrary(name, GdLibraryKind.getOrCreate())
val libModel = library.modifiableModel
libModel.addRoot(path, type)
libModel.commit()

Problem is that even though this runs sucessfully, later on getting Library in any of those ways:

ApplicationManager.getApplication().getService(ModifiableModelsProvider::class.java).libraryTableModifiableModel.libraries
LibraryTablesRegistrar.getInstance().libraryTable.modifiableModel.libraries
LibraryTablesRegistrar.getInstance().libraryTable.libraries

all returns empty array.

Does anyone know what might be wrong? I'm kind of lost in what are those different tables and modifiableModels and where to register/retrieve Libraries so I can access them across all projects, not just the current one.

0
5 comments

Oh, I've managed to overlook that I'm commiting Library instead of LibraryTable.

Another case where taking a break from code solves it.
Thank you for pointing me to tests... saw it right away.

0

One additional question please:
I'm having hard time persisting LibraryState.

Library is now created and saved ok with:

val library = modifier.createLibrary(name, GdLibraryKind)
val libModifier = library.modifiableModel as ModifiableModelEx
val state = libModifier.properties.state as GdLibrary
state.version = "xyz"

libModifier.addRoot(path, type)
libModifier.commit()
modifier.commit()

Properties class

@State(name = "GdLibraryProperties", storages = [Storage("GdLibraryProperties.xml")])
class GdLibraryProperties : LibraryProperties<GdLibrary>() {
   
    @Tag("config")
var config: GdLibrary = GdLibrary()

  override fun equals(other: Any?): Boolean { ... }

override fun hashCode(): Int {
      return config.hashCode()
}

override fun getState(): GdLibrary {
return config
}

override fun loadState(state: GdLibrary) {
config = state
}
}

And the state itself:

class GdLibrary {
@Tag("path")
var path: String = ""

@Tag("version")
var version: String = ""
}

But the state is not saved and after getting Library back it's always empty

ApplicationManager
.getApplication()
  .getService(ModifiableModelsProvider::class.java)
  .libraryTableModifiableModel
.libraries
.forEach { it.modifiableModel.properties.state.version }
0

Just a little update, I've been looking into community idea and noticed, that Kotlin's LibraryPropeties used itself in generic type so I've tried to update it like:

@State(name = "GdLibraryProperties", storages = [Storage("GdLibraryProperties.xml")])
class GdLibraryProperties : LibraryProperties<GdLibraryProperties>() {

@Tag("path")
var path: String = ""

@Tag("version")
var version: String = ""

override fun equals(other: Any?): Boolean {
if (other !is GdLibraryProperties) return false
return path == other.path
&& version == other.version
}

override fun hashCode(): Int {
return this.hashCode()
}

override fun getState(): GdLibraryProperties {
return this
}

override fun loadState(state: GdLibraryProperties) {
path = state.path
version = state.version
}
}

But result is still the same and State is not persisted for some reason.

0

I've found it, properties are not getted mutable and copied I suppose, so after altering them I have to call

(library.modifiableModel as ModifiableModelEx).properties = props
0

Please sign in to leave a comment.