Search in sources :

Example 1 with GradleExecutionWorkspace

use of org.jetbrains.plugins.gradle.settings.GradleExecutionWorkspace 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 GradleExecutionWorkspace

use of org.jetbrains.plugins.gradle.settings.GradleExecutionWorkspace in project intellij-community by JetBrains.

the class BaseGradleProjectResolverExtension method buildDependency.

@NotNull
private static ModuleDependencyData buildDependency(@NotNull ProjectResolverContext resolverContext, @NotNull DataNode<ModuleData> ownerModule, @NotNull IdeaModuleDependency dependency, @NotNull DataNode<ProjectData> ideProject) throws IllegalStateException {
    IdeaModule module = dependency.getDependencyModule();
    if (module == null) {
        if (resolverContext.getSettings() != null) {
            String moduleName = dependency.getTargetModuleName();
            GradleExecutionWorkspace executionWorkspace = resolverContext.getSettings().getExecutionWorkspace();
            ModuleData moduleData = executionWorkspace.findModuleDataByName(moduleName);
            if (moduleData != null) {
                return new ModuleDependencyData(ownerModule.getData(), moduleData);
            } else {
                for (IdeaProject project : resolverContext.getModels().getIncludedBuilds()) {
                    moduleData = executionWorkspace.findModuleDataByName(project.getName() + ':' + moduleName);
                    if (moduleData != null) {
                        return new ModuleDependencyData(ownerModule.getData(), moduleData);
                    }
                }
            }
            return new ModuleDependencyData(ownerModule.getData(), new ModuleData("", GradleConstants.SYSTEM_ID, StdModuleTypes.JAVA.getId(), moduleName, "", ""));
        }
        throw new IllegalStateException(String.format("Can't parse gradle module dependency '%s'. Reason: referenced module is null", dependency));
    }
    String moduleName = module.getName();
    if (moduleName == null) {
        throw new IllegalStateException(String.format("Can't parse gradle module dependency '%s'. Reason: referenced module name is undefined (module: '%s') ", dependency, module));
    }
    Set<String> registeredModuleNames = ContainerUtilRt.newHashSet();
    Collection<DataNode<ModuleData>> modulesDataNode = ExternalSystemApiUtil.getChildren(ideProject, ProjectKeys.MODULE);
    for (DataNode<ModuleData> moduleDataNode : modulesDataNode) {
        String name = moduleDataNode.getData().getExternalName();
        registeredModuleNames.add(name);
        if (name.equals(moduleName)) {
            return new ModuleDependencyData(ownerModule.getData(), moduleDataNode.getData());
        }
    }
    throw new IllegalStateException(String.format("Can't parse gradle module dependency '%s'. Reason: no module with such name (%s) is found. Registered modules: %s", dependency, moduleName, registeredModuleNames));
}
Also used : DataNode(com.intellij.openapi.externalSystem.model.DataNode) GradleExecutionWorkspace(org.jetbrains.plugins.gradle.settings.GradleExecutionWorkspace) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DataNode (com.intellij.openapi.externalSystem.model.DataNode)2 NotNull (org.jetbrains.annotations.NotNull)2 GradleExecutionWorkspace (org.jetbrains.plugins.gradle.settings.GradleExecutionWorkspace)2 DependencyScope (com.intellij.openapi.roots.DependencyScope)1 File (java.io.File)1 Collection (java.util.Collection)1 Nullable (org.jetbrains.annotations.Nullable)1 DefaultExternalDependencyId (org.jetbrains.plugins.gradle.DefaultExternalDependencyId)1 ExternalDependencyId (org.jetbrains.plugins.gradle.ExternalDependencyId)1