Skip to content
This repository was archived by the owner on Apr 8, 2021. It is now read-only.

Commit 2adedf3

Browse files
committed
Fix whatDependsOn to show only original dependencies (not dependency after evictions).
Fixes #109
1 parent 221e5fb commit 2adedf3

File tree

4 files changed

+71
-24
lines changed

4 files changed

+71
-24
lines changed

src/main/scala/net/virtualvoid/sbt/graph/GraphTransformations.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package net.virtualvoid.sbt.graph
1818

1919
object GraphTransformations {
2020
def reverseGraphStartingAt(graph: ModuleGraph, root: ModuleId): ModuleGraph = {
21-
val deps = graph.reverseDependencyMap
21+
val deps = graph.reverseOriginalDependencyMap
2222

2323
def visit(module: ModuleId, visited: Set[ModuleId]): Seq[(ModuleId, ModuleId)] =
2424
if (visited(module))

src/main/scala/net/virtualvoid/sbt/graph/model.scala

+31-4
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,41 @@ case class ModuleGraph(nodes: Seq[Module], edges: Seq[Edge]) {
5151
def module(id: ModuleId): Module = modules(id)
5252

5353
lazy val dependencyMap: Map[ModuleId, Seq[Module]] =
54-
createMap(identity)
54+
createMap(identity, onlyOriginalDependencies = false)
5555

5656
lazy val reverseDependencyMap: Map[ModuleId, Seq[Module]] =
57-
createMap { case (a, b) (b, a) }
57+
createMap(_.swap, onlyOriginalDependencies = false)
5858

59-
def createMap(bindingFor: ((ModuleId, ModuleId)) (ModuleId, ModuleId)): Map[ModuleId, Seq[Module]] = {
59+
lazy val reverseOriginalDependencyMap: Map[ModuleId, Seq[Module]] =
60+
createMap(_.swap, onlyOriginalDependencies = true)
61+
62+
/**
63+
* @param onlyOriginalDependencies Keep only dependency edges that are original,
64+
* i.e. dependency relationships established due to evictions are ignored
65+
*/
66+
def createMap(
67+
bindingFor: ((ModuleId, ModuleId)) (ModuleId, ModuleId),
68+
onlyOriginalDependencies: Boolean): Map[ModuleId, Seq[Module]] = {
6069
val m = new HashMap[ModuleId, Set[Module]] with MultiMap[ModuleId, Module]
61-
edges.foreach { entry
70+
val relevantEdges =
71+
if (onlyOriginalDependencies) {
72+
edges
73+
.groupBy {
74+
case (dependant, dependency)
75+
(
76+
dependant.organisation,
77+
dependant.name,
78+
dependency.organisation,
79+
dependency.name)
80+
}
81+
.mapValues { edgeGroup
82+
edgeGroup.find { case (_, dependency) module(dependency).isEvicted }
83+
.getOrElse(edgeGroup.head)
84+
}
85+
.values
86+
.toSeq
87+
} else edges
88+
relevantEdges.foreach { entry
6289
val (f, t) = bindingFor(entry)
6390
m.addBinding(f, module(t))
6491
}

src/sbt-test/sbt-dependency-graph/whatDependsOn-without-previous-initialization/build.sbt

+20-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ libraryDependencies ++= Seq(
1616
val check = TaskKey[Unit]("check")
1717

1818
check := {
19-
def sanitize(str: String): String = str.split('\n').map(_.trim).mkString("\n")
19+
def sanitize(str: String): String = {
20+
def trimRight(s: String) = s.replaceAll("""\s*$""", "")
21+
str.split('\n').map(trimRight).mkString("\n")
22+
}
2023
def checkOutput(output: String, expected: String): Unit =
2124
require(sanitize(expected) == sanitize(output), s"Tree should have been [\n${sanitize(expected)}\n] but was [\n${sanitize(output)}\n]")
2225

@@ -31,26 +34,33 @@ check := {
3134
| |
3235
| +-org.codehaus.jackson:jackson-mapper-asl:1.9.11
3336
| +-com.codahale:jerkson_2.9.1:0.5.0 [S]
34-
| | +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
35-
| |
36-
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
37+
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
3738
| """.stripMargin
3839

3940
checkOutput(withVersion, expectedGraphWithVersion)
4041

42+
val withEvictedVersion =
43+
(whatDependsOn in Compile)
44+
.toTask(" org.codehaus.jackson jackson-mapper-asl 1.9.10")
45+
.value
46+
val expectedGraphWithEvictedVersion =
47+
"""org.codehaus.jackson:jackson-mapper-asl:1.9.10 (evicted by: 1.9.11)
48+
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
49+
""".stripMargin
50+
51+
checkOutput(withEvictedVersion, expectedGraphWithEvictedVersion)
52+
4153
val withoutVersion =
4254
(whatDependsOn in Compile)
4355
.toTask(" org.codehaus.jackson jackson-mapper-asl")
4456
.value
4557
val expectedGraphWithoutVersion =
4658
"""org.codehaus.jackson:jackson-mapper-asl:1.9.11
47-
| +-com.codahale:jerkson_2.9.1:0.5.0 [S]
48-
| | +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
49-
| |
50-
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
59+
| +-com.codahale:jerkson_2.9.1:0.5.0 [S]
60+
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
5161
|
5262
|org.codehaus.jackson:jackson-mapper-asl:1.9.10 (evicted by: 1.9.11)
53-
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
54-
| """.stripMargin
63+
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
64+
""".stripMargin
5565
checkOutput(withoutVersion, expectedGraphWithoutVersion)
5666
}

src/sbt-test/sbt-dependency-graph/whatDependsOn/build.sbt

+19-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ libraryDependencies ++= Seq(
1212
val check = TaskKey[Unit]("check")
1313

1414
check := {
15-
def sanitize(str: String): String = str.split('\n').map(_.trim).mkString("\n")
15+
def sanitize(str: String): String = {
16+
def trimRight(s: String) = s.replaceAll("""\s*$""", "")
17+
str.split('\n').map(trimRight).mkString("\n")
18+
}
1619
def checkOutput(output: String, expected: String): Unit =
1720
require(sanitize(expected) == sanitize(output), s"Tree should have been [\n${sanitize(expected)}\n] but was [\n${sanitize(output)}\n]")
1821

@@ -27,26 +30,33 @@ check := {
2730
| |
2831
| +-org.codehaus.jackson:jackson-mapper-asl:1.9.11
2932
| +-com.codahale:jerkson_2.9.1:0.5.0 [S]
30-
| | +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
31-
| |
32-
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
33+
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
3334
| """.stripMargin
3435

3536
checkOutput(withVersion, expectedGraphWithVersion)
3637

38+
val withEvictedVersion =
39+
(whatDependsOn in Compile)
40+
.toTask(" org.codehaus.jackson jackson-mapper-asl 1.9.10")
41+
.value
42+
val expectedGraphWithEvictedVersion =
43+
"""org.codehaus.jackson:jackson-mapper-asl:1.9.10 (evicted by: 1.9.11)
44+
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
45+
""".stripMargin
46+
47+
checkOutput(withEvictedVersion, expectedGraphWithEvictedVersion)
48+
3749
val withoutVersion =
3850
(whatDependsOn in Compile)
3951
.toTask(" org.codehaus.jackson jackson-mapper-asl")
4052
.value
4153
val expectedGraphWithoutVersion =
4254
"""org.codehaus.jackson:jackson-mapper-asl:1.9.11
43-
| +-com.codahale:jerkson_2.9.1:0.5.0 [S]
44-
| | +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
45-
| |
46-
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
55+
| +-com.codahale:jerkson_2.9.1:0.5.0 [S]
56+
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
4757
|
4858
|org.codehaus.jackson:jackson-mapper-asl:1.9.10 (evicted by: 1.9.11)
49-
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
59+
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
5060
| """.stripMargin
5161
checkOutput(withoutVersion, expectedGraphWithoutVersion)
5262
}

0 commit comments

Comments
 (0)