16
16
17
17
package com.linecorp.support.project.multi.log.git.recursive
18
18
19
- import org.gradle.api.Action
20
- import org.gradle.api.DefaultTask
21
- import org.gradle.api.GradleException
22
- import org.gradle.api.Plugin
23
- import org.gradle.api.Project
19
+ import org.gradle.api.*
24
20
import org.gradle.api.artifacts.ProjectDependency
25
21
import org.gradle.api.tasks.Input
26
22
import org.gradle.api.tasks.Internal
@@ -32,17 +28,31 @@ import org.gradle.kotlin.dsl.register
32
28
import org.gradle.kotlin.dsl.withType
33
29
import org.gradle.process.ExecSpec
34
30
import java.io.ByteArrayOutputStream
31
+ import javax.inject.Inject
32
+
33
+ interface RecursiveGitLogParams {
34
+ @get:Input
35
+ val moduleNameTransformer: (Project ) -> String
36
+ @get:Input
37
+ val logClassifiers: Map <String , (Project ) - > Boolean >
38
+ @get:Input
39
+ val tagPattern: String
40
+ @get:Input
41
+ val trackFilePatterns: List <String >
42
+ @get:Input
43
+ val logPattern: String
44
+ }
35
45
36
- open class RecursiveGitLogPluginExtension {
46
+ open class RecursiveGitLogPluginExtension : RecursiveGitLogParams {
37
47
companion object {
38
48
const val EXTENSION_NAME = " recursive-git-log-plugin"
39
49
}
40
50
41
- var moduleNameTransformer: (Project ) -> String = { it.toString() }
42
- var logClassifiers = mutableMapOf<String , (Project ) - > Boolean > ()
43
- var tagPattern = " v*"
44
- var trackFilePatterns = mutableListOf (" build.gradl*" )
45
- var logPattern = " %s\n Assignee: @%an\n Reviewed-by: @%cn\n "
51
+ override var moduleNameTransformer: (Project ) -> String = { it.toString() }
52
+ override var logClassifiers = mutableMapOf<String , (Project ) - > Boolean > ()
53
+ override var tagPattern = " v*"
54
+ override var trackFilePatterns = mutableListOf (" build.gradl*" )
55
+ override var logPattern = " %s\n Assignee: @%an\n Reviewed-by: @%cn\n "
46
56
}
47
57
48
58
open class RecursiveGitLogPlugin : Plugin <Project > {
@@ -59,19 +69,12 @@ open class RecursiveGitLogPlugin : Plugin<Project> {
59
69
)
60
70
61
71
tasks {
62
- register(" gitLog" , GitLogTask ::class ) {
63
- group = GROUP
64
- description = """
65
- Produce logs per module affected between two specific points.
66
- -Plog.git.from=<from:latestTag>
67
- -Plog.git.to=<to:HEAD>
68
- """ .trimIndent()
69
-
70
- extension = ext
71
-
72
- (findProperty(FROM_KEY ) as String? )?.let { from = it }
73
- (findProperty(TO_KEY ) as String? )?.let { to = it }
74
- }
72
+ register(
73
+ " gitLog" , GitLogTask ::class ,
74
+ ext,
75
+ (findProperty(FROM_KEY ) as String? ) ? : " " ,
76
+ (findProperty(TO_KEY ) as String? ) ? : " "
77
+ )
75
78
}
76
79
}
77
80
}
@@ -110,17 +113,23 @@ open class GitAwareTask : DefaultTask() {
110
113
}
111
114
}
112
115
113
- open class GitLogTask : GitAwareTask () {
114
- @OutputDirectory
115
- val outputDir = project.buildDir
116
+ open class GitLogTask @Inject constructor(
117
+ param : RecursiveGitLogParams ,
118
+ @Input val from : String ,
119
+ @Input val to : String
120
+ ) : GitAwareTask(), RecursiveGitLogParams by param {
121
+ @Internal
122
+ override fun getGroup () = RecursiveGitLogPlugin .GROUP
116
123
117
- @Input
118
- var extension: RecursiveGitLogPluginExtension = RecursiveGitLogPluginExtension ()
124
+ @Internal
125
+ override fun getDescription () = """
126
+ Produce logs per module affected between two specific points.
127
+ -Plog.git.from=<from:latestTag>
128
+ -Plog.git.to=<to:HEAD>
129
+ """ .trimIndent()
119
130
120
- @Input
121
- var from = " "
122
- @Input
123
- var to = " "
131
+ @OutputDirectory
132
+ val outputDir = project.buildDir
124
133
125
134
@Internal
126
135
val resolutionCache = mutableMapOf<Project , List <String >>()
@@ -167,12 +176,12 @@ open class GitLogTask : GitAwareTask() {
167
176
168
177
private fun Sequence<Project>.toGitLog (from : String , to : String = ""): String {
169
178
return map {
170
- extension. moduleNameTransformer(it) to gitCommand(
179
+ moduleNameTransformer(it) to gitCommand(
171
180
" log" ,
172
- " --pretty=${extension. logPattern} " ,
181
+ " --pretty=${logPattern} " ,
173
182
" $from ..$to " ,
174
183
it.projectDir.toString(),
175
- * extension. trackFilePatterns.toTypedArray(),
184
+ * trackFilePatterns.toTypedArray(),
176
185
* resolveDependencies(it).toTypedArray()
177
186
)
178
187
}.filter { it.second.isNotBlank() }
@@ -181,12 +190,12 @@ open class GitLogTask : GitAwareTask() {
181
190
182
191
@TaskAction
183
192
fun act () {
184
- val from = this .from.takeIf (String ::isNotBlank) ? : getLastTag(extension. tagPattern)
193
+ val from = this .from.takeIf (String ::isNotBlank) ? : getLastTag(tagPattern)
185
194
val to = this .to
186
195
val diffFileName = diffFileName(from, to)
187
196
188
- if (extension. logClassifiers.isNotEmpty()) {
189
- extension. logClassifiers
197
+ if (logClassifiers.isNotEmpty()) {
198
+ logClassifiers
190
199
.mapValues { project.subprojects.asSequence().filter(it.value::invoke).toGitLog(from, to) }
191
200
.forEach { (classifier, log) ->
192
201
project.file(" $outputDir /${diffFileName} _$classifier .log" ).writeText(log)
0 commit comments