Search in sources :

Example 61 with Library

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

the class AndroidRootUtil method fillExternalLibrariesAndModules.

private static void fillExternalLibrariesAndModules(@NotNull final Module module, @NotNull final Set<VirtualFile> outputDirs, @NotNull final Set<Module> visited, @Nullable final Set<VirtualFile> libraries, final boolean exportedLibrariesOnly, final boolean recursive) {
    if (!visited.add(module)) {
        return;
    }
    ApplicationManager.getApplication().runReadAction(() -> {
        ModuleRootManager manager = ModuleRootManager.getInstance(module);
        for (OrderEntry entry : manager.getOrderEntries()) {
            if (!(entry instanceof ExportableOrderEntry) || ((ExportableOrderEntry) entry).getScope() != DependencyScope.COMPILE) {
                continue;
            }
            if (libraries != null && entry instanceof LibraryOrderEntry) {
                LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) entry;
                Library library = libraryOrderEntry.getLibrary();
                if (library != null && (!exportedLibrariesOnly || libraryOrderEntry.isExported())) {
                    for (VirtualFile file : library.getFiles(OrderRootType.CLASSES)) {
                        if (!file.exists()) {
                            continue;
                        }
                        if (file.getFileType() instanceof ArchiveFileType) {
                            if (file.getFileSystem() instanceof JarFileSystem) {
                                VirtualFile localFile = JarFileSystem.getInstance().getVirtualFileForJar(file);
                                if (localFile != null) {
                                    libraries.add(localFile);
                                }
                            } else {
                                libraries.add(file);
                            }
                        } else if (file.isDirectory() && !(file.getFileSystem() instanceof JarFileSystem)) {
                            collectClassFilesAndJars(file, libraries, new HashSet<>());
                        }
                    }
                }
            } else if (entry instanceof ModuleOrderEntry) {
                Module depModule = ((ModuleOrderEntry) entry).getModule();
                if (depModule == null) {
                    continue;
                }
                AndroidFacet facet = AndroidFacet.getInstance(depModule);
                boolean libraryProject = facet != null && facet.isLibraryProject();
                CompilerModuleExtension extension = CompilerModuleExtension.getInstance(depModule);
                if (extension != null) {
                    VirtualFile classDir = extension.getCompilerOutputPath();
                    if (libraryProject) {
                        VirtualFile tmpArtifactsDir = AndroidDexCompiler.getOutputDirectoryForDex(depModule);
                        if (tmpArtifactsDir != null) {
                            VirtualFile packedClassesJar = tmpArtifactsDir.findChild(CLASSES_JAR_FILE_NAME);
                            if (packedClassesJar != null) {
                                outputDirs.add(packedClassesJar);
                            }
                        }
                    } else // do not support android-app->android-app compile dependencies
                    if (facet == null && !outputDirs.contains(classDir) && classDir != null && classDir.exists()) {
                        outputDirs.add(classDir);
                    }
                }
                if (recursive) {
                    fillExternalLibrariesAndModules(depModule, outputDirs, visited, libraries, !libraryProject || exportedLibrariesOnly, true);
                }
            }
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArchiveFileType(com.intellij.ide.highlighter.ArchiveFileType) JarFileSystem(com.intellij.openapi.vfs.JarFileSystem) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) AndroidMavenUtil.isMavenizedModule(org.jetbrains.android.maven.AndroidMavenUtil.isMavenizedModule)

Example 62 with Library

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

the class GradleSyncTest method aarSourceAttachments.

@Test
public void aarSourceAttachments() throws IOException {
    guiTest.importSimpleApplication();
    IdeFrameFixture ideFrame = guiTest.ideFrame();
    Project project = ideFrame.getProject();
    Module appModule = ideFrame.getModule("app");
    execute(new GuiTask() {

        @Override
        protected void executeInEDT() throws Throwable {
            runWriteCommandAction(project, () -> {
                GradleBuildModel buildModel = GradleBuildModel.get(appModule);
                String newDependency = "com.mapbox.mapboxsdk:mapbox-android-sdk:0.7.4@aar";
                buildModel.dependencies().addArtifact(COMPILE, newDependency);
                buildModel.applyChanges();
            });
        }
    });
    ideFrame.requestProjectSync().waitForGradleProjectSyncToFinish();
    // Verify that the library has sources.
    LibraryTable libraryTable = ProjectLibraryTable.getInstance(project);
    String libraryName = "mapbox-android-sdk-0.7.4";
    Library library = libraryTable.getLibraryByName(libraryName);
    VirtualFile[] files = library.getFiles(SOURCES);
    assertThat(files).asList().hasSize(1);
}
Also used : GuiTask(org.fest.swing.edt.GuiTask) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) GradleBuildModel(com.android.tools.idea.gradle.dsl.model.GradleBuildModel) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) Test(org.junit.Test)

Example 63 with Library

use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.

the class AppEngineSupportProvider method addSupport.

private void addSupport(final Module module, final ModifiableRootModel rootModel, FrameworkSupportModel frameworkSupportModel, String sdkPath, @Nullable PersistenceApi persistenceApi) {
    FacetType<AppEngineFacet, AppEngineFacetConfiguration> facetType = AppEngineFacet.getFacetType();
    AppEngineFacet appEngineFacet = FacetManager.getInstance(module).addFacet(facetType, facetType.getDefaultFacetName(), null);
    AppEngineWebIntegration webIntegration = AppEngineWebIntegration.getInstance();
    webIntegration.registerFrameworkInModel(frameworkSupportModel, appEngineFacet);
    final AppEngineFacetConfiguration facetConfiguration = appEngineFacet.getConfiguration();
    facetConfiguration.setSdkHomePath(sdkPath);
    final AppEngineSdk sdk = appEngineFacet.getSdk();
    final Artifact webArtifact = findOrCreateWebArtifact(appEngineFacet);
    final VirtualFile webDescriptorDir = webIntegration.suggestParentDirectoryForAppEngineWebXml(module, rootModel);
    if (webDescriptorDir != null) {
        VirtualFile descriptor = createFileFromTemplate(AppEngineTemplateGroupDescriptorFactory.APP_ENGINE_WEB_XML_TEMPLATE, webDescriptorDir, AppEngineUtil.APP_ENGINE_WEB_XML_NAME);
        if (descriptor != null) {
            webIntegration.addDescriptor(webArtifact, module.getProject(), descriptor);
        }
    }
    final Project project = module.getProject();
    webIntegration.addDevServerToModuleDependencies(rootModel, sdk);
    final Library apiJar = addProjectLibrary(module, "AppEngine API", sdk.getUserLibraryPaths(), VirtualFile.EMPTY_ARRAY);
    rootModel.addLibraryEntry(apiJar);
    webIntegration.addLibraryToArtifact(apiJar, webArtifact, project);
    if (persistenceApi != null) {
        facetConfiguration.setRunEnhancerOnMake(true);
        facetConfiguration.setPersistenceApi(persistenceApi);
        facetConfiguration.getFilesToEnhance().addAll(AppEngineUtil.getDefaultSourceRootsToEnhance(rootModel));
        try {
            final VirtualFile[] sourceRoots = rootModel.getSourceRoots();
            final VirtualFile sourceRoot;
            if (sourceRoots.length > 0) {
                sourceRoot = sourceRoots[0];
            } else {
                sourceRoot = findOrCreateChildDirectory(rootModel.getContentRoots()[0], "src");
            }
            VirtualFile metaInf = findOrCreateChildDirectory(sourceRoot, "META-INF");
            if (persistenceApi == PersistenceApi.JDO || persistenceApi == PersistenceApi.JDO3) {
                createFileFromTemplate(AppEngineTemplateGroupDescriptorFactory.APP_ENGINE_JDO_CONFIG_TEMPLATE, metaInf, AppEngineUtil.JDO_CONFIG_XML_NAME);
            } else {
                final VirtualFile file = createFileFromTemplate(AppEngineTemplateGroupDescriptorFactory.APP_ENGINE_JPA_CONFIG_TEMPLATE, metaInf, AppEngineUtil.JPA_CONFIG_XML_NAME);
                if (file != null) {
                    webIntegration.setupJpaSupport(module, file);
                }
            }
        } catch (IOException e) {
            LOG.error(e);
        }
        final Library library = addProjectLibrary(module, "AppEngine ORM", Collections.singletonList(sdk.getOrmLibDirectoryPath()), sdk.getOrmLibSources());
        rootModel.addLibraryEntry(library);
        webIntegration.addLibraryToArtifact(library, webArtifact, project);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) Library(com.intellij.openapi.roots.libraries.Library) IOException(java.io.IOException) AppEngineSdk(com.intellij.appengine.sdk.AppEngineSdk) Artifact(com.intellij.packaging.artifacts.Artifact)

Example 64 with Library

use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.

the class AppEngineSupportProvider method addProjectLibrary.

private static Library addProjectLibrary(final Module module, final String name, final List<String> jarDirectories, final VirtualFile[] sources) {
    return new WriteAction<Library>() {

        protected void run(@NotNull final Result<Library> result) {
            final LibraryTable libraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(module.getProject());
            Library library = libraryTable.getLibraryByName(name);
            if (library == null) {
                library = libraryTable.createLibrary(name);
                final Library.ModifiableModel model = library.getModifiableModel();
                for (String path : jarDirectories) {
                    String url = VfsUtilCore.pathToUrl(path);
                    VirtualFileManager.getInstance().refreshAndFindFileByUrl(url);
                    model.addJarDirectory(url, false);
                }
                for (VirtualFile sourceRoot : sources) {
                    model.addRoot(sourceRoot, OrderRootType.SOURCES);
                }
                model.commit();
            }
            result.setResult(library);
        }
    }.execute().getResultObject();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) WriteAction(com.intellij.openapi.application.WriteAction) Library(com.intellij.openapi.roots.libraries.Library) NotNull(org.jetbrains.annotations.NotNull) ValidationResult(com.intellij.facet.ui.ValidationResult) Result(com.intellij.openapi.application.Result)

Example 65 with Library

use of com.intellij.openapi.roots.libraries.Library in project intellij-community by JetBrains.

the class LibrariesUtil method populateOrderEntries.

private static void populateOrderEntries(@NotNull Module module, Condition<Library> condition, ArrayList<Library> libraries, boolean exportedOnly, Set<Module> visited) {
    if (!visited.add(module)) {
        return;
    }
    for (OrderEntry entry : ModuleRootManager.getInstance(module).getOrderEntries()) {
        if (entry instanceof LibraryOrderEntry) {
            LibraryOrderEntry libEntry = (LibraryOrderEntry) entry;
            if (exportedOnly && !libEntry.isExported()) {
                continue;
            }
            Library library = libEntry.getLibrary();
            if (condition.value(library)) {
                libraries.add(library);
            }
        } else if (entry instanceof ModuleOrderEntry) {
            final Module dep = ((ModuleOrderEntry) entry).getModule();
            if (dep != null) {
                populateOrderEntries(dep, condition, libraries, true, visited);
            }
        }
    }
}
Also used : Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module)

Aggregations

Library (com.intellij.openapi.roots.libraries.Library)240 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)58 VirtualFile (com.intellij.openapi.vfs.VirtualFile)58 Module (com.intellij.openapi.module.Module)41 NotNull (org.jetbrains.annotations.NotNull)31 File (java.io.File)29 LibraryEx (com.intellij.openapi.roots.impl.libraries.LibraryEx)25 Nullable (org.jetbrains.annotations.Nullable)25 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)23 Project (com.intellij.openapi.project.Project)22 ProjectLibraryTable (com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable)20 ArrayList (java.util.ArrayList)15 Sdk (com.intellij.openapi.projectRoots.Sdk)13 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)13 OrderEntry (com.intellij.openapi.roots.OrderEntry)10 THashSet (gnu.trove.THashSet)9 IOException (java.io.IOException)9 Element (org.jdom.Element)9 OrderRoot (com.intellij.openapi.roots.libraries.ui.OrderRoot)8 LibrariesContainer (com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer)7