rspec/rules/S6632/kotlin/rule.adoc
2023-08-16 10:09:30 +02:00

89 lines
2.3 KiB
Plaintext

== Why is this an issue?
Custom tasks and plugins should be moved from the build script to the `buildSrc` directory
to encapsulate custom build logic and separate it against its application in the build script.
This helps to keep the build script clean and easy to understand.
=== What is the potential impact?
==== Readability and Understanding
This change makes the build script easier to read and understand due to encapsulation and separation of concerns.
== How to fix it
Move custom tasks, plugins, and configuration extensions from your `./build.gradle.kts` file
to source files in `./buildSrc/src/main/kotlin`.
You should apply package names and folders like for any other source code.
Please see the attached links for details about how to write custom plugins and the proper use of the `./buildSrc` directory.
=== Code examples
==== Noncompliant code example
**./build.gradle.kts**
[source,kotlin,diff-id=1,diff-type=noncompliant]
----
open class MyCustomTask : DefaultTask() {
// ...
}
tasks.register<MyCustomTask>("myCustom")
open class MyCustomExtension {
var flag: Boolean = false
}
extensions.create<MyCustomExtension>("myCustomExtension")
val myCustomExtension: MyCustomExtension by project
myCustomExtension.flag = true
----
==== Compliant solution
**./buildSrc/src/main/kotlin/mypackage/MyCustomTask.kt**
[source,kotlin]
----
package mypackage
import org.gradle.api.DefaultTask
open class MyCustomTask : DefaultTask() {
// ...
}
----
**./buildSrc/src/main/kotlin/mypackage/MyCustomExtension.kt**
[source,kotlin]
----
package mypackage
open class MyCustomExtension {
var flag: Boolean = false
}
----
**./build.gradle.kts**
[source,kotlin,diff-id=1,diff-type=compliant]
----
import mypackage.MyCustomExtension
import mypackage.MyCustomTask
tasks.register<MyCustomTask>("myCustom")
extensions.create<MyCustomExtension>("myCustomExtension")
val myCustomExtension: MyCustomExtension by project
myCustomExtension.flag = true
----
== Resources
=== Documentation
* https://docs.gradle.org/current/userguide/organizing_gradle_projects.html#sec:build_sources[Gradle Documentation - Use "buildSrc" to abstract imperative logic]
* https://docs.gradle.org/current/userguide/custom_tasks.html#custom_tasks[Gradle Documentation - Developing Custom Gradle Task Types]