From 710a949f4adbfea05261b012a5b9583392bd2b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Kwiecin=CC=81ski?= Date: Sat, 2 Apr 2022 20:49:18 +0200 Subject: [PATCH] Avoid using `project` reference. Avoid breaking Configuration Cache accidentally --- .../kotlinter/tasks/ConfigurableKtLintTask.kt | 26 +++++++++++-------- .../gradle/kotlinter/tasks/FormatTask.kt | 17 +++++++----- .../gradle/kotlinter/tasks/GitHookTasks.kt | 2 +- .../gradle/kotlinter/tasks/LintTask.kt | 19 +++++++++----- 4 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt index 8935daf4..e76eaaba 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt @@ -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 @@ -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 = property() + val indentSize: Property = objectFactory.property() @Input - val experimentalRules: Property = property(default = DEFAULT_EXPERIMENTAL_RULES) + val experimentalRules: Property = objectFactory.property(default = DEFAULT_EXPERIMENTAL_RULES) @Input - val disabledRules: ListProperty = listProperty(default = DEFAULT_DISABLED_RULES.toList()) + val disabledRules: ListProperty = objectFactory.listProperty(default = DEFAULT_DISABLED_RULES.toList()) @Internal protected fun getKtLintParams(): KtLintParams = KtLintParams( @@ -33,18 +37,18 @@ abstract class ConfigurableKtLintTask : SourceTask() { ) } -internal inline fun DefaultTask.property(default: T? = null): Property = - project.objects.property(T::class.java).apply { +internal inline fun ObjectFactory.property(default: T? = null): Property = + property(T::class.java).apply { set(default) } -internal inline fun DefaultTask.listProperty(default: Iterable = emptyList()): ListProperty = - project.objects.listProperty(T::class.java).apply { +internal inline fun ObjectFactory.listProperty(default: Iterable = emptyList()): ListProperty = + listProperty(T::class.java).apply { set(default) } -internal inline fun DefaultTask.mapProperty(default: Map = emptyMap()): MapProperty = - project.objects.mapProperty(K::class.java, V::class.java).apply { +internal inline fun ObjectFactory.mapProperty(default: Map = emptyMap()): MapProperty = + mapProperty(K::class.java, V::class.java).apply { set(default) } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt index 1ee104d2..cae4f0c6 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt @@ -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 @@ -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 } @@ -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) } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/GitHookTasks.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/GitHookTasks.kt index 7800352e..d1d9fdb3 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/GitHookTasks.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/GitHookTasks.kt @@ -34,7 +34,7 @@ open class InstallPrePushHookTask : InstallHookTask("pre-push") { */ abstract class InstallHookTask(@get:Internal val hookFileName: String) : DefaultTask() { @Input - val gitDirPath: Property = property(default = ".git") + val gitDirPath: Property = project.objects.property(default = ".git") @get:Internal abstract val hookContent: String diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt index f52fb79b..c0aa674d 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt @@ -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 @@ -22,20 +24,23 @@ 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 = mapProperty(default = emptyMap()) + val reports: MapProperty = objectFactory.mapProperty(default = emptyMap()) @InputFiles @PathSensitive(PathSensitivity.RELATIVE) override fun getSource(): FileTree = super.getSource() @Input - val ignoreFailures: Property = property(default = DEFAULT_IGNORE_FAILURES) - - private val projectDir = project.projectDir + val ignoreFailures: Property = objectFactory.property(default = DEFAULT_IGNORE_FAILURES) @TaskAction fun run() { @@ -43,7 +48,7 @@ open class LintTask @Inject constructor( 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()) }