From 36e7d7e2679098391a32bb8aad166aa95c1c208d Mon Sep 17 00:00:00 2001 From: Paul Woitaschek Date: Sat, 10 Sep 2022 07:39:33 +0200 Subject: [PATCH] Make the git hook tasks compatible with configuration cache (#270) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mateusz Kwieciński --- .../jmailen/gradle/kotlinter/tasks/GitHookTasks.kt | 12 ++++++++---- .../gradle/kotlinter/functional/WithGradleTest.kt | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) 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 664ba302..d7293210 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/GitHookTasks.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/GitHookTasks.kt @@ -8,7 +8,7 @@ import org.gradle.api.tasks.TaskAction import org.jmailen.gradle.kotlinter.support.VersionProperties import java.io.File -open class InstallPreCommitHookTask : InstallHookTask("pre-commit") { +abstract class InstallPreCommitHookTask : InstallHookTask("pre-commit") { override val hookContent = """ if ! ${'$'}GRADLEW formatKotlin ; then @@ -18,7 +18,7 @@ open class InstallPreCommitHookTask : InstallHookTask("pre-commit") { """.trimIndent() } -open class InstallPrePushHookTask : InstallHookTask("pre-push") { +abstract class InstallPrePushHookTask : InstallHookTask("pre-push") { override val hookContent = """ if ! ${'$'}GRADLEW lintKotlin ; then @@ -33,9 +33,13 @@ open class InstallPrePushHookTask : InstallHookTask("pre-push") { * Install or update a kotlinter-gradle hook. */ abstract class InstallHookTask(@get:Internal val hookFileName: String) : DefaultTask() { + @Input val gitDirPath: Property = project.objects.property(default = ".git") + @Input + val rootProjectDir = project.objects.property(default = project.rootProject.rootDir) + @get:Internal abstract val hookContent: String @@ -74,7 +78,7 @@ abstract class InstallHookTask(@get:Internal val hookFileName: String) : Default } private fun getHookFile(warn: Boolean = false): File? { - val gitDir = project.rootProject.file(gitDirPath.get()) + val gitDir = File(rootProjectDir.get(), gitDirPath.get()) if (!gitDir.isDirectory) { if (warn) logger.warn("skipping hook creation because $gitDir is not a directory") return null @@ -97,7 +101,7 @@ abstract class InstallHookTask(@get:Internal val hookFileName: String) : Default "gradlew" } - val gradlew = File(project.rootDir, gradlewFilename) + val gradlew = File(rootProjectDir.get(), gradlewFilename) if (gradlew.exists() && gradlew.isFile && gradlew.canExecute()) { logger.info("Using gradlew wrapper at ${gradlew.invariantSeparatorsPath}") gradlew.invariantSeparatorsPath diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/WithGradleTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/WithGradleTest.kt index 7fe5b078..f38c8297 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/WithGradleTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/WithGradleTest.kt @@ -46,5 +46,5 @@ abstract class WithGradleTest { private fun WithGradleTest.defaultRunner(vararg args: String) = GradleRunner.create() .withProjectDir(testProjectDir.root) - .withArguments(args.toList() + "--stacktrace") + .withArguments(args.toList() + listOf("--stacktrace", "--configuration-cache")) .forwardOutput()