Skip to content

Commit

Permalink
Fix whatDependsOn to show only original dependencies (not dependency …
Browse files Browse the repository at this point in the history
…after evictions).

Fixes sbt#109
  • Loading branch information
jatcwang committed Dec 27, 2019
1 parent 221e5fb commit ad4c40a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package net.virtualvoid.sbt.graph

object GraphTransformations {
def reverseGraphStartingAt(graph: ModuleGraph, root: ModuleId): ModuleGraph = {
val deps = graph.reverseDependencyMap
val deps = graph.reverseOriginalDependencyMap

def visit(module: ModuleId, visited: Set[ModuleId]): Seq[(ModuleId, ModuleId)] =
if (visited(module))
Expand Down
39 changes: 35 additions & 4 deletions src/main/scala/net/virtualvoid/sbt/graph/model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,45 @@ case class ModuleGraph(nodes: Seq[Module], edges: Seq[Edge]) {
def module(id: ModuleId): Module = modules(id)

lazy val dependencyMap: Map[ModuleId, Seq[Module]] =
createMap(identity)
createMap(identity, onlyOriginalDependencies = false)

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

def createMap(bindingFor: ((ModuleId, ModuleId)) (ModuleId, ModuleId)): Map[ModuleId, Seq[Module]] = {
lazy val reverseOriginalDependencyMap: Map[ModuleId, Seq[Module]] =
createMap(_.swap, onlyOriginalDependencies = true)

/**
* @param onlyOriginalDependencies Keep only dependency edges that are original,
* i.e. dependency relationships established due to evictions are ignored
*/
def createMap(
bindingFor: ((ModuleId, ModuleId)) (ModuleId, ModuleId),
onlyOriginalDependencies: Boolean): Map[ModuleId, Seq[Module]] = {
val m = new HashMap[ModuleId, Set[Module]] with MultiMap[ModuleId, Module]
edges.foreach { entry
val relevantEdges =
if (onlyOriginalDependencies) {
edges
.groupBy {
case (dependant, dependency)
(
dependant.organisation,
dependant.name,
dependency.organisation,
dependency.name)
}
.mapValues {
case Seq(edge) edge
case edgeGroup @ Seq(_, _) edgeGroup.find { case (_, dependency) module(dependency).isEvicted }
.getOrElse(throw new IllegalStateException("Expecting 1 dependency edge to be evicted but got none. " +
"This is probably a bug in sbt-dependency-graph"))
case _ throw new IllegalStateException("Expecting dependency graph's edge to be of length 1 or 2 when " +
"grouped by organization/name. This is probably a bug in sbt-dependency-graph")
}
.values
.toSeq
} else edges
relevantEdges.foreach { entry
val (f, t) = bindingFor(entry)
m.addBinding(f, module(t))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ libraryDependencies ++= Seq(
val check = TaskKey[Unit]("check")

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

Expand All @@ -31,26 +34,33 @@ check := {
| |
| +-org.codehaus.jackson:jackson-mapper-asl:1.9.11
| +-com.codahale:jerkson_2.9.1:0.5.0 [S]
| | +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
| |
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
| """.stripMargin

checkOutput(withVersion, expectedGraphWithVersion)

val withEvictedVersion =
(whatDependsOn in Compile)
.toTask(" org.codehaus.jackson jackson-mapper-asl 1.9.10")
.value
val expectedGraphWithEvictedVersion =
"""org.codehaus.jackson:jackson-mapper-asl:1.9.10 (evicted by: 1.9.11)
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
""".stripMargin

checkOutput(withEvictedVersion, expectedGraphWithEvictedVersion)

val withoutVersion =
(whatDependsOn in Compile)
.toTask(" org.codehaus.jackson jackson-mapper-asl")
.value
val expectedGraphWithoutVersion =
"""org.codehaus.jackson:jackson-mapper-asl:1.9.11
| +-com.codahale:jerkson_2.9.1:0.5.0 [S]
| | +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
| |
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
| +-com.codahale:jerkson_2.9.1:0.5.0 [S]
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
|
|org.codehaus.jackson:jackson-mapper-asl:1.9.10 (evicted by: 1.9.11)
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
| """.stripMargin
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
""".stripMargin
checkOutput(withoutVersion, expectedGraphWithoutVersion)
}
28 changes: 19 additions & 9 deletions src/sbt-test/sbt-dependency-graph/whatDependsOn/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ libraryDependencies ++= Seq(
val check = TaskKey[Unit]("check")

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

Expand All @@ -27,26 +30,33 @@ check := {
| |
| +-org.codehaus.jackson:jackson-mapper-asl:1.9.11
| +-com.codahale:jerkson_2.9.1:0.5.0 [S]
| | +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
| |
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
| """.stripMargin

checkOutput(withVersion, expectedGraphWithVersion)

val withEvictedVersion =
(whatDependsOn in Compile)
.toTask(" org.codehaus.jackson jackson-mapper-asl 1.9.10")
.value
val expectedGraphWithEvictedVersion =
"""org.codehaus.jackson:jackson-mapper-asl:1.9.10 (evicted by: 1.9.11)
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
""".stripMargin

checkOutput(withEvictedVersion, expectedGraphWithEvictedVersion)

val withoutVersion =
(whatDependsOn in Compile)
.toTask(" org.codehaus.jackson jackson-mapper-asl")
.value
val expectedGraphWithoutVersion =
"""org.codehaus.jackson:jackson-mapper-asl:1.9.11
| +-com.codahale:jerkson_2.9.1:0.5.0 [S]
| | +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
| |
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
| +-com.codahale:jerkson_2.9.1:0.5.0 [S]
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
|
|org.codehaus.jackson:jackson-mapper-asl:1.9.10 (evicted by: 1.9.11)
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
| +-default:whatdependson_2.9.1:0.1.0-SNAPSHOT [S]
| """.stripMargin
checkOutput(withoutVersion, expectedGraphWithoutVersion)
}

0 comments on commit ad4c40a

Please sign in to comment.