Search in sources :

Example 1 with ExternalDependencyId

use of org.jetbrains.plugins.gradle.ExternalDependencyId 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 2 with ExternalDependencyId

use of org.jetbrains.plugins.gradle.ExternalDependencyId 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)2 DefaultExternalDependencyId (org.jetbrains.plugins.gradle.DefaultExternalDependencyId)2 ExternalDependencyId (org.jetbrains.plugins.gradle.ExternalDependencyId)2 DataNode (com.intellij.openapi.externalSystem.model.DataNode)1 File (java.io.File)1 Collection (java.util.Collection)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1 GradleExecutionWorkspace (org.jetbrains.plugins.gradle.settings.GradleExecutionWorkspace)1