Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update structure of buildSrc #212

Merged
merged 1 commit into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import com.jraska.github.client.*
import com.jraska.github.client.tasks.*

buildscript {
ext.kotlin_version = '1.3.50'
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package com.jraska.github.client

import com.jraska.github.client.graph.DependencyGraph
import org.gradle.api.Project
import org.gradle.api.internal.artifacts.dependencies.DefaultProjectDependency

object DependencyTreeFactory {
object GradleDependencyGraphFactory {

fun create(project: Project): DependencyGraph {
val dependencies = project.listDependencyPairs()

val modulesTree = DependencyGraph()
dependencies.forEach {
modulesTree.addEdge(it.first, it.second)
}

return modulesTree
return DependencyGraph.create(dependencies)
}

private fun Project.listDependencyPairs(): List<Pair<String, String>> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jraska.github.client
package com.jraska.github.client.graph

class DependencyGraph() {
private val nodes = mutableMapOf<String, Node>()
Expand Down Expand Up @@ -47,10 +47,6 @@ class DependencyGraph() {
)
}

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

fun subTree(key: String): DependencyGraph {
val dependencyTree = DependencyGraph()

Expand All @@ -66,6 +62,10 @@ class DependencyGraph() {
}
}

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

private fun countEdges(): Int {
return nodes().flatMap { node -> node.dependsOn }.count()
}
Expand Down Expand Up @@ -100,4 +100,12 @@ class DependencyGraph() {
}
}
}

companion object {
fun create(dependencies: List<Pair<String, String>>): DependencyGraph {
val graph = DependencyGraph()
dependencies.forEach { graph.addEdge(it.first, it.second) }
return graph
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jraska.github.client
package com.jraska.github.client.graph

data class GraphStatistics(
val modulesCount: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.jraska.github.client
package com.jraska.github.client.graph

object GraphvizGenerator {
object GraphvizWriter {
fun toGraphviz(dependencyGraph: DependencyGraph, groups: Set<String> = emptySet()): String {

val longestPathConnections = dependencyGraph.longestPath()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jraska.github.client
package com.jraska.github.client.graph

class LongestPath(
val nodeNames: List<String>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.jraska.github.client
package com.jraska.github.client.tasks

import com.jraska.github.client.GradleDependencyGraphFactory
import com.jraska.github.client.graph.DependencyGraph
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.tasks.Input
Expand All @@ -11,7 +13,7 @@ open class AssertCrossLayerDependencies : DefaultTask() {

@TaskAction
fun run() {
val modulesTree = DependencyTreeFactory.create(project)
val modulesTree = GradleDependencyGraphFactory.create(project)

verifyAllLayersHaveModule(modulesTree)

Expand All @@ -27,7 +29,7 @@ open class AssertCrossLayerDependencies : DefaultTask() {
private fun buildErrorMessage(againstLayerDependencies: List<Pair<DependencyGraph.Node, DependencyGraph.Node>>): String {
val errorsMessage = againstLayerDependencies.joinToString("\n") { " Module '${it.first.key}' cannot depend on '${it.second.key}'." }

return "Dependencies agaist direction of layers '${layersDependencyString()}' are not allowed. The violating dependencies are: \n$errorsMessage"
return "Dependencies against direction of layers '${layersDependencyString()}' are not allowed. The violating dependencies are: \n$errorsMessage"
}

private fun layersDependencyString(): String {
Expand All @@ -38,10 +40,7 @@ open class AssertCrossLayerDependencies : DefaultTask() {
val nodes = modulesTree.nodes()

for (layerPrefix in layersFromTheTop) {
val someNodeInLayer = nodes.find { it.key.startsWith(layerPrefix) }
if (someNodeInLayer == null) {
throw GradleException("There is no module, which belongs to layer '$layerPrefix'")
}
nodes.find { it.key.startsWith(layerPrefix) } ?: throw GradleException("There is no module, which belongs to layer '$layerPrefix'")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.jraska.github.client
package com.jraska.github.client.tasks

import com.jraska.github.client.GradleDependencyGraphFactory
import com.jraska.github.client.graph.DependencyGraph
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.tasks.Input
Expand All @@ -11,7 +13,7 @@ open class AssertInLayerDependencies : DefaultTask() {

@TaskAction
fun run() {
val modulesTree = DependencyTreeFactory.create(project)
val modulesTree = GradleDependencyGraphFactory.create(project)

val inLayerDependencies = modulesTree.nodes()
.filter { it.key.startsWith(layerPrefix) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jraska.github.client
package com.jraska.github.client.tasks

import com.jraska.github.client.GradleDependencyGraphFactory
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.tasks.Input
Expand All @@ -14,7 +15,7 @@ open class AssertModuleTreeHeightTask : DefaultTask() {

@TaskAction
fun run() {
val modulesTree = DependencyTreeFactory.create(project)
val modulesTree = GradleDependencyGraphFactory.create(project)

val height = modulesTree.heightOf(moduleName)
if (height > maxHeight) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.jraska.github.client.tasks

import com.jraska.github.client.GradleDependencyGraphFactory
import com.jraska.github.client.graph.GraphvizWriter
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction

open class GenerateModulesGraphTask : DefaultTask() {
@Input
var layers: Array<String> = emptyArray()

@TaskAction
fun run() {
val allModulesTree = GradleDependencyGraphFactory.create(project)

println(allModulesTree.statistics())
println(GraphvizWriter.toGraphviz(allModulesTree, layers.toSet()))
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.jraska.github.client
package com.jraska.github.client.tasks

import com.jraska.github.client.GradleDependencyGraphFactory
import com.jraska.github.client.graph.GraphvizWriter
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
Expand All @@ -10,10 +12,10 @@ open class GenerateOneModuleGraphTask : DefaultTask() {

@TaskAction
fun run() {
val allModulesTree = DependencyTreeFactory.create(project)
val allModulesTree = GradleDependencyGraphFactory.create(project)
val moduleTree = allModulesTree.subTree(moduleName)

println(moduleTree.statistics())
println(GraphvizGenerator.toGraphviz(moduleTree))
println(GraphvizWriter.toGraphviz(moduleTree))
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.jraska.github.client.graph

import org.junit.Test

class DependencyGraphTest {
@Test
fun correctHeightIsMaintained() {
val dependencyTree = DependencyGraph.create(
listOf(
"app" to "feature",
"app" to "lib",
"feature" to "lib",
"lib" to "core"
)
)

assert(dependencyTree.heightOf("app") == 3)
}

@Test
fun findsProperLongestPath() {
val dependencyTree = DependencyGraph.create(
listOf(
"app" to "feature",
"app" to "lib",
"app" to "core",
"feature" to "lib",
"lib" to "core"
)
)

assert(dependencyTree.longestPath("app").nodeNames == listOf("app", "feature", "lib", "core"))
}

@Test
fun findsProperRoot() {
val dependencyTree = DependencyGraph.create(
listOf(
"feature" to "lib",
"lib" to "core",
"app" to "feature",
"app" to "lib",
"app" to "core"
)
)

assert(dependencyTree.findRoot().key == "app")
}

@Test
fun createSubtreeProperly() {
val dependencyTree = DependencyGraph.create(
listOf(
"feature" to "lib",
"lib" to "core",
"app" to "feature",
"feature" to "core",
"app" to "core"
)
)

val subTree = dependencyTree.subTree("feature")

assert(subTree.findRoot().key == "feature")
assert(subTree.heightOf("feature") == 2)
assert(subTree.longestPath("feature").nodeNames == listOf("feature", "lib", "core"))
}

@Test
fun createCountStatisticsWell() {
val dependencyTree = DependencyGraph.create(
listOf(
"feature" to "lib",
"lib" to "core",
"app" to "feature",
"feature" to "core",
"app" to "core"
)
)

val statistics = dependencyTree.statistics()

assert(statistics.height == 3)
assert(statistics.modulesCount == 4)
assert(statistics.edgesCount == 5)
}
}