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

add json output support #90

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ trait DependencyGraphKeys {
"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 jsonTree = TaskKey[String]("dependency-tree-json-string",
"Returns a json string dependency tree of non-evicted/used modules")
val dependencyJsonTree = TaskKey[File]("dependency-tree-json",
"Prints an json tree of all the dependencies to the console")
val dependencyJsonFile = SettingKey[File]("dependency-json-file",
"The location the json file should be generated at")
val dependencyTree = TaskKey[Unit]("dependency-tree",
"Prints an ascii tree of all the dependencies to the console")
val dependencyList = TaskKey[Unit]("dependency-list",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ object DependencyGraphSettings {
}
},
asciiTree <<= moduleGraph map rendering.AsciiTree.asciiTree,
jsonTree <<= moduleGraph map { (g: ModuleGraph) ⇒ rendering.JsonTree.json(g, { m: Module ⇒ m.isUsed }).toString() },
dependencyJsonTree <<= writeToFile(jsonTree, dependencyJsonFile),
dependencyJsonFile <<= target / "dependencies-%s.js".format(config.toString),
dependencyTree <<= print(asciiTree),
dependencyGraphMLFile <<= target / "dependencies-%s.graphml".format(config.toString),
dependencyGraphML <<= dependencyGraphMLTask,
Expand Down
26 changes: 26 additions & 0 deletions src/main/scala/net/virtualvoid/sbt/graph/rendering/JsonTree.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.virtualvoid.sbt.graph.rendering

import net.virtualvoid.sbt.graph.{ Module, ModuleGraph }

import scala.util.parsing.json.{ JSONArray, JSONObject }

object JsonTree {

def jsonModule(module: Module)(children: Seq[JSONObject]): JSONObject =
new JSONObject(Map(
"module" -> module.id.name,
"group" -> module.id.organisation,
"version" -> module.id.version,
"evicted" -> module.isEvicted,
"errors" -> module.error.getOrElse("None"),
"license" -> module.license.getOrElse("Unknown"),
"children" -> new JSONArray(children.toList)))

private def postOrder[T](p: Module ⇒ Boolean)(f: Module ⇒ Seq[T] ⇒ T)(graph: ModuleGraph)(module: Module): T = {
val children = graph.dependencyMap.getOrElse(module.id, Seq()).filter(p).map(postOrder(p)(f)(graph))
f(module)(children)
}

def json(graph: ModuleGraph, filter: Module ⇒ Boolean): JSONArray =
new JSONArray(graph.roots.map(postOrder(filter)(jsonModule)(graph)(_)).toList)
}