Search in sources :

Example 61 with LibraryTable

use of com.intellij.openapi.roots.libraries.LibraryTable in project intellij-plugins by JetBrains.

the class DartFileListener method updatePackagesLibraryRoots.

@NotNull
public static Library updatePackagesLibraryRoots(@NotNull final Project project, @NotNull final DartLibInfo libInfo) {
    final LibraryTable projectLibraryTable = ProjectLibraryTable.getInstance(project);
    final Library existingLibrary = projectLibraryTable.getLibraryByName(DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME);
    final Library library = existingLibrary != null ? existingLibrary : WriteAction.compute(() -> {
        final LibraryTableBase.ModifiableModel libTableModel = ProjectLibraryTable.getInstance(project).getModifiableModel();
        final Library lib = libTableModel.createLibrary(DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME, DartPackagesLibraryType.LIBRARY_KIND);
        libTableModel.commit();
        return lib;
    });
    final String[] existingUrls = library.getUrls(OrderRootType.CLASSES);
    final Collection<String> libRootUrls = libInfo.getLibRootUrls();
    if ((!libInfo.isProjectWithoutPubspec() && isBrokenPackageMap(((LibraryEx) library).getProperties())) || existingUrls.length != libRootUrls.size() || !libRootUrls.containsAll(Arrays.asList(existingUrls))) {
        ApplicationManager.getApplication().runWriteAction(() -> {
            final LibraryEx.ModifiableModelEx model = (LibraryEx.ModifiableModelEx) library.getModifiableModel();
            for (String url : existingUrls) {
                model.removeRoot(url, OrderRootType.CLASSES);
            }
            for (String url : libRootUrls) {
                model.addRoot(url, OrderRootType.CLASSES);
            }
            final DartPackagesLibraryProperties libraryProperties = new DartPackagesLibraryProperties();
            libraryProperties.setPackageNameToDirsMap(libInfo.getPackagesMap());
            model.setProperties(libraryProperties);
            model.commit();
        });
    }
    return library;
}
Also used : DartPackagesLibraryProperties(com.jetbrains.lang.dart.sdk.DartPackagesLibraryProperties) ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) LibraryEx(com.intellij.openapi.roots.impl.libraries.LibraryEx) Library(com.intellij.openapi.roots.libraries.Library) NotNull(org.jetbrains.annotations.NotNull)

Example 62 with LibraryTable

use of com.intellij.openapi.roots.libraries.LibraryTable in project intellij-plugins by JetBrains.

the class FlexTestUtils method createConfigEditor.

public static FlexProjectConfigurationEditor createConfigEditor(final Module... modules) {
    @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") final Map<Module, ModifiableRootModel> models = new FactoryMap<Module, ModifiableRootModel>() {

        @Override
        protected ModifiableRootModel create(final Module module) {
            final ModifiableRootModel result = ModuleRootManager.getInstance(module).getModifiableModel();
            Disposer.register(module, new Disposable() {

                @Override
                public void dispose() {
                    if (!result.isDisposed()) {
                        result.dispose();
                    }
                }
            });
            return result;
        }
    };
    return new FlexProjectConfigurationEditor(modules[0].getProject(), new FlexProjectConfigurationEditor.ProjectModifiableModelProvider() {

        @Override
        public Module[] getModules() {
            return modules;
        }

        @Override
        public ModifiableRootModel getModuleModifiableModel(Module module) {
            return models.get(module);
        }

        @Override
        public void addListener(FlexBCConfigurator.Listener listener, Disposable parentDisposable) {
        // ignore
        }

        @Override
        public void commitModifiableModels() throws ConfigurationException {
            ApplicationManager.getApplication().runWriteAction(() -> {
                for (ModifiableRootModel model : models.values()) {
                    if (model.isChanged()) {
                        model.commit();
                    }
                }
            });
        }

        public Library findSourceLibraryForLiveName(final String name, final String level) {
            return findSourceLibrary(name, level);
        }

        public Library findSourceLibrary(final String name, final String level) {
            return getLibrariesTable(level).getLibraryByName(name);
        }

        private LibraryTable getLibrariesTable(final String level) {
            if (LibraryTablesRegistrar.APPLICATION_LEVEL.equals(level)) {
                return ApplicationLibraryTable.getApplicationTable();
            } else {
                assert LibraryTablesRegistrar.PROJECT_LEVEL.equals(level);
                return ProjectLibraryTable.getInstance(modules[0].getProject());
            }
        }
    });
}
Also used : FactoryMap(com.intellij.util.containers.FactoryMap) Disposable(com.intellij.openapi.Disposable) FlexBCConfigurator(com.intellij.lang.javascript.flex.projectStructure.FlexBCConfigurator) FlexProjectConfigurationEditor(com.intellij.lang.javascript.flex.projectStructure.model.impl.FlexProjectConfigurationEditor) ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) ApplicationLibraryTable(com.intellij.openapi.roots.impl.libraries.ApplicationLibraryTable) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) ConfigurationException(com.intellij.openapi.options.ConfigurationException) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module)

Example 63 with LibraryTable

use of com.intellij.openapi.roots.libraries.LibraryTable in project intellij-plugins by JetBrains.

the class FlexTestUtils method doAddFlexLibrary.

private static void doAddFlexLibrary(boolean isProjectLibrary, Module module, String libraryName, boolean overwrite, String libraryRoot, @Nullable String classesPath, @Nullable String sourcesPath, @Nullable String asdocPath, LinkageType linkageType) {
    ModifiableRootModel moduleModifiableModel = ModuleRootManager.getInstance(module).getModifiableModel();
    WriteAction.run(() -> {
        try {
            // first let's create Flex library
            final LibraryTable libraryTable;
            if (isProjectLibrary) {
                libraryTable = ProjectLibraryTable.getInstance(module.getProject());
            } else {
                libraryTable = moduleModifiableModel.getModuleLibraryTable();
            }
            Library library = libraryTable.getLibraryByName(libraryName);
            if (library != null && overwrite) {
                libraryTable.removeLibrary(library);
                library = null;
            }
            if (library == null) {
                LibraryTable.ModifiableModel libraryTableModifiableModel = libraryTable.getModifiableModel();
                library = libraryTableModifiableModel.createLibrary(libraryName, FlexLibraryType.FLEX_LIBRARY);
                LibraryEx.ModifiableModelEx libraryModel = (LibraryEx.ModifiableModelEx) library.getModifiableModel();
                libraryModel.setProperties(new FlexLibraryProperties(FlexLibraryIdGenerator.generateId()));
                addRootIfNotNull(libraryRoot, classesPath, libraryModel, OrderRootType.CLASSES, ".swc", ".zip");
                addRootIfNotNull(libraryRoot, sourcesPath, libraryModel, OrderRootType.SOURCES, ".zip");
                addRootIfNotNull(libraryRoot, asdocPath, libraryModel, JavadocOrderRootType.getInstance(), ".zip");
                libraryModel.commit();
                libraryTableModifiableModel.commit();
            }
            moduleModifiableModel.commit();
            // then add Flex library to build configuration dependency
            final String committedLibraryId;
            if (isProjectLibrary) {
                committedLibraryId = FlexProjectRootsUtil.getLibraryId(ProjectLibraryTable.getInstance(module.getProject()).getLibraryByName(libraryName));
            } else {
                final OrderEntry entry = ContainerUtil.find(ModuleRootManager.getInstance(module).getOrderEntries(), orderEntry -> orderEntry instanceof LibraryOrderEntry && ((LibraryOrderEntry) orderEntry).getLibraryName().equals(libraryName));
                committedLibraryId = FlexProjectRootsUtil.getLibraryId(((LibraryOrderEntry) entry).getLibrary());
            }
            if (ModuleType.get(module) == FlexModuleType.getInstance()) {
                modifyConfigs(module.getProject(), e -> {
                    final ModifiableFlexBuildConfiguration[] bcs = e.getConfigurations(module);
                    final ModifiableDependencyEntry dependencyEntry;
                    if (isProjectLibrary) {
                        dependencyEntry = e.createSharedLibraryEntry(bcs[0].getDependencies(), libraryName, LibraryTablesRegistrar.PROJECT_LEVEL);
                    } else {
                        dependencyEntry = e.createModuleLibraryEntry(bcs[0].getDependencies(), committedLibraryId);
                    }
                    dependencyEntry.getDependencyType().setLinkageType(linkageType);
                    bcs[0].getDependencies().getModifiableEntries().add(dependencyEntry);
                });
            }
        } finally {
            if (!moduleModifiableModel.isDisposed()) {
                moduleModifiableModel.dispose();
            }
        }
    });
}
Also used : FlexLibraryProperties(com.intellij.lang.javascript.flex.library.FlexLibraryProperties) LibraryEx(com.intellij.openapi.roots.impl.libraries.LibraryEx) ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) ApplicationLibraryTable(com.intellij.openapi.roots.impl.libraries.ApplicationLibraryTable) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) Library(com.intellij.openapi.roots.libraries.Library)

Example 64 with LibraryTable

use of com.intellij.openapi.roots.libraries.LibraryTable in project intellij-plugins by JetBrains.

the class FlexProjectConfigTest method createModuleLibrary.

private String createModuleLibrary() {
    final ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(myModule).getModifiableModel();
    final LibraryTable.ModifiableModel libraryTable = modifiableModel.getModuleLibraryTable().getModifiableModel();
    LibraryEx library = (LibraryEx) libraryTable.createLibrary("test", FlexLibraryType.FLEX_LIBRARY);
    String libraryId = UUID.randomUUID().toString();
    ((FlexLibraryProperties) library.getProperties()).setId(libraryId);
    ApplicationManager.getApplication().runWriteAction(() -> {
        libraryTable.commit();
        modifiableModel.commit();
    });
    return libraryId;
}
Also used : LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) FlexLibraryProperties(com.intellij.lang.javascript.flex.library.FlexLibraryProperties) LibraryEx(com.intellij.openapi.roots.impl.libraries.LibraryEx)

Aggregations

LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)64 Library (com.intellij.openapi.roots.libraries.Library)43 ProjectLibraryTable (com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable)18 VirtualFile (com.intellij.openapi.vfs.VirtualFile)16 Module (com.intellij.openapi.module.Module)10 NotNull (org.jetbrains.annotations.NotNull)7 Project (com.intellij.openapi.project.Project)5 LibraryEx (com.intellij.openapi.roots.impl.libraries.LibraryEx)5 LibraryTablesRegistrar (com.intellij.openapi.roots.libraries.LibraryTablesRegistrar)4 File (java.io.File)4 Element (org.jdom.Element)4 LibraryImpl (com.intellij.openapi.roots.impl.libraries.LibraryImpl)3 ArrayList (java.util.ArrayList)3 FlexLibraryProperties (com.intellij.lang.javascript.flex.library.FlexLibraryProperties)2 Application (com.intellij.openapi.application.Application)2 Result (com.intellij.openapi.application.Result)2 Sdk (com.intellij.openapi.projectRoots.Sdk)2 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)2 OrderEntry (com.intellij.openapi.roots.OrderEntry)2 ModuleLibraryTable (com.intellij.openapi.roots.impl.ModuleLibraryTable)2