Skip to content

Commit 92a8f57

Browse files
committed
add file-output option to dependencyTree
1 parent 7561c07 commit 92a8f57

File tree

6 files changed

+64
-17
lines changed

6 files changed

+64
-17
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: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
package net.virtualvoid.sbt.graph
1818

19-
import java.nio.file.{ Files, Path, Paths }
19+
import java.nio.file.Files.newOutputStream
20+
import java.nio.file.{ Path, Paths }
2021

2122
import net.virtualvoid.sbt.graph.GraphTransformations.reverseGraphStartingAt
2223
import net.virtualvoid.sbt.graph.backend.{ IvyReport, SbtUpdateReport }
@@ -48,7 +49,7 @@ object DependencyGraphSettings {
4849
Seq(Compile, Test, IntegrationTest, Runtime, Provided, Optional).flatMap(ivyReportForConfig)
4950

5051
def ivyReportForConfig(config: Configuration) = inConfig(config)(Seq(
51-
ivyReport := { Def.task { ivyReportFunction.value.apply(config.toString) } dependsOn (ignoreMissingUpdate) }.value,
52+
ivyReport := { Def.task { ivyReportFunction.value.apply(config.toString) } dependsOn ignoreMissingUpdate }.value,
5253
crossProjectId := sbt.CrossVersion(scalaVersion.value, scalaBinaryVersion.value)(projectID.value),
5354
moduleGraphSbt :=
5455
ignoreMissingUpdate
@@ -83,13 +84,15 @@ object DependencyGraphSettings {
8384
filterRulesParser.parsed: _*
8485
),
8586
dependencyTree := {
87+
val tree = asciiTree.evaluated
8688
dependencyTreeOutputPathParser.parsed match {
8789
case Some(path)
88-
val os = Files.newOutputStream(path)
89-
os.write(asciiTree.evaluated.getBytes)
90+
streams.value.log.info(s"Writing dependency-tree to path: $path")
91+
val os = newOutputStream(path)
92+
os.write(tree.getBytes)
9093
os.close()
9194
case _
92-
streams.value.log.info(asciiTree.evaluated)
95+
streams.value.log.info(tree)
9396
}
9497

9598
},
@@ -196,23 +199,19 @@ object DependencyGraphSettings {
196199
val dependencyTreeOutputPathParser: State Parser[Option[Path]] = { (state: State)
197200
(
198201
Space ~
199-
(token("--out") | token("-o")) ~>
200-
StringBasic
202+
(token("--out") | token("-o")) ~ Space ~>
203+
StringBasic
201204
)
202-
.map(Paths.get(_))
203-
.?
204-
}
205-
206-
val filterRulesParser_ = {
207-
(Space ~> token(StringBasic, "filter")).*.map {
208-
_.map(FilterRule(_))
209-
}
205+
.map(Paths.get(_))
206+
.?
210207
}
211208

212209
val filterRulesParser: Def.Initialize[State Parser[Seq[FilterRule]]] =
213210
resolvedScoped { ctx
214211
(state: State)
215-
filterRulesParser_
212+
(Space ~> token(StringBasic, "filter")).*.map {
213+
_.map(FilterRule(_))
214+
}
216215
}
217216

218217
val artifactIdParser: Def.Initialize[State Parser[ModuleId]] =
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)