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

The following examples show how to use com.intellij.openapi.externalSystem.model.DataNode#createChild() . 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: 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 2
Source File: PantsSourceRootsExtension.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
private void createContentRoots(@NotNull DataNode<ModuleData> moduleDataNode, @NotNull final TargetInfo targetInfo) {
  final Set<ContentRoot> roots = targetInfo.getRoots();
  if (roots.isEmpty()) {
    return;
  }

  for (String baseRoot : findBaseRoots(targetInfo, roots)) {
    final ContentRootData contentRoot = new ContentRootData(PantsConstants.SYSTEM_ID, baseRoot);
    moduleDataNode.createChild(ProjectKeys.CONTENT_ROOT, contentRoot);

    for (ContentRoot sourceRoot : roots) {
      final String sourceRootPathToAdd = getSourceRootRegardingTargetType(targetInfo, sourceRoot);
      if (FileUtil.isAncestor(baseRoot, sourceRootPathToAdd, false)) {
        try {
          contentRoot.storePath(
            targetInfo.getSourcesType().toExternalSystemSourceType(),
            sourceRootPathToAdd,
            doNotSupportPackagePrefixes(targetInfo) ? null : sourceRoot.getPackagePrefix()
          );
        }
        catch (IllegalArgumentException e) {
          LOG.warn(e);
          // todo(fkorotkov): log and investigate exceptions from ContentRootData.storePath(ContentRootData.java:94)
        }
      }
    }
  }
}
 
Example 3
Source File: PantsCreateModulesExtension.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
private DataNode<ModuleData> createModuleData(
  @NotNull DataNode<ProjectData> projectInfoDataNode,
  @NotNull String targetName,
  @NotNull TargetInfo targetInfo,
  @NotNull PantsCompileOptionsExecutor executor
) {
  final String moduleName = PantsUtil.getCanonicalModuleName(targetName);

  final ModuleData moduleData = new ModuleData(
    targetName,
    PantsConstants.SYSTEM_ID,
    ModuleTypeId.JAVA_MODULE,
    moduleName,
    projectInfoDataNode.getData().getIdeProjectFileDirectoryPath() + "/" + moduleName,
    new File(executor.getBuildRoot(), targetName).getAbsolutePath()
  );

  final DataNode<ModuleData> moduleDataNode = projectInfoDataNode.createChild(ProjectKeys.MODULE, moduleData);

  DataNode<ProjectSdkData> sdk = ExternalSystemApiUtil.find(projectInfoDataNode, ProjectSdkData.KEY);
  if(sdk != null){
    ModuleSdkData moduleSdk = new ModuleSdkData(sdk.getData().getSdkName());
    moduleDataNode.createChild(ModuleSdkData.KEY, moduleSdk);
  }

  final TargetMetadata metadata = new TargetMetadata(PantsConstants.SYSTEM_ID, moduleName);
  metadata.setTargetAddresses(ContainerUtil.map(targetInfo.getAddressInfos(), TargetAddressInfo::getTargetAddress));
  metadata.setTargetAddressInfoSet(targetInfo.getAddressInfos());
  metadata.setLibraryExcludes(targetInfo.getExcludes());
  moduleDataNode.createChild(TargetMetadata.KEY, metadata);

  return moduleDataNode;
}
 
Example 4
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 5
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 6
Source File: PythonRequirementsResolver.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
private DataNode<ModuleData> createRequirementsModule(
  @NotNull DataNode<ProjectData> projectDataNode,
  @NotNull PythonSetup pythonSetup,
  @NotNull PantsCompileOptionsExecutor executor
) {
  final String moduleName = "python_requirements";
  final ModuleData moduleData = new ModuleData(
    moduleName,
    PantsConstants.SYSTEM_ID,
    ModuleTypeId.JAVA_MODULE,
    moduleName,
    projectDataNode.getData().getIdeProjectFileDirectoryPath() + "/" + moduleName,
    new File(executor.getBuildRoot(), moduleName).getAbsolutePath()
  );

  final DataNode<ModuleData> moduleDataNode = projectDataNode.createChild(ProjectKeys.MODULE, moduleData);

  final File chroot = new File(pythonSetup.getDefaultInterpreterInfo().getChroot());
  for (File dep : FileUtil.notNullize(new File(chroot, ".deps").listFiles())) {
    if (!dep.isDirectory()) {
      continue;
    }
    final ContentRootData contentRoot = new ContentRootData(PantsConstants.SYSTEM_ID, dep.getAbsolutePath());
    contentRoot.storePath(ExternalSystemSourceType.SOURCE, dep.getAbsolutePath());
    moduleDataNode.createChild(ProjectKeys.CONTENT_ROOT, contentRoot);
  }

  return moduleDataNode;
}
 
Example 7
Source File: PantsSystemProjectResolver.java    From intellij-pants-plugin with Apache License 2.0 4 votes vote down vote up
@NotNull
private DataNode<ProjectData> resolveProjectInfoImpl(
  @NotNull ExternalSystemTaskId id,
  @NotNull final PantsCompileOptionsExecutor executor,
  @NotNull ExternalSystemTaskNotificationListener listener,
  @NotNull PantsExecutionSettings settings,
  boolean isPreviewMode
) throws ExternalSystemException, IllegalArgumentException, IllegalStateException {
  String projectName = settings.getProjectName().orElseGet(executor::getDefaultProjectName);
  Path projectPath = Paths.get(
    executor.getBuildRoot().getPath(),
    ".idea",
    "pants-projects",
    // Use a timestamp hash to avoid module creation dead lock
    // when overlapping targets were imported into multiple projects
    // from the same Pants repo.
    DigestUtils.sha1Hex(Long.toString(System.currentTimeMillis())),
    executor.getProjectRelativePath()
  );

  final ProjectData projectData = new ProjectData(
    PantsConstants.SYSTEM_ID,
    projectName,
    projectPath.toString(),
    executor.getProjectPath()
  );
  final DataNode<ProjectData> projectDataNode = new DataNode<>(ProjectKeys.PROJECT, projectData, null);

  PantsUtil.findPantsExecutable(executor.getProjectPath())
    .flatMap(file -> PantsSdkUtil.getDefaultJavaSdk(file.getPath(), null))
    .map(sdk -> new ProjectSdkData(sdk.getName()))
    .ifPresent(sdk -> projectDataNode.createChild(ProjectSdkData.KEY, sdk));

  if (!isPreviewMode) {
    PantsExternalMetricsListenerManager.getInstance().logIsIncrementalImport(settings.incrementalImportDepth().isPresent());
    resolveUsingPantsGoal(id, executor, listener, projectDataNode);

    if (!containsContentRoot(projectDataNode, executor.getProjectDir())) {
      // Add a module with content root as import project directory path.
      // This will allow all the files in the imported project directory will be indexed by the plugin.
      final String moduleName = executor.getRootModuleName();
      final ModuleData moduleData = new ModuleData(
        PantsConstants.PANTS_PROJECT_MODULE_ID_PREFIX + moduleName,
        PantsConstants.SYSTEM_ID,
        ModuleTypeId.JAVA_MODULE,
        moduleName + PantsConstants.PANTS_PROJECT_MODULE_SUFFIX,
        projectData.getIdeProjectFileDirectoryPath() + "/" + moduleName,
        executor.getProjectPath()
      );
      final DataNode<ModuleData> moduleDataNode = projectDataNode.createChild(ProjectKeys.MODULE, moduleData);
      final ContentRootData contentRoot = new ContentRootData(PantsConstants.SYSTEM_ID, executor.getProjectDir());
      if (FileUtil.filesEqual(executor.getBuildRoot(), new File(executor.getProjectPath()))) {
        contentRoot.storePath(ExternalSystemSourceType.EXCLUDED, executor.getBuildRoot().getPath() + "/.idea");
      }
      moduleDataNode.createChild(ProjectKeys.CONTENT_ROOT, contentRoot);
    }
  }

  return projectDataNode;
}
 
Example 8
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);
  }
}