Skip to content

Commit b4a7f12

Browse files
Return dependency list in Fetch results (#112)
1 parent d5eb4d2 commit b4a7f12

File tree

3 files changed

+55
-17
lines changed

3 files changed

+55
-17
lines changed

interface-test/src/test/java/coursierapi/test/FetchTests.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import coursierapi.Dependency;
44
import coursierapi.Fetch;
5+
import coursierapi.FetchResult;
56
import coursierapi.error.CoursierError;
67
import org.junit.Test;
78
import static org.junit.Assert.*;
@@ -19,21 +20,27 @@ public void simple() {
1920
Fetch fetch = Fetch.create()
2021
.addDependencies(dep);
2122

22-
List<File> files;
23+
FetchResult result;
2324
try {
24-
files = fetch.fetch();
25+
result = fetch.fetchResult();
2526
} catch (CoursierError e) {
2627
throw new RuntimeException(e);
2728
}
2829

2930
Set<String> fileNames = new HashSet<>();
30-
for (File f : files) {
31+
for (File f : result.getFiles()) {
3132
fileNames.add(f.getName());
3233
}
3334

3435
Set<String> expectedFileNames = new HashSet<>(Arrays.asList("shapeless_2.13-2.3.3.jar", "scala-library-2.13.0.jar"));
3536

37+
List<Dependency> expectedDependencies = new ArrayList<>(Arrays.asList(
38+
Dependency.of("com.chuusai", "shapeless_2.13", "2.3.3").withConfiguration("default"),
39+
Dependency.of("org.scala-lang", "scala-library", "2.13.0").withConfiguration("default")
40+
));
41+
3642
assertEquals(expectedFileNames, fileNames);
43+
assertEquals(expectedDependencies, result.getDependencies());
3744

3845
}
3946

interface/src/main/java/coursierapi/FetchResult.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@
1010
public final class FetchResult implements Serializable {
1111

1212
private List<Map.Entry<Artifact, File>> artifacts;
13+
private List<Dependency> dependencies;
1314

14-
private FetchResult(List<Map.Entry<Artifact, File>> artifacts) {
15+
private FetchResult(List<Map.Entry<Artifact, File>> artifacts, List<Dependency> dependencies) {
1516
this.artifacts = Collections.unmodifiableList(new ArrayList<>(artifacts));
17+
this.dependencies = Collections.unmodifiableList(new ArrayList<>(dependencies));
1618
}
1719

1820
public static FetchResult of(List<Map.Entry<Artifact, File>> artifacts) {
19-
return new FetchResult(artifacts);
21+
return new FetchResult(artifacts, Collections.emptyList());
22+
}
23+
24+
public static FetchResult of(List<Map.Entry<Artifact, File>> artifacts, List<Dependency> dependencies) {
25+
return new FetchResult(artifacts, dependencies);
2026
}
2127

2228
@Override
@@ -25,19 +31,20 @@ public boolean equals(Object obj) {
2531
return true;
2632
if (obj instanceof FetchResult) {
2733
FetchResult other = (FetchResult) obj;
28-
return this.artifacts.equals(other.artifacts);
34+
return this.artifacts.equals(other.artifacts) &&
35+
this.dependencies.equals(other.dependencies);
2936
}
3037
return false;
3138
}
3239

3340
@Override
3441
public int hashCode() {
35-
return 37 * (17 + artifacts.hashCode());
42+
return 37 * (37 * (17 + artifacts.hashCode())) + dependencies.hashCode();
3643
}
3744

3845
@Override
3946
public String toString() {
40-
StringBuilder b = new StringBuilder("FetchResult(");
47+
StringBuilder b = new StringBuilder("FetchResult(artifacts=[");
4148
boolean isFirst = true;
4249
for (Map.Entry<Artifact, File> entry : artifacts) {
4350
if (isFirst) {
@@ -50,7 +57,20 @@ public String toString() {
5057
b.append(": ");
5158
b.append(entry.getValue().toString());
5259
}
53-
b.append(")");
60+
b.append("], dependencies=[");
61+
62+
isFirst = true;
63+
for (Dependency dependency : dependencies) {
64+
if (isFirst) {
65+
isFirst = false;
66+
} else {
67+
b.append(", ");
68+
}
69+
70+
b.append(dependency.toString());
71+
}
72+
b.append("])");
73+
5474
return b.toString();
5575
}
5676

@@ -65,4 +85,8 @@ public List<File> getFiles() {
6585
}
6686
return l;
6787
}
88+
89+
public List<Dependency> getDependencies() {
90+
return dependencies;
91+
}
6892
}

interface/src/main/scala/coursier/internal/api/ApiHelper.scala

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,10 @@ object ApiHelper {
331331
val fetch0 = fetch(apiFetch)
332332
val either =
333333
if (apiFetch.getFetchCacheIKnowWhatImDoing == null)
334-
fetch0.eitherResult().map(_.artifacts)
334+
fetch0.eitherResult()
335335
else {
336-
val dummyArtifact = coursier.util.Artifact("", Map(), Map(), false, false, None)
337-
fetch0.either().map(_.map((dummyArtifact, _)))
336+
val dummyArtifact = coursier.util.Artifact("", Map(), Map(), changing = false, optional = false, None)
337+
fetch0.either().map(files => Fetch.Result().withExtraArtifacts(files.map((dummyArtifact, _))))
338338
}
339339

340340
// TODO Pass exception causes if any
@@ -366,16 +366,23 @@ object ApiHelper {
366366

367367
throw ex
368368

369-
case Right(artifacts) =>
370-
val l = new ju.ArrayList[ju.Map.Entry[coursierapi.Artifact, File]]
371-
for ((a, f) <- artifacts) {
369+
case Right(result) =>
370+
val artifactFiles = new ju.ArrayList[ju.Map.Entry[coursierapi.Artifact, File]]
371+
for ((a, f) <- result.artifacts) {
372372
val credentials0 = a.authentication.map(credentials).orNull
373373
val a0 = coursierapi.Artifact.of(a.url, a.changing, a.optional, credentials0)
374374
val ent = new ju.AbstractMap.SimpleEntry(a0, f)
375-
l.add(ent)
375+
artifactFiles.add(ent)
376376
}
377377

378-
coursierapi.FetchResult.of(l)
378+
val deps = new ju.ArrayList[coursierapi.Dependency]
379+
result
380+
.resolution
381+
.orderedDependencies
382+
.map(dependency)
383+
.foreach(deps.add)
384+
385+
coursierapi.FetchResult.of(artifactFiles, deps)
379386
}
380387
}
381388

0 commit comments

Comments
 (0)