refac: смена группы и пакета
This commit is contained in:
10
src/main/groovy/ru/di9/gradle/githooks/GitHook.groovy
Normal file
10
src/main/groovy/ru/di9/gradle/githooks/GitHook.groovy
Normal file
@@ -0,0 +1,10 @@
|
||||
package ru.di9.gradle.githooks
|
||||
|
||||
class GitHook {
|
||||
final String name
|
||||
String task
|
||||
|
||||
GitHook(String name) {
|
||||
this.name = name
|
||||
}
|
||||
}
|
||||
20
src/main/groovy/ru/di9/gradle/githooks/GitHooksPlugin.groovy
Normal file
20
src/main/groovy/ru/di9/gradle/githooks/GitHooksPlugin.groovy
Normal file
@@ -0,0 +1,20 @@
|
||||
package ru.di9.gradle.githooks
|
||||
|
||||
import org.gradle.api.NamedDomainObjectContainer
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Project
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
class GitHooksPlugin implements Plugin<Project> {
|
||||
|
||||
@Override
|
||||
void apply(Project project) {
|
||||
NamedDomainObjectContainer<GitHook> container = project.container(GitHook)
|
||||
project.extensions.add("githooks", container)
|
||||
|
||||
project.tasks.register("githooks", GitHooksTask, {
|
||||
group = "other"
|
||||
description = "setup git hooks"
|
||||
})
|
||||
}
|
||||
}
|
||||
55
src/main/groovy/ru/di9/gradle/githooks/GitHooksTask.groovy
Normal file
55
src/main/groovy/ru/di9/gradle/githooks/GitHooksTask.groovy
Normal file
@@ -0,0 +1,55 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user