Search in sources :

Example 1 with DependencyScope

use of com.intellij.openapi.roots.DependencyScope in project kotlin by JetBrains.

the class ExternalSystemImportingTestCase method assertModuleModuleDepScope.

protected void assertModuleModuleDepScope(String moduleName, String depName, DependencyScope... scopes) {
    List<ModuleOrderEntry> deps = getModuleModuleDeps(moduleName, depName);
    Set<DependencyScope> actualScopes = new HashSet<DependencyScope>();
    for (ModuleOrderEntry dep : deps) {
        actualScopes.add(dep.getScope());
    }
    HashSet<DependencyScope> expectedScopes = new HashSet<DependencyScope>(Arrays.asList(scopes));
    assertEquals("Dependency '" + depName + "' for module '" + moduleName + "' has unexpected scope", expectedScopes, actualScopes);
}
Also used : DependencyScope(com.intellij.openapi.roots.DependencyScope) ModuleOrderEntry(com.intellij.openapi.roots.ModuleOrderEntry)

Example 2 with DependencyScope

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

the class ModuleSourceItemGroup method collectDependentModules.

private static void collectDependentModules(final Module module, Set<Module> modules, ArtifactEditorContext context) {
    if (!modules.add(module))
        return;
    for (OrderEntry entry : context.getModulesProvider().getRootModel(module).getOrderEntries()) {
        if (entry instanceof ModuleOrderEntry) {
            final ModuleOrderEntry moduleEntry = (ModuleOrderEntry) entry;
            final Module dependency = moduleEntry.getModule();
            final DependencyScope scope = moduleEntry.getScope();
            if (dependency != null && scope.isForProductionRuntime()) {
                collectDependentModules(dependency, modules, context);
            }
        }
    }
}
Also used : OrderEntry(com.intellij.openapi.roots.OrderEntry) ModuleOrderEntry(com.intellij.openapi.roots.ModuleOrderEntry) DependencyScope(com.intellij.openapi.roots.DependencyScope) ModuleOrderEntry(com.intellij.openapi.roots.ModuleOrderEntry) Module(com.intellij.openapi.module.Module)

Example 3 with DependencyScope

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

the class LibraryCompositionSettings method addLibraries.

@Nullable
public Library addLibraries(@NotNull final ModifiableRootModel rootModel, @NotNull final List<Library> addedLibraries, @Nullable final LibrariesContainer librariesContainer) {
    Library newLibrary = createLibrary(rootModel, librariesContainer);
    if (newLibrary != null) {
        addedLibraries.add(newLibrary);
        DependencyScope scope = LibraryDependencyScopeSuggester.getDefaultScope(newLibrary);
        if (getLibraryLevel() != LibrariesContainer.LibraryLevel.MODULE) {
            rootModel.addLibraryEntry(newLibrary).setScope(scope);
        } else {
            LibraryOrderEntry orderEntry = rootModel.findLibraryOrderEntry(newLibrary);
            assert orderEntry != null;
            orderEntry.setScope(scope);
        }
    }
    if (mySelectedLibrary != null) {
        addedLibraries.add(mySelectedLibrary);
        rootModel.addLibraryEntry(mySelectedLibrary).setScope(LibraryDependencyScopeSuggester.getDefaultScope(mySelectedLibrary));
    }
    if (myLibraryProvider != null) {
        Library library = myLibraryProvider.createLibrary(myLibraryDescription.getSuitableLibraryKinds());
        addedLibraries.add(library);
        rootModel.addLibraryEntry(library).setScope(LibraryDependencyScopeSuggester.getDefaultScope(library));
    }
    return newLibrary;
}
Also used : DependencyScope(com.intellij.openapi.roots.DependencyScope) Library(com.intellij.openapi.roots.libraries.Library) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with DependencyScope

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

the class GradleProjectResolverUtil method doBuildDependencies.

private static void doBuildDependencies(@NotNull ProjectResolverContext resolverCtx, @NotNull Map<String, Pair<DataNode<GradleSourceSetData>, ExternalSourceSet>> sourceSetMap, @NotNull final Map<String, String> artifactsMap, @NotNull Map<ExternalDependencyId, ExternalDependency> mergedDependencyMap, @NotNull DataNode<? extends ExternalEntityData> ownerDataNode, @NotNull Collection<ExternalDependency> dependencies, @Nullable DataNode<ProjectData> ideProject) throws IllegalStateException {
    Map<ExternalDependencyId, ExternalDependency> dependencyMap = ContainerUtil.newLinkedHashMap();
    for (ExternalDependency dependency : dependencies) {
        final ExternalDependency dep = dependencyMap.get(dependency.getId());
        if (dep instanceof AbstractExternalDependency) {
            dep.getDependencies().addAll(ContainerUtil.subtract(dependency.getDependencies(), dep.getDependencies()));
        } else {
            dependencyMap.put(dependency.getId(), dependency);
        }
    }
    for (ExternalDependency dependency : dependencyMap.values()) {
        final ExternalDependency mergedDependency = ContainerUtil.getOrElse(mergedDependencyMap, dependency.getId(), dependency);
        DependencyScope dependencyScope = getDependencyScope(mergedDependency.getScope());
        ModuleData ownerModule = null;
        if (ownerDataNode.getData() instanceof ModuleData) {
            ownerModule = (ModuleData) ownerDataNode.getData();
        } else if (ownerDataNode.getData() instanceof DependencyData) {
            ownerModule = ((DependencyData) ownerDataNode.getData()).getOwnerModule();
        }
        assert ownerModule != null;
        DataNode<? extends ExternalEntityData> depOwnerDataNode = null;
        if (mergedDependency instanceof ExternalProjectDependency) {
            class ProjectDependencyInfo {

                @NotNull
                ModuleData myModuleData;

                @Nullable
                ExternalSourceSet mySourceSet;

                Collection<File> dependencyArtifacts;

                public ProjectDependencyInfo(@NotNull ModuleData moduleData, @Nullable ExternalSourceSet sourceSet, Collection<File> dependencyArtifacts) {
                    this.myModuleData = moduleData;
                    this.mySourceSet = sourceSet;
                    this.dependencyArtifacts = dependencyArtifacts;
                }
            }
            final ExternalProjectDependency projectDependency = (ExternalProjectDependency) mergedDependency;
            Collection<ProjectDependencyInfo> projectDependencyInfos = ContainerUtil.newArrayList();
            String selectionReason = projectDependency.getSelectionReason();
            if ("composite build substitution".equals(selectionReason) && resolverCtx.getSettings() != null) {
                GradleExecutionWorkspace executionWorkspace = resolverCtx.getSettings().getExecutionWorkspace();
                ModuleData moduleData = executionWorkspace.findModuleDataByArtifacts(projectDependency.getProjectDependencyArtifacts());
                if (moduleData != null) {
                    projectDependencyInfos.add(new ProjectDependencyInfo(moduleData, null, projectDependency.getProjectDependencyArtifacts()));
                }
            } else {
                String moduleId = getModuleId(projectDependency);
                Pair<DataNode<GradleSourceSetData>, ExternalSourceSet> projectPair = sourceSetMap.get(moduleId);
                if (projectPair == null) {
                    for (File file : projectDependency.getProjectDependencyArtifacts()) {
                        moduleId = artifactsMap.get(ExternalSystemApiUtil.toCanonicalPath(file.getAbsolutePath()));
                        if (moduleId == null)
                            continue;
                        projectPair = sourceSetMap.get(moduleId);
                        if (projectPair == null)
                            continue;
                        projectDependencyInfos.add(new ProjectDependencyInfo(projectPair.first.getData(), projectPair.second, Collections.singleton(file)));
                    }
                } else {
                    projectDependencyInfos.add(new ProjectDependencyInfo(projectPair.first.getData(), projectPair.second, projectDependency.getProjectDependencyArtifacts()));
                }
            }
            if (projectDependencyInfos.isEmpty()) {
                final LibraryLevel level = LibraryLevel.MODULE;
                final LibraryData library = new LibraryData(GradleConstants.SYSTEM_ID, "");
                LibraryDependencyData libraryDependencyData = new LibraryDependencyData(ownerModule, library, level);
                libraryDependencyData.setScope(dependencyScope);
                libraryDependencyData.setOrder(mergedDependency.getClasspathOrder());
                libraryDependencyData.setExported(mergedDependency.getExported());
                if (!projectDependency.getProjectDependencyArtifacts().isEmpty()) {
                    for (File artifact : projectDependency.getProjectDependencyArtifacts()) {
                        library.addPath(LibraryPathType.BINARY, artifact.getPath());
                    }
                    depOwnerDataNode = ownerDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, libraryDependencyData);
                } else {
                    depOwnerDataNode = ownerDataNode;
                }
            } else {
                for (ProjectDependencyInfo projectDependencyInfo : projectDependencyInfos) {
                    ModuleDependencyData moduleDependencyData = new ModuleDependencyData(ownerModule, projectDependencyInfo.myModuleData);
                    moduleDependencyData.setScope(dependencyScope);
                    if (projectDependencyInfo.mySourceSet != null && "test".equals(projectDependencyInfo.mySourceSet.getName())) {
                        moduleDependencyData.setProductionOnTestDependency(true);
                    }
                    moduleDependencyData.setOrder(mergedDependency.getClasspathOrder());
                    moduleDependencyData.setExported(mergedDependency.getExported());
                    moduleDependencyData.setModuleDependencyArtifacts(ContainerUtil.map(projectDependencyInfo.dependencyArtifacts, File::getPath));
                    depOwnerDataNode = ownerDataNode.createChild(ProjectKeys.MODULE_DEPENDENCY, moduleDependencyData);
                }
                // since we can not determine from what project dependency artifact it was originated
                if (projectDependencyInfos.size() > 1) {
                    depOwnerDataNode = ownerDataNode;
                }
            }
        } else if (mergedDependency instanceof ExternalLibraryDependency) {
            String libraryName = mergedDependency.getId().getPresentableName();
            final LibraryLevel level = StringUtil.isNotEmpty(libraryName) ? LibraryLevel.PROJECT : LibraryLevel.MODULE;
            final LibraryData library = new LibraryData(GradleConstants.SYSTEM_ID, libraryName);
            LibraryDependencyData libraryDependencyData = new LibraryDependencyData(ownerModule, library, level);
            libraryDependencyData.setScope(dependencyScope);
            libraryDependencyData.setOrder(mergedDependency.getClasspathOrder());
            libraryDependencyData.setExported(mergedDependency.getExported());
            library.addPath(LibraryPathType.BINARY, ((ExternalLibraryDependency) mergedDependency).getFile().getAbsolutePath());
            File sourcePath = ((ExternalLibraryDependency) mergedDependency).getSource();
            if (sourcePath != null) {
                library.addPath(LibraryPathType.SOURCE, sourcePath.getAbsolutePath());
            }
            File javaDocPath = ((ExternalLibraryDependency) mergedDependency).getJavadoc();
            if (javaDocPath != null) {
                library.addPath(LibraryPathType.DOC, javaDocPath.getAbsolutePath());
            }
            depOwnerDataNode = ownerDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, libraryDependencyData);
            if (StringUtil.isNotEmpty(libraryName)) {
                linkProjectLibrary(ideProject, library);
            }
        } else if (mergedDependency instanceof ExternalMultiLibraryDependency) {
            final LibraryLevel level = LibraryLevel.MODULE;
            String libraryName = mergedDependency.getId().getPresentableName();
            final LibraryData library = new LibraryData(GradleConstants.SYSTEM_ID, libraryName);
            LibraryDependencyData libraryDependencyData = new LibraryDependencyData(ownerModule, library, level);
            libraryDependencyData.setScope(dependencyScope);
            libraryDependencyData.setOrder(mergedDependency.getClasspathOrder());
            libraryDependencyData.setExported(mergedDependency.getExported());
            for (File file : ((ExternalMultiLibraryDependency) mergedDependency).getFiles()) {
                library.addPath(LibraryPathType.BINARY, file.getAbsolutePath());
            }
            for (File file : ((ExternalMultiLibraryDependency) mergedDependency).getSources()) {
                library.addPath(LibraryPathType.SOURCE, file.getAbsolutePath());
            }
            for (File file : ((ExternalMultiLibraryDependency) mergedDependency).getJavadoc()) {
                library.addPath(LibraryPathType.DOC, file.getAbsolutePath());
            }
            depOwnerDataNode = ownerDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, libraryDependencyData);
        } else if (mergedDependency instanceof FileCollectionDependency) {
            final LibraryLevel level = LibraryLevel.MODULE;
            String libraryName = "";
            final LibraryData library = new LibraryData(GradleConstants.SYSTEM_ID, libraryName);
            LibraryDependencyData libraryDependencyData = new LibraryDependencyData(ownerModule, library, level);
            libraryDependencyData.setScope(dependencyScope);
            libraryDependencyData.setOrder(mergedDependency.getClasspathOrder());
            libraryDependencyData.setExported(mergedDependency.getExported());
            for (File file : ((FileCollectionDependency) mergedDependency).getFiles()) {
                library.addPath(LibraryPathType.BINARY, file.getAbsolutePath());
            }
            ownerDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, libraryDependencyData);
        } else if (mergedDependency instanceof UnresolvedExternalDependency) {
            final LibraryLevel level = LibraryLevel.PROJECT;
            String libraryName = mergedDependency.getId().getPresentableName();
            final LibraryData library = new LibraryData(GradleConstants.SYSTEM_ID, libraryName, true);
            LibraryDependencyData libraryDependencyData = new LibraryDependencyData(ownerModule, library, level);
            libraryDependencyData.setScope(dependencyScope);
            final String failureMessage = ((UnresolvedExternalDependency) mergedDependency).getFailureMessage();
            if (failureMessage != null) {
                library.addPath(LibraryPathType.BINARY, failureMessage);
            }
            ownerDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, libraryDependencyData);
            linkProjectLibrary(ideProject, library);
        }
        if (depOwnerDataNode != null) {
            doBuildDependencies(resolverCtx, sourceSetMap, artifactsMap, mergedDependencyMap, depOwnerDataNode, dependency.getDependencies(), ideProject);
        }
    }
}
Also used : DependencyScope(com.intellij.openapi.roots.DependencyScope) GradleExecutionWorkspace(org.jetbrains.plugins.gradle.settings.GradleExecutionWorkspace) NotNull(org.jetbrains.annotations.NotNull) DataNode(com.intellij.openapi.externalSystem.model.DataNode) ExternalDependencyId(org.jetbrains.plugins.gradle.ExternalDependencyId) DefaultExternalDependencyId(org.jetbrains.plugins.gradle.DefaultExternalDependencyId) Collection(java.util.Collection) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with DependencyScope

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

the class GradleProjectResolverUtil method buildDependencies.

public static void buildDependencies(@NotNull ProjectResolverContext resolverCtx, @NotNull Map<String, Pair<DataNode<GradleSourceSetData>, ExternalSourceSet>> sourceSetMap, @NotNull final Map<String, String> artifactsMap, @NotNull DataNode<? extends ExternalEntityData> ownerDataNode, @NotNull Collection<ExternalDependency> dependencies, @Nullable DataNode<ProjectData> ideProject) throws IllegalStateException {
    Map<ExternalDependencyId, ExternalDependency> dependencyMap = ContainerUtil.newHashMap();
    Queue<ExternalDependency> queue = ContainerUtil.newLinkedList(dependencies);
    while (!queue.isEmpty()) {
        final ExternalDependency dependency = queue.remove();
        ExternalDependency seenDependency = dependencyMap.get(dependency.getId());
        if (seenDependency != null) {
            if (dependency instanceof ExternalLibraryDependency) {
                if (seenDependency instanceof ExternalLibraryDependency && !FileUtil.filesEqual(((ExternalLibraryDependency) seenDependency).getFile(), ((ExternalLibraryDependency) dependency).getFile())) {
                    DefaultExternalMultiLibraryDependency mergedDependency = new DefaultExternalMultiLibraryDependency();
                    mergedDependency.setName(dependency.getId().getName());
                    mergedDependency.setGroup(dependency.getId().getGroup());
                    mergedDependency.setVersion(dependency.getId().getVersion());
                    mergedDependency.setPackaging(dependency.getId().getPackaging());
                    mergedDependency.setClassifier(dependency.getId().getClassifier());
                    mergedDependency.setScope(dependency.getScope());
                    mergedDependency.setClasspathOrder(dependency.getClasspathOrder());
                    mergedDependency.getDependencies().addAll(dependency.getDependencies());
                    mergedDependency.getFiles().addAll(ContainerUtil.packNullables(((ExternalLibraryDependency) seenDependency).getFile(), ((ExternalLibraryDependency) dependency).getFile()));
                    mergedDependency.getSources().addAll((ContainerUtil.packNullables(((ExternalLibraryDependency) seenDependency).getSource(), ((ExternalLibraryDependency) dependency).getSource())));
                    mergedDependency.getJavadoc().addAll((ContainerUtil.packNullables(((ExternalLibraryDependency) seenDependency).getJavadoc(), ((ExternalLibraryDependency) dependency).getJavadoc())));
                    dependencyMap.put(dependency.getId(), mergedDependency);
                    continue;
                } else if (seenDependency instanceof DefaultExternalMultiLibraryDependency) {
                    DefaultExternalMultiLibraryDependency mergedDependency = (DefaultExternalMultiLibraryDependency) seenDependency;
                    mergedDependency.getFiles().addAll(ContainerUtil.packNullables(((ExternalLibraryDependency) dependency).getFile()));
                    mergedDependency.getSources().addAll(ContainerUtil.packNullables(((ExternalLibraryDependency) dependency).getSource()));
                    mergedDependency.getJavadoc().addAll(ContainerUtil.packNullables(((ExternalLibraryDependency) dependency).getJavadoc()));
                    continue;
                }
            }
            DependencyScope prevScope = seenDependency.getScope() == null ? DependencyScope.COMPILE : DependencyScope.valueOf(seenDependency.getScope());
            DependencyScope currentScope = dependency.getScope() == null ? DependencyScope.COMPILE : DependencyScope.valueOf(dependency.getScope());
            if (prevScope.isForProductionCompile())
                continue;
            if (prevScope.isForProductionRuntime() && currentScope.isForProductionRuntime())
                continue;
        }
        dependencyMap.put(new DefaultExternalDependencyId(dependency.getId()), dependency);
        queue.addAll(dependency.getDependencies());
    }
    doBuildDependencies(resolverCtx, sourceSetMap, artifactsMap, dependencyMap, ownerDataNode, dependencies, ideProject);
}
Also used : DependencyScope(com.intellij.openapi.roots.DependencyScope) DefaultExternalDependencyId(org.jetbrains.plugins.gradle.DefaultExternalDependencyId) ExternalDependencyId(org.jetbrains.plugins.gradle.ExternalDependencyId) DefaultExternalDependencyId(org.jetbrains.plugins.gradle.DefaultExternalDependencyId)

Aggregations

DependencyScope (com.intellij.openapi.roots.DependencyScope)13 NotNull (org.jetbrains.annotations.NotNull)4 File (java.io.File)3 ModuleOrderEntry (com.intellij.openapi.roots.ModuleOrderEntry)2 Nullable (org.jetbrains.annotations.Nullable)2 DefaultExternalDependencyId (org.jetbrains.plugins.gradle.DefaultExternalDependencyId)2 ExternalDependencyId (org.jetbrains.plugins.gradle.ExternalDependencyId)2 DependencySetupErrors (com.android.tools.idea.gradle.project.sync.setup.module.common.DependencySetupErrors)1 FilePaths.findParentContentEntry (com.android.tools.idea.gradle.util.FilePaths.findParentContentEntry)1 Result (com.intellij.openapi.application.Result)1 WriteAction (com.intellij.openapi.application.WriteAction)1 DataNode (com.intellij.openapi.externalSystem.model.DataNode)1 ExternalSystemTaskId (com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId)1 NotificationData (com.intellij.openapi.externalSystem.service.notification.NotificationData)1 Module (com.intellij.openapi.module.Module)1 IndexNotReadyException (com.intellij.openapi.project.IndexNotReadyException)1 Project (com.intellij.openapi.project.Project)1 ContentEntry (com.intellij.openapi.roots.ContentEntry)1 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)1 OrderEntry (com.intellij.openapi.roots.OrderEntry)1