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

Add Gradle plugin to aggregate all graph tasks #215

Merged
merged 3 commits into from
Dec 13, 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
8 changes: 8 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ dependencies {
androidTestImplementation project(':core-testing')
}

apply plugin: 'com.jraska.module.graph'

moduleGraphRules {
maxHeight = 4
moduleLayersFromTheTop = [":feature", ":lib", ":core"]
restrinctInLayerDependencies = [":feature", ":lib"]
}

apply plugin: 'com.google.gms.google-services'
repositories {
mavenCentral()
Expand Down
23 changes: 1 addition & 22 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import com.jraska.github.client.tasks.*

buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.3.61'
repositories {
google()
maven { url 'https://maven.fabric.io/public' }
Expand Down Expand Up @@ -30,25 +28,6 @@ task clean(type: Delete) {
delete rootProject.buildDir
}

task generateModulesGraph(type: GenerateModulesGraphTask)

task generateOneModuleGraph(type: GenerateOneModuleGraphTask) {
moduleName = ":app"
}

task assertDependencyTreeHeight(type: AssertModuleTreeHeightTask) {
moduleName = ":app"
maxHeight = 4
}

task assertNoFeatureDependencies(type: AssertInLayerDependencies) {
layerPrefix = ":feature"
}

task assertCorrectLayerDependencies(type: AssertCrossLayerDependencies) {
layersFromTheTop = [":feature", ":lib", ":core"]
}

subprojects {
apply plugin: "org.jlleitschuh.gradle.ktlint"
project.plugins.whenPluginAdded { plugin ->
Expand Down
15 changes: 12 additions & 3 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
buildscript {
repositories {
jcenter()
// maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61"
}
}

apply plugin: 'kotlin'
apply plugin: 'java-gradle-plugin'

repositories {
jcenter()
}

dependencies {
implementation gradleApi()
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.50"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.61"

testImplementation 'junit:junit:4.12'
}
Expand All @@ -31,3 +31,12 @@ compileTestKotlin {
jvmTarget = "1.8"
}
}

gradlePlugin {
plugins {
moduleGraphAssertions {
id = 'com.jraska.module.graph'
implementationClass = 'com.jraska.module.graph.ModuleGraphAssertionsPlugin'
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.gradle.api.GradleException
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction

open class AssertCrossLayerDependencies : DefaultTask() {
open class AssertLayersOrderTask : DefaultTask() {
@Input
lateinit var layersFromTheTop: Array<String>

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

open class AssertInLayerDependencies : DefaultTask() {
open class AssertNoInLayerDependencies : DefaultTask() {
@Input
lateinit var layerPrefix: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ open class GenerateModulesGraphTask : DefaultTask() {

println(allModulesTree.statistics())
println(GraphvizWriter.toGraphviz(allModulesTree, layers.toSet()))
println(KotlinCodeWriter.toKotlinCode(allModulesTree))
}
}
17 changes: 17 additions & 0 deletions buildSrc/src/main/java/com/jraska/module/graph/Api.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.jraska.module.graph

object Api {
object Tasks {
const val GENERATE_GRAPHVIZ = "generateModulesGrapvizText"

const val ASSERT_ALL = "assertModulesGraph"
const val ASSERT_MAX_HEIGHT = "assertMaxHeight"
const val ASSERT_LAYER_ORDER = "assertModuleLayersOrder"
const val ASSERT_NO_IN_LAYER_PREFIX = "assertNoDependenciesWithin"
}

const val CHECK_TASK = "check"

const val EXTENSION_ROOT = "moduleGraphRules"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.jraska.module.graph

open class GraphRulesExtension {
var appModuleName = ":app"
var maxHeight: Int = 0
var moduleLayersFromTheTop = emptyArray<String>()
var restrinctInLayerDependencies = emptyArray<String>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.jraska.module.graph

import com.jraska.github.client.tasks.AssertLayersOrderTask
import com.jraska.github.client.tasks.AssertModuleTreeHeightTask
import com.jraska.github.client.tasks.AssertNoInLayerDependencies
import com.jraska.github.client.tasks.GenerateModulesGraphTask
import com.jraska.module.graph.Api.Tasks
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.language.base.plugins.LifecycleBasePlugin.VERIFICATION_GROUP
import java.util.Locale

@Suppress("unused") // Used as plugin
class ModuleGraphAssertionsPlugin : Plugin<Project> {

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

project.afterEvaluate {
addModulesAssertions(project, graphRules)
}
}

private fun addModulesAssertions(project: Project, graphRules: GraphRulesExtension) {
project.addModuleGraphGeneration()

val allAssertionsTask = project.tasks.create(Tasks.ASSERT_ALL)
allAssertionsTask.group = VERIFICATION_GROUP
project.tasks.find { it.name == Api.CHECK_TASK }?.dependsOn(allAssertionsTask)

project.addMaxHeightTasks(graphRules).forEach { allAssertionsTask.dependsOn(it) }
project.addModuleLayersTasks(graphRules).forEach { allAssertionsTask.dependsOn(it) }
project.addInLayerDependencyTasks(graphRules).forEach { allAssertionsTask.dependsOn(it) }
}

private fun Project.addModuleGraphGeneration() {
tasks.create(Tasks.GENERATE_GRAPHVIZ, GenerateModulesGraphTask::class.java)
}

private fun Project.addMaxHeightTasks(graphRules: GraphRulesExtension): List<Task> {
if (graphRules.maxHeight <= 0) {
return emptyList()
}

val task = tasks.create(Tasks.ASSERT_MAX_HEIGHT, AssertModuleTreeHeightTask::class.java)
task.maxHeight = graphRules.maxHeight
task.moduleName = graphRules.appModuleName
task.group = VERIFICATION_GROUP

return listOf(task)
}

private fun Project.addModuleLayersTasks(graphRules: GraphRulesExtension): List<Task> {
if (graphRules.moduleLayersFromTheTop.isEmpty()) {
return emptyList()
}

val task = tasks.create(Tasks.ASSERT_LAYER_ORDER, AssertLayersOrderTask::class.java)
task.layersFromTheTop = graphRules.moduleLayersFromTheTop
task.group = VERIFICATION_GROUP

return listOf(task)
}

private fun Project.addInLayerDependencyTasks(graphRules: GraphRulesExtension): List<Task> {
return graphRules.restrinctInLayerDependencies.map { layerPrefix ->
val taskNameSuffix = layerPrefix.replace(":", "").capitalizeFirst()
val task = tasks.create("${Tasks.ASSERT_NO_IN_LAYER_PREFIX}$taskNameSuffix", AssertNoInLayerDependencies::class.java)
task.layerPrefix = layerPrefix
task.group = VERIFICATION_GROUP

return@map task
}
}

private fun String.capitalizeFirst(): String {
return this.substring(0, 1).toUpperCase(Locale.US).plus(this.substring(1))
}
}