Search in sources :

Example 6 with DataNode

use of com.intellij.openapi.externalSystem.model.DataNode in project intellij-community by JetBrains.

the class JavaEEGradleProjectResolverExtension method populateModuleExtraModels.

@Override
public void populateModuleExtraModels(@NotNull IdeaModule gradleModule, @NotNull final DataNode<ModuleData> ideModule) {
    final WebConfiguration webConfiguration = resolverCtx.getExtraProject(gradleModule, WebConfiguration.class);
    final NotNullLazyValue<DataNode<? extends ModuleData>> findTargetModuleNode = new NotNullLazyValue<DataNode<? extends ModuleData>>() {

        @NotNull
        @Override
        protected DataNode<? extends ModuleData> compute() {
            final String mainSourceSetModuleId = ideModule.getData().getId() + ":main";
            DataNode<? extends ModuleData> targetModuleNode = ExternalSystemApiUtil.find(ideModule, GradleSourceSetData.KEY, node -> mainSourceSetModuleId.equals(node.getData().getId()));
            if (targetModuleNode == null) {
                targetModuleNode = ideModule;
            }
            return targetModuleNode;
        }
    };
    if (webConfiguration != null) {
        final List<War> warModels = ContainerUtil.map(webConfiguration.getWarModels(), new Function<WebConfiguration.WarModel, War>() {

            @Override
            public War fun(WebConfiguration.WarModel model) {
                War war = new War(model.getWarName(), model.getWebAppDirName(), model.getWebAppDir());
                war.setWebXml(model.getWebXml());
                war.setWebResources(mapWebResources(model.getWebResources()));
                war.setClasspath(model.getClasspath());
                war.setManifestContent(model.getManifestContent());
                war.setArchivePath(model.getArchivePath());
                return war;
            }
        });
        findTargetModuleNode.getValue().createChild(WebConfigurationModelData.KEY, new WebConfigurationModelData(GradleConstants.SYSTEM_ID, warModels));
    }
    final EarConfiguration earConfiguration = resolverCtx.getExtraProject(gradleModule, EarConfiguration.class);
    if (earConfiguration != null) {
        final List<Ear> warModels = ContainerUtil.map(earConfiguration.getEarModels(), new Function<EarConfiguration.EarModel, Ear>() {

            @Override
            public Ear fun(EarConfiguration.EarModel model) {
                Ear ear = new Ear(model.getEarName(), model.getAppDirName(), model.getLibDirName());
                ear.setManifestContent(model.getManifestContent());
                ear.setDeploymentDescriptor(model.getDeploymentDescriptor());
                ear.setResources(mapEarResources(model.getResources()));
                ear.setArchivePath(model.getArchivePath());
                return ear;
            }
        });
        final Collection<DependencyData> deployDependencies = GradleProjectResolverUtil.getIdeDependencies(resolverCtx, findTargetModuleNode.getValue(), earConfiguration.getDeployDependencies());
        final Collection<DependencyData> earlibDependencies = GradleProjectResolverUtil.getIdeDependencies(resolverCtx, findTargetModuleNode.getValue(), earConfiguration.getEarlibDependencies());
        findTargetModuleNode.getValue().createChild(EarConfigurationModelData.KEY, new EarConfigurationModelData(GradleConstants.SYSTEM_ID, warModels, deployDependencies, earlibDependencies));
    }
    nextResolver.populateModuleExtraModels(gradleModule, ideModule);
}
Also used : DependencyData(com.intellij.openapi.externalSystem.model.project.DependencyData) NotNullLazyValue(com.intellij.openapi.util.NotNullLazyValue) EarConfiguration(org.jetbrains.plugins.gradle.model.ear.EarConfiguration) DataNode(com.intellij.openapi.externalSystem.model.DataNode) ModuleData(com.intellij.openapi.externalSystem.model.project.ModuleData) WebConfiguration(org.jetbrains.plugins.gradle.model.web.WebConfiguration)

Example 7 with DataNode

use of com.intellij.openapi.externalSystem.model.DataNode 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 8 with DataNode

use of com.intellij.openapi.externalSystem.model.DataNode in project intellij-community by JetBrains.

the class GradleViewContributor method addCustomSourceSetsNodes.

private static void addCustomSourceSetsNodes(@NotNull ExternalProjectsView externalProjectsView, @NotNull MultiMap<Key<?>, DataNode<?>> dataNodes, @NotNull List<ExternalSystemNode<?>> result) {
    final Collection<DataNode<?>> sourceSetsDataNodes = dataNodes.get(GradleSourceSetData.KEY);
    if (!sourceSetsDataNodes.isEmpty()) {
        final ExternalSystemNode sourceSetsNode = new SourceSetsNode(externalProjectsView);
        for (DataNode<?> dataNode : sourceSetsDataNodes) {
            //noinspection unchecked
            sourceSetsNode.add(new SourceSetNode(externalProjectsView, (DataNode<GradleSourceSetData>) dataNode));
        }
        result.add(sourceSetsNode);
    }
}
Also used : DataNode(com.intellij.openapi.externalSystem.model.DataNode) ExternalSystemNode(com.intellij.openapi.externalSystem.view.ExternalSystemNode)

Example 9 with DataNode

use of com.intellij.openapi.externalSystem.model.DataNode in project intellij-community by JetBrains.

the class GradleArgumentsCompletionProvider method getVariants.

protected List<LookupElement> getVariants(@NotNull final DataNode<ProjectData> projectDataNode, @NotNull final String modulePath) {
    final DataNode<ModuleData> moduleDataNode = findModuleDataNode(projectDataNode, modulePath);
    if (moduleDataNode == null) {
        return Collections.emptyList();
    }
    final ModuleData moduleData = moduleDataNode.getData();
    final boolean isRoot = projectDataNode.getData().getLinkedExternalProjectPath().equals(moduleData.getLinkedExternalProjectPath());
    final Collection<DataNode<TaskData>> tasks = ExternalSystemApiUtil.getChildren(moduleDataNode, ProjectKeys.TASK);
    List<LookupElement> elements = ContainerUtil.newArrayListWithCapacity(tasks.size());
    for (DataNode<TaskData> taskDataNode : tasks) {
        final TaskData taskData = taskDataNode.getData();
        elements.add(LookupElementBuilder.create(taskData.getName()).withIcon(ExternalSystemIcons.Task));
        if (!taskData.isInherited()) {
            elements.add(LookupElementBuilder.create((isRoot ? ':' : moduleData.getId() + ':') + taskData.getName()).withIcon(ExternalSystemIcons.Task));
        }
    }
    return elements;
}
Also used : DataNode(com.intellij.openapi.externalSystem.model.DataNode) ModuleData(com.intellij.openapi.externalSystem.model.project.ModuleData) LookupElement(com.intellij.codeInsight.lookup.LookupElement) TaskData(com.intellij.openapi.externalSystem.model.task.TaskData)

Example 10 with DataNode

use of com.intellij.openapi.externalSystem.model.DataNode in project intellij-community by JetBrains.

the class GradleProjectResolver method mergeModuleContentRoots.

private static void mergeModuleContentRoots(@NotNull Map<String, Counter> weightMap, @NotNull ExternalProject externalProject, @NotNull DataNode<? extends ModuleData> moduleNode) {
    final File buildDir = externalProject.getBuildDir();
    final MultiMap<String, ContentRootData> sourceSetRoots = MultiMap.create();
    Collection<DataNode<ContentRootData>> contentRootNodes = ExternalSystemApiUtil.findAll(moduleNode, ProjectKeys.CONTENT_ROOT);
    if (contentRootNodes.size() <= 1)
        return;
    for (DataNode<ContentRootData> contentRootNode : contentRootNodes) {
        File root = new File(contentRootNode.getData().getRootPath());
        if (FileUtil.isAncestor(buildDir, root, true))
            continue;
        while (weightMap.containsKey(root.getParent()) && weightMap.get(root.getParent()).count <= 1) {
            root = root.getParentFile();
        }
        ContentRootData mergedContentRoot = null;
        String rootPath = ExternalSystemApiUtil.toCanonicalPath(root.getAbsolutePath());
        Set<String> paths = ContainerUtil.newHashSet(sourceSetRoots.keySet());
        for (String path : paths) {
            if (FileUtil.isAncestor(rootPath, path, true)) {
                Collection<ContentRootData> values = sourceSetRoots.remove(path);
                if (values != null) {
                    sourceSetRoots.putValues(rootPath, values);
                }
            } else if (FileUtil.isAncestor(path, rootPath, false)) {
                Collection<ContentRootData> contentRoots = sourceSetRoots.get(path);
                for (ContentRootData rootData : contentRoots) {
                    if (StringUtil.equals(rootData.getRootPath(), path)) {
                        mergedContentRoot = rootData;
                        break;
                    }
                }
                if (mergedContentRoot == null) {
                    mergedContentRoot = contentRoots.iterator().next();
                }
                break;
            }
            if (sourceSetRoots.size() == 1)
                break;
        }
        if (mergedContentRoot == null) {
            mergedContentRoot = new ContentRootData(GradleConstants.SYSTEM_ID, root.getAbsolutePath());
            sourceSetRoots.putValue(mergedContentRoot.getRootPath(), mergedContentRoot);
        }
        for (ExternalSystemSourceType sourceType : ExternalSystemSourceType.values()) {
            for (ContentRootData.SourceRoot sourceRoot : contentRootNode.getData().getPaths(sourceType)) {
                mergedContentRoot.storePath(sourceType, sourceRoot.getPath(), sourceRoot.getPackagePrefix());
            }
        }
        contentRootNode.clear(true);
    }
    for (Map.Entry<String, Collection<ContentRootData>> entry : sourceSetRoots.entrySet()) {
        final String rootPath = entry.getKey();
        final ContentRootData ideContentRoot = new ContentRootData(GradleConstants.SYSTEM_ID, rootPath);
        for (ContentRootData rootData : entry.getValue()) {
            for (ExternalSystemSourceType sourceType : ExternalSystemSourceType.values()) {
                Collection<ContentRootData.SourceRoot> roots = rootData.getPaths(sourceType);
                for (ContentRootData.SourceRoot sourceRoot : roots) {
                    ideContentRoot.storePath(sourceType, sourceRoot.getPath(), sourceRoot.getPackagePrefix());
                }
            }
        }
        moduleNode.createChild(ProjectKeys.CONTENT_ROOT, ideContentRoot);
    }
}
Also used : DataNode(com.intellij.openapi.externalSystem.model.DataNode) File(java.io.File) MultiMap(com.intellij.util.containers.MultiMap)

Aggregations

DataNode (com.intellij.openapi.externalSystem.model.DataNode)51 ModuleData (com.intellij.openapi.externalSystem.model.project.ModuleData)22 ProjectData (com.intellij.openapi.externalSystem.model.project.ProjectData)14 NotNull (org.jetbrains.annotations.NotNull)12 Module (com.intellij.openapi.module.Module)11 File (java.io.File)9 Project (com.intellij.openapi.project.Project)7 GradleModuleModel (com.android.tools.idea.gradle.project.model.GradleModuleModel)6 Nullable (org.jetbrains.annotations.Nullable)6 GradleSourceSetData (org.jetbrains.plugins.gradle.model.data.GradleSourceSetData)5 ExternalProjectInfo (com.intellij.openapi.externalSystem.model.ExternalProjectInfo)4 IdeModifiableModelsProviderImpl (com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProviderImpl)4 IdeaModule (org.gradle.tooling.model.idea.IdeaModule)4 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3 Task (com.intellij.openapi.progress.Task)3 Pair (com.intellij.openapi.util.Pair)3 MultiMap (com.intellij.util.containers.MultiMap)3 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2 NdkModuleModel (com.android.tools.idea.gradle.project.model.NdkModuleModel)2 JavaProjectData (com.intellij.externalSystem.JavaProjectData)2