Skip to content

Commit c6d3efe

Browse files
authored
Add longest path to statistics (#67)
* Add longest path to statistics * Add generateModulesGraphStatistics task * Add module statistics into readme
1 parent fa40072 commit c6d3efe

File tree

10 files changed

+83
-18
lines changed

10 files changed

+83
-18
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,19 @@ moduleGraphAssert {
5555
- This generates a graph of dependent modules when the plugin is applied.
5656
- The longest path of the project is in red.
5757
- If you utilise [Configuration on demand](https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:configuration_on_demand) Gradle feature, please use `--no-configure-on-demand` flag along the `generateModulesGraphvizText` task.
58-
- Adding the parameter `modules.graph.print.statistics` causes information about the graph to be included.
5958
- You can set the `modules.graph.of.module` parameter if you are only interested in a sub-graph of the module graph.
6059
```
61-
./gradlew generateModulesGraphvizText -Pmodules.graph.print.statistics=true -Pmodules.graph.of.module=:feature-one
60+
./gradlew generateModulesGraphvizText -Pmodules.graph.of.module=:feature-one
6261
```
6362
- Adding the parameter `modules.graph.output.gv` saves the graphViz file to the specified path
6463
```
6564
./gradlew generateModulesGraphvizText -Pmodules.graph.output.gv=all_modules
6665
```
66+
67+
### Graph statistics
68+
- Executing the task `generateModulesGraphStatistics` prints the information about the graph.
69+
- Statistics printed: Modules Count, [Edges Count](https://en.wikipedia.org/wiki/Glossary_of_graph_theory_terms#edge), [Height](https://en.wikipedia.org/wiki/Glossary_of_graph_theory_terms#height) and [Longest Path](https://en.wikipedia.org/wiki/Longest_path_problem)
70+
- Parameter `-Pmodules.graph.of.module` is supported as well.
71+
```
72+
./gradlew generateModulesGraphStatistics -Pmodules.graph.of.module=:feature-one
73+
```

plugin/src/main/kotlin/com/jraska/module/graph/DependencyGraph.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ class DependencyGraph private constructor() {
5151
return GraphStatistics(
5252
modulesCount = nodes.size,
5353
edgesCount = edgesCount,
54-
height = height
54+
height = height,
55+
longestPath = longestPath()
5556
)
5657
}
5758

plugin/src/main/kotlin/com/jraska/module/graph/GraphStatistics.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ package com.jraska.module.graph
33
data class GraphStatistics(
44
val modulesCount: Int,
55
val edgesCount: Int,
6-
val height: Int
6+
val height: Int,
7+
val longestPath: LongestPath
78
)

plugin/src/main/kotlin/com/jraska/module/graph/LongestPath.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ class LongestPath(
66
fun pathString(): String {
77
return nodeNames.joinToString(" -> ")
88
}
9+
10+
override fun toString() = "'${pathString()}'"
911
}

plugin/src/main/kotlin/com/jraska/module/graph/assertion/Api.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.jraska.module.graph.assertion
33
object Api {
44
object Tasks {
55
const val GENERATE_GRAPHVIZ = "generateModulesGraphvizText"
6+
const val GENERATE_GRAPH_STATISTICS = "generateModulesGraphStatistics"
67

78
const val ASSERT_ALL = "assertModuleGraph"
89
const val ASSERT_MAX_HEIGHT = "assertMaxHeight"
@@ -11,7 +12,9 @@ object Api {
1112
}
1213

1314
object Parameters {
15+
@Deprecated("Will be removed in version 2.0")
1416
const val PRINT_STATISTICS = "modules.graph.print.statistics"
17+
1518
const val PRINT_ONLY_MODULE = "modules.graph.of.module"
1619
const val OUTPUT_PATH = "modules.graph.output.gv"
1720
}

plugin/src/main/kotlin/com/jraska/module/graph/assertion/ModuleGraphAssertionsPlugin.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.jraska.module.graph.ModuleNameRegexMatcher
55
import com.jraska.module.graph.Parse
66
import com.jraska.module.graph.assertion.Api.Tasks
77
import com.jraska.module.graph.assertion.tasks.AssertGraphTask
8+
import com.jraska.module.graph.assertion.tasks.GenerateModulesGraphStatisticsTask
89
import com.jraska.module.graph.assertion.tasks.GenerateModulesGraphTask
910
import org.gradle.api.Plugin
1011
import org.gradle.api.Project
@@ -28,6 +29,7 @@ class ModuleGraphAssertionsPlugin : Plugin<Project> {
2829

2930
internal fun addModulesAssertions(project: Project, graphRules: GraphRulesExtension) {
3031
project.addModuleGraphGeneration(graphRules)
32+
project.addModuleGraphStatisticsGeneration(graphRules)
3133

3234
val allAssertionsTask = project.tasks.register(Tasks.ASSERT_ALL) { it.group = VERIFICATION_GROUP }
3335

@@ -55,6 +57,12 @@ class ModuleGraphAssertionsPlugin : Plugin<Project> {
5557
}
5658
}
5759

60+
private fun Project.addModuleGraphStatisticsGeneration(graphRules: GraphRulesExtension) {
61+
tasks.register(Tasks.GENERATE_GRAPH_STATISTICS, GenerateModulesGraphStatisticsTask::class.java) {
62+
it.configurationsToLook = graphRules.configurations
63+
}
64+
}
65+
5866
private fun Project.addMaxHeightTask(graphRules: GraphRulesExtension): TaskProvider<AssertGraphTask>? {
5967
if (graphRules.maxHeight <= 0) {
6068
return null
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.jraska.module.graph.assertion.tasks
2+
3+
import org.gradle.api.DefaultTask
4+
import org.gradle.api.tasks.Input
5+
import org.gradle.api.tasks.TaskAction
6+
7+
open class GenerateModulesGraphStatisticsTask : DefaultTask() {
8+
@Input
9+
lateinit var configurationsToLook: Set<String>
10+
11+
@TaskAction
12+
fun run() {
13+
val dependencyGraph = project.createDependencyGraph(configurationsToLook)
14+
println(dependencyGraph.statistics())
15+
}
16+
}

plugin/src/main/kotlin/com/jraska/module/graph/assertion/tasks/GenerateModulesGraphTask.kt

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.jraska.module.graph.GraphvizWriter
55
import com.jraska.module.graph.assertion.Api
66
import com.jraska.module.graph.assertion.GradleDependencyGraphFactory
77
import org.gradle.api.DefaultTask
8+
import org.gradle.api.Project
89
import org.gradle.api.tasks.Input
910
import org.gradle.api.tasks.TaskAction
1011
import java.io.File
@@ -15,9 +16,11 @@ open class GenerateModulesGraphTask : DefaultTask() {
1516

1617
@TaskAction
1718
fun run() {
18-
val dependencyGraph = createDependencyGraph()
19+
val dependencyGraph = project.createDependencyGraph(configurationsToLook)
1920

2021
if (shouldPrintStatistics()) {
22+
println("*Deprecation*: ${Api.Parameters.PRINT_STATISTICS} parameter is deprecated and will be removed in version 2.0. \n" +
23+
"Please use ${Api.Tasks.GENERATE_GRAPH_STATISTICS} task instead.")
2124
println(dependencyGraph.statistics())
2225
}
2326
val graphviz = GraphvizWriter.toGraphviz(dependencyGraph)
@@ -32,19 +35,6 @@ open class GenerateModulesGraphTask : DefaultTask() {
3235
}
3336
}
3437

35-
private fun createDependencyGraph(): DependencyGraph {
36-
val dependencyGraph = GradleDependencyGraphFactory.create(project, configurationsToLook)
37-
38-
if (project.hasProperty(Api.Parameters.PRINT_ONLY_MODULE)) {
39-
val moduleName = project.property(Api.Parameters.PRINT_ONLY_MODULE) as String?
40-
if (moduleName != null) {
41-
return dependencyGraph.subTree(moduleName)
42-
}
43-
}
44-
45-
return dependencyGraph
46-
}
47-
4838
private fun shouldPrintStatistics(): Boolean {
4939
return project.hasProperty(Api.Parameters.PRINT_STATISTICS) && project.property(Api.Parameters.PRINT_STATISTICS) == "true"
5040
}
@@ -57,3 +47,16 @@ open class GenerateModulesGraphTask : DefaultTask() {
5747
return File(project.property(Api.Parameters.OUTPUT_PATH).toString())
5848
}
5949
}
50+
51+
internal fun Project.createDependencyGraph(configurationsToLook: Set<String>): DependencyGraph {
52+
val dependencyGraph = GradleDependencyGraphFactory.create(this, configurationsToLook)
53+
54+
if (project.hasProperty(Api.Parameters.PRINT_ONLY_MODULE)) {
55+
val moduleName = project.property(Api.Parameters.PRINT_ONLY_MODULE) as String?
56+
if (moduleName != null) {
57+
return dependencyGraph.subTree(moduleName)
58+
}
59+
}
60+
61+
return dependencyGraph
62+
}

plugin/src/test/kotlin/com/jraska/module/graph/DependencyGraphTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class DependencyGraphTest {
8787
assert(dependencyGraph.findRoot().key == ":app")
8888
assert(dependencyGraph.heightOf(":app") == 0)
8989
assert(dependencyGraph.longestPath().nodeNames.equals(listOf(":app")))
90+
assert(dependencyGraph.longestPath().nodeNames.equals(dependencyGraph.statistics().longestPath.nodeNames))
9091
}
9192

9293
@Test(expected = IllegalArgumentException::class)
@@ -149,5 +150,12 @@ class DependencyGraphTest {
149150
assert(statistics.height == 5)
150151
assert(statistics.modulesCount == 16)
151152
assert(statistics.edgesCount == 45)
153+
assert(statistics.longestPath.nodeNames == listOf(
154+
":feature:settings",
155+
":app",
156+
":feature:settings_entrance",
157+
":lib:dynamic-features",
158+
":core-android",
159+
":core"))
152160
}
153161
}

plugin/src/test/kotlin/com/jraska/module/graph/assertion/ModuleGraphAssertionsPluginTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,20 @@ class ModuleGraphAssertionsPluginTest {
5353
assert(it.configurationsToLook == Api.API_IMPLEMENTATON_CONFIGURATIONS)
5454
}
5555
}
56+
57+
@Test
58+
fun testPrintGraphvizTextsIsAdded() {
59+
project.plugins.apply(ModuleGraphAssertionsPlugin::class.java)
60+
project.evaluate()
61+
62+
assert(project.tasks.findByName("generateModulesGraphvizText") != null)
63+
}
64+
65+
@Test
66+
fun testPrintTaskStatisticsIsAdded() {
67+
project.plugins.apply(ModuleGraphAssertionsPlugin::class.java)
68+
project.evaluate()
69+
70+
assert(project.tasks.findByName("generateModulesGraphStatistics") != null)
71+
}
5672
}

0 commit comments

Comments
 (0)