Search in sources :

Example 1 with GradleSourceSetData

use of org.jetbrains.plugins.gradle.model.data.GradleSourceSetData in project intellij-community by JetBrains.

the class BaseGradleProjectResolverExtension method populateModuleDependencies.

@Override
public void populateModuleDependencies(@NotNull IdeaModule gradleModule, @NotNull DataNode<ModuleData> ideModule, @NotNull final DataNode<ProjectData> ideProject) {
    ExternalProject externalProject = resolverCtx.getExtraProject(gradleModule, ExternalProject.class);
    if (externalProject != null) {
        final Map<String, Pair<DataNode<GradleSourceSetData>, ExternalSourceSet>> sourceSetMap = ideProject.getUserData(GradleProjectResolver.RESOLVED_SOURCE_SETS);
        final Map<String, String> artifactsMap = ideProject.getUserData(CONFIGURATION_ARTIFACTS);
        assert artifactsMap != null;
        if (resolverCtx.isResolveModulePerSourceSet()) {
            assert sourceSetMap != null;
            processSourceSets(resolverCtx, gradleModule, externalProject, ideModule, new SourceSetsProcessor() {

                @Override
                public void process(@NotNull DataNode<? extends ModuleData> dataNode, @NotNull ExternalSourceSet sourceSet) {
                    buildDependencies(resolverCtx, sourceSetMap, artifactsMap, dataNode, sourceSet.getDependencies(), ideProject);
                }
            });
            return;
        }
    }
    final List<? extends IdeaDependency> dependencies = gradleModule.getDependencies().getAll();
    if (dependencies == null)
        return;
    List<String> orphanModules = ContainerUtil.newArrayList();
    for (IdeaDependency dependency : dependencies) {
        if (dependency == null) {
            continue;
        }
        DependencyScope scope = parseScope(dependency.getScope());
        if (dependency instanceof IdeaModuleDependency) {
            ModuleDependencyData d = buildDependency(resolverCtx, ideModule, (IdeaModuleDependency) dependency, ideProject);
            d.setExported(dependency.getExported());
            if (scope != null) {
                d.setScope(scope);
            }
            ideModule.createChild(ProjectKeys.MODULE_DEPENDENCY, d);
            ModuleData targetModule = d.getTarget();
            if (targetModule.getId().isEmpty() && targetModule.getLinkedExternalProjectPath().isEmpty()) {
                orphanModules.add(targetModule.getExternalName());
            }
        } else if (dependency instanceof IdeaSingleEntryLibraryDependency) {
            LibraryDependencyData d = buildDependency(gradleModule, ideModule, (IdeaSingleEntryLibraryDependency) dependency, ideProject);
            d.setExported(dependency.getExported());
            if (scope != null) {
                d.setScope(scope);
            }
            ideModule.createChild(ProjectKeys.LIBRARY_DEPENDENCY, d);
        }
    }
    if (!orphanModules.isEmpty()) {
        ExternalSystemTaskId taskId = resolverCtx.getExternalSystemTaskId();
        Project project = taskId.findProject();
        if (project != null) {
            String msg = "Can't find the following module" + (orphanModules.size() > 1 ? "s" : "") + ": " + StringUtil.join(orphanModules, ", ") + "\nIt can be caused by composite build configuration inside your *.gradle scripts with Gradle version older than 3.3." + "\nTry Gradle 3.3 or better or enable 'Create separate module per source set' option";
            NotificationData notification = new NotificationData("Gradle project structure problems", msg, NotificationCategory.WARNING, NotificationSource.PROJECT_SYNC);
            ExternalSystemNotificationManager.getInstance(project).showNotification(taskId.getProjectSystemId(), notification);
        }
    }
}
Also used : ExternalSystemTaskId(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId) DependencyScope(com.intellij.openapi.roots.DependencyScope) GradleSourceSetData(org.jetbrains.plugins.gradle.model.data.GradleSourceSetData) Project(com.intellij.openapi.project.Project) Pair(com.intellij.openapi.util.Pair) NotificationData(com.intellij.openapi.externalSystem.service.notification.NotificationData)

Example 2 with GradleSourceSetData

use of org.jetbrains.plugins.gradle.model.data.GradleSourceSetData in project intellij-community by JetBrains.

the class BaseGradleProjectResolverExtension method createModule.

@NotNull
@Override
public DataNode<ModuleData> createModule(@NotNull IdeaModule gradleModule, @NotNull DataNode<ProjectData> projectDataNode) {
    DataNode<ModuleData> mainModuleNode = createMainModule(resolverCtx, gradleModule, projectDataNode);
    final ModuleData mainModuleData = mainModuleNode.getData();
    final String mainModuleConfigPath = mainModuleData.getLinkedExternalProjectPath();
    final String mainModuleFileDirectoryPath = mainModuleData.getModuleFileDirectoryPath();
    ExternalProject externalProject = resolverCtx.getExtraProject(gradleModule, ExternalProject.class);
    if (resolverCtx.isResolveModulePerSourceSet() && externalProject != null) {
        String[] moduleGroup = null;
        if (!ModuleGrouperKt.isQualifiedModuleNamesEnabled()) {
            String gradlePath = gradleModule.getGradleProject().getPath();
            final boolean isRootModule = StringUtil.isEmpty(gradlePath) || ":".equals(gradlePath);
            moduleGroup = isRootModule ? new String[] { mainModuleData.getInternalName() } : ArrayUtil.remove(gradlePath.split(":"), 0);
            mainModuleData.setIdeModuleGroup(isRootModule ? null : moduleGroup);
        }
        for (ExternalSourceSet sourceSet : externalProject.getSourceSets().values()) {
            final String moduleId = getModuleId(resolverCtx, gradleModule, sourceSet);
            final String moduleExternalName = gradleModule.getName() + ":" + sourceSet.getName();
            final String moduleInternalName = getInternalModuleName(gradleModule, externalProject, sourceSet.getName());
            GradleSourceSetData sourceSetData = new GradleSourceSetData(moduleId, moduleExternalName, moduleInternalName, mainModuleFileDirectoryPath, mainModuleConfigPath);
            sourceSetData.setGroup(externalProject.getGroup());
            sourceSetData.setVersion(externalProject.getVersion());
            sourceSetData.setIdeModuleGroup(moduleGroup);
            sourceSetData.setSourceCompatibility(sourceSet.getSourceCompatibility());
            sourceSetData.setTargetCompatibility(sourceSet.getTargetCompatibility());
            final Set<File> artifacts = ContainerUtil.newTroveSet(FileUtil.FILE_HASHING_STRATEGY);
            if ("main".equals(sourceSet.getName())) {
                final Set<File> defaultArtifacts = externalProject.getArtifactsByConfiguration().get("default");
                if (defaultArtifacts != null) {
                    artifacts.addAll(defaultArtifacts);
                }
                if (externalProject.getArtifactsByConfiguration().get("archives") != null) {
                    final Set<File> archivesArtifacts = ContainerUtil.newHashSet(externalProject.getArtifactsByConfiguration().get("archives"));
                    final Set<File> testsArtifacts = externalProject.getArtifactsByConfiguration().get("tests");
                    if (testsArtifacts != null) {
                        archivesArtifacts.removeAll(testsArtifacts);
                    }
                    artifacts.addAll(archivesArtifacts);
                }
            } else {
                if ("test".equals(sourceSet.getName())) {
                    sourceSetData.setProductionModuleId(getInternalModuleName(gradleModule, externalProject, "main"));
                    final Set<File> testsArtifacts = externalProject.getArtifactsByConfiguration().get("tests");
                    if (testsArtifacts != null) {
                        artifacts.addAll(testsArtifacts);
                    }
                }
            }
            artifacts.addAll(sourceSet.getArtifacts());
            sourceSetData.setArtifacts(ContainerUtil.newArrayList(artifacts));
            DataNode<GradleSourceSetData> sourceSetDataNode = mainModuleNode.createChild(GradleSourceSetData.KEY, sourceSetData);
            final Map<String, Pair<DataNode<GradleSourceSetData>, ExternalSourceSet>> sourceSetMap = projectDataNode.getUserData(GradleProjectResolver.RESOLVED_SOURCE_SETS);
            assert sourceSetMap != null;
            sourceSetMap.put(moduleId, Pair.create(sourceSetDataNode, sourceSet));
        }
    } else {
        try {
            IdeaJavaLanguageSettings languageSettings = gradleModule.getJavaLanguageSettings();
            if (languageSettings != null) {
                if (languageSettings.getLanguageLevel() != null) {
                    mainModuleData.setSourceCompatibility(languageSettings.getLanguageLevel().toString());
                }
                if (languageSettings.getTargetBytecodeVersion() != null) {
                    mainModuleData.setTargetCompatibility(languageSettings.getTargetBytecodeVersion().toString());
                }
            }
        } catch (UnsupportedMethodException ignore) {
        // org.gradle.tooling.model.idea.IdeaModule.getJavaLanguageSettings method supported since Gradle 2.11
        }
    }
    final ProjectData projectData = projectDataNode.getData();
    if (StringUtil.equals(mainModuleData.getLinkedExternalProjectPath(), projectData.getLinkedExternalProjectPath())) {
        projectData.setGroup(mainModuleData.getGroup());
        projectData.setVersion(mainModuleData.getVersion());
    }
    return mainModuleNode;
}
Also used : GradleSourceSetData(org.jetbrains.plugins.gradle.model.data.GradleSourceSetData) UnsupportedMethodException(org.gradle.tooling.model.UnsupportedMethodException) File(java.io.File) JavaProjectData(com.intellij.externalSystem.JavaProjectData) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GradleSourceSetData

use of org.jetbrains.plugins.gradle.model.data.GradleSourceSetData in project intellij-community by JetBrains.

the class GradleManager method configureExecutionWorkspace.

/**
   * Add composite participants
   */
private static void configureExecutionWorkspace(@Nullable GradleProjectSettings compositeRootSettings, GradleSettings settings, GradleExecutionSettings result, Project project, String projectPath) {
    if (compositeRootSettings == null || compositeRootSettings.getCompositeBuild() == null)
        return;
    GradleProjectSettings.CompositeBuild compositeBuild = compositeRootSettings.getCompositeBuild();
    if (compositeBuild.getCompositeDefinitionSource() == CompositeDefinitionSource.SCRIPT) {
        if (pathsEqual(compositeRootSettings.getExternalProjectPath(), projectPath))
            return;
        for (BuildParticipant buildParticipant : compositeBuild.getCompositeParticipants()) {
            if (pathsEqual(buildParticipant.getRootPath(), projectPath))
                continue;
            if (buildParticipant.getProjects().stream().anyMatch(path -> pathsEqual(path, projectPath))) {
                continue;
            }
            result.getExecutionWorkspace().addBuildParticipant(new GradleBuildParticipant(buildParticipant.getRootPath()));
        }
        return;
    }
    for (GradleProjectSettings projectSettings : settings.getLinkedProjectsSettings()) {
        if (projectSettings == compositeRootSettings)
            continue;
        if (compositeBuild.getCompositeParticipants().stream().noneMatch(participant -> pathsEqual(participant.getRootPath(), projectSettings.getExternalProjectPath()))) {
            continue;
        }
        GradleBuildParticipant buildParticipant = new GradleBuildParticipant(projectSettings.getExternalProjectPath());
        ExternalProjectInfo projectData = ProjectDataManager.getInstance().getExternalProjectData(project, GradleConstants.SYSTEM_ID, projectSettings.getExternalProjectPath());
        if (projectData == null || projectData.getExternalProjectStructure() == null)
            continue;
        Collection<DataNode<ModuleData>> moduleNodes = ExternalSystemApiUtil.findAll(projectData.getExternalProjectStructure(), ProjectKeys.MODULE);
        for (DataNode<ModuleData> moduleNode : moduleNodes) {
            ModuleData moduleData = moduleNode.getData();
            if (moduleData.getArtifacts().isEmpty()) {
                Collection<DataNode<GradleSourceSetData>> sourceSetNodes = ExternalSystemApiUtil.findAll(moduleNode, GradleSourceSetData.KEY);
                for (DataNode<GradleSourceSetData> sourceSetNode : sourceSetNodes) {
                    buildParticipant.addModule(sourceSetNode.getData());
                }
            } else {
                buildParticipant.addModule(moduleData);
            }
        }
        result.getExecutionWorkspace().addBuildParticipant(buildParticipant);
    }
}
Also used : ExternalProjectInfo(com.intellij.openapi.externalSystem.model.ExternalProjectInfo) DataNode(com.intellij.openapi.externalSystem.model.DataNode) GradleSourceSetData(org.jetbrains.plugins.gradle.model.data.GradleSourceSetData) BuildParticipant(org.jetbrains.plugins.gradle.model.data.BuildParticipant) ModuleData(com.intellij.openapi.externalSystem.model.project.ModuleData)

Example 4 with GradleSourceSetData

use of org.jetbrains.plugins.gradle.model.data.GradleSourceSetData in project intellij-community by JetBrains.

the class GradleProjectResolver method mergeSourceSetContentRoots.

private static void mergeSourceSetContentRoots(@NotNull Map<String, Pair<DataNode<ModuleData>, IdeaModule>> moduleMap, @NotNull ProjectResolverContext resolverCtx) {
    final Factory<Counter> counterFactory = () -> new Counter();
    final Map<String, Counter> weightMap = ContainerUtil.newHashMap();
    for (final Pair<DataNode<ModuleData>, IdeaModule> pair : moduleMap.values()) {
        final DataNode<ModuleData> moduleNode = pair.first;
        for (DataNode<ContentRootData> contentRootNode : ExternalSystemApiUtil.findAll(moduleNode, ProjectKeys.CONTENT_ROOT)) {
            File file = new File(contentRootNode.getData().getRootPath());
            while (file != null) {
                ContainerUtil.getOrCreate(weightMap, file.getPath(), counterFactory).increment();
                file = file.getParentFile();
            }
        }
        for (DataNode<GradleSourceSetData> sourceSetNode : ExternalSystemApiUtil.findAll(moduleNode, GradleSourceSetData.KEY)) {
            final Set<String> set = ContainerUtil.newHashSet();
            for (DataNode<ContentRootData> contentRootNode : ExternalSystemApiUtil.findAll(sourceSetNode, ProjectKeys.CONTENT_ROOT)) {
                File file = new File(contentRootNode.getData().getRootPath());
                while (file != null) {
                    set.add(file.getPath());
                    file = file.getParentFile();
                }
            }
            for (String path : set) {
                ContainerUtil.getOrCreate(weightMap, path, counterFactory).increment();
            }
        }
    }
    for (final Pair<DataNode<ModuleData>, IdeaModule> pair : moduleMap.values()) {
        final DataNode<ModuleData> moduleNode = pair.first;
        final ExternalProject externalProject = resolverCtx.getExtraProject(pair.second, ExternalProject.class);
        if (externalProject == null)
            continue;
        if (resolverCtx.isResolveModulePerSourceSet()) {
            for (DataNode<GradleSourceSetData> sourceSetNode : ExternalSystemApiUtil.findAll(moduleNode, GradleSourceSetData.KEY)) {
                mergeModuleContentRoots(weightMap, externalProject, sourceSetNode);
            }
        } else {
            mergeModuleContentRoots(weightMap, externalProject, moduleNode);
        }
    }
}
Also used : GradleSourceSetData(org.jetbrains.plugins.gradle.model.data.GradleSourceSetData) IdeaModule(org.gradle.tooling.model.idea.IdeaModule) DataNode(com.intellij.openapi.externalSystem.model.DataNode) File(java.io.File)

Example 5 with GradleSourceSetData

use of org.jetbrains.plugins.gradle.model.data.GradleSourceSetData in project intellij-community by JetBrains.

the class GradleProjectResolver method mergeLibraryAndModuleDependencyData.

private static void mergeLibraryAndModuleDependencyData(DataNode<ProjectData> projectDataNode, @Nullable File gradleHomeDir, @Nullable GradleVersion gradleVersion) {
    final Map<String, Pair<DataNode<GradleSourceSetData>, ExternalSourceSet>> sourceSetMap = projectDataNode.getUserData(RESOLVED_SOURCE_SETS);
    assert sourceSetMap != null;
    final Map<String, Pair<String, ExternalSystemSourceType>> moduleOutputsMap = projectDataNode.getUserData(MODULES_OUTPUTS);
    assert moduleOutputsMap != null;
    final Map<String, String> artifactsMap = projectDataNode.getUserData(CONFIGURATION_ARTIFACTS);
    assert artifactsMap != null;
    final Collection<DataNode<LibraryDependencyData>> libraryDependencies = ExternalSystemApiUtil.findAllRecursively(projectDataNode, ProjectKeys.LIBRARY_DEPENDENCY);
    for (DataNode<LibraryDependencyData> libraryDependencyDataNode : libraryDependencies) {
        final DataNode<?> libraryNodeParent = libraryDependencyDataNode.getParent();
        if (libraryNodeParent == null)
            continue;
        final LibraryDependencyData libraryDependencyData = libraryDependencyDataNode.getData();
        final LibraryData libraryData = libraryDependencyData.getTarget();
        final Set<String> libraryPaths = libraryData.getPaths(LibraryPathType.BINARY);
        if (libraryPaths.isEmpty())
            continue;
        if (StringUtil.isNotEmpty(libraryData.getExternalName()))
            continue;
        boolean projectDependencyCandidate = libraryPaths.size() == 1 && !libraryDependencyDataNode.getChildren().isEmpty();
        final LinkedList<String> unprocessedPaths = ContainerUtil.newLinkedList(libraryPaths);
        while (!unprocessedPaths.isEmpty()) {
            final String path = unprocessedPaths.remove();
            Set<String> targetModuleOutputPaths = null;
            final String moduleId;
            final Pair<String, ExternalSystemSourceType> sourceTypePair = moduleOutputsMap.get(path);
            if (sourceTypePair == null) {
                moduleId = artifactsMap.get(path);
                if (moduleId != null) {
                    targetModuleOutputPaths = ContainerUtil.set(path);
                }
            } else {
                moduleId = sourceTypePair.first;
            }
            if (moduleId == null)
                continue;
            final Pair<DataNode<GradleSourceSetData>, ExternalSourceSet> pair = sourceSetMap.get(moduleId);
            if (pair == null) {
                continue;
            }
            final ModuleData moduleData = pair.first.getData();
            if (targetModuleOutputPaths == null) {
                final Set<String> compileSet = ContainerUtil.newHashSet();
                Map<ExternalSystemSourceType, String> gradleOutputs = pair.first.getUserData(GRADLE_OUTPUTS);
                if (gradleOutputs != null) {
                    ContainerUtil.addAllNotNull(compileSet, gradleOutputs.get(ExternalSystemSourceType.SOURCE), gradleOutputs.get(ExternalSystemSourceType.RESOURCE));
                }
                if (!compileSet.isEmpty() && ContainerUtil.intersects(libraryPaths, compileSet)) {
                    targetModuleOutputPaths = compileSet;
                } else {
                    final Set<String> testSet = ContainerUtil.newHashSet();
                    if (gradleOutputs != null) {
                        ContainerUtil.addAllNotNull(testSet, gradleOutputs.get(ExternalSystemSourceType.TEST), gradleOutputs.get(ExternalSystemSourceType.TEST_RESOURCE));
                    }
                    if (!testSet.isEmpty() && ContainerUtil.intersects(libraryPaths, testSet)) {
                        targetModuleOutputPaths = testSet;
                    }
                }
            }
            final ModuleData ownerModule = libraryDependencyData.getOwnerModule();
            final ModuleDependencyData moduleDependencyData = new ModuleDependencyData(ownerModule, moduleData);
            moduleDependencyData.setScope(libraryDependencyData.getScope());
            if ("test".equals(pair.second.getName())) {
                moduleDependencyData.setProductionOnTestDependency(true);
            }
            final DataNode<ModuleDependencyData> found = ExternalSystemApiUtil.find(libraryNodeParent, ProjectKeys.MODULE_DEPENDENCY, node -> {
                if (moduleDependencyData.getInternalName().equals(node.getData().getInternalName())) {
                    moduleDependencyData.setModuleDependencyArtifacts(node.getData().getModuleDependencyArtifacts());
                }
                final boolean result;
                if (moduleDependencyData.getScope() == DependencyScope.PROVIDED) {
                    moduleDependencyData.setScope(node.getData().getScope());
                    result = moduleDependencyData.equals(node.getData());
                    moduleDependencyData.setScope(DependencyScope.PROVIDED);
                } else {
                    result = moduleDependencyData.equals(node.getData());
                }
                return result;
            });
            if (targetModuleOutputPaths != null) {
                if (found == null) {
                    DataNode<ModuleDependencyData> moduleDependencyNode = libraryNodeParent.createChild(ProjectKeys.MODULE_DEPENDENCY, moduleDependencyData);
                    if (projectDependencyCandidate) {
                        for (DataNode<?> node : libraryDependencyDataNode.getChildren()) {
                            moduleDependencyNode.addChild(node);
                        }
                    }
                }
                libraryPaths.removeAll(targetModuleOutputPaths);
                unprocessedPaths.removeAll(targetModuleOutputPaths);
                if (libraryPaths.isEmpty()) {
                    libraryDependencyDataNode.clear(true);
                    break;
                }
                continue;
            } else {
                // do not add the path as library dependency if another module dependency is already contain the path as one of its output paths
                if (found != null) {
                    libraryPaths.remove(path);
                    if (libraryPaths.isEmpty()) {
                        libraryDependencyDataNode.clear(true);
                        break;
                    }
                    continue;
                }
            }
            final ExternalSourceDirectorySet directorySet = pair.second.getSources().get(sourceTypePair.second);
            if (directorySet != null) {
                for (File file : directorySet.getSrcDirs()) {
                    libraryData.addPath(LibraryPathType.SOURCE, file.getAbsolutePath());
                }
            }
        }
        if (libraryDependencyDataNode.getParent() != null) {
            if (libraryPaths.size() > 1) {
                List<String> toRemove = ContainerUtil.newSmartList();
                for (String path : libraryPaths) {
                    final File binaryPath = new File(path);
                    if (binaryPath.isFile()) {
                        final LibraryData extractedLibrary = new LibraryData(libraryDependencyData.getOwner(), "");
                        extractedLibrary.addPath(LibraryPathType.BINARY, path);
                        if (gradleHomeDir != null && gradleVersion != null) {
                            attachGradleSdkSources(binaryPath, extractedLibrary, gradleHomeDir, gradleVersion);
                        }
                        LibraryDependencyData extractedDependencyData = new LibraryDependencyData(libraryDependencyData.getOwnerModule(), extractedLibrary, LibraryLevel.MODULE);
                        libraryDependencyDataNode.getParent().createChild(ProjectKeys.LIBRARY_DEPENDENCY, extractedDependencyData);
                        toRemove.add(path);
                    }
                }
                libraryPaths.removeAll(toRemove);
                if (libraryPaths.isEmpty()) {
                    libraryDependencyDataNode.clear(true);
                }
            }
        }
    }
}
Also used : GradleSourceSetData(org.jetbrains.plugins.gradle.model.data.GradleSourceSetData) DataNode(com.intellij.openapi.externalSystem.model.DataNode) Pair(com.intellij.openapi.util.Pair) File(java.io.File)

Aggregations

GradleSourceSetData (org.jetbrains.plugins.gradle.model.data.GradleSourceSetData)8 DataNode (com.intellij.openapi.externalSystem.model.DataNode)5 Pair (com.intellij.openapi.util.Pair)5 File (java.io.File)5 JavaProjectData (com.intellij.externalSystem.JavaProjectData)2 IdeaModule (org.gradle.tooling.model.idea.IdeaModule)2 NotNull (org.jetbrains.annotations.NotNull)2 ParametersList (com.intellij.execution.configurations.ParametersList)1 ExternalProjectInfo (com.intellij.openapi.externalSystem.model.ExternalProjectInfo)1 ModuleData (com.intellij.openapi.externalSystem.model.project.ModuleData)1 ExternalSystemTaskId (com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId)1 NotificationData (com.intellij.openapi.externalSystem.service.notification.NotificationData)1 Project (com.intellij.openapi.project.Project)1 DependencyScope (com.intellij.openapi.roots.DependencyScope)1 UnsupportedMethodException (org.gradle.tooling.model.UnsupportedMethodException)1 BuildEnvironment (org.gradle.tooling.model.build.BuildEnvironment)1 BasicIdeaProject (org.gradle.tooling.model.idea.BasicIdeaProject)1 IdeaProject (org.gradle.tooling.model.idea.IdeaProject)1 GradleVersion (org.gradle.util.GradleVersion)1 BuildParticipant (org.jetbrains.plugins.gradle.model.data.BuildParticipant)1