Programmatically add external libraries for custom LibraryKind

HI

I have a custom language plugin and I want to programmatically add some libraries so they appear in the external libraries tree.

I can create a library but it only gets added to the Module Settings > Libraries and not the external libraries tree - it also doesn't get added to the module libraries - but there is a UI notification that says there is a Problem - the library is not used and If I click FIX it puts it in the module libraries in an unticked state and for Compile. It does not put it in external libraries though.

I also added a custom library type so when I choose add a library in the Module Settings > Libraries - I can choose a custom language library and add the library fine - it shows up in Libraries with the custom language logo next to it - and it also appears in External Libraries with the standard jar icon.

How can I solve the following 2 problems:

1. Make my library appear in external libraries
2. Make my library my custom library type instead of a java type library

I have been banging my head against this issue for quite some time and have tried many different ways but none of them are working.

Here is my code for reference:

private static void processDLibs(@NotNull AnActionEvent e, @NotNull Module module) {

    Project project = getEventProject(e);
    LibraryTable projectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(project);
    final LibraryTable.ModifiableModel projectLibraryModel = projectLibraryTable.getModifiableModel();

    Library library = projectLibraryModel.createLibrary("d-unit-0.7.2");
    final Library.ModifiableModel libraryModel = library.getModifiableModel();
    String pathUrl = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, "/Users/kings/.dub/packages/d-unit-0.7.2");
    VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(pathUrl);
    libraryModel.addRoot(file, OrderRootType.CLASSES);

    ApplicationManager.getApplication().runWriteAction(new Runnable() {

        @Override
        public void run() {
            libraryModel.commit();
            projectLibraryModel.commit();
        }
    });
}
6 comments
Comment actions Permalink

just an update - I found by adding an extra line to my existing code I can get the libraries to appear in the external libraries area. But I still have not figured out how to make the library be of my custom LibraryKind.

This is the code I currently have which does show in external libraries now:

    private static void createLibraryDependency(final Module module, Project project, String libraryName, String libraryPath) {
        LibraryTable projectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(project);
        final
LibraryTable.ModifiableModel projectLibraryModel = projectLibraryTable.getModifiableModel();

        final
Library library = projectLibraryModel.createLibrary(libraryName);
        final
Library.ModifiableModel libraryModel = library.getModifiableModel();
        
String pathUrl = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, libraryPath);
        
VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(pathUrl);

        if
(file != null) {
            libraryModel.addRoot(file, OrderRootType.CLASSES);
            
ApplicationManager.getApplication().runWriteAction(new Runnable() {
                @Override
                public void run
() {
                    libraryModel.commit();
                    projectLibraryModel
.commit();
                    
ModuleRootModificationUtil.addDependency(module, library);
                
}
            });
        
}
    }


I have tried to solve the remaining problem of getting the library to show as my custom LibraryKind - I can get it to work initially by doing this:

((LibraryImpl) libraryModel).setKind(DLanguageLibraryType.DLANGUAGE_KIND);


However this does work but then I keep getting a pop up notification saying that the project could not be saved with NULL. Everytime the IDE tries to save it brings up the pop up saying project could not be saved. So although it does mark the external libraries correctly - Its not a solutions.

0
Comment actions Permalink

by "external libraries area" you mean editor project structure treeview (where modules, packages, sources etc appear), not File > Project Structure, right?

> I also added a custom library type so when I choose add a library in the Module Settings > Libraries - I can choose a custom language library and add the library fine - it shows up in Libraries with the custom language logo next to it - and it also appears in External Libraries with the standard jar icon.

is this all it takes for custom library added by user (in File > Project Structure) to appear in External Libraries - a custom library type?

0
Comment actions Permalink

Hi

Yes I mean the default Left hand "Project" pane where the module sources are shown. At the bottom of the tree there is a section called External Libraries.

I added some extension points for custom libraries so that I can go to Module Settings > Libraries and choose a library to add and it will appear as an external library but marked as my custom library type - so it shows in External Libraries with my custom icon.

However the code I included in this question above - just programmatically creates a default library and not a custom library. I would like to programmatically create a library that is my custom library type.

I'm not sure if it's possible because the only way I can get it to partially work programmatically is to cast the modifiable model of the library to (LibraryImpl) but then when the plugin runs the IDE cannot save the project and a pop up says "Cannot Save Project - null)

0
Comment actions Permalink

Hello Kingsley

> I added some extension points for custom libraries

could you hint: which extension points? ;)


> programmatically create a library that is my custom library type

not sure how to do this.



> the IDE cannot save the project and a pop up says "Cannot Save Project - null)

is there no detailed stack trace with the error?

did you check Event Log?

are you running plugin in debug mode from plugin source project or deployed & installed?

0
Comment actions Permalink

Hi

Yeah I'm running in debug mode but there are no errors in the event log - I will have investigate further.

The custom library code is here:
https://github.com/kingsleyh/DLanguage/tree/master/src/net/masterthought/dlanguage/library

and the project.xml has the extension points for it:
https://github.com/kingsleyh/DLanguage/blob/master/META-INF/plugin.xml

Then when the plugin runs you can go to Module Settings > Libraries and you get the option to choose a library of the custom type to add. When you add it - it appears in the list of Libraries, the External Libraries list in left pane, as well as in the project/module.

0
Comment actions Permalink

very neat.

thank you very much for the detail.


actually "Cannot Save Project - null" error rings a bell. I ran into this after reorganizing project structure: rearranging modules, manually (outside of Idea) moving .iml file, source files.

maybe try to create a new project (not for plugin - for the project using the plugin)?

also try to open this error causing project with your plugin disabled - this would allow to narrow down the problem a bit: is it caused by your plugin or not.


some of these reported bugs may shed some light too:

https://youtrack.jetbrains.com/issues/IDEA?q=%22Could+not+save+project%22

0

Please sign in to leave a comment.