Skip to content

Commit 51b228f

Browse files
committed
add file-output option to dependencyTree
1 parent 001d519 commit 51b228f

File tree

6 files changed

+79
-7
lines changed

6 files changed

+79
-7
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ dependencyTree foo bar -baz // include only deps that contain "foo" or "bar" an
7777

7878
In all cases, the full paths to dependencies that match the query are displayed (which can mean that dependencies are displayed even though they would have been excluded in their own right, because they form part of a chain to a dependency that was not excluded).
7979

80+
#### Writing output to file
81+
82+
`dependencyTree` can have its output written to a file:
83+
84+
```
85+
$ sbt
86+
> dependencyTree -o foo
87+
```
88+
89+
or, directly from the shell:
90+
91+
```bash
92+
sbt 'dependency-tree -o foo'
93+
```
94+
8095
## Configuration settings
8196

8297
* `filterScalaLibrary`: Defines if the scala library should be excluded from the output of the dependency-* functions.

src/main/scala/net/virtualvoid/sbt/graph/DependencyGraphSettings.scala

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package net.virtualvoid.sbt.graph
1818

19+
import java.nio.file.Files.newOutputStream
20+
import java.nio.file.{ Path, Paths }
21+
1922
import net.virtualvoid.sbt.graph.GraphTransformations.reverseGraphStartingAt
2023
import net.virtualvoid.sbt.graph.backend.{ IvyReport, SbtUpdateReport }
2124
import net.virtualvoid.sbt.graph.model.{ FilterRule, ModuleGraph, ModuleId }
@@ -46,7 +49,7 @@ object DependencyGraphSettings {
4649
Seq(Compile, Test, IntegrationTest, Runtime, Provided, Optional).flatMap(ivyReportForConfig)
4750

4851
def ivyReportForConfig(config: Configuration) = inConfig(config)(Seq(
49-
ivyReport := { Def.task { ivyReportFunction.value.apply(config.toString) } dependsOn (ignoreMissingUpdate) }.value,
52+
ivyReport := { Def.task { ivyReportFunction.value.apply(config.toString) } dependsOn ignoreMissingUpdate }.value,
5053
crossProjectId := sbt.CrossVersion(scalaVersion.value, scalaBinaryVersion.value)(projectID.value),
5154
moduleGraphSbt :=
5255
ignoreMissingUpdate
@@ -80,7 +83,19 @@ object DependencyGraphSettings {
8083
moduleGraph.value,
8184
filterRulesParser.parsed: _*
8285
),
83-
dependencyTree := streams.value.log.info(asciiTree.evaluated),
86+
dependencyTree := {
87+
val tree = asciiTree.evaluated
88+
dependencyTreeOutputPathParser.parsed match {
89+
case Some(path)
90+
streams.value.log.info(s"Writing dependency-tree to path: $path")
91+
val os = newOutputStream(path)
92+
os.write(tree.getBytes)
93+
os.close()
94+
case _
95+
streams.value.log.info(tree)
96+
}
97+
98+
},
8499
dependencyGraphMLFile := { target.value / "dependencies-%s.graphml".format(config.toString) },
85100
dependencyGraphML := dependencyGraphMLTask.value,
86101
dependencyDotFile := { target.value / "dependencies-%s.dot".format(config.toString) },
@@ -174,16 +189,26 @@ object DependencyGraphSettings {
174189
}.mkString("\n\n")
175190
streams.log.info(output)
176191
}
177-
val shouldForceParser: State Parser[Boolean] = { (state: State)
178-
import sbt.complete.DefaultParsers._
179192

193+
import sbt.complete.DefaultParsers._
194+
195+
val shouldForceParser: State Parser[Boolean] = { (state: State)
180196
(Space ~> token("--force")).?.map(_.isDefined)
181197
}
182198

199+
val dependencyTreeOutputPathParser: State Parser[Option[Path]] = { (state: State)
200+
(
201+
Space ~
202+
(token("--out") | token("-o")) ~ Space ~>
203+
StringBasic
204+
)
205+
.map(Paths.get(_))
206+
.?
207+
}
208+
183209
val filterRulesParser: Def.Initialize[State Parser[Seq[FilterRule]]] =
184210
resolvedScoped { ctx
185211
(state: State)
186-
import sbt.complete.DefaultParsers._
187212
(Space ~> token(StringBasic, "filter")).*.map {
188213
_.map(FilterRule(_))
189214
}
@@ -194,7 +219,6 @@ object DependencyGraphSettings {
194219
(state: State)
195220
val graph = loadFromContext(moduleGraphStore, ctx, state) getOrElse ModuleGraph(Nil, Nil)
196221

197-
import sbt.complete.DefaultParsers._
198222
graph
199223
.nodes
200224
.map(_.id)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.nio.file.{ Files, Paths }
2+
3+
import scala.io.Source
4+
5+
scalaVersion := "2.9.1"
6+
7+
resolvers += "typesafe maven" at "https://repo.typesafe.com/typesafe/maven-releases/"
8+
9+
libraryDependencies ++= Seq(
10+
"com.codahale" % "jerkson_2.9.1" % "0.5.0"
11+
)
12+
13+
InputKey[Unit]("check") := {
14+
val is = Files.newInputStream(Paths.get("foo"))
15+
val tree = Source.fromInputStream(is).mkString
16+
is.close()
17+
18+
require(
19+
tree ==
20+
"""default:dependencytreefile_2.9.1:0.1-SNAPSHOT [S]
21+
| +-com.codahale:jerkson_2.9.1:0.5.0 [S]
22+
| +-org.codehaus.jackson:jackson-core-asl:1.9.11
23+
| +-org.codehaus.jackson:jackson-mapper-asl:1.9.11
24+
| +-org.codehaus.jackson:jackson-core-asl:1.9.11
25+
| """
26+
.stripMargin,
27+
s"Tree didn't match expected:\n$tree"
28+
)
29+
()
30+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("org.hammerlab" % "sbt-dependency-graph" % sys.props("project.version"))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
> dependencyTree -o foo
2+
> check
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % sys.props("project.version"))
1+
addSbtPlugin("org.hammerlab" % "sbt-dependency-graph" % sys.props("project.version"))

0 commit comments

Comments
 (0)