Saving custom Library globally
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.
Please sign in to leave a comment.
A complete shot from the hip, but please look at this and compare. Maybe it helps:
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.
One additional question please:
I'm having hard time persisting LibraryState.
Library is now created and saved ok with:
Properties class
And the state itself:
But the state is not saved and after getting Library back it's always empty
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:
But result is still the same and State is not persisted for some reason.
I've found it, properties are not getted mutable and copied I suppose, so after altering them I have to call