phpstorm Dynamically creating an external library

Answered

How to include an external library in phpstorm? 

I tried this

Path path = getOrCreateVersionPath(version);
LibraryTable table = LibraryTablesRegistrar.getInstance().getLibraryTable(project);
Library library = table.getLibraryByName(LIB_NAME);
if (library == null) {
    library = LibraryUtil.createLibrary(table, LIB_NAME);
}
Library.ModifiableModel model = library.getModifiableModel();
model.addRoot(VfsUtil.getUrlForLibraryRoot(path), OrderRootType.CLASSES);
model.commit();

This works in IDEA, but doesn't work in phpstorm

For phpstorm, it works like this

<extensions defaultExtensionNs="com.jetbrains.php">
    <libraryRoot id="mylib"  runtime="false" path="/src" />
</extensions>

But I need to dynamically change source versions depending on user settings

0
2 comments

Hi, Prihod! We have a set of API to allow to add libraries from plugins, please check out  `com.jetbrains.php.config.library.PhpLibraryRoot`, `com.jetbrains.php.config.library.PhpLibraryRoot#EP_NAME`, `com.jetbrains.php.config.library.PhpLibraryRootProvider`. This should help to achieve what you need!

0

If I use 

com.jetbrains.php.config.library.PhpLibraryRootProvider

the files are not visible in “External Libraries”, although they are indexed.

 

I did it like this

Path path = getOrCreateVersionPath(version);
VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByNioFile(path);
if (file != null) {
    Module[] modules = ModuleManager.getInstance(project).getModules();
    if (modules.length > 0) {
        ModifiableRootModel model = ModuleRootManager.getInstance(modules[0]).getModifiableModel();
        LibraryTable table = model.getModuleLibraryTable();
        Library library = table.getLibraryByName(LIB_NAME);
        if (library != null) {
            ModuleRootModificationUtil.removeDependency(modules[0], library);
        }
        ModuleRootModificationUtil.addModuleLibrary(modules[0], LIB_NAME, Collections.singletonList(file.getUrl()), Collections.emptyList(), DependencyScope.COMPILE);
    }
}
0

Please sign in to leave a comment.