public static Map downloadSources()

in plugin/src/main/java/com/uber/okbuck/core/util/ProjectUtil.java [166:220]


  public static Map<ComponentIdentifier, ResolvedArtifactResult> downloadSources(
      Project project, Set<ResolvedArtifactResult> artifacts) {

    // Download sources if enabled via intellij extension
    if (!ProjectUtil.getOkBuckExtension(project).getIntellijExtension().downloadSources()) {
      return new HashMap<>();
    }

    DependencyHandler dependencies = project.getDependencies();

    try {
      Set<ComponentIdentifier> identifiers =
          artifacts
              .stream()
              .filter(artifact -> canHaveSources(artifact.getFile()))
              .map(artifact -> artifact.getId().getComponentIdentifier())
              .collect(Collectors.toSet());

      @SuppressWarnings("unchecked")
      Class<? extends Artifact>[] artifactTypesArray =
          (Class<? extends Artifact>[]) new Class<?>[] {SourcesArtifact.class};

      Set<ComponentArtifactsResult> results =
          dependencies
              .createArtifactResolutionQuery()
              .forComponents(identifiers)
              .withArtifacts(JvmLibrary.class, artifactTypesArray)
              .execute()
              .getResolvedComponents();

      // Only has one type.
      Class<? extends Artifact> type = artifactTypesArray[0];

      return results
          .stream()
          .map(artifactsResult -> artifactsResult.getArtifacts(type))
          .flatMap(Set::stream)
          .filter(artifactResult -> artifactResult instanceof ResolvedArtifactResult)
          .map(artifactResult -> (ResolvedArtifactResult) artifactResult)
          .filter(artifactResult -> FileUtil.isZipFile(artifactResult.getFile()))
          .collect(
              Collectors.toMap(
                  resolvedArtifact -> resolvedArtifact.getId().getComponentIdentifier(),
                  resolvedArtifact -> resolvedArtifact));

    } catch (Throwable t) {
      System.out.println(
          "Unable to download sources for project "
              + project.toString()
              + " with error "
              + t.toString());

      return new HashMap<>();
    }
  }