Skip to content

Commit

Permalink
change: Quality Plugin is now automatically applied (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski authored Apr 10, 2021
1 parent 7f23857 commit 14c0f6b
Show file tree
Hide file tree
Showing 20 changed files with 45 additions and 118 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Divide plugins into multiple functional groups to avoid adding unnecessary dependencies
- Update _Detekt_ to version _1.16.0_ see [release notes](https://github.com/detekt/detekt/releases/tag/v1.16.0)
- Update _kotlinter-gradle_ to version _3.4.0_, see [release notes](https://github.com/jeremymailen/kotlinter-gradle/releases/tag/3.4.0)
- Quality plugin can't be disabled anymore

## [0.23.0] - 2021-02-06
## Changed
Expand Down
8 changes: 3 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,17 @@ apply plugin: 'com.starter.config'
commonConfig {
javaVersion JavaVersion.VERSION_1_8
javaFilesAllowed true
javaFilesAllowed = true
androidPlugin {
compileSdkVersion 29
minSdkVersion 23
targetSdkVersion 29
}
qualityPlugin {
enabled true
formatOnCompile false
formatOnCompile = false
}
versioningPlugin {
enabled true
enabled = true
}
}
```
Expand All @@ -165,7 +164,6 @@ commonConfig {
- `androidPlugin`:
- contains values passed to _Android Gradle Plugin_
- `qualityPlugin`:
- `enabled` - enables/disables [Quality Plugin](#quality-plugin)
- `formatOnCompile` - defines if ktlint should format source code on every compilation
- `versioningPlugin`:
- `enabled` - enables/disables [Versioning Plugin](#versioning-plugin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AndroidApplicationPlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
pluginManager.apply("com.android.application")
pluginManager.apply("kotlin-android")
pluginManager.apply("com.starter.quality")
pluginManager.apply(ConfigurationPlugin::class.java)

extensions.create("projectConfig", AndroidApplicationConfigExtension::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AndroidLibraryPlugin : Plugin<Project> {
override fun apply(target: Project): Unit = with(target) {
pluginManager.apply("com.android.library")
pluginManager.apply("kotlin-android")
pluginManager.apply("com.starter.quality")
pluginManager.apply(ConfigurationPlugin::class.java)

val rootConfig = this.rootConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,26 +216,4 @@ internal class AndroidApplicataionPluginTest : WithGradleProjectTest() {
assertThat(qualityEnabled.task(":module1:projectCodeStyle")?.outcome).isNotNull()
assertThat(qualityEnabled.task(":module2:projectCodeStyle")?.outcome).isNotNull()
}

@Test
fun `does not configure quality plugin if disabled using configuration plugin`() {
@Language("groovy")
val qualityScript =
"""
plugins {
id('com.starter.config')
}
commonConfig {
qualityPlugin {
enabled = false
}
}
""".trimIndent()
rootBuildScript.writeText(qualityScript)

val qualityDisabled = runTask("projectCodeStyle", shouldFail = true)

assertThat(qualityDisabled.output).contains("Task 'projectCodeStyle' not found ")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,26 +266,4 @@ internal class AndroidLibraryPluginTest : WithGradleProjectTest() {
assertThat(qualityEnabled.task(":module1:projectCodeStyle")?.outcome).isNotNull()
assertThat(qualityEnabled.task(":module2:projectCodeStyle")?.outcome).isNotNull()
}

@Test
fun `does not configured quality plugin if disabled using configuration plugin`() {
@Language("groovy")
val qualityScript =
"""
plugins {
id('com.starter.config')
}
commonConfig {
qualityPlugin {
enabled = false
}
}
""".trimIndent()
rootBuildScript.writeText(qualityScript)

val qualityDisabled = runTask("projectCodeStyle", shouldFail = true)

assertThat(qualityDisabled.output).contains("Task 'projectCodeStyle' not found ")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ internal class AndroidQualityPluginTest : WithGradleProjectTest() {
}
commonConfig {
javaFilesAllowed false
javaFilesAllowed = false
}
""".trimIndent()
rootDirectory.resolve("build.gradle").writeText(buildscript)
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ allprojects {
tasks.withType(KotlinCompile).configureEach {
kotlinOptions {
apiVersion = '1.3'
useIR = true
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.project.starter.config

import org.gradle.api.Project
import org.gradle.api.model.ObjectFactory
import org.gradle.api.plugins.ExtensionContainer

inline fun <reified T> ExtensionContainer.getByType() =
Expand All @@ -10,3 +11,7 @@ inline fun <reified T> Project.withExtension(crossinline action: Project.(T) ->
afterEvaluate {
it.action(it.extensions.getByType())
}

inline fun <reified T> ObjectFactory.property(value: T?) = property(T::class.java).apply {
value(value)
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,10 @@ open class RootConfigExtension(

open class QualityPluginConfig(
var formatOnCompile: Boolean = false,
var enabled: Boolean = true
) {
fun formatOnCompile(value: Boolean) {
formatOnCompile = value
}

fun enabled(value: Boolean) {
enabled = value
}
}

open class AndroidPluginConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ internal class CommonSettingsPluginTest : WithGradleProjectTest() {
targetSdkVersion = 30
}
qualityPlugin {
enabled = true
formatOnCompile = true
}
versioningPlugin {
Expand Down Expand Up @@ -89,7 +88,6 @@ internal class CommonSettingsPluginTest : WithGradleProjectTest() {
targetSdkVersion 30
}
qualityPlugin {
enabled true
formatOnCompile true
}
versioningPlugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,17 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
class ConfigurationPlugin : Plugin<Project> {

override fun apply(target: Project): Unit = with(target) {
afterEvaluate {
if (rootConfig.quality.enabled) {
pluginManager.apply("com.starter.quality")
}
}
afterEvaluate {
if (rootConfig.versioning.enabled) {
if (!rootProject.pluginManager.hasPlugin("com.starter.versioning")) {
logger.info("Apply com.starter.versioning to $rootProject")
rootProject.pluginManager.apply(VersioningPlugin::class.java)
}
}
tasks.withType(KotlinCompile::class.java).configureEach {
it.kotlinOptions.jvmTarget = rootConfig.javaVersion.toString()
}
}
configureRepositories()

val javaVersion = rootConfig.javaVersion
tasks.withType(KotlinCompile::class.java).configureEach {
it.kotlinOptions.jvmTarget = javaVersion.toString()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class KotlinLibraryPlugin : Plugin<Project> {

override fun apply(target: Project) = with(target) {
pluginManager.apply("kotlin")
pluginManager.apply("com.starter.quality")
pluginManager.apply(ConfigurationPlugin::class.java)

extensions.create("projectConfig", KotlinLibraryConfigExtension::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,28 +145,6 @@ internal class KotlinLibraryPluginTest : WithGradleProjectTest() {
assertThat(qualityEnabled.task(":module2:projectCodeStyle")?.outcome).isNotNull()
}

@Test
fun `does not configure quality plugin if disabled using configuration plugin`() {
@Language("groovy")
val qualityScript =
"""
plugins {
id('com.starter.config')
}
commonConfig {
qualityPlugin {
enabled = false
}
}
""".trimIndent()
rootBuildScript.writeText(qualityScript)

val qualityDisabled = runTask("projectCodeStyle", shouldFail = true)

assertThat(qualityDisabled.output).contains("Task 'projectCodeStyle' not found ")
}

@Test
fun `configures versioning plugin by default`() {
val git = setupGit(origin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ package com.project.starter.quality.internal

import com.android.build.gradle.BaseExtension
import com.android.build.gradle.api.AndroidSourceSet
import com.android.build.gradle.internal.tasks.factory.dependsOn
import com.project.starter.config.extensions.RootConfigExtension
import com.project.starter.config.plugins.rootConfig
import com.project.starter.config.withExtension
import com.project.starter.quality.extensions.JavaSourcesAware
import com.project.starter.quality.tasks.GenerateCheckstyleBaselineTask.Companion.addGenerateCheckstyleBaselineTask
Expand All @@ -15,7 +14,7 @@ import org.gradle.api.file.FileTree
import org.gradle.api.plugins.quality.Checkstyle
import org.gradle.api.plugins.quality.CheckstyleExtension

private val pluginsWithConfgiuration = listOf(
private val pluginsWithConfiguration = listOf(
Config(
plugin = "kotlin",
starterPlugin = "com.starter.library.kotlin",
Expand All @@ -39,8 +38,8 @@ private data class Config(
val configuration: Project.() -> Unit
)

internal fun Project.configureCheckstyle(rootConfig: RootConfigExtension) {
pluginsWithConfgiuration.forEach { (plugin, starterPlugin, configuration) ->
internal fun Project.configureCheckstyle() {
pluginsWithConfiguration.forEach { (plugin, starterPlugin, configuration) ->
pluginManager.withPlugin(plugin) {
if (pluginManager.hasPlugin(starterPlugin)) {
withExtension<JavaSourcesAware> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.project.starter.quality.internal

import com.project.starter.config.extensions.RootConfigExtension
import com.project.starter.config.plugins.rootConfig
import com.project.starter.quality.tasks.ProjectCodeStyleTask
import io.gitlab.arturbosch.detekt.Detekt
import io.gitlab.arturbosch.detekt.DetektPlugin
import io.gitlab.arturbosch.detekt.extensions.DetektExtension
import org.gradle.api.Project

internal fun Project.configureDetekt(config: RootConfigExtension) {
internal fun Project.configureDetekt() {
pluginManager.apply(DetektPlugin::class.java)

extensions.configure(DetektExtension::class.java) { detekt ->
Expand All @@ -26,6 +26,6 @@ internal fun Project.configureDetekt(config: RootConfigExtension) {
it.dependsOn("$path:detekt")
}
tasks.withType(Detekt::class.java) {
it.jvmTarget = config.javaVersion.toString()
it.jvmTarget = rootConfig.javaVersion.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,10 @@ class QualityPlugin : Plugin<Project> {
repositories.mavenCentral()
addProjectCodeStyleTask()
configureKtlint()
configureDetekt(rootConfig)
configureCheckstyle(rootConfig)
configureDetekt()
configureCheckstyle()
configureIssueCheckerTask()

val config = rootConfig.quality
afterEvaluate {
if (config.formatOnCompile) {
applyFormatOnRecompile()
}
}
configureFormatOnRecompile()
}

private fun Project.configureIssueCheckerTask() {
Expand All @@ -59,20 +53,26 @@ class QualityPlugin : Plugin<Project> {
}
}

private fun Project.applyFormatOnRecompile() {
private fun Project.configureFormatOnRecompile() {
pluginManager.withPlugin("kotlin") {
tasks.named("compileKotlin") {
it.dependsOn("$path:formatKotlin")
if (rootConfig.quality.formatOnCompile) {
it.dependsOn("$path:formatKotlin")
}
}
}
pluginManager.withPlugin("com.android.library") {
tasks.named("preBuild") {
it.dependsOn("$path:formatKotlin")
if (rootConfig.quality.formatOnCompile) {
it.dependsOn("$path:formatKotlin")
}
}
}
pluginManager.withPlugin("com.android.application") {
tasks.named("preBuild") {
it.dependsOn("$path:formatKotlin")
if (rootConfig.quality.formatOnCompile) {
it.dependsOn("$path:formatKotlin")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ internal class QualityPluginTest : WithGradleProjectTest() {
commonConfig {
qualityPlugin {
formatOnCompile = true
formatOnCompile true
}
}
""".trimIndent()
Expand Down Expand Up @@ -138,7 +138,7 @@ internal class QualityPluginTest : WithGradleProjectTest() {
}
commonConfig {
javaFilesAllowed false
javaFilesAllowed = false
}
repositories.jcenter()
Expand Down
6 changes: 3 additions & 3 deletions sample/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ buildscript {
apply plugin: 'com.starter.config'

commonConfig {
javaFilesAllowed false
javaFilesAllowed = false
versioningPlugin {
enabled false
enabled = false
}
}
}
6 changes: 3 additions & 3 deletions sample/kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ buildscript {
apply plugin: 'com.starter.config'

commonConfig {
javaFilesAllowed false
javaFilesAllowed = false
versioningPlugin {
enabled false
enabled = false
}
}
}

0 comments on commit 14c0f6b

Please sign in to comment.