From 48ae52f9cfef9193207c4697b12873f01db339c5 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Fri, 7 Oct 2022 18:06:22 +0530 Subject: refactor(build-logic): make `GitHooksTask` more lazy --- .../app/passwordstore/gradle/GitHooksPlugin.kt | 4 ++-- .../app/passwordstore/gradle/tasks/GitHooks.kt | 22 +++++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) (limited to 'build-logic') diff --git a/build-logic/automation-plugins/src/main/kotlin/app/passwordstore/gradle/GitHooksPlugin.kt b/build-logic/automation-plugins/src/main/kotlin/app/passwordstore/gradle/GitHooksPlugin.kt index 0b2b299c..f1b7c39d 100644 --- a/build-logic/automation-plugins/src/main/kotlin/app/passwordstore/gradle/GitHooksPlugin.kt +++ b/build-logic/automation-plugins/src/main/kotlin/app/passwordstore/gradle/GitHooksPlugin.kt @@ -11,8 +11,8 @@ class GitHooksPlugin : Plugin { override fun apply(project: Project) { project.tasks.register("installGitHooks") { val projectDirectory = project.layout.projectDirectory - hookScript.set(projectDirectory.file("scripts/pre-push-hook.sh").asFile.readText()) - hookOutput.set(projectDirectory.file(".git/hooks/pre-push").asFile) + hookSource.set(projectDirectory.file("scripts/pre-push-hook.sh")) + hookOutput.set(projectDirectory.file(".git/hooks/pre-push")) } } } diff --git a/build-logic/automation-plugins/src/main/kotlin/app/passwordstore/gradle/tasks/GitHooks.kt b/build-logic/automation-plugins/src/main/kotlin/app/passwordstore/gradle/tasks/GitHooks.kt index f563ada0..37fad7ef 100644 --- a/build-logic/automation-plugins/src/main/kotlin/app/passwordstore/gradle/tasks/GitHooks.kt +++ b/build-logic/automation-plugins/src/main/kotlin/app/passwordstore/gradle/tasks/GitHooks.kt @@ -5,8 +5,8 @@ package app.passwordstore.gradle.tasks -import java.io.File import java.nio.file.Files +import java.nio.file.StandardCopyOption import java.nio.file.attribute.PosixFilePermission.GROUP_EXECUTE import java.nio.file.attribute.PosixFilePermission.GROUP_READ import java.nio.file.attribute.PosixFilePermission.OTHERS_EXECUTE @@ -15,23 +15,31 @@ import java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE import java.nio.file.attribute.PosixFilePermission.OWNER_READ import java.nio.file.attribute.PosixFilePermission.OWNER_WRITE import org.gradle.api.DefaultTask -import org.gradle.api.provider.Property +import org.gradle.api.file.RegularFileProperty import org.gradle.api.tasks.CacheableTask -import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.OutputFile +import org.gradle.api.tasks.PathSensitive +import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.TaskAction @CacheableTask abstract class GitHooks : DefaultTask() { - @get:Input abstract val hookScript: Property + @get:InputFile + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val hookSource: RegularFileProperty - @get:OutputFile abstract val hookOutput: Property + @get:OutputFile abstract val hookOutput: RegularFileProperty @TaskAction fun install() { - hookOutput.get().writeText(hookScript.get()) + Files.copy( + hookSource.asFile.get().toPath(), + hookOutput.asFile.get().toPath(), + StandardCopyOption.REPLACE_EXISTING, + ) Files.setPosixFilePermissions( - hookOutput.get().toPath(), + hookOutput.asFile.get().toPath(), setOf( OWNER_READ, OWNER_WRITE, -- cgit v1.2.3