| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package capsule; |
| 22 |
|
| 23 |
import java.io.PrintStream; |
| 24 |
import java.util.ArrayDeque; |
| 25 |
import java.util.Collections; |
| 26 |
import java.util.Deque; |
| 27 |
import java.util.HashMap; |
| 28 |
import java.util.Iterator; |
| 29 |
import java.util.Set; |
| 30 |
|
| 31 |
import org.eclipse.aether.artifact.Artifact; |
| 32 |
import org.eclipse.aether.graph.Dependency; |
| 33 |
import org.eclipse.aether.graph.DependencyNode; |
| 34 |
import org.eclipse.aether.graph.DependencyVisitor; |
| 35 |
import org.eclipse.aether.util.artifact.ArtifactIdUtils; |
| 36 |
import org.eclipse.aether.util.graph.manager.DependencyManagerUtils; |
| 37 |
import org.eclipse.aether.util.graph.transformer.ConflictResolver; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public final class ConsoleDependencyGraphDumper implements DependencyVisitor { |
| 43 |
private final PrintStream out; |
| 44 |
private final Deque<ChildInfo> childInfos = new ArrayDeque<ChildInfo>(); |
| 45 |
private final Set<Artifact> visitedNodes = Collections.newSetFromMap(new HashMap<Artifact, Boolean>(512)); |
| 46 |
|
| 47 |
public ConsoleDependencyGraphDumper(PrintStream out) { |
| 48 |
this.out = out; |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public boolean visitEnter(DependencyNode node) { |
| 53 |
final boolean visited = !visit(node); |
| 54 |
final String nstr = formatNode(node); |
| 55 |
if (nstr != null) |
| 56 |
out.println(formatIndentation() + nstr + (visited ? " (*)" : "")); |
| 57 |
childInfos.add(new ChildInfo(node.getChildren().size())); |
| 58 |
return !visited; |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public boolean visitLeave(DependencyNode node) { |
| 63 |
if (!childInfos.isEmpty()) |
| 64 |
childInfos.removeLast(); |
| 65 |
if (!childInfos.isEmpty()) |
| 66 |
childInfos.getLast().index++; |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
private boolean visit(DependencyNode node) { |
| 71 |
return visitedNodes.add(node.getArtifact()); |
| 72 |
} |
| 73 |
|
| 74 |
private String formatIndentation() { |
| 75 |
final StringBuilder buffer = new StringBuilder(128); |
| 76 |
for (Iterator<ChildInfo> it = childInfos.iterator(); it.hasNext();) |
| 77 |
buffer.append(it.next().formatIndentation(!it.hasNext())); |
| 78 |
|
| 79 |
return buffer.toString(); |
| 80 |
} |
| 81 |
|
| 82 |
private String formatNode(DependencyNode node) { |
| 83 |
final Artifact a = node.getArtifact(); |
| 84 |
if (a == null) |
| 85 |
return null; |
| 86 |
|
| 87 |
final StringBuilder buffer = new StringBuilder(128); |
| 88 |
buffer.append(toString(a)); |
| 89 |
|
| 90 |
final Dependency d = node.getDependency(); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
final String premanagedVersion = DependencyManagerUtils.getPremanagedVersion(node); |
| 100 |
if (premanagedVersion != null && !premanagedVersion.equals(a.getBaseVersion())) |
| 101 |
buffer.append(" (version managed from ").append(premanagedVersion).append(")"); |
| 102 |
|
| 103 |
final String premanagedScope = DependencyManagerUtils.getPremanagedScope(node); |
| 104 |
if (premanagedScope != null && !premanagedScope.equals(d.getScope())) |
| 105 |
buffer.append(" (scope managed from ").append(premanagedScope).append(")"); |
| 106 |
|
| 107 |
final DependencyNode winner = (DependencyNode) node.getData().get(ConflictResolver.NODE_DATA_WINNER); |
| 108 |
if (winner != null && !ArtifactIdUtils.equalsId(a, winner.getArtifact())) { |
| 109 |
Artifact w = winner.getArtifact(); |
| 110 |
buffer.append(" -> "); |
| 111 |
if (ArtifactIdUtils.toVersionlessId(a).equals(ArtifactIdUtils.toVersionlessId(w))) |
| 112 |
buffer.append(w.getVersion()); |
| 113 |
else |
| 114 |
buffer.append(w); |
| 115 |
} |
| 116 |
return buffer.toString(); |
| 117 |
} |
| 118 |
|
| 119 |
private String toString(Artifact a) { |
| 120 |
return a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion(); |
| 121 |
} |
| 122 |
|
| 123 |
private static class ChildInfo { |
| 124 |
final int count; |
| 125 |
int index; |
| 126 |
|
| 127 |
public ChildInfo(int count) { |
| 128 |
this.count = count; |
| 129 |
} |
| 130 |
|
| 131 |
public String formatIndentation(boolean end) { |
| 132 |
final boolean last = index + 1 >= count; |
| 133 |
if (end) |
| 134 |
return last ? "\\--- " : "+--- "; |
| 135 |
else |
| 136 |
return last ? " " : "| "; |
| 137 |
} |
| 138 |
} |
| 139 |
} |