Skip to content

Commit

Permalink
Avoid using project reference. Avoid breaking Configuration Cache a…
Browse files Browse the repository at this point in the history
…ccidentally
  • Loading branch information
mateuszkwiecinski committed Apr 3, 2022
1 parent c5a854d commit 710a949
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jmailen.gradle.kotlinter.tasks

import org.gradle.api.DefaultTask
import org.gradle.api.file.ProjectLayout
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
Expand All @@ -13,17 +14,20 @@ import org.jmailen.gradle.kotlinter.KotlinterExtension.Companion.DEFAULT_DISABLE
import org.jmailen.gradle.kotlinter.KotlinterExtension.Companion.DEFAULT_EXPERIMENTAL_RULES
import org.jmailen.gradle.kotlinter.support.KtLintParams

abstract class ConfigurableKtLintTask : SourceTask() {
abstract class ConfigurableKtLintTask(
projectLayout: ProjectLayout,
objectFactory: ObjectFactory,
) : SourceTask() {

@Input
@Optional
val indentSize: Property<Int> = property()
val indentSize: Property<Int> = objectFactory.property()

@Input
val experimentalRules: Property<Boolean> = property(default = DEFAULT_EXPERIMENTAL_RULES)
val experimentalRules: Property<Boolean> = objectFactory.property(default = DEFAULT_EXPERIMENTAL_RULES)

@Input
val disabledRules: ListProperty<String> = listProperty(default = DEFAULT_DISABLED_RULES.toList())
val disabledRules: ListProperty<String> = objectFactory.listProperty(default = DEFAULT_DISABLED_RULES.toList())

@Internal
protected fun getKtLintParams(): KtLintParams = KtLintParams(
Expand All @@ -33,18 +37,18 @@ abstract class ConfigurableKtLintTask : SourceTask() {
)
}

internal inline fun <reified T> DefaultTask.property(default: T? = null): Property<T> =
project.objects.property(T::class.java).apply {
internal inline fun <reified T> ObjectFactory.property(default: T? = null): Property<T> =
property(T::class.java).apply {
set(default)
}

internal inline fun <reified T> DefaultTask.listProperty(default: Iterable<T> = emptyList()): ListProperty<T> =
project.objects.listProperty(T::class.java).apply {
internal inline fun <reified T> ObjectFactory.listProperty(default: Iterable<T> = emptyList()): ListProperty<T> =
listProperty(T::class.java).apply {
set(default)
}

internal inline fun <reified K, reified V> DefaultTask.mapProperty(default: Map<K, V> = emptyMap()): MapProperty<K, V> =
project.objects.mapProperty(K::class.java, V::class.java).apply {
internal inline fun <reified K, reified V> ObjectFactory.mapProperty(default: Map<K, V> = emptyMap()): MapProperty<K, V> =
mapProperty(K::class.java, V::class.java).apply {
set(default)
}

Expand Down
17 changes: 11 additions & 6 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.jmailen.gradle.kotlinter.tasks

import org.gradle.api.GradleException
import org.gradle.api.file.ProjectLayout
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
Expand All @@ -12,14 +14,17 @@ import org.jmailen.gradle.kotlinter.tasks.format.FormatWorkerAction
import javax.inject.Inject

open class FormatTask @Inject constructor(
private val workerExecutor: WorkerExecutor
) : ConfigurableKtLintTask() {
private val workerExecutor: WorkerExecutor,
objectFactory: ObjectFactory,
private val projectLayout: ProjectLayout,
) : ConfigurableKtLintTask(
projectLayout = projectLayout,
objectFactory = objectFactory,
) {

@OutputFile
@Optional
val report: RegularFileProperty = project.objects.fileProperty()

private val projectDir = project.projectDir
val report: RegularFileProperty = objectFactory.fileProperty()

init {
outputs.upToDateWhen { false }
Expand All @@ -31,7 +36,7 @@ open class FormatTask @Inject constructor(
submit(FormatWorkerAction::class.java) { p ->
p.name.set(name)
p.files.from(source)
p.projectDirectory.set(projectDir)
p.projectDirectory.set(projectLayout.projectDirectory.asFile)
p.ktLintParams.set(getKtLintParams())
p.output.set(report)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ open class InstallPrePushHookTask : InstallHookTask("pre-push") {
*/
abstract class InstallHookTask(@get:Internal val hookFileName: String) : DefaultTask() {
@Input
val gitDirPath: Property<String> = property(default = ".git")
val gitDirPath: Property<String> = project.objects.property(default = ".git")

@get:Internal
abstract val hookContent: String
Expand Down
19 changes: 12 additions & 7 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.jmailen.gradle.kotlinter.tasks

import org.gradle.api.GradleException
import org.gradle.api.file.FileTree
import org.gradle.api.file.ProjectLayout
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.CacheableTask
Expand All @@ -22,28 +24,31 @@ import javax.inject.Inject

@CacheableTask
open class LintTask @Inject constructor(
private val workerExecutor: WorkerExecutor
) : ConfigurableKtLintTask() {
private val workerExecutor: WorkerExecutor,
objectFactory: ObjectFactory,
private val projectLayout: ProjectLayout,
) : ConfigurableKtLintTask(
projectLayout = projectLayout,
objectFactory = objectFactory,
) {

@OutputFiles
val reports: MapProperty<String, File> = mapProperty(default = emptyMap())
val reports: MapProperty<String, File> = objectFactory.mapProperty(default = emptyMap())

@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
override fun getSource(): FileTree = super.getSource()

@Input
val ignoreFailures: Property<Boolean> = property(default = DEFAULT_IGNORE_FAILURES)

private val projectDir = project.projectDir
val ignoreFailures: Property<Boolean> = objectFactory.property(default = DEFAULT_IGNORE_FAILURES)

@TaskAction
fun run() {
val result = with(workerExecutor.noIsolation()) {
submit(LintWorkerAction::class.java) { p ->
p.name.set(name)
p.files.from(source)
p.projectDirectory.set(projectDir)
p.projectDirectory.set(projectLayout.projectDirectory.asFile)
p.reporters.putAll(reports)
p.ktLintParams.set(getKtLintParams())
}
Expand Down

0 comments on commit 710a949

Please sign in to comment.