Debian Codegraph @

src/main/java/capsule/ConsoleDependencyGraphDumper.java

raw symbols SCIP
1 /*
2 * Capsule
3 * Copyright (c) 2014-2015, Parallel Universe Software Co. All rights reserved.
4 *
5 * This program and the accompanying materials are licensed under the terms
6 * of the Eclipse Public License v1.0, available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 */
9 /*
10 * *****************************************************************************
11 * Copyright (c) 2010, 2013 Sonatype, Inc.
12 * All rights reserved. This program and the accompanying materials
13 * are made available under the terms of the Eclipse Public License v1.0
14 * which accompanies this distribution, and is available at
15 * http://www.eclipse.org/legal/epl-v10.html
16 *
17 * Contributors:
18 * Sonatype, Inc. - initial API and implementation
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 * A dependency visitor that dumps the graph to the console.
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)); // new IdentityHashMap<DependencyNode, Boolean>
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 // if (d != null && d.getScope().length() > 0) {
93 // buffer.append(" [").append(d.getScope());
94 // if (d.isOptional())
95 // buffer.append(", optional");
96 // buffer.append("]");
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 }