Java Code Examples for com.intellij.openapi.externalSystem.model.DataNode#getData()

The following examples show how to use com.intellij.openapi.externalSystem.model.DataNode#getData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PantsMetadataService.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public void importData(
  @NotNull final Collection<DataNode<TargetMetadata>> toImport,
  @Nullable ProjectData projectData,
  @NotNull Project project,
  @NotNull IdeModifiableModelsProvider modelsProvider
) {
  // for existing projects. for new projects PantsSettings.defaultSettings will provide the version.
  PantsSettings.getInstance(project).setResolverVersion(PantsResolver.VERSION);
  for (DataNode<TargetMetadata> node : toImport) {
    final TargetMetadata metadata = node.getData();
    final Module module = modelsProvider.findIdeModule(metadata.getModuleName());
    if (module != null) {
      module.setOption(PantsConstants.PANTS_LIBRARY_EXCLUDES_KEY, PantsUtil.dehydrateTargetAddresses(metadata.getLibraryExcludes()));
      module.setOption(PantsConstants.PANTS_TARGET_ADDRESSES_KEY, PantsUtil.dehydrateTargetAddresses(metadata.getTargetAddresses()));
      module.setOption(PantsConstants.PANTS_TARGET_ADDRESS_INFOS_KEY, gson.toJson(metadata.getTargetAddressInfoSet()));
      module.setOption(PantsConstants.PANTS_OPTION_LINKED_PROJECT_PATH, Paths.get(projectData.getLinkedExternalProjectPath()).normalize().toString());
      ExternalSystemModulePropertyManager.getInstance(module).setExternalModuleType(PantsConstants.PANTS_TARGET_MODULE_TYPE);
    }
  }
}
 
Example 2
Source File: PantsResolverTestBase.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
public void assertSourceRoot(String moduleName, final String path) {
  final DataNode<ModuleData> moduleNode = findModule(moduleName);
  assertModuleExists(moduleName, moduleNode);
  final Collection<DataNode<ContentRootData>> contentRoots = ExternalSystemApiUtil.findAll(moduleNode, ProjectKeys.CONTENT_ROOT);
  assertFalse(String.format("No content root for module %s", moduleName), contentRoots.isEmpty());
  for (DataNode<ContentRootData> contentRoot : contentRoots) {
    final ContentRootData contentRootData = contentRoot.getData();
    for (PantsSourceType type : PantsSourceType.values()) {
      for (ContentRootData.SourceRoot sourceRoot : contentRootData.getPaths(type.toExternalSystemSourceType())) {
        final File expectedFile = new File(new File(""), path);
        if (StringUtil.equalsIgnoreCase(expectedFile.getPath(), sourceRoot.getPath())) {
          return;
        }
      }
    }
  }
  fail(String.format("Source root %s is not found for %s!", path, moduleName));
}
 
Example 3
Source File: ProjectDataServiceImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void importData(@Nonnull Collection<DataNode<ProjectData>> toImport, @Nonnull Project project, boolean synchronous) {
  if (toImport.size() != 1) {
    throw new IllegalArgumentException(String.format("Expected to get a single project but got %d: %s", toImport.size(), toImport));
  }
  DataNode<ProjectData> node = toImport.iterator().next();
  ProjectData projectData = node.getData();
  
  if (!ExternalSystemApiUtil.isNewProjectConstruction() && !ExternalSystemUtil.isOneToOneMapping(project, node)) {
    return;
  }
  
  if (!project.getName().equals(projectData.getInternalName())) {
    renameProject(projectData.getInternalName(), projectData.getOwner(), project, synchronous);
  }
}
 
Example 4
Source File: ModuleDataService.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@RequiredUIAccess
private Collection<DataNode<ModuleData>> filterExistingModules(@Nonnull Collection<DataNode<ModuleData>> modules, @Nonnull Project project) {
  Collection<DataNode<ModuleData>> result = ContainerUtilRt.newArrayList();
  for (DataNode<ModuleData> node : modules) {
    ModuleData moduleData = node.getData();
    Module module = ProjectStructureHelper.findIdeModule(moduleData, project);
    if (module == null) {
      result.add(node);
    }
    else {
      setModuleOptions(module, null, node);
    }
  }
  return result;
}
 
Example 5
Source File: GradleMonitor.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private boolean hasFormatPlugin(DataNode<?> node) {
	if (node == null) {
		return false;
	}
	Object data = node.getData();
	if (data instanceof TaskData && isFormatPlugin((TaskData) data)) {
		return true;
	}
	for (DataNode<?> child : node.getChildren()) {
		if (hasFormatPlugin(child)) {
			return true;
		}
	}
	return false;
}
 
Example 6
Source File: AndroidUtils.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@NotNull
@SuppressWarnings("DuplicatedCode")
private static Map<ProjectData, MultiMap<String, String>> getTasksMap(Project project) {
  Map<ProjectData, MultiMap<String, String>> tasks = new LinkedHashMap<>();
  for (GradleProjectSettings setting : GradleSettings.getInstance(project).getLinkedProjectsSettings()) {
    final ExternalProjectInfo projectData =
      ProjectDataManager.getInstance().getExternalProjectData(project, GradleConstants.SYSTEM_ID, setting.getExternalProjectPath());
    if (projectData == null || projectData.getExternalProjectStructure() == null) continue;

    MultiMap<String, String> projectTasks = MultiMap.createOrderedSet();
    for (DataNode<ModuleData> moduleDataNode : getChildren(projectData.getExternalProjectStructure(), ProjectKeys.MODULE)) {
      String gradlePath;
      String moduleId = moduleDataNode.getData().getId();
      if (moduleId.charAt(0) != ':') {
        int colonIndex = moduleId.indexOf(':');
        gradlePath = colonIndex > 0 ? moduleId.substring(colonIndex) : ":";
      }
      else {
        gradlePath = moduleId;
      }
      for (DataNode<TaskData> node : getChildren(moduleDataNode, ProjectKeys.TASK)) {
        TaskData taskData = node.getData();
        String taskName = taskData.getName();
        if (isNotEmpty(taskName)) {
          String taskPathPrefix = ":".equals(gradlePath) || taskName.startsWith(gradlePath) ? "" : (gradlePath + ':');
          projectTasks.putValue(taskPathPrefix, taskName);
        }
      }
    }
    tasks.put(projectData.getExternalProjectStructure().getData(), projectTasks);
  }
  return tasks;
}
 
Example 7
Source File: AndroidUtils.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@NotNull
@SuppressWarnings("DuplicatedCode")
private static Map<ProjectData, MultiMap<String, String>> getTasksMap(Project project) {
  Map<ProjectData, MultiMap<String, String>> tasks = new LinkedHashMap<>();
  for (GradleProjectSettings setting : GradleSettings.getInstance(project).getLinkedProjectsSettings()) {
    final ExternalProjectInfo projectData =
      ProjectDataManager.getInstance().getExternalProjectData(project, GradleConstants.SYSTEM_ID, setting.getExternalProjectPath());
    if (projectData == null || projectData.getExternalProjectStructure() == null) continue;

    MultiMap<String, String> projectTasks = MultiMap.createOrderedSet();
    for (DataNode<ModuleData> moduleDataNode : getChildren(projectData.getExternalProjectStructure(), ProjectKeys.MODULE)) {
      String gradlePath;
      String moduleId = moduleDataNode.getData().getId();
      if (moduleId.charAt(0) != ':') {
        int colonIndex = moduleId.indexOf(':');
        gradlePath = colonIndex > 0 ? moduleId.substring(colonIndex) : ":";
      }
      else {
        gradlePath = moduleId;
      }
      for (DataNode<TaskData> node : getChildren(moduleDataNode, ProjectKeys.TASK)) {
        TaskData taskData = node.getData();
        String taskName = taskData.getName();
        if (isNotEmpty(taskName)) {
          String taskPathPrefix = ":".equals(gradlePath) || taskName.startsWith(gradlePath) ? "" : (gradlePath + ':');
          projectTasks.putValue(taskPathPrefix, taskName);
        }
      }
    }
    tasks.put(projectData.getExternalProjectStructure().getData(), projectTasks);
  }
  return tasks;
}
 
Example 8
Source File: PantsSystemProjectResolver.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
private boolean containsContentRoot(@NotNull DataNode<ProjectData> projectDataNode, @NotNull String path) {
  for (DataNode<ModuleData> moduleDataNode : ExternalSystemApiUtil.findAll(projectDataNode, ProjectKeys.MODULE)) {
    for (DataNode<ContentRootData> contentRootDataNode : ExternalSystemApiUtil.findAll(moduleDataNode, ProjectKeys.CONTENT_ROOT)) {
      final ContentRootData contentRootData = contentRootDataNode.getData();
      if (FileUtil.isAncestor(contentRootData.getRootPath(), path, false)) {
        return true;
      }
    }
  }

  return false;
}
 
Example 9
Source File: PantsModuleDependenciesExtension.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
private void addModuleDependency(DataNode<ModuleData> moduleDataNode, DataNode<ModuleData> submoduleDataNode, boolean exported) {
  final List<ModuleDependencyData> subModuleDeps = PantsUtil.findChildren(submoduleDataNode, ProjectKeys.MODULE_DEPENDENCY);
  for (ModuleDependencyData dep : subModuleDeps) {
    if (dep.getTarget().equals(moduleDataNode.getData())) {
      return;
    }
  }
  final ModuleDependencyData moduleDependencyData = new ModuleDependencyData(
    moduleDataNode.getData(),
    submoduleDataNode.getData()
  );
  moduleDependencyData.setExported(exported);
  moduleDataNode.createChild(ProjectKeys.MODULE_DEPENDENCY, moduleDependencyData);
}
 
Example 10
Source File: PythonPexResolver.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void resolve(
  @NotNull ProjectInfo projectInfo,
  @NotNull PantsCompileOptionsExecutor executor,
  @NotNull DataNode<ProjectData> projectDataNode,
  @NotNull Map<String, DataNode<ModuleData>> modules,
  @NotNull Optional<BuildGraph> buildGraph
) {
  final Optional<VirtualFile> buildRoot = PantsUtil.findBuildRoot(projectDataNode.getData().getLinkedExternalProjectPath());
  final Optional<VirtualFile> bootstrappedPants =  buildRoot.map(file -> file.findChild(PantsConstants.PANTS_PEX));
  final Optional<VirtualFile> pexFile = bootstrappedPants.flatMap(file -> findSpecificPexVersionInHomeDirectory(buildRoot));
  if (pexFile.isPresent()) {
    final LibraryData libraryData = new LibraryData(PantsConstants.SYSTEM_ID, PantsConstants.PANTS_LIBRARY_NAME);
    libraryData.addPath(LibraryPathType.BINARY, pexFile.get().getPath());
    projectDataNode.createChild(ProjectKeys.LIBRARY, libraryData);

    for (DataNode<ModuleData> moduleDataNode : modules.values()) {
      final LibraryDependencyData library = new LibraryDependencyData(
        moduleDataNode.getData(),
        libraryData,
        LibraryLevel.PROJECT
      );
      library.setExported(false);
      moduleDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, library);
    }
  }
}
 
Example 11
Source File: PythonRequirementsResolver.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void resolve(
  @NotNull ProjectInfo projectInfo,
  @NotNull PantsCompileOptionsExecutor executor,
  @NotNull DataNode<ProjectData> projectDataNode,
  @NotNull Map<String, DataNode<ModuleData>> modules,
  @NotNull Optional<BuildGraph> buildGraph
) {
  final PythonSetup pythonSetup = projectInfo.getPythonSetup();
  if (pythonSetup == null) {
    LOG.warn("Current version of Pants doesn't provide information about Python setup. Please upgrade!");
    return;
  }

  final DataNode<ModuleData> requirementsModuleDataNode = createRequirementsModule(projectDataNode, pythonSetup, executor);
  requirementsModuleDataNode.createChild(
    PythonSetupData.KEY,
    new PythonSetupData(requirementsModuleDataNode.getData(), pythonSetup.getDefaultInterpreterInfo())
  );

  for (Map.Entry<String, TargetInfo> targetInfoEntry : projectInfo.getSortedTargets()) {
    final String targetName = targetInfoEntry.getKey();
    final TargetInfo targetInfo = targetInfoEntry.getValue();
    final DataNode<ModuleData> moduleDataNode = modules.get(targetName);
    if (targetInfo.isPythonTarget() && moduleDataNode != null) {
      final ModuleDependencyData moduleDependencyData = new ModuleDependencyData(
        moduleDataNode.getData(),
        requirementsModuleDataNode.getData()
      );
      moduleDependencyData.setExported(true);
      moduleDataNode.createChild(ProjectKeys.MODULE_DEPENDENCY, moduleDependencyData);
      moduleDataNode.createChild(
        PythonSetupData.KEY,
        new PythonSetupData(moduleDataNode.getData(), pythonSetup.getDefaultInterpreterInfo())
      );
    }
  }
}
 
Example 12
Source File: LibraryDependencyDataService.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void importMissingProjectLibraries(@Nonnull Module module, @Nonnull Collection<DataNode<LibraryDependencyData>> nodesToImport, boolean synchronous) {
  LibraryTable libraryTable = ProjectLibraryTable.getInstance(module.getProject());
  List<DataNode<LibraryData>> librariesToImport = ContainerUtilRt.newArrayList();
  for (DataNode<LibraryDependencyData> dataNode : nodesToImport) {
    final LibraryDependencyData dependencyData = dataNode.getData();
    if (dependencyData.getLevel() != LibraryLevel.PROJECT) {
      continue;
    }
    final Library library = libraryTable.getLibraryByName(dependencyData.getInternalName());
    if (library == null) {
      DataNode<ProjectData> projectNode = dataNode.getDataNode(ProjectKeys.PROJECT);
      if (projectNode != null) {
        DataNode<LibraryData> libraryNode = ExternalSystemApiUtil.find(projectNode, ProjectKeys.LIBRARY, new BooleanFunction<DataNode<LibraryData>>() {
          @Override
          public boolean fun(DataNode<LibraryData> node) {
            return node.getData().equals(dependencyData.getTarget());
          }
        });
        if (libraryNode != null) {
          librariesToImport.add(libraryNode);
        }
      }
    }
  }
  if (!librariesToImport.isEmpty()) {
    LibraryDataService.getInstance().importData(librariesToImport, module.getProject(), synchronous);
  }
}
 
Example 13
Source File: PantsLibrariesExtension.java    From intellij-pants-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void resolve(
  @NotNull ProjectInfo projectInfo,
  @NotNull PantsCompileOptionsExecutor executor,
  @NotNull DataNode<ProjectData> projectDataNode,
  @NotNull Map<String, DataNode<ModuleData>> modules,
  @NotNull Optional<BuildGraph> buildGraph
) {
  for (Map.Entry<String, TargetInfo> entry : projectInfo.getSortedTargets()) {
    final TargetInfo targetInfo = entry.getValue();

    if (executor.getOptions().isImportSourceDepsAsJars()) {
      if (targetInfo.isPythonTarget()) {
        continue;
      }
    }
    else if (!targetInfo.isJarLibrary()) {
      continue;
    }

    final String jarTarget = entry.getKey();
    final LibraryData libraryData = new LibraryData(PantsConstants.SYSTEM_ID, jarTarget);

    for (String libraryId : targetInfo.getLibraries()) {
      final LibraryInfo libraryInfo = projectInfo.getLibraries(libraryId);
      if (libraryInfo == null) {
        LOG.debug("Couldn't find library " + libraryId);
        continue;
      }

      addPathLoLibrary(libraryData, executor, LibraryPathType.BINARY, libraryInfo.getDefault());
      addPathLoLibrary(libraryData, executor, LibraryPathType.SOURCE, libraryInfo.getSources());
      addPathLoLibrary(libraryData, executor, LibraryPathType.DOC, libraryInfo.getJavadoc());

      for (String otherLibraryInfo : libraryInfo.getJarsWithCustomClassifiers()) {
        addPathLoLibrary(libraryData, executor, LibraryPathType.BINARY, otherLibraryInfo);
      }
    }

    projectDataNode.createChild(ProjectKeys.LIBRARY, libraryData);
    final DataNode<ModuleData> moduleDataNode = modules.get(jarTarget);
    if (moduleDataNode == null) {
      continue;
    }

    final LibraryDependencyData library = new LibraryDependencyData(
      moduleDataNode.getData(),
      libraryData,
      LibraryLevel.PROJECT
    );
    library.setExported(true);
    moduleDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, library);
  }
}
 
Example 14
Source File: PantsPythonSetupDataService.java    From intellij-pants-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void importData(
  @NotNull final Collection<DataNode<PythonSetupData>> toImport,
  @Nullable ProjectData projectData,
  @NotNull Project project,
  @NotNull final IdeModifiableModelsProvider modelsProvider
) {
  final Set<PythonInterpreterInfo> interpreters = ContainerUtil.map2Set(
    toImport,
    node -> node.getData().getInterpreterInfo()
  );

  if (interpreters.isEmpty()) {
    return;
  }

  final Map<PythonInterpreterInfo, Sdk> interpreter2sdk = new HashMap<>();

  /**
   * TODO(yic): to move it to a thread appropriate place.
   *
   *  1) testPyTestRunConfiguration(com.twitter.intellij.pants.execution.OSSPantsPythonRunConfigurationIntegrationTest)
   *  com.intellij.testFramework.LoggedErrorProcessor$TestLoggerAssertionError: Can't update SDK under write action, not allowed in background
   */
  //ExternalSystemApiUtil.executeProjectChangeAction(false, new DisposeAwareProjectChange(project) {
  //  @Override
  //  public void execute() {
  //    for (final PythonInterpreterInfo interpreterInfo : interpreters) {
  //      //final String binFolder = PathUtil.getParentPath(interpreter);
  //      //final String interpreterHome = PathUtil.getParentPath(binFolder);
  //      final String interpreter = interpreterInfo.getBinary();
  //      Sdk pythonSdk = PythonSdkType.findSdkByPath(interpreter);
  //      if (pythonSdk == null) {
  //        final ProjectJdkTable jdkTable = ProjectJdkTable.getInstance();
  //        pythonSdk = jdkTable.createSdk(PathUtil.getFileName(interpreter), PythonSdkType.getInstance());
  //        jdkTable.addJdk(pythonSdk);
  //        final SdkModificator modificator = pythonSdk.getSdkModificator();
  //        modificator.setHomePath(interpreter);
  //        modificator.commitChanges();
  //        PythonSdkType.getInstance().setupSdkPaths(pythonSdk);
  //      }
  //      interpreter2sdk.put(interpreterInfo, pythonSdk);
  //    }
  //  }
  //});

  for (DataNode<PythonSetupData> dataNode : toImport) {
    final PythonSetupData pythonSetupData = dataNode.getData();
    final Sdk pythonSdk = interpreter2sdk.get(pythonSetupData.getInterpreterInfo());
    final Module module = modelsProvider.findIdeModule(pythonSetupData.getOwnerModuleData());
    if (module == null) {
      return;
    }
    FacetManager facetManager = FacetManager.getInstance(module);
    PythonFacet facet = facetManager.getFacetByType(PythonFacetType.getInstance().getId());
    if (facet == null) {
      facet = facetManager.createFacet(PythonFacetType.getInstance(), "Python", null);
      facet.getConfiguration().setSdk(pythonSdk);

      final ModifiableFacetModel facetModel = modelsProvider.getModifiableFacetModel(module);
      facetModel.addFacet(facet);
      TestRunnerService.getInstance(module).setProjectConfiguration("py.test");
    }
  }
}
 
Example 15
Source File: ModuleDataService.java    From consulo with Apache License 2.0 4 votes vote down vote up
@RequiredUIAccess
private static void setModuleOptions(@Nonnull final Module module,
                                     @javax.annotation.Nullable final ModifiableRootModel originalModel,
                                     @Nonnull final DataNode<ModuleData> moduleDataNode) {

  ModuleData moduleData = moduleDataNode.getData();
  module.putUserData(MODULE_DATA_KEY, moduleData);

  final ProjectData projectData = moduleDataNode.getData(ProjectKeys.PROJECT);
  if (projectData == null) {
    throw new IllegalArgumentException("projectData is null");
  }

  ModifiableRootModel otherModel = originalModel == null ? ModuleRootManager.getInstance(module).getModifiableModel() : originalModel;

  // remove prev. extension
  ExternalSystemMutableModuleExtension<?> oldExtension = otherModel.getExtension(ExternalSystemMutableModuleExtension.class);
  if (oldExtension != null) {
    oldExtension.setEnabled(false);
    oldExtension.removeAllOptions();
  }

  ExternalSystemMutableModuleExtension<?> newExtension = otherModel.getExtensionWithoutCheck(projectData.getOwner().getId());
  if (newExtension == null) {
    LOG.error("ModuleExtension is not registered for externalSystem: " + projectData.getOwner().getId());
    return;
  }

  newExtension.setEnabled(true);
  newExtension.setOption(ExternalSystemConstants.EXTERNAL_SYSTEM_ID_KEY, moduleData.getOwner().toString());
  newExtension.setOption(ExternalSystemConstants.LINKED_PROJECT_PATH_KEY, moduleData.getLinkedExternalProjectPath());
  newExtension.setOption(ExternalSystemConstants.LINKED_PROJECT_ID_KEY, moduleData.getId());
  if (moduleData.getGroup() != null) {
    newExtension.setOption(ExternalSystemConstants.EXTERNAL_SYSTEM_MODULE_GROUP_KEY, moduleData.getGroup());
  }
  if (moduleData.getVersion() != null) {
    newExtension.setOption(ExternalSystemConstants.EXTERNAL_SYSTEM_MODULE_VERSION_KEY, moduleData.getVersion());
  }

  newExtension.setOption(ExternalSystemConstants.ROOT_PROJECT_PATH_KEY, projectData.getLinkedExternalProjectPath());

  if (originalModel == null) {
    WriteAction.run(otherModel::commit);
  }
}
 
Example 16
Source File: ToolWindowTaskService.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public ExternalConfigPathAware fun(DataNode<TaskData> node) {
  ModuleData moduleData = node.getData(ProjectKeys.MODULE);
  return moduleData == null ? node.getData(ProjectKeys.PROJECT) : moduleData;
}