From d3cd49bd739e5199d4b6ac14b811367f9f597931 Mon Sep 17 00:00:00 2001 From: Matt Russell Date: Tue, 26 Jun 2012 08:06:06 +0100 Subject: [PATCH] Add support for ASCII-art dependency graphs --- build.sbt | 2 +- project.sbt | 7 ++++++- .../sbt/graph/IvyGraphMLDependencies.scala | 20 +++++++++++++++++-- .../net/virtualvoid/sbt/graph/Plugin.scala | 15 +++++++++++++- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/build.sbt b/build.sbt index bd07b2865..4518fef11 100644 --- a/build.sbt +++ b/build.sbt @@ -1,3 +1,3 @@ seq(lsSettings :_*) -CrossBuilding.crossSbtVersions := Seq("0.11.1", "0.11.2", "0.11.3", "0.12.0-Beta2") \ No newline at end of file +CrossBuilding.crossSbtVersions := Seq("0.11.1", "0.11.2", "0.11.3", "0.12.0-Beta2") diff --git a/project.sbt b/project.sbt index e38108c21..751b4e9ae 100644 --- a/project.sbt +++ b/project.sbt @@ -4,7 +4,7 @@ name := "sbt-dependency-graph" organization := "net.virtual-void" -version := "0.6.0" +version := "0.6.1-SNAPSHOT" homepage := Some(url("http://github.com/jrudolph/sbt-dependency-graph")) @@ -16,3 +16,8 @@ licenses in GlobalScope += "Apache License 2.0" -> url("https://github.com/jrudo (description in LsKeys.lsync) := "An sbt plugin to visualize dependencies of your build." + +libraryDependencies += "com.github.mdr" %% "ascii-graphs" % "0.0.1-SNAPSHOT" + +resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots" + diff --git a/src/main/scala/net/virtualvoid/sbt/graph/IvyGraphMLDependencies.scala b/src/main/scala/net/virtualvoid/sbt/graph/IvyGraphMLDependencies.scala index 95dc2b1f5..404ff5501 100644 --- a/src/main/scala/net/virtualvoid/sbt/graph/IvyGraphMLDependencies.scala +++ b/src/main/scala/net/virtualvoid/sbt/graph/IvyGraphMLDependencies.scala @@ -23,6 +23,8 @@ import collection.mutable.MultiMap import collection.mutable.{Set => MSet} import sbt.Graph import xml.{Document, XML, Node} +import com.github.mdr.ascii.layout +import com.github.mdr.ascii.layout._ object IvyGraphMLDependencies extends App { case class Module(organisation: String, name: String, version: String) { @@ -44,8 +46,22 @@ object IvyGraphMLDependencies extends App { ModuleGraph(nodes, edges) } - - def ascii(ivyReportFile: String): String = { + private def asciiGraph(moduleGraph: ModuleGraph): layout.Graph[String] = { + def renderVertex(module: Module): String = { + module.name + "\n" + module.organisation + "\n" + module.version + } + val vertices = moduleGraph.nodes.map(renderVertex).toList + val edges = moduleGraph.edges.toList.map { case (from, to) ⇒ (renderVertex(from), renderVertex(to)) } + layout.Graph(vertices, edges) + } + + def asciiGraph(ivyReportFile: String): String = { + val doc = buildDoc(ivyReportFile) + val graph = buildGraph(doc) + Layouter.renderGraph(asciiGraph(graph)) + } + + def asciiTree(ivyReportFile: String): String = { val doc = buildDoc(ivyReportFile) val graph = buildGraph(doc) import graph._ diff --git a/src/main/scala/net/virtualvoid/sbt/graph/Plugin.scala b/src/main/scala/net/virtualvoid/sbt/graph/Plugin.scala index 9668c1507..425bff16c 100755 --- a/src/main/scala/net/virtualvoid/sbt/graph/Plugin.scala +++ b/src/main/scala/net/virtualvoid/sbt/graph/Plugin.scala @@ -28,6 +28,10 @@ object Plugin extends sbt.Plugin { "Returns a string containing the ascii representation of the dependency graph for a project") val dependencyGraph = TaskKey[Unit]("dependency-graph", "Prints the ascii graph to the console") + val asciiTree = TaskKey[String]("dependency-tree-string", + "Returns a string containing an ascii tree representation of the dependency graph for a project") + val dependencyTree = TaskKey[Unit]("dependency-tree", + "Prints the ascii tree to the console") val ivyReportFunction = TaskKey[String => File]("ivy-report-function", "A function which returns the file containing the ivy report from the ivy cache for a given configuration") val ivyReport = TaskKey[File]("ivy-report", @@ -44,12 +48,14 @@ object Plugin extends sbt.Plugin { ivyReport <<= ivyReportFunction map (_(config.toString)) dependsOn(update), asciiGraph <<= asciiGraphTask, dependencyGraph <<= printAsciiGraphTask, + asciiTree <<= asciiTreeTask, + dependencyTree <<= printAsciiTreeTask, dependencyGraphMLFile <<= target / "dependencies-%s.graphml".format(config.toString), dependencyGraphML <<= dependencyGraphMLTask )) def asciiGraphTask = (ivyReport) map { report => - IvyGraphMLDependencies.ascii(report.getAbsolutePath) + IvyGraphMLDependencies.asciiGraph(report.getAbsolutePath) } def printAsciiGraphTask = @@ -62,6 +68,13 @@ object Plugin extends sbt.Plugin { resultFile } + def asciiTreeTask = (ivyReport) map { report => + IvyGraphMLDependencies.asciiTree(report.getAbsolutePath) + } + + def printAsciiTreeTask = + (streams, asciiTree) map (_.log.info(_)) + def crossName(ivyModule: IvySbt#Module) = ivyModule.moduleSettings match { case ic: InlineConfiguration => ic.module.name