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

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 #109
  • Loading branch information
jatcwang committed Dec 27, 2019
1 parent 221e5fb commit 2adedf3
Show file tree
Hide file tree
Showing 4 changed files with 71 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
35 changes: 31 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,41 @@ 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 { edgeGroup
edgeGroup.find { case (_, dependency) module(dependency).isEvicted }
.getOrElse(edgeGroup.head)
}
.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 2adedf3

Please sign in to comment.