Search in sources :

Example 36 with LibraryTable

use of com.intellij.openapi.roots.libraries.LibraryTable in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoModuleLibrariesInitializer method removeLibraryIfNeeded.

private void removeLibraryIfNeeded() {
    ApplicationManager.getApplication().assertIsDispatchThread();
    ModifiableModelsProvider modelsProvider = ModifiableModelsProvider.SERVICE.getInstance();
    ModifiableRootModel model = modelsProvider.getModuleModifiableModel(myModule);
    LibraryOrderEntry goLibraryEntry = OrderEntryUtil.findLibraryOrderEntry(model, getLibraryName());
    if (goLibraryEntry != null) {
        ApplicationManager.getApplication().runWriteAction(() -> {
            Library library = goLibraryEntry.getLibrary();
            if (library != null) {
                LibraryTable table = library.getTable();
                if (table != null) {
                    table.removeLibrary(library);
                    model.removeOrderEntry(goLibraryEntry);
                    modelsProvider.commitModuleModifiableModel(model);
                }
            } else {
                modelsProvider.disposeModuleModifiableModel(model);
            }
        });
    } else {
        ApplicationManager.getApplication().runWriteAction(() -> modelsProvider.disposeModuleModifiableModel(model));
    }
}
Also used : LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) Library(com.intellij.openapi.roots.libraries.Library)

Example 37 with LibraryTable

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

the class GradleSyncTest method shouldUseLibrary.

@Test
public void shouldUseLibrary() throws IOException {
    guiTest.importSimpleApplication();
    IdeFrameFixture ideFrame = guiTest.ideFrame();
    Project project = ideFrame.getProject();
    // Make sure the library was added.
    LibraryTable libraryTable = ProjectLibraryTable.getInstance(project);
    String libraryName = "org.apache.http.legacy-" + TestUtils.getLatestAndroidPlatform();
    Library library = libraryTable.getLibraryByName(libraryName);
    // Verify that the library has the right j
    VirtualFile[] jarFiles = library.getFiles(CLASSES);
    assertThat(jarFiles).asList().hasSize(1);
    VirtualFile jarFile = jarFiles[0];
    assertEquals("org.apache.http.legacy.jar", jarFile.getName());
    // Verify that the module depends on the library
    Module appModule = ideFrame.getModule("app");
    AtomicBoolean dependencyFound = new AtomicBoolean();
    new ReadAction() {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(appModule).getModifiableModel();
            try {
                for (OrderEntry orderEntry : modifiableModel.getOrderEntries()) {
                    if (orderEntry instanceof LibraryOrderEntry) {
                        LibraryOrderEntry libraryDependency = (LibraryOrderEntry) orderEntry;
                        if (libraryDependency.getLibrary() == library) {
                            dependencyFound.set(true);
                        }
                    }
                }
            } finally {
                modifiableModel.dispose();
            }
        }
    }.execute();
    assertTrue("Module app should depend on library '" + library.getName() + "'", dependencyFound.get());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Result(com.intellij.openapi.application.Result) Project(com.intellij.openapi.project.Project) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) ReadAction(com.intellij.openapi.application.ReadAction) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) Test(org.junit.Test)

Example 38 with LibraryTable

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

the class TestArtifactSearchScopesTest method testNotExcludeLibrariesInMainArtifact.

public void testNotExcludeLibrariesInMainArtifact() throws Exception {
    TestArtifactSearchScopes scopes = loadMultiProjectAndTestScopes();
    LibraryTable libraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(myFixture.getProject());
    Library gson = libraryTable.getLibraryByName("gson-2.2.4");
    // In the beginning only unit test exclude gson
    assertScopeContainsLibrary(scopes.getUnitTestExcludeScope(), gson, true);
    assertScopeContainsLibrary(scopes.getAndroidTestExcludeScope(), gson, false);
    // Now add gson to unit test dependencies as well
    VirtualFile buildFile = getGradleBuildFile(scopes.getModule());
    assertNotNull(buildFile);
    appendToFile(virtualToIoFile(buildFile), "\n\ndependencies { compile 'com.google.code.gson:gson:2.2.4' }\n");
    final CountDownLatch latch = new CountDownLatch(1);
    GradleSyncListener postSetupListener = new GradleSyncListener.Adapter() {

        @Override
        public void syncSucceeded(@NotNull Project project) {
            latch.countDown();
        }

        @Override
        public void syncFailed(@NotNull Project project, @NotNull String errorMessage) {
            latch.countDown();
        }
    };
    GradleSyncState.subscribe(getProject(), postSetupListener);
    runWriteCommandAction(getProject(), () -> {
        GradleSyncInvoker.Request request = new GradleSyncInvoker.Request().setGenerateSourcesOnSuccess(false);
        GradleSyncInvoker.getInstance().requestProjectSync(getProject(), request, null);
    });
    latch.await();
    // Now both test should not exclude gson
    scopes = TestArtifactSearchScopes.get(scopes.getModule());
    assertNotNull(scopes);
    gson = libraryTable.getLibraryByName("gson-2.2.4");
    assertScopeContainsLibrary(scopes.getUnitTestExcludeScope(), gson, false);
    assertScopeContainsLibrary(scopes.getAndroidTestExcludeScope(), gson, false);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) GradleSyncListener(com.android.tools.idea.gradle.project.sync.GradleSyncListener) GradleSyncInvoker(com.android.tools.idea.gradle.project.sync.GradleSyncInvoker) Library(com.intellij.openapi.roots.libraries.Library) CountDownLatch(java.util.concurrent.CountDownLatch) NotNull(org.jetbrains.annotations.NotNull)

Example 39 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 40 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)

Aggregations

LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)65 Library (com.intellij.openapi.roots.libraries.Library)44 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