-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generateProjectDependencyGraph Task and ProjectGenerator extensio…
…n for proper multi project support.
- Loading branch information
1 parent
49f20ac
commit f962495
Showing
14 changed files
with
328 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/main/kotlin/com/vanniktech/dependency/graph/generator/ProjectDependencyGraphGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.vanniktech.dependency.graph.generator | ||
|
||
import guru.nidi.graphviz.attribute.Color | ||
import guru.nidi.graphviz.attribute.Font | ||
import guru.nidi.graphviz.attribute.Label | ||
import guru.nidi.graphviz.attribute.Rank | ||
import guru.nidi.graphviz.attribute.Shape | ||
import guru.nidi.graphviz.attribute.Style | ||
import guru.nidi.graphviz.model.Factory.graph | ||
import guru.nidi.graphviz.model.Factory.mutGraph | ||
import guru.nidi.graphviz.model.Factory.mutNode | ||
import guru.nidi.graphviz.model.Link | ||
import guru.nidi.graphviz.model.MutableGraph | ||
import org.gradle.api.Project | ||
import org.gradle.api.artifacts.ProjectDependency | ||
import java.util.Locale.US | ||
|
||
// Based on https://github.com/JakeWharton/SdkSearch/blob/766d612ed52cdf3af9cd0728b6afd87006746ae5/gradle/projectDependencyGraph.gradle | ||
internal class ProjectDependencyGraphGenerator( | ||
private val project: Project, | ||
private val projectGenerator: DependencyGraphGeneratorExtension.ProjectGenerator | ||
) { | ||
fun generateGraph(): MutableGraph { | ||
val graph = mutGraph().setDirected(true) | ||
graph.graphAttrs().add(Label.of(project.name).locate(Label.Location.TOP), Font.size(DEFAULT_FONT_SIZE)) | ||
graph.nodeAttrs().add(Font.name("Times New Roman"), Style.FILLED) | ||
|
||
val rootProjects = project.allprojects.toMutableList() | ||
val projects = mutableSetOf<Project>() | ||
val dependencies = mutableListOf<ProjectDependencyContainer>() | ||
|
||
addProjects(projects, rootProjects, dependencies, graph) | ||
rankRootProjects(graph, projects, rootProjects) | ||
addDependencies(dependencies, graph) | ||
|
||
return projectGenerator.graph(graph) | ||
} | ||
|
||
@Suppress("Detekt.ComplexMethod") private fun addProjects(projects: MutableSet<Project>, rootProjects: MutableList<Project>, dependencies: MutableList<ProjectDependencyContainer>, graph: MutableGraph) { | ||
project.allprojects | ||
.filter { projectGenerator.includeProject(it) } | ||
.flatMap { project -> project.configurations.map { project to it } } | ||
.flatMap { (project, configuration) -> | ||
configuration.dependencies | ||
.withType(ProjectDependency::class.java) | ||
.map { it.dependencyProject } | ||
.flatMap { projectDependency -> | ||
projects.add(project) | ||
projects.add(projectDependency) | ||
|
||
rootProjects.remove(projectDependency) | ||
dependencies.add(ProjectDependencyContainer(project, projectDependency, configuration.name.toLowerCase(US).endsWith("implementation"))) | ||
listOf(project, projectDependency) | ||
} | ||
} | ||
.forEach { project -> | ||
val node = mutNode(project.path) | ||
|
||
if (rootProjects.contains(project)) { | ||
node.add(Shape.RECTANGLE) | ||
} else if (project.isCommonsProject()) { | ||
node.add(Style.DASHED, Color.BLACK) | ||
} | ||
|
||
when { | ||
project.isJsProject() -> node.add(Color.rgb("#fff176").fill()) | ||
project.isAndroidProject() -> node.add(Color.rgb("#81c784").fill()) | ||
project.isKotlinProject() -> node.add(Color.rgb("#ffb74d").fill()) | ||
project.isJavaProject() -> node.add(Color.rgb("#ff8a65").fill()) | ||
else -> node.add(Color.rgb("#e0e0e0").fill()) | ||
} | ||
|
||
graph.add(projectGenerator.projectNode(node, project)) | ||
} | ||
} | ||
|
||
@Suppress("Detekt.SpreadOperator") private fun rankRootProjects(graph: MutableGraph, projects: MutableSet<Project>, rootProjects: MutableList<Project>) { | ||
graph.add(graph() | ||
.graphAttr() | ||
.with(Rank.SAME) | ||
.with(*projects.filter { rootProjects.contains(it) }.map { mutNode(it.path) }.toTypedArray())) | ||
} | ||
|
||
private fun addDependencies(dependencies: MutableList<ProjectDependencyContainer>, graph: MutableGraph) { | ||
dependencies | ||
.filterNot { (from, to, _) -> !from.isCommonsProject() && to.isCommonsProject() } | ||
.forEach { (from, to, isImplementation) -> | ||
val fromNode = graph.nodes().find { it.name().toString() == from.path } | ||
val toNode = graph.nodes().find { it.name().toString() == to.path } | ||
|
||
if (fromNode != null && toNode != null) { | ||
val link = Link.to(toNode) | ||
graph.add(fromNode.addLink(if (isImplementation) link.with(Style.DOTTED) else link)) | ||
} | ||
} | ||
} | ||
|
||
internal data class ProjectDependencyContainer( | ||
val from: Project, | ||
val to: Project, | ||
val isImplementation: Boolean | ||
) | ||
|
||
internal companion object { | ||
const val DEFAULT_FONT_SIZE = 35 | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...n/kotlin/com/vanniktech/dependency/graph/generator/ProjectDependencyGraphGeneratorTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.vanniktech.dependency.graph.generator | ||
|
||
import com.vanniktech.dependency.graph.generator.DependencyGraphGeneratorExtension.ProjectGenerator | ||
import guru.nidi.graphviz.engine.Graphviz | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.tasks.InputFile | ||
import org.gradle.api.tasks.OutputDirectory | ||
import org.gradle.api.tasks.TaskAction | ||
import java.io.File | ||
|
||
open class ProjectDependencyGraphGeneratorTask : DefaultTask() { | ||
lateinit var projectGenerator: ProjectGenerator // TODO does this need to be an input? Quick testing shows no. | ||
@InputFile lateinit var inputFile: File | ||
|
||
@OutputDirectory lateinit var outputDirectory: File | ||
|
||
@TaskAction fun run() { | ||
val graph = ProjectDependencyGraphGenerator(project, projectGenerator).generateGraph() | ||
File(outputDirectory, projectGenerator.outputFileNameDot).writeText(graph.toString()) | ||
|
||
val graphviz = Graphviz.fromGraph(graph) | ||
|
||
projectGenerator.outputFormats.forEach { | ||
graphviz.render(it).toFile(File(outputDirectory, projectGenerator.outputFileName)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
...t/java/com/vanniktech/dependency/graph/generator/DependencyGraphGeneratorExtensionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
package com.vanniktech.dependency.graph.generator | ||
|
||
import com.vanniktech.dependency.graph.generator.DependencyGraphGeneratorExtension.Generator.Companion.ALL | ||
import com.vanniktech.dependency.graph.generator.DependencyGraphGeneratorExtension.Generator | ||
import com.vanniktech.dependency.graph.generator.DependencyGraphGeneratorExtension.ProjectGenerator | ||
import org.assertj.core.api.Java6Assertions.assertThat | ||
import org.junit.Test | ||
|
||
class DependencyGraphGeneratorExtensionTest { | ||
@Test fun defaults() { | ||
val defaults = DependencyGraphGeneratorExtension() | ||
assertThat(defaults.generators).containsExactly(ALL) | ||
assertThat(defaults.generators).containsExactly(Generator.ALL) | ||
assertThat(defaults.projectGenerators).containsExactly(ProjectGenerator.ALL) | ||
} | ||
} |
Oops, something went wrong.