From 75a53984047dc0f8145a0676baa6a2ec15fa9d7d Mon Sep 17 00:00:00 2001 From: TzeKei Lee Date: Thu, 15 Dec 2016 02:25:47 +0800 Subject: [PATCH 1/2] Allow running whatDependsOn without version --- .../sbt/graph/DependencyGraphSettings.scala | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/scala/net/virtualvoid/sbt/graph/DependencyGraphSettings.scala b/src/main/scala/net/virtualvoid/sbt/graph/DependencyGraphSettings.scala index ab0fd6779..d423548c4 100644 --- a/src/main/scala/net/virtualvoid/sbt/graph/DependencyGraphSettings.scala +++ b/src/main/scala/net/virtualvoid/sbt/graph/DependencyGraphSettings.scala @@ -98,7 +98,10 @@ object DependencyGraphSettings { }, whatDependsOn <<= InputTask(artifactIdParser) { module ⇒ (module, streams, moduleGraph) map { (module, streams, graph) ⇒ - streams.log.info(rendering.AsciiTree.asciiTree(GraphTransformations.reverseGraphStartingAt(graph, module))) + if (module.version.isEmpty) { + val modules = graph.reverseDependencyMap.filter(m ⇒ m._1.copy(version = "") == module).keys + modules.foreach(m ⇒ streams.log.info(rendering.AsciiTree.asciiTree(GraphTransformations.reverseGraphStartingAt(graph, m)))) + } else streams.log.info(rendering.AsciiTree.asciiTree(GraphTransformations.reverseGraphStartingAt(graph, module))) } }, licenseInfo <<= (moduleGraph, streams) map showLicenseInfo)) @@ -188,11 +191,14 @@ object DependencyGraphSettings { import sbt.complete.DefaultParsers._ graph.nodes.map(_.id).map { case id @ ModuleId(org, name, version) ⇒ - (Space ~ token(org) ~ token(Space ~ name) ~ token(Space ~ version)).map(_ ⇒ id) + (Space ~ token(org) ~ token(Space ~ name) ~ token(Space ~ version).?).map(_._2 match { + case Some(_) ⇒ id + case None ⇒ id.copy(version = "") + }) }.reduceOption(_ | _).getOrElse { - (Space ~> token(StringBasic, "organization") ~ Space ~ token(StringBasic, "module") ~ Space ~ token(StringBasic, "version")).map { - case ((((org, _), mod), _), version) ⇒ - ModuleId(org, mod, version) + (Space ~> token(StringBasic, "organization") ~ Space ~ token(StringBasic, "module") ~ (Space ~ token(StringBasic, "version")).?).map { + case (((org, _), mod), version) ⇒ + ModuleId(org, mod, version.map(_._2).getOrElse("")) } } } From 9305521f19155567390f5f747865aaab40684815 Mon Sep 17 00:00:00 2001 From: Johannes Rudolph Date: Mon, 16 Jul 2018 18:48:33 +0200 Subject: [PATCH 2/2] Add test for whatDependsOn --- .../sbt/graph/DependencyGraphKeys.scala | 2 +- .../sbt/graph/DependencyGraphSettings.scala | 14 +++-- .../whatDependsOn/build.sbt | 52 +++++++++++++++++++ .../whatDependsOn/project/plugins.sbt | 1 + .../sbt-dependency-graph/whatDependsOn/test | 3 ++ 5 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 src/sbt-test/sbt-dependency-graph/whatDependsOn/build.sbt create mode 100644 src/sbt-test/sbt-dependency-graph/whatDependsOn/project/plugins.sbt create mode 100644 src/sbt-test/sbt-dependency-graph/whatDependsOn/test diff --git a/src/main/scala/net/virtualvoid/sbt/graph/DependencyGraphKeys.scala b/src/main/scala/net/virtualvoid/sbt/graph/DependencyGraphKeys.scala index ef19a0a6d..fbc6ae440 100644 --- a/src/main/scala/net/virtualvoid/sbt/graph/DependencyGraphKeys.scala +++ b/src/main/scala/net/virtualvoid/sbt/graph/DependencyGraphKeys.scala @@ -93,7 +93,7 @@ trait DependencyGraphKeys { // internal private[graph] val moduleGraphStore = TaskKey[ModuleGraph]("module-graph-store", "The stored module-graph from the last run") - private[graph] val whatDependsOn = InputKey[Unit]("what-depends-on", "Shows information about what depends on the given module") + val whatDependsOn = InputKey[String]("what-depends-on", "Shows information about what depends on the given module") private[graph] val crossProjectId = SettingKey[ModuleID]("dependency-graph-cross-project-id") } diff --git a/src/main/scala/net/virtualvoid/sbt/graph/DependencyGraphSettings.scala b/src/main/scala/net/virtualvoid/sbt/graph/DependencyGraphSettings.scala index 7c53ea49b..10ff06018 100644 --- a/src/main/scala/net/virtualvoid/sbt/graph/DependencyGraphSettings.scala +++ b/src/main/scala/net/virtualvoid/sbt/graph/DependencyGraphSettings.scala @@ -99,10 +99,15 @@ object DependencyGraphSettings { case Some(version) ⇒ ModuleId(org, name, version) :: Nil case None ⇒ graph.nodes.filter(m ⇒ m.id.organisation == org && m.id.name == name).map(_.id) } - modules - .foreach { module ⇒ - streams.value.log.info(rendering.AsciiTree.asciiTree(GraphTransformations.reverseGraphStartingAt(graph, module))) - } + val output = + modules + .map { module ⇒ + rendering.AsciiTree.asciiTree(GraphTransformations.reverseGraphStartingAt(graph, module)) + } + .mkString("\n") + + streams.value.log.info(output) + output }, licenseInfo := showLicenseInfo(moduleGraph.value, streams.value)) ++ AsciiGraph.asciiGraphSetttings) @@ -192,7 +197,6 @@ object DependencyGraphSettings { case ((org, name), version) ⇒ ArtifactPattern(org, name, version) } } - .reduceOption(_ | _).getOrElse(failure("No dependencies found")) } diff --git a/src/sbt-test/sbt-dependency-graph/whatDependsOn/build.sbt b/src/sbt-test/sbt-dependency-graph/whatDependsOn/build.sbt new file mode 100644 index 000000000..19c9108a1 --- /dev/null +++ b/src/sbt-test/sbt-dependency-graph/whatDependsOn/build.sbt @@ -0,0 +1,52 @@ +version := "0.1.0-SNAPSHOT" + +scalaVersion := "2.9.1" + +resolvers += "typesafe maven" at "https://repo.typesafe.com/typesafe/maven-releases/" + +libraryDependencies ++= Seq( + "com.codahale" % "jerkson_2.9.1" % "0.5.0", + "org.codehaus.jackson" % "jackson-mapper-asl" % "1.9.10" // as another version of asl +) + +val check = TaskKey[Unit]("check") + +check := { + def sanitize(str: String): String = str.split('\n').map(_.trim).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]") + + val withVersion = + (whatDependsOn in Compile) + .toTask(" org.codehaus.jackson jackson-core-asl 1.9.11") + .value + val expectedGraphWithVersion = + """org.codehaus.jackson:jackson-core-asl:1.9.11 + | +-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.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] + | """.stripMargin + + checkOutput(withVersion, expectedGraphWithVersion) + + 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] + | + |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(withoutVersion, expectedGraphWithoutVersion) +} diff --git a/src/sbt-test/sbt-dependency-graph/whatDependsOn/project/plugins.sbt b/src/sbt-test/sbt-dependency-graph/whatDependsOn/project/plugins.sbt new file mode 100644 index 000000000..6fdebb6d6 --- /dev/null +++ b/src/sbt-test/sbt-dependency-graph/whatDependsOn/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % sys.props("project.version")) diff --git a/src/sbt-test/sbt-dependency-graph/whatDependsOn/test b/src/sbt-test/sbt-dependency-graph/whatDependsOn/test new file mode 100644 index 000000000..961ba2cf3 --- /dev/null +++ b/src/sbt-test/sbt-dependency-graph/whatDependsOn/test @@ -0,0 +1,3 @@ +# to initialize parser with deps +> compile:moduleGraph +> check \ No newline at end of file