Skip to content

Commit 4c7b56d

Browse files
authored
Tasks caching (#410)
1 parent 95a349b commit 4c7b56d

File tree

10 files changed

+209
-90
lines changed

10 files changed

+209
-90
lines changed

gradle-plugin/src/main/kotlin/kotlinx/rpc/buf/tasks/BufGenerateTask.kt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@ package kotlinx.rpc.buf.tasks
77
import kotlinx.rpc.proto.PROTO_GROUP
88
import kotlinx.rpc.rpcExtension
99
import org.gradle.api.Project
10-
import org.gradle.api.file.FileCollection
1110
import org.gradle.api.provider.ListProperty
1211
import org.gradle.api.provider.Property
13-
import org.gradle.api.provider.Provider
1412
import org.gradle.api.tasks.Input
15-
import org.gradle.api.tasks.InputFiles
1613
import org.gradle.api.tasks.Optional
1714
import org.gradle.api.tasks.OutputDirectory
1815
import org.gradle.api.tasks.TaskProvider
1916
import java.io.File
2017
import kotlinx.rpc.buf.BufGenerateExtension
18+
import org.gradle.api.provider.Provider
19+
import org.gradle.api.tasks.InputDirectory
2120

2221
/**
2322
* Buf `generate` command.
@@ -26,8 +25,12 @@ import kotlinx.rpc.buf.BufGenerateExtension
2625
*/
2726
public abstract class BufGenerateTask : BufExecTask() {
2827
// unsued, but required for Gradle to properly recognize inputs
29-
@get:InputFiles
30-
internal abstract val protoFiles: ListProperty<File>
28+
@get:InputDirectory
29+
internal abstract val protoFilesDir: Property<File>
30+
31+
// unsued, but required for Gradle to properly recognize inputs
32+
@get:InputDirectory
33+
internal abstract val importFilesDir: Property<File>
3134

3235
/**
3336
* Whether to include imports.
@@ -103,15 +106,16 @@ public abstract class BufGenerateTask : BufExecTask() {
103106

104107
internal fun Project.registerBufGenerateTask(
105108
name: String,
106-
workingDir: Provider<File>,
107-
outputDirectory: Provider<File>,
108-
protoFiles: Provider<FileCollection>,
109+
workingDir: File,
110+
outputDirectory: File,
111+
protoFilesDir: File,
112+
importFilesDir: File,
109113
configure: BufGenerateTask.() -> Unit = {},
110114
): TaskProvider<BufGenerateTask> {
111115
val capitalName = name.replaceFirstChar { it.uppercase() }
112116
val bufGenerateTaskName = "${BufGenerateTask.NAME_PREFIX}$capitalName"
113117

114-
return registerBufExecTask<BufGenerateTask>(bufGenerateTaskName, workingDir) {
118+
return registerBufExecTask<BufGenerateTask>(bufGenerateTaskName, provider { workingDir }) {
115119
group = PROTO_GROUP
116120
description = "Generates code from .proto files using 'buf generate'"
117121

@@ -122,7 +126,9 @@ internal fun Project.registerBufGenerateTask(
122126
errorFormat.set(generate.errorFormat)
123127

124128
this.outputDirectory.set(outputDirectory)
125-
this.protoFiles.set(protoFiles)
129+
130+
this.protoFilesDir.set(protoFilesDir)
131+
this.importFilesDir.set(importFilesDir)
126132

127133
configure()
128134
}

gradle-plugin/src/main/kotlin/kotlinx/rpc/buf/tasks/GenerateBufGenYaml.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import kotlinx.rpc.buf.BUF_GEN_YAML
88
import kotlinx.rpc.proto.PROTO_FILES_DIR
99
import kotlinx.rpc.proto.PROTO_GROUP
1010
import kotlinx.rpc.proto.ProtocPlugin
11-
import kotlinx.rpc.proto.protoBuildDirSourceSets
1211
import kotlinx.rpc.util.ensureRegularFileExists
1312
import org.gradle.api.DefaultTask
1413
import org.gradle.api.GradleException
@@ -136,7 +135,7 @@ public abstract class GenerateBufGenYaml : DefaultTask() {
136135

137136
internal fun Project.registerGenerateBufGenYamlTask(
138137
name: String,
139-
dir: String,
138+
buildSourceSetsDir: File,
140139
protocPlugins: Iterable<ProtocPlugin>,
141140
configure: GenerateBufGenYaml.() -> Unit = {},
142141
): TaskProvider<GenerateBufGenYaml> {
@@ -176,8 +175,7 @@ internal fun Project.registerGenerateBufGenYamlTask(
176175

177176
plugins.set(pluginsProvider)
178177

179-
val bufGenYamlFile = project.protoBuildDirSourceSets
180-
.resolve(dir)
178+
val bufGenYamlFile = buildSourceSetsDir
181179
.resolve(BUF_GEN_YAML)
182180
.ensureRegularFileExists()
183181

gradle-plugin/src/main/kotlin/kotlinx/rpc/buf/tasks/GenerateBufYaml.kt

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55
package kotlinx.rpc.buf.tasks
66

77
import kotlinx.rpc.buf.BUF_YAML
8-
import kotlinx.rpc.proto.PROTO_FILES_IMPORT_DIR
9-
import kotlinx.rpc.proto.PROTO_FILES_DIR
108
import kotlinx.rpc.proto.PROTO_GROUP
11-
import kotlinx.rpc.proto.protoBuildDirSourceSets
12-
import kotlinx.rpc.util.ensureDirectoryExists
139
import kotlinx.rpc.util.ensureRegularFileExists
1410
import org.gradle.api.DefaultTask
1511
import org.gradle.api.Project
1612
import org.gradle.api.provider.Property
13+
import org.gradle.api.tasks.Input
1714
import org.gradle.api.tasks.InputDirectory
18-
import org.gradle.api.tasks.Optional
1915
import org.gradle.api.tasks.OutputFile
2016
import org.gradle.api.tasks.TaskAction
2117
import org.gradle.api.tasks.TaskProvider
@@ -29,10 +25,12 @@ public abstract class GenerateBufYaml : DefaultTask() {
2925
@get:InputDirectory
3026
internal abstract val protoSourceDir: Property<File>
3127

32-
@get:Optional
3328
@get:InputDirectory
3429
internal abstract val importSourceDir: Property<File>
3530

31+
@get:Input
32+
internal abstract val withImport: Property<Boolean>
33+
3634
/**
3735
* The `buf.yaml` file to generate/update.
3836
*/
@@ -69,8 +67,8 @@ public abstract class GenerateBufYaml : DefaultTask() {
6967
writer.appendLine(" - path: $modulePath")
7068
}
7169

72-
val importDir = importSourceDir.orNull
73-
if (importDir != null && importDir.exists()) {
70+
val importDir = importSourceDir.get()
71+
if (withImport.get() && importDir.exists()) {
7472
val modulePath = importDir.relativeTo(file.parentFile)
7573
writer.appendLine(" - path: $modulePath")
7674
}
@@ -86,28 +84,19 @@ public abstract class GenerateBufYaml : DefaultTask() {
8684

8785
internal fun Project.registerGenerateBufYamlTask(
8886
name: String,
89-
dir: String,
87+
buildSourceSetsDir: File,
88+
buildSourceSetsProtoDir: File,
89+
buildSourceSetsImportDir: File,
9090
withImport: Boolean,
9191
configure: GenerateBufYaml.() -> Unit = {},
9292
): TaskProvider<GenerateBufYaml> {
9393
val capitalizeName = name.replaceFirstChar { it.uppercase() }
9494
return tasks.register<GenerateBufYaml>("${GenerateBufYaml.NAME_PREFIX}$capitalizeName") {
95-
val baseDir = project.protoBuildDirSourceSets.resolve(dir)
96-
val protoDir = baseDir
97-
.resolve(PROTO_FILES_DIR)
98-
.ensureDirectoryExists()
99-
100-
protoSourceDir.set(protoDir)
101-
102-
if (withImport) {
103-
val importDir = baseDir
104-
.resolve(PROTO_FILES_IMPORT_DIR)
105-
.ensureDirectoryExists()
106-
107-
importSourceDir.set(importDir)
108-
}
95+
protoSourceDir.set(buildSourceSetsProtoDir)
96+
importSourceDir.set(buildSourceSetsImportDir)
97+
this.withImport.set(withImport)
10998

110-
val bufYamlFile = baseDir
99+
val bufYamlFile = buildSourceSetsDir
111100
.resolve(BUF_YAML)
112101
.ensureRegularFileExists()
113102

gradle-plugin/src/main/kotlin/kotlinx/rpc/grpc/DefaultGrpcExtension.kt

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import kotlinx.rpc.proto.ProtocPlugin.Companion.GRPC_JAVA
1818
import kotlinx.rpc.proto.ProtocPlugin.Companion.GRPC_KOTLIN
1919
import kotlinx.rpc.proto.ProtocPlugin.Companion.KXRPC
2020
import kotlinx.rpc.proto.ProtocPlugin.Companion.PROTOBUF_JAVA
21+
import kotlinx.rpc.util.ensureDirectoryExists
2122
import org.gradle.api.Action
2223
import org.gradle.api.GradleException
2324
import org.gradle.api.NamedDomainObjectContainer
@@ -59,7 +60,7 @@ internal open class DefaultGrpcExtension @Inject constructor(
5960

6061
createDefaultProtocPlugins()
6162

62-
project.configureProtoExtensions { _, protoSourceSet ->
63+
project.protoSourceSets.forEach { protoSourceSet ->
6364
protoSourceSet.protocPlugin(protocPlugins.protobufJava)
6465
protoSourceSet.protocPlugin(protocPlugins.grpcJava)
6566
protoSourceSet.protocPlugin(protocPlugins.grpcKotlin)
@@ -80,7 +81,15 @@ internal open class DefaultGrpcExtension @Inject constructor(
8081
@Suppress("detekt.LongMethod", "detekt.CyclomaticComplexMethod")
8182
private fun Project.configureTasks(protoSourceSet: DefaultProtoSourceSet) {
8283
val baseName = protoSourceSet.name
83-
val baseGenDir = project.protoBuildDirSourceSets.resolve(baseName)
84+
85+
val buildSourceSetsDir = project.protoBuildDirSourceSets.resolve(baseName)
86+
.ensureDirectoryExists()
87+
88+
val buildSourceSetsProtoDir = buildSourceSetsDir.resolve(PROTO_FILES_DIR)
89+
.ensureDirectoryExists()
90+
91+
val buildSourceSetsImportDir = buildSourceSetsDir.resolve(PROTO_FILES_IMPORT_DIR)
92+
.ensureDirectoryExists()
8493

8594
val pairSourceSet = protoSourceSet.correspondingMainSourceSetOrNull()
8695

@@ -93,27 +102,27 @@ internal open class DefaultGrpcExtension @Inject constructor(
93102
}
94103

95104
val protoFiles = protoSourceSet.proto
96-
val hasFiles = !protoFiles.isEmpty
97105

98106
val generateBufYamlTask = registerGenerateBufYamlTask(
99107
name = baseName,
100-
dir = baseName,
108+
buildSourceSetsDir = buildSourceSetsDir,
109+
buildSourceSetsProtoDir = buildSourceSetsProtoDir,
110+
buildSourceSetsImportDir = buildSourceSetsImportDir,
101111
withImport = pairSourceSet != null,
102112
)
103113

104114
val generateBufGenYamlTask = registerGenerateBufGenYamlTask(
105115
name = baseName,
106-
dir = baseName,
116+
buildSourceSetsDir = buildSourceSetsDir,
107117
protocPlugins = includedProtocPlugins,
108118
) {
109119
dependsOn(generateBufYamlTask)
110120
}
111121

112122
val processProtoTask = registerProcessProtoFilesTask(
113123
name = baseName,
114-
baseGenDir = provider { baseGenDir },
124+
destination = buildSourceSetsProtoDir,
115125
protoFiles = protoFiles,
116-
toDir = PROTO_FILES_DIR,
117126
) {
118127
dependsOn(generateBufYamlTask)
119128
dependsOn(generateBufGenYamlTask)
@@ -124,9 +133,8 @@ internal open class DefaultGrpcExtension @Inject constructor(
124133

125134
registerProcessProtoFilesTask(
126135
name = "${baseName}Import",
127-
baseGenDir = provider { baseGenDir },
136+
destination = buildSourceSetsImportDir,
128137
protoFiles = importProtoFiles,
129-
toDir = PROTO_FILES_IMPORT_DIR,
130138
) {
131139
dependsOn(generateBufYamlTask)
132140
dependsOn(generateBufGenYamlTask)
@@ -138,19 +146,14 @@ internal open class DefaultGrpcExtension @Inject constructor(
138146

139147
val out = protoBuildDirGenerated.resolve(baseName)
140148

149+
val destinationFileTree = fileTree(buildSourceSetsProtoDir)
150+
141151
val bufGenerateTask = registerBufGenerateTask(
142152
name = baseName,
143-
workingDir = provider { baseGenDir },
144-
outputDirectory = provider { out },
145-
protoFiles = provider {
146-
protoFiles.asFileTree.let {
147-
if (pairSourceSet != null) {
148-
it + pairSourceSet.proto
149-
} else {
150-
it
151-
}
152-
}
153-
},
153+
workingDir = buildSourceSetsDir,
154+
outputDirectory = out,
155+
protoFilesDir = buildSourceSetsProtoDir,
156+
importFilesDir = buildSourceSetsImportDir,
154157
) {
155158
dependsOn(generateBufGenYamlTask)
156159
dependsOn(generateBufYamlTask)
@@ -163,7 +166,7 @@ internal open class DefaultGrpcExtension @Inject constructor(
163166
dependsOn(pairSourceSet.generateTask)
164167
}
165168

166-
onlyIf { hasFiles }
169+
onlyIf { !destinationFileTree.filter { it.extension == "proto" }.isEmpty }
167170
}
168171

169172
protoSourceSet.generateTask.set(bufGenerateTask)
@@ -236,7 +239,7 @@ internal open class DefaultGrpcExtension @Inject constructor(
236239

237240
val customTask = registerBufExecTask(
238241
clazz = definition.kClass,
239-
workingDir = provider { baseGenDir },
242+
workingDir = provider { buildSourceSetsDir },
240243
name = taskName,
241244
) {
242245
dependsOn(generateBufYamlTask)
@@ -246,7 +249,7 @@ internal open class DefaultGrpcExtension @Inject constructor(
246249
dependsOn(processImportProtoTask)
247250
}
248251

249-
onlyIf { hasFiles }
252+
onlyIf { !destinationFileTree.filter { it.extension == "proto" }.isEmpty }
250253
}
251254

252255
when {
@@ -310,7 +313,7 @@ internal open class DefaultGrpcExtension @Inject constructor(
310313
}
311314

312315
name.lowercase().endsWith("test") -> {
313-
project.protoSourceSets.getByName(correspondingMainName()) as DefaultProtoSourceSet
316+
project.protoSourceSets.findByName(correspondingMainName()) as? DefaultProtoSourceSet
314317
}
315318

316319
else -> {

gradle-plugin/src/main/kotlin/kotlinx/rpc/proto/DefaultProtoSourceSet.kt

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,15 @@ internal open class DefaultProtoSourceSet @Inject constructor(
5454
PROTO_SOURCE_DIRECTORY_NAME,
5555
"Proto sources",
5656
).apply {
57-
srcDirs("src/$name/proto")
57+
srcDirs("src/${this@DefaultProtoSourceSet.name}/proto")
5858
}
5959

6060
override fun proto(action: Action<SourceDirectorySet>) {
6161
action.execute(proto)
6262
}
6363
}
6464

65-
internal fun Project.configureProtoExtensions(
66-
configure: Project.(
67-
languageSourceSet: Any,
68-
protoSourceSet: DefaultProtoSourceSet,
69-
) -> Unit
70-
) {
65+
internal fun Project.createProtoExtensions() {
7166
fun findOrCreateAndConfigure(languageSourceSetName: String, languageSourceSet: Any) {
7267
val container = project.findOrCreate(PROTO_SOURCE_SETS) {
7368
val container = objects.domainObjectContainer(
@@ -82,7 +77,7 @@ internal fun Project.configureProtoExtensions(
8277

8378
val protoSourceSet = container.maybeCreate(languageSourceSetName) as DefaultProtoSourceSet
8479

85-
configure(languageSourceSet, protoSourceSet)
80+
protoSourceSet.languageSourceSets.add(languageSourceSet)
8681
}
8782

8883
project.withKotlinJvmExtension {
@@ -109,13 +104,3 @@ internal fun Project.configureProtoExtensions(
109104
}
110105
}
111106
}
112-
113-
internal fun Project.createProtoExtensions() {
114-
configureProtoExtensions { languageSourceSet, sourceSet ->
115-
sourceSet.initExtension(languageSourceSet)
116-
}
117-
}
118-
119-
private fun DefaultProtoSourceSet.initExtension(languageSourceSet: Any) {
120-
this.languageSourceSets.add(languageSourceSet)
121-
}

0 commit comments

Comments
 (0)