56 lines
1.7 KiB
Groovy
56 lines
1.7 KiB
Groovy
package ru.di9.gradle.githooks
|
|
|
|
import org.gradle.api.DefaultTask
|
|
import org.gradle.api.InvalidUserDataException
|
|
import org.gradle.api.NamedDomainObjectContainer
|
|
import org.gradle.api.tasks.TaskAction
|
|
|
|
import java.nio.file.Path
|
|
|
|
class GitHooksTask extends DefaultTask {
|
|
|
|
private static final String hookDir = ".git/hooks"
|
|
private static final List<String> hooks = [
|
|
"applypatch-msg",
|
|
"commit-msg",
|
|
"fsmonitor-watchman",
|
|
"post-update",
|
|
"pre-applypatch",
|
|
"pre-commit",
|
|
"pre-merge-commit",
|
|
"prepare-commit-msg",
|
|
"pre-push",
|
|
"pre-rebase",
|
|
"pre-receive",
|
|
"push-to-checkout",
|
|
"sendemail-validate",
|
|
"update"
|
|
]
|
|
|
|
@TaskAction
|
|
void execute() throws IOException {
|
|
def gitHooks = getProject().getExtensions().getByName("githooks") as NamedDomainObjectContainer<GitHook>
|
|
|
|
gitHooks.each { hook ->
|
|
if (!hooks.contains(hook.name)) {
|
|
throw new InvalidUserDataException("Unknown hook '${hook.name}'")
|
|
}
|
|
|
|
if (hook.task == null || hook.task.isEmpty() || hook.task.isBlank()) {
|
|
throw new InvalidUserDataException("Error set hook '${hook.task}': field 'task' must not be empty")
|
|
}
|
|
|
|
def hookFile = Path.of(hookDir, hook.name).toFile()
|
|
if (hookFile.exists()) {
|
|
logger.warn("Hook '${hook.name}' already exists. It will be overwritten.")
|
|
}
|
|
|
|
hookFile.write(
|
|
"""#!/bin/sh
|
|
|./gradlew --console=plain -q ${hook.task}"""
|
|
.stripMargin())
|
|
hookFile.setExecutable(true)
|
|
}
|
|
}
|
|
}
|