Skip to content

Commit

Permalink
Make the git hook tasks compatible with configuration cache (#270)
Browse files Browse the repository at this point in the history
Co-authored-by: Mateusz Kwieciński <[email protected]>
  • Loading branch information
PaulWoitaschek and mateuszkwiecinski authored Sep 10, 2022
1 parent 21b86c3 commit 36e7d7e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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<String> = project.objects.property(default = ".git")

@Input
val rootProjectDir = project.objects.property(default = project.rootProject.rootDir)

@get:Internal
abstract val hookContent: String

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 36e7d7e

Please sign in to comment.