forked from QuickBirdEng/SurveyKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-hook-install.gradle.kts
80 lines (74 loc) · 2.81 KB
/
git-hook-install.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.io.File
/*
################################################################################################
GLOBALS
################################################################################################
*/
val hookStartIdentifier = "#QBS-HOOK-START"
val hookEndIdentifier = "#QBS-HOOK-END"
val existingGitHookFolder = File(".git${File.separator}hooks")
val existingGitHooks by lazy {
existingGitHookFolder
.listFiles()
?.filterNot { it.isSample() }
?: throw IllegalStateException("Missing ${existingGitHookFolder.absolutePath}")
}
val projectSpecificHookFolder = File("git-hooks")
val projectSpecificGitHooks = projectSpecificHookFolder.listFiles()
/*
################################################################################################
HELPER FUNCTIONS
################################################################################################
*/
fun copyGitHook(folder: File, gitHook: File) {
val target = File(folder, gitHook.name)
val wrappedHook = gitHook.readText().wrapInHookIdentifiers()
target.writeText(wrappedHook)
target.setExecutable(true)
}
fun getExistingGitHook(fileName: String) =
existingGitHooks.firstOrNull { file -> file.name == fileName }
fun File.isSample() = name.endsWith(".sample")
fun File.insertHookIfNeeded(text: String) {
val fileContent = readText()
/*
hook is up to date
*/
if (fileContent.contains(text)) return
val hook = text.wrapInHookIdentifiers()
/*
hook already exists, update it
*/
if (fileContent.containsQbHook()) {
val startIndex = fileContent.indexOf(hookStartIdentifier)
val endIndex = fileContent.indexOf(hookEndIdentifier) + hookEndIdentifier.length
val updatedContent = fileContent.replaceRange(startIndex, endIndex, hook)
this.writeText(updatedContent)
} else {
/*
hook isn't installed, append to the current file
*/
this.appendText("\n\n$hook")
}
}
fun String.containsQbHook() = contains(hookStartIdentifier) && contains(hookEndIdentifier)
fun String.wrapInHookIdentifiers() = "$hookStartIdentifier\n$this\n$hookEndIdentifier"
/*
################################################################################################
TASK
################################################################################################
*/
task("installGitHooks") {
try {
projectSpecificGitHooks.forEach { gitHook ->
val existingHook = getExistingGitHook(gitHook.name)
if (existingHook != null) {
existingHook.insertHookIfNeeded(gitHook.readText())
} else {
copyGitHook(existingGitHookFolder, gitHook)
}
}
} catch (e: Throwable) {
print("Something went wrong while installing the git hooks: $e.message")
}
}