Skip to content

Commit

Permalink
Formated code + baseline
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrakal committed Mar 12, 2024
1 parent 774ea36 commit 6135466
Show file tree
Hide file tree
Showing 23 changed files with 336 additions and 206 deletions.
3 changes: 3 additions & 0 deletions config/ktlint/baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<baseline version="1.0">
</baseline>
10 changes: 10 additions & 0 deletions plugin/config/ktlint/baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<baseline version="1.0">
<file name="src/main/kotlin/com/jraska/module/graph/assertion/ModuleTreeHeightAssert.kt">
<error line="27" column="141" source="standard:max-line-length" />
<error line="37" column="141" source="standard:max-line-length" />
</file>
<file name="src/test/kotlin/com/jraska/module/graph/assertion/OnAnyBuildAssertTest.kt">
<error line="128" column="141" source="standard:max-line-length" />
</file>
</baseline>
17 changes: 17 additions & 0 deletions plugin/detekt-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" ?>
<SmellBaseline>
<ManuallySuppressedIssues></ManuallySuppressedIssues>
<CurrentIssues>
<ID>LongMethod:DependencyGraphTest.kt$DependencyGraphTest$@Test fun countsStatisticsWell()</ID>
<ID>MaxLineLength:ModuleTreeHeightAssert.kt$ModuleTreeHeightAssert$"Module $moduleName is allowed to have maximum height of $maxHeight, but has $height, problematic dependencies: ${longestPath.pathString()}"</ID>
<ID>MaxLineLength:ModuleTreeHeightAssert.kt$ModuleTreeHeightAssert$"Module Graph is allowed to have maximum height of $maxHeight, but has $height, problematic dependencies: ${longestPath.pathString()}"</ID>
<ID>MaxLineLength:OnAnyBuildAssertTest.kt$OnAnyBuildAssertTest$"""["App"(':app') -&gt; "Implementation"(':core'), "App"(':app') -&gt; "Implementation"(':feature')] not allowed by any of ['Implementation -&gt; Api', 'App -&gt; Api']"""</ID>
<ID>MaxLineLength:OnAnyBuildAssertTest.kt$OnAnyBuildAssertTest$"Module :app is allowed to have maximum height of 1, but has 2, problematic dependencies: :app -&gt; :core -&gt; :core-api"</ID>
<ID>ReturnCount:GradleDependencyGraphFactory.kt$GradleDependencyGraphFactory$fun create( project: Project, configurationsToLook: Set&lt;String&gt;, ): DependencyGraph</ID>
<ID>SerialVersionUIDInSerializableClass:DependencyGraph.kt$DependencyGraph$SerializableGraph : Serializable</ID>
<ID>SerialVersionUIDInSerializableClass:ModuleTreeHeightAssert.kt$ModuleTreeHeightAssert : GraphAssertSerializable</ID>
<ID>SwallowedException:ModuleGraphAssertionsPlugin.kt$ModuleGraphAssertionsPlugin$checkNotFound: UnknownTaskException</ID>
<ID>TooManyFunctions:DependencyGraph.kt$DependencyGraph</ID>
<ID>TooManyFunctions:ModuleGraphAssertionsPlugin.kt$ModuleGraphAssertionsPlugin : Plugin</ID>
</CurrentIssues>
</SmellBaseline>
27 changes: 16 additions & 11 deletions plugin/src/main/kotlin/com/jraska/module/graph/DependencyGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ class DependencyGraph private constructor() {
}

fun longestPath(key: String): LongestPath {
val nodeNames = nodes.getValue(key)
.longestPath()
.map { it.key }
val nodeNames =
nodes.getValue(key)
.longestPath()
.map { it.key }

return LongestPath(nodeNames)
}
Expand All @@ -54,7 +55,7 @@ class DependencyGraph private constructor() {
modulesCount = nodes.size,
edgesCount = edgesCount,
height = height,
longestPath = longestPath()
longestPath = longestPath(),
)
}

Expand All @@ -74,11 +75,14 @@ class DependencyGraph private constructor() {
fun serializableGraph(): SerializableGraph {
return SerializableGraph(
ArrayList(dependencyPairs()),
nodes.keys.first()
nodes.keys.first(),
)
}

private fun addConnections(node: Node, into: MutableList<Pair<String, String>>) {
private fun addConnections(
node: Node,
into: MutableList<Pair<String, String>>,
) {
node.dependsOn.forEach {
into.add(node.key to it.key)
addConnections(it, into)
Expand All @@ -91,7 +95,7 @@ class DependencyGraph private constructor() {

class SerializableGraph(
val dependencyPairs: ArrayList<Pair<String, String>>,
val firstModule: String
val firstModule: String,
) : Serializable

class Node(val key: String) {
Expand Down Expand Up @@ -135,9 +139,7 @@ class DependencyGraph private constructor() {
}

fun create(dependencies: List<Pair<String, String>>): DependencyGraph {
if (dependencies.isEmpty()) {
throw IllegalArgumentException("Graph cannot be empty. Use createSingular for cases with no dependencies")
}
require(dependencies.isNotEmpty()) { "Graph cannot be empty. Use createSingular for cases with no dependencies" }

val graph = DependencyGraph()
dependencies.forEach { graph.addEdge(it.first, it.second) }
Expand All @@ -156,7 +158,10 @@ class DependencyGraph private constructor() {
}
}

private fun DependencyGraph.addEdge(from: String, to: String) {
private fun DependencyGraph.addEdge(
from: String,
to: String,
) {
getOrCreate(from).dependsOn.add(getOrCreate(to))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ data class GraphStatistics(
val modulesCount: Int,
val edgesCount: Int,
val height: Int,
val longestPath: LongestPath
val longestPath: LongestPath,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jraska.module.graph

data class LongestPath(
val nodeNames: List<String>
val nodeNames: List<String>,
) {
fun pathString(): String {
return nodeNames.joinToString(" -> ")
Expand Down
9 changes: 6 additions & 3 deletions plugin/src/main/kotlin/com/jraska/module/graph/Parse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ object Parse {
return matcher(matcherText, NO_DEPENDENCY_SIGN_DIVIDER)
}

private fun matcher(matcherText: String, divider: String): RegexpDependencyMatcher {
if (!matcherText.contains(divider)) {
throw IllegalArgumentException("Incorrect format. Expected: 'regexp${divider}regexp', found $matcherText")
private fun matcher(
matcherText: String,
divider: String,
): RegexpDependencyMatcher {
require(matcherText.contains(divider)) {
"Incorrect format. Expected: 'regexp${divider}regexp', found $matcherText"
}

return RegexpDependencyMatcher(matcherText.toRegex(), divider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package com.jraska.module.graph

class RegexpDependencyMatcher(
private val matchingRegex: Regex,
private val divider: String
private val divider: String,
) : DependencyMatcher {

override fun matches(dependency: Pair<String, String>): Boolean {
val dependencyToMatch = "${dependency.first}$divider${dependency.second}"
return matchingRegex.matches(dependencyToMatch)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.jraska.module.graph.assertion

import com.jraska.module.graph.DependencyGraph
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.Project
import org.gradle.api.artifacts.ProjectDependency

object GradleDependencyGraphFactory {

fun create(project: Project, configurationsToLook: Set<String>): DependencyGraph {
fun create(
project: Project,
configurationsToLook: Set<String>,
): DependencyGraph {
val modulesWithDependencies = project.listAllDependencies(configurationsToLook)
val dependencies = modulesWithDependencies.flatMap { module ->
module.second.map { module.first to it }
}
val dependencies =
modulesWithDependencies.flatMap { module ->
module.second.map { module.first to it }
}

val moduleDisplayName = project.moduleDisplayName()
if(dependencies.isEmpty()) {
if (dependencies.isEmpty()) {
return DependencyGraph.createSingular(moduleDisplayName)
}

Expand All @@ -32,13 +35,14 @@ object GradleDependencyGraphFactory {
private fun Project.listAllDependencies(configurationsToLook: Set<String>): List<Pair<String, List<String>>> {
return (rootProject.subprojects + rootProject)
.map { project ->
project.moduleDisplayName() to project.configurations
.filter { configurationsToLook.contains(it.name) }
.flatMap { configuration ->
configuration.dependencies.filterIsInstance(ProjectDependency::class.java)
.map { it.dependencyProject }
}
.map { it.moduleDisplayName() }
project.moduleDisplayName() to
project.configurations
.filter { configurationsToLook.contains(it.name) }
.flatMap { configuration ->
configuration.dependencies.filterIsInstance(ProjectDependency::class.java)
.map { it.dependencyProject }
}
.map { it.moduleDisplayName() }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ class ModuleGraphAssertionsPlugin : Plugin<Project> {
private var outputFilePath: String? = null

override fun apply(project: Project) {
val graphRules = project.extensions.create(GraphRulesExtension::class.java, Api.EXTENSION_ROOT, GraphRulesExtension::class.java)
val graphRules =
project.extensions.create(
GraphRulesExtension::class.java,
Api.EXTENSION_ROOT,
GraphRulesExtension::class.java,
)

project.afterEvaluate {
addModulesAssertions(project, graphRules)
Expand Down Expand Up @@ -169,7 +174,11 @@ class ModuleGraphAssertionsPlugin : Plugin<Project> {
}
}

private fun restrictedDependenciesAssert(graphRules: GraphRulesExtension) = RestrictedDependenciesAssert(graphRules.restricted, aliases)
private fun restrictedDependenciesAssert(graphRules: GraphRulesExtension) =
RestrictedDependenciesAssert(
graphRules.restricted,
aliases,
)

private fun Project.addModuleAllowedRulesTask(graphRules: GraphRulesExtension): TaskProvider<AssertGraphTask>? {
if (graphRules.shouldAssertAllowed()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.io.Serializable

class ModuleTreeHeightAssert(
private val moduleName: String?,
private val maxHeight: Int
private val maxHeight: Int,
) : GraphAssert, Serializable {
override fun assert(dependencyGraph: DependencyGraph) {
if (moduleName == null) {
Expand All @@ -16,19 +16,26 @@ class ModuleTreeHeightAssert(
}
}

private fun assertModuleHeight(dependencyGraph: DependencyGraph, moduleName: String) {
private fun assertModuleHeight(
dependencyGraph: DependencyGraph,
moduleName: String,
) {
val height = dependencyGraph.heightOf(moduleName)
if (height > maxHeight) {
val longestPath = dependencyGraph.longestPath(moduleName)
throw GradleException("Module $moduleName is allowed to have maximum height of $maxHeight, but has $height, problematic dependencies: ${longestPath.pathString()}")
throw GradleException(
"Module $moduleName is allowed to have maximum height of $maxHeight, but has $height, problematic dependencies: ${longestPath.pathString()}",
)
}
}

private fun assertWholeGraphHeight(dependencyGraph: DependencyGraph) {
val height = dependencyGraph.height()
if (height > maxHeight) {
val longestPath = dependencyGraph.longestPath()
throw GradleException("Module Graph is allowed to have maximum height of $maxHeight, but has $height, problematic dependencies: ${longestPath.pathString()}")
throw GradleException(
"Module Graph is allowed to have maximum height of $maxHeight, but has $height, problematic dependencies: ${longestPath.pathString()}",
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction

open class AssertGraphTask : DefaultTask() {

@Input
lateinit var assertion: GraphAssert

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction

open class GenerateModulesGraphStatisticsTask : DefaultTask() {

@Optional
@Input
var onlyModuleStatistics: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ class DependencyGraphPerformanceTest {
fun setUp() {
val uri = javaClass.classLoader.getResource("graph/large-graph.txt")
val file = File(uri?.path!!)
val dependencyPairs = file.readLines().map {
val parts = it.split(" -> ")
parts[0] to parts[1]
}
val dependencyPairs =
file.readLines().map {
val parts = it.split(" -> ")
parts[0] to parts[1]
}

dependencyGraph = DependencyGraph.create(dependencyPairs)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ class DependencyGraphSerializationTest {

@Test
fun graphIsSerializable() {
val originalGraph = DependencyGraph.create(
"feature" to "lib",
"lib" to "core",
"app" to "feature",
"feature" to "core",
"app" to "core"
)
val originalGraph =
DependencyGraph.create(
"feature" to "lib",
"lib" to "core",
"app" to "feature",
"feature" to "core",
"app" to "core",
)

val deserializedGraph = serializeAndDeserializeGraph(originalGraph)

Expand Down
Loading

0 comments on commit 6135466

Please sign in to comment.