Skip to content

Commit 5512a8f

Browse files
author
Anton Troshin
authored
Merge pull request #3 from SupersonicAds/feature/antontroshin/projectReverseDependencyTree
Add sonicDependenciesReverseTree task
2 parents 45631a8 + 5b3dd5c commit 5512a8f

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ It helps to keep track of module dependencies and library version used in each m
1616
Add the plugin as SBT dependency to your `project/plugins.sbt`
1717

1818
```
19-
addSbtPlugin("com.supersonic" % "sonic-dependency-tree" % "0.0.2")
19+
addSbtPlugin("com.supersonic" % "sonic-dependency-tree" % "0.0.3")
2020
```
2121

2222
Since it is an Auto Plugin, no need to explicitly enable it.
@@ -64,6 +64,8 @@ sonicDependenciesUploadFilename in Global := "dependencies.json"
6464
- Prints dependency list with commit as JSON Array
6565
- `sonicDependenciesUploadToS3` : `Task`
6666
- Upload full dependency tree to S3
67+
- `sonicDependenciesReverseTree` : `Task`
68+
- Print reverse dependencies tree for a project
6769

6870

6971
## S3 configuration

src/main/scala/com/supersonic/SonicDependencyTreePlugin.scala

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import scala.sys.process.Process
99
import scala.util.{Failure, Success, Try}
1010

1111
object SonicDependencyTreePlugin extends AutoPlugin {
12+
1213
object autoImport {
1314
lazy val sonicDependenciesGitCommit = taskKey[Unit]("Prints the current git HEAD SHA-1 commit hash")
1415

@@ -21,6 +22,7 @@ object SonicDependencyTreePlugin extends AutoPlugin {
2122
lazy val sonicDependenciesS3Credentials = settingKey[Seq[Credentials]]("The S3 credentials to use to upload to S3 with, if empty defaults taken from the environment or IAM will be used")
2223
lazy val sonicDependenciesUploadFilename = settingKey[String]("Name of the file that will be uploaded to S3, default is value of setting \"{gitCommitHash}.json\"")
2324
lazy val sonicDependenciesUploadToS3 = taskKey[Unit]("Upload full dependency tree to S3")
25+
lazy val sonicDependenciesReverseTree = taskKey[Unit]("Print tree of dependencies for current module")
2426
}
2527

2628
import autoImport._
@@ -41,6 +43,41 @@ object SonicDependencyTreePlugin extends AutoPlugin {
4143
sonicDependenciesUploadToS3 := uploadDependenciesTask.value
4244
)
4345

46+
override def projectSettings: Seq[Def.Setting[_]] = projectSettingsPrivate
47+
48+
private lazy val projectSettingsPrivate = Seq(
49+
sonicDependenciesReverseTree := reverseDependencyMapTask.value,
50+
)
51+
52+
private def reverseDependencyMapTask = Def.task {
53+
54+
val projectMap = Keys.loadedBuild.value.allProjectRefs.toMap
55+
56+
val projectFlatList = projectMap.toList.flatMap {
57+
case (ref, project) => project.dependencies.map(dep => ref.project -> dep.project.project)
58+
}
59+
val reverseDeps = projectFlatList.groupBy(_._2)
60+
val flatReverse = reverseDeps.map(x => x._1 -> x._2.map(_._1))
61+
62+
def findParent(id: String, depth: Int): Unit = {
63+
if (depth == 0) println(s"Reverse dependencies tree of ${id}")
64+
val maybeParents = flatReverse.get(id)
65+
maybeParents.foreach {
66+
parents =>
67+
parents.foreach { parent =>
68+
val project = s"$parent"
69+
println(s"${depthLines(depth)}$project")
70+
findParent(parent, depth + 1)
71+
}
72+
}
73+
}
74+
75+
val projectRef = Keys.thisProjectRef.value
76+
findParent(projectRef.project, 0)
77+
}
78+
79+
private def depthLines(depth: Int) = Range(0, depth).map(_ => "|..").mkString
80+
4481
private def uploadDependenciesTask = Def.task {
4582

4683
val s3BucketValue = sonicDependenciesS3Bucket.value

0 commit comments

Comments
 (0)