Search in sources :

Example 26 with ModuleData

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

the class GradleExecutionWorkspace method findModuleDataByName.

public ModuleData findModuleDataByName(String moduleName) {
    ModuleData result = null;
    Pair<DataNode<ModuleData>, IdeaModule> modulePair = myModuleMap.get(moduleName);
    if (modulePair == null) {
        modulePair = myModuleMap.get(":" + moduleName);
    }
    if (modulePair != null) {
        return modulePair.first.getData();
    }
    for (GradleBuildParticipant buildParticipant : myBuildParticipants) {
        result = buildParticipant.findModuleDataByName(moduleName);
        if (result != null)
            break;
    }
    return result;
}
Also used : IdeaModule(org.gradle.tooling.model.idea.IdeaModule) DataNode(com.intellij.openapi.externalSystem.model.DataNode) ModuleData(com.intellij.openapi.externalSystem.model.project.ModuleData)

Example 27 with ModuleData

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

the class GradleTestRunConfigurationProducer method getTasksToRun.

@NotNull
public static List<String> getTasksToRun(@NotNull Module module) {
    for (GradleTestTasksProvider provider : GradleTestTasksProvider.EP_NAME.getExtensions()) {
        final List<String> tasks = provider.getTasks(module);
        if (!ContainerUtil.isEmpty(tasks)) {
            return tasks;
        }
    }
    final List<String> result;
    final String externalProjectId = ExternalSystemApiUtil.getExternalProjectId(module);
    if (externalProjectId == null)
        return ContainerUtil.emptyList();
    final String projectPath = ExternalSystemApiUtil.getExternalProjectPath(module);
    if (projectPath == null)
        return ContainerUtil.emptyList();
    final ExternalProjectInfo externalProjectInfo = ExternalSystemUtil.getExternalProjectInfo(module.getProject(), GradleConstants.SYSTEM_ID, projectPath);
    if (externalProjectInfo == null)
        return ContainerUtil.emptyList();
    boolean trimSourceSet = false;
    if (StringUtil.endsWith(externalProjectId, ":test") || StringUtil.endsWith(externalProjectId, ":main")) {
        result = TEST_SOURCE_SET_TASKS;
        trimSourceSet = true;
    } else {
        final DataNode<ModuleData> moduleNode = GradleProjectResolverUtil.findModule(externalProjectInfo.getExternalProjectStructure(), projectPath);
        if (moduleNode == null)
            return ContainerUtil.emptyList();
        final DataNode<TaskData> taskNode;
        final String sourceSetId = StringUtil.substringAfter(externalProjectId, moduleNode.getData().getExternalName() + ':');
        if (sourceSetId == null) {
            taskNode = ExternalSystemApiUtil.find(moduleNode, ProjectKeys.TASK, node -> GradleCommonClassNames.GRADLE_API_TASKS_TESTING_TEST.equals(node.getData().getType()) && StringUtil.equals("test", node.getData().getName()));
        } else {
            trimSourceSet = true;
            taskNode = ExternalSystemApiUtil.find(moduleNode, ProjectKeys.TASK, node -> GradleCommonClassNames.GRADLE_API_TASKS_TESTING_TEST.equals(node.getData().getType()) && StringUtil.startsWith(sourceSetId, node.getData().getName()));
        }
        if (taskNode == null)
            return ContainerUtil.emptyList();
        final String taskName = taskNode.getData().getName();
        result = ContainerUtil.list("clean" + StringUtil.capitalize(taskName), taskName);
    }
    final String path;
    if (!externalProjectId.startsWith(":")) {
        path = ":";
    } else {
        final List<String> pathParts = StringUtil.split(externalProjectId, ":");
        if (trimSourceSet && !pathParts.isEmpty())
            pathParts.remove(pathParts.size() - 1);
        final String join = StringUtil.join(pathParts, ":");
        path = ":" + join + (!join.isEmpty() ? ":" : "");
    }
    return ContainerUtil.map(result, s -> path + s);
}
Also used : ExternalProjectInfo(com.intellij.openapi.externalSystem.model.ExternalProjectInfo) ConfigurationFromContext(com.intellij.execution.actions.ConfigurationFromContext) ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) ProjectKeys(com.intellij.openapi.externalSystem.model.ProjectKeys) RunnerAndConfigurationSettings(com.intellij.execution.RunnerAndConfigurationSettings) ConfigurationType(com.intellij.execution.configurations.ConfigurationType) ContainerUtil(com.intellij.util.containers.ContainerUtil) GradleSystemRunningSettings(org.jetbrains.plugins.gradle.settings.GradleSystemRunningSettings) ExternalProjectInfo(com.intellij.openapi.externalSystem.model.ExternalProjectInfo) ExternalSystemApiUtil(com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil) DataNode(com.intellij.openapi.externalSystem.model.DataNode) PsiElement(com.intellij.psi.PsiElement) PreferredTestRunner(org.jetbrains.plugins.gradle.settings.GradleSystemRunningSettings.PreferredTestRunner) RunManagerEx(com.intellij.execution.RunManagerEx) FileUtil(com.intellij.openapi.util.io.FileUtil) Module(com.intellij.openapi.module.Module) ExternalSystemRunConfiguration(com.intellij.openapi.externalSystem.service.execution.ExternalSystemRunConfiguration) StringUtil(com.intellij.openapi.util.text.StringUtil) ModuleData(com.intellij.openapi.externalSystem.model.project.ModuleData) GradleProjectResolverUtil(org.jetbrains.plugins.gradle.service.project.GradleProjectResolverUtil) ExternalSystemUtil(com.intellij.openapi.externalSystem.util.ExternalSystemUtil) Nullable(org.jetbrains.annotations.Nullable) GradleCommonClassNames(org.jetbrains.plugins.gradle.service.resolve.GradleCommonClassNames) List(java.util.List) GradleConstants(org.jetbrains.plugins.gradle.util.GradleConstants) TaskData(com.intellij.openapi.externalSystem.model.task.TaskData) RunConfigurationProducer(com.intellij.execution.actions.RunConfigurationProducer) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) ModuleData(com.intellij.openapi.externalSystem.model.project.ModuleData) TaskData(com.intellij.openapi.externalSystem.model.task.TaskData) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with ModuleData

use of com.intellij.openapi.externalSystem.model.project.ModuleData in project android by JetBrains.

the class ProjectSubset method findAndIncludeModuleContainingSourceFile.

/**
   * Finds and includes the module that contains the given file.
   * <p>
   * When using the "Project Subset" feature it is possible that the user knows which file she wants to edit but not the module where
   * such file is. This method tries to find the module that includes the given file in the folders that it marked as "source", either
   * production or test code.
   * </p>
   * <p>
   * The search is based on the Gradle models for both Android and Java modules. If the search finds more than one module that might contain
   * the file, the IDE will display a dialog where the user can see the potential matches and choose the module to include in the project.
   * </p>
   *
   * @param virtualFile the given file.
   */
public void findAndIncludeModuleContainingSourceFile(@NotNull VirtualFile virtualFile) {
    DataNode<ProjectData> projectInfo = DataNodeCaches.getInstance(myProject).getCachedProjectData();
    if (projectInfo == null) {
        return;
    }
    Collection<DataNode<ModuleData>> moduleInfos = findAll(projectInfo, MODULE);
    if (!moduleInfos.isEmpty()) {
        File file = virtualToIoFile(virtualFile);
        new Task.Modal(myProject, "Looking up Module", false) {

            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                List<ModuleSearchResult> results = Lists.newArrayList();
                String[] storedSelection = getSelection();
                Set<String> selection = storedSelection != null ? Sets.newHashSet(storedSelection) : Sets.newHashSet();
                List<DataNode<ModuleData>> selectedModules = Lists.newArrayList();
                int doneCount = 0;
                for (DataNode<ModuleData> moduleNode : moduleInfos) {
                    indicator.setFraction(++doneCount / moduleInfos.size());
                    ModuleData module = moduleNode.getData();
                    String name = module.getExternalName();
                    boolean selected = selection.contains(name);
                    if (selected) {
                        // This module is already included in the project. We need to mark it as "selected" so when we are done searching we don't
                        // exclude it by accident.
                        selectedModules.add(moduleNode);
                    }
                    ModuleSearchResult result = containsSourceFile(moduleNode, file, selected);
                    if (result != null) {
                        // Even though the module is already included, we add it to the search results, because the module might not be the one that
                        // actually contains the file, and the user might need to exclude it in the case that the module that contains the file has
                        // the same path as the already-included module.
                        results.add(result);
                    }
                }
                int resultCount = results.size();
                if (resultCount == 0) {
                    // Nothing found.
                    invokeLaterIfNeeded(() -> {
                        String text = String.format("Unable to find a module containing the file '%1$s' in a source directory.", file.getName());
                        AndroidGradleNotification notification = AndroidGradleNotification.getInstance(ProjectSubset.this.myProject);
                        notification.showBalloon(MODULE_LOOKUP_MESSAGE_TITLE, text, ERROR);
                    });
                } else if (resultCount == 1) {
                    // If there is one result,just apply it.
                    addResultAndPopulateProject(results.get(0), projectInfo, selectedModules, file);
                } else {
                    // We need to let user decide which modules to include.
                    showModuleSelectionDialog(results, projectInfo, selectedModules, file);
                }
            }
        }.queue();
    }
}
Also used : Task(com.intellij.openapi.progress.Task) DataNode(com.intellij.openapi.externalSystem.model.DataNode) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ModuleData(com.intellij.openapi.externalSystem.model.project.ModuleData) AndroidGradleNotification(com.android.tools.idea.gradle.project.AndroidGradleNotification) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File) ProjectData(com.intellij.openapi.externalSystem.model.project.ProjectData)

Example 29 with ModuleData

use of com.intellij.openapi.externalSystem.model.project.ModuleData in project android by JetBrains.

the class ProjectSubset method showModuleSelectionDialog.

/**
   * Displays the "Select Modules" dialog. This method is invoked when the search for a module containing a file returns more than one
   * result. The user now needs to select the module(s) to include.
   *
   * @param searchResults includes the modules that might contain the given file.
   * @param selection     all the modules that need to be included in the project.
   * @param file          the file to include in the project.
   */
private void showModuleSelectionDialog(@NotNull List<ModuleSearchResult> searchResults, @NotNull DataNode<ProjectData> projectInfo, @NotNull List<DataNode<ModuleData>> selection, @NotNull File file) {
    List<DataNode<ModuleData>> finalSelection = Lists.newArrayList(selection);
    List<DataNode<ModuleData>> modulesToDisplayInDialog = Lists.newArrayList();
    Map<String, ModuleSearchResult> resultsByModuleName = Maps.newHashMap();
    for (ModuleSearchResult result : searchResults) {
        DataNode<ModuleData> module = result.moduleNode;
        modulesToDisplayInDialog.add(module);
        if (result.selected) {
            finalSelection.remove(module);
        }
        String moduleName = getNameOf(module);
        resultsByModuleName.put(moduleName, result);
    }
    invokeLaterIfNeeded(() -> {
        ModulesToImportDialog dialog = new ModulesToImportDialog(modulesToDisplayInDialog, myProject);
        String description = String.format("The file '%1$s' may be include in one of the following modules.", file.getName());
        dialog.setDescription(description);
        dialog.clearSelection();
        if (dialog.showAndGet()) {
            Collection<DataNode<ModuleData>> selectedModules = dialog.getSelectedModules();
            if (!selectedModules.isEmpty()) {
                for (DataNode<ModuleData> selectedModule : selectedModules) {
                    String name = getNameOf(selectedModule);
                    ModuleSearchResult result = resultsByModuleName.get(name);
                    if (result != null) {
                        SourceFileContainerInfo containerInfo = result.containerInfo;
                        if (containerInfo != null) {
                            containerInfo.updateSelectedVariantIn(selectedModule);
                        }
                    }
                }
                finalSelection.addAll(selectedModules);
                setSelection(finalSelection);
                populate(myProject, projectInfo, finalSelection, DEFAULT_REQUEST);
            }
        }
    });
}
Also used : SourceFileContainerInfo(com.android.tools.idea.gradle.project.model.AndroidModuleModel.SourceFileContainerInfo) DataNode(com.intellij.openapi.externalSystem.model.DataNode) ModuleData(com.intellij.openapi.externalSystem.model.project.ModuleData)

Example 30 with ModuleData

use of com.intellij.openapi.externalSystem.model.project.ModuleData in project android by JetBrains.

the class DataNodeCaches method indexByModuleName.

@NotNull
private static Map<String, DataNode<ModuleData>> indexByModuleName(@NotNull Collection<DataNode<ModuleData>> moduleDataNodes) {
    Map<String, DataNode<ModuleData>> mapping = Maps.newHashMap();
    for (DataNode<ModuleData> moduleDataNode : moduleDataNodes) {
        ModuleData data = moduleDataNode.getData();
        mapping.put(data.getExternalName(), moduleDataNode);
    }
    return mapping;
}
Also used : DataNode(com.intellij.openapi.externalSystem.model.DataNode) ModuleData(com.intellij.openapi.externalSystem.model.project.ModuleData) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ModuleData (com.intellij.openapi.externalSystem.model.project.ModuleData)36 DataNode (com.intellij.openapi.externalSystem.model.DataNode)22 ProjectData (com.intellij.openapi.externalSystem.model.project.ProjectData)13 Module (com.intellij.openapi.module.Module)9 NotNull (org.jetbrains.annotations.NotNull)6 GradleModuleModel (com.android.tools.idea.gradle.project.model.GradleModuleModel)5 TaskData (com.intellij.openapi.externalSystem.model.task.TaskData)4 ExternalProjectInfo (com.intellij.openapi.externalSystem.model.ExternalProjectInfo)3 ProjectSystemId (com.intellij.openapi.externalSystem.model.ProjectSystemId)3 AndroidGradleNotification (com.android.tools.idea.gradle.project.AndroidGradleNotification)2 SourceFileContainerInfo (com.android.tools.idea.gradle.project.model.AndroidModuleModel.SourceFileContainerInfo)2 ExternalProjectPojo (com.intellij.openapi.externalSystem.model.project.ExternalProjectPojo)2 ModuleDependencyData (com.intellij.openapi.externalSystem.model.project.ModuleDependencyData)2 AbstractExternalSystemLocalSettings (com.intellij.openapi.externalSystem.settings.AbstractExternalSystemLocalSettings)2 ProjectNode (com.intellij.openapi.externalSystem.view.ProjectNode)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 Project (com.intellij.openapi.project.Project)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 File (java.io.File)2 Variant (com.android.builder.model.Variant)1