Search in sources :

Example 36 with DataNode

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

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

the class GradleProjectImportBuilder method createFinalImportCallback.

@Override
protected ExternalProjectRefreshCallback createFinalImportCallback(@NotNull final Project project, @NotNull ExternalProjectSettings projectSettings) {
    return new ExternalProjectRefreshCallback() {

        @Override
        public void onSuccess(@Nullable final DataNode<ProjectData> externalProject) {
            if (externalProject == null)
                return;
            Runnable selectDataTask = () -> {
                ExternalProjectDataSelectorDialog dialog = new ExternalProjectDataSelectorDialog(project, new InternalExternalProjectInfo(GradleConstants.SYSTEM_ID, projectSettings.getExternalProjectPath(), externalProject));
                if (dialog.hasMultipleDataToSelect()) {
                    dialog.showAndGet();
                } else {
                    Disposer.dispose(dialog.getDisposable());
                }
            };
            Runnable importTask = () -> ServiceManager.getService(ProjectDataManager.class).importData(externalProject, project, false);
            if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
                ApplicationManager.getApplication().invokeLater(() -> {
                    selectDataTask.run();
                    ApplicationManager.getApplication().executeOnPooledThread(importTask);
                });
            } else {
                importTask.run();
            }
        }

        @Override
        public void onFailure(@NotNull String errorMessage, @Nullable String errorDetails) {
        }
    };
}
Also used : ProjectDataManager(com.intellij.openapi.externalSystem.service.project.manage.ProjectDataManager) DataNode(com.intellij.openapi.externalSystem.model.DataNode) ExternalProjectDataSelectorDialog(com.intellij.openapi.externalSystem.service.ui.ExternalProjectDataSelectorDialog) ExternalProjectRefreshCallback(com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback) InternalExternalProjectInfo(com.intellij.openapi.externalSystem.model.internal.InternalExternalProjectInfo) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable)

Example 38 with DataNode

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

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

Example 40 with DataNode

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

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