Skip to content

Commit 4f5c707

Browse files
committed
Fixed plugin issues found with tests
1 parent b7df24d commit 4f5c707

File tree

10 files changed

+71
-58
lines changed

10 files changed

+71
-58
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ internal fun BufExecTask.execBuf(args: Iterable<Any>) {
5656
val configValue = configFile.orNull
5757
if (configValue != null) {
5858
add("--config")
59-
add(configValue.absolutePath)
59+
add(configValue.path)
6060
}
6161

6262
if (debug.get()) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import kotlinx.rpc.buf.BUF_GEN_YAML
2727
import kotlinx.rpc.buf.BUF_YAML
2828
import kotlinx.rpc.buf.BufTasksExtension
2929
import org.gradle.api.logging.LogLevel
30+
import org.gradle.api.tasks.Classpath
3031

3132
/**
3233
* Abstract base class for `buf` tasks.
@@ -60,6 +61,8 @@ public abstract class BufExecTask : DefaultTask() {
6061
* The working directory for the `buf` command.
6162
*/
6263
@get:InputDirectory
64+
// https://docs.gradle.org/current/userguide/incremental_build.html#sec:configure_input_normalization
65+
@get:Classpath
6366
public abstract val workingDir: Property<File>
6467

6568
/**

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import org.gradle.api.tasks.OutputDirectory
1515
import org.gradle.api.tasks.TaskProvider
1616
import java.io.File
1717
import kotlinx.rpc.buf.BufGenerateExtension
18-
import org.gradle.api.provider.Provider
1918
import org.gradle.api.tasks.InputDirectory
2019

2120
/**
@@ -24,11 +23,11 @@ import org.gradle.api.tasks.InputDirectory
2423
* @see <a href="https://buf.build/docs/reference/cli/buf/generate/">buf generate</a>
2524
*/
2625
public abstract class BufGenerateTask : BufExecTask() {
27-
// unsued, but required for Gradle to properly recognize inputs
26+
// unsued, but required for Gradle to properly recognise inputs
2827
@get:InputDirectory
2928
internal abstract val protoFilesDir: Property<File>
3029

31-
// unsued, but required for Gradle to properly recognize inputs
30+
// unsued, but required for Gradle to properly recognise inputs
3231
@get:InputDirectory
3332
internal abstract val importFilesDir: Property<File>
3433

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal data class ResolvedGrpcPlugin(
2626
val type: Type,
2727
val locator: List<String>,
2828
val out: String,
29-
val options: Map<String, Any?>,
29+
val options: Map<String, Any>,
3030
val strategy: String?,
3131
val includeImports: Boolean?,
3232
val includeWkt: Boolean?,
@@ -91,6 +91,13 @@ public abstract class GenerateBufGenYaml : DefaultTask() {
9191
}
9292

9393
writer.appendLine(" - ${plugin.type.name}: $locatorLine")
94+
writer.appendLine(" out: ${plugin.out}")
95+
if (plugin.options.isNotEmpty()) {
96+
writer.appendLine(" opt:")
97+
plugin.options.forEach { (key, value) ->
98+
writer.appendLine(" - $key=$value")
99+
}
100+
}
94101
if (plugin.strategy != null) {
95102
writer.appendLine(" strategy: ${plugin.strategy}")
96103
}
@@ -112,13 +119,6 @@ public abstract class GenerateBufGenYaml : DefaultTask() {
112119
writer.appendLine(" - $type")
113120
}
114121
}
115-
writer.appendLine(" out: ${plugin.out}")
116-
if (plugin.options.isNotEmpty()) {
117-
writer.appendLine(" opt:")
118-
plugin.options.forEach { (key, value) ->
119-
writer.appendLine(" - $key${if (value != null) "=$value" else ""}")
120-
}
121-
}
122122
}
123123

124124
writer.appendLine("inputs:")

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import org.gradle.api.DefaultTask
1111
import org.gradle.api.Project
1212
import org.gradle.api.provider.Property
1313
import org.gradle.api.tasks.Input
14-
import org.gradle.api.tasks.InputDirectory
1514
import org.gradle.api.tasks.OutputFile
1615
import org.gradle.api.tasks.TaskAction
1716
import org.gradle.api.tasks.TaskProvider
@@ -22,11 +21,11 @@ import java.io.File
2221
* Generates/updates a Buf `buf.yaml` file.
2322
*/
2423
public abstract class GenerateBufYaml : DefaultTask() {
25-
@get:InputDirectory
26-
internal abstract val protoSourceDir: Property<File>
24+
@get:Input
25+
internal abstract val protoSourceDir: Property<String>
2726

28-
@get:InputDirectory
29-
internal abstract val importSourceDir: Property<File>
27+
@get:Input
28+
internal abstract val importSourceDir: Property<String>
3029

3130
@get:Input
3231
internal abstract val withImport: Property<Boolean>
@@ -61,13 +60,15 @@ public abstract class GenerateBufYaml : DefaultTask() {
6160

6261
writer.appendLine("modules:")
6362

64-
val protoDir = protoSourceDir.get()
63+
val protoDirName = protoSourceDir.get()
64+
val protoDir = file.parentFile.resolve(protoDirName)
6565
if (protoDir.exists()) {
6666
val modulePath = protoDir.relativeTo(file.parentFile)
6767
writer.appendLine(" - path: $modulePath")
6868
}
6969

70-
val importDir = importSourceDir.get()
70+
val importDirName = importSourceDir.get()
71+
val importDir = file.parentFile.resolve(importDirName)
7172
if (withImport.get() && importDir.exists()) {
7273
val modulePath = importDir.relativeTo(file.parentFile)
7374
writer.appendLine(" - path: $modulePath")
@@ -92,8 +93,8 @@ internal fun Project.registerGenerateBufYamlTask(
9293
): TaskProvider<GenerateBufYaml> {
9394
val capitalizeName = name.replaceFirstChar { it.uppercase() }
9495
return tasks.register<GenerateBufYaml>("${GenerateBufYaml.NAME_PREFIX}$capitalizeName") {
95-
protoSourceDir.set(buildSourceSetsProtoDir)
96-
importSourceDir.set(buildSourceSetsImportDir)
96+
protoSourceDir.set(buildSourceSetsProtoDir.name)
97+
importSourceDir.set(buildSourceSetsImportDir.name)
9798
this.withImport.set(withImport)
9899

99100
val bufYamlFile = buildSourceSetsDir

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

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ internal open class DefaultGrpcExtension @Inject constructor(
8080

8181
@Suppress("detekt.LongMethod", "detekt.CyclomaticComplexMethod")
8282
private fun Project.configureTasks(protoSourceSet: DefaultProtoSourceSet) {
83+
// todo remove after KRPC-180
84+
if (!protoSourceSet.languageSourceSets.isPresent) {
85+
logger.debug(
86+
"Language source sets are not set for proto source set '${protoSourceSet.name}', skipping buf tasks configuration"
87+
)
88+
89+
return
90+
}
91+
8392
val baseName = protoSourceSet.name
8493

8594
val buildSourceSetsDir = project.protoBuildDirSourceSets.resolve(baseName)
@@ -103,30 +112,11 @@ internal open class DefaultGrpcExtension @Inject constructor(
103112

104113
val protoFiles = protoSourceSet.proto
105114

106-
val generateBufYamlTask = registerGenerateBufYamlTask(
107-
name = baseName,
108-
buildSourceSetsDir = buildSourceSetsDir,
109-
buildSourceSetsProtoDir = buildSourceSetsProtoDir,
110-
buildSourceSetsImportDir = buildSourceSetsImportDir,
111-
withImport = pairSourceSet != null,
112-
)
113-
114-
val generateBufGenYamlTask = registerGenerateBufGenYamlTask(
115-
name = baseName,
116-
buildSourceSetsDir = buildSourceSetsDir,
117-
protocPlugins = includedProtocPlugins,
118-
) {
119-
dependsOn(generateBufYamlTask)
120-
}
121-
122115
val processProtoTask = registerProcessProtoFilesTask(
123116
name = baseName,
124117
destination = buildSourceSetsProtoDir,
125118
protoFiles = protoFiles,
126-
) {
127-
dependsOn(generateBufYamlTask)
128-
dependsOn(generateBufGenYamlTask)
129-
}
119+
)
130120

131121
val processImportProtoTask = if (pairSourceSet != null) {
132122
val importProtoFiles = pairSourceSet.proto
@@ -136,14 +126,33 @@ internal open class DefaultGrpcExtension @Inject constructor(
136126
destination = buildSourceSetsImportDir,
137127
protoFiles = importProtoFiles,
138128
) {
139-
dependsOn(generateBufYamlTask)
140-
dependsOn(generateBufGenYamlTask)
141129
dependsOn(processProtoTask)
142130
}
143131
} else {
144132
null
145133
}
146134

135+
val generateBufYamlTask = registerGenerateBufYamlTask(
136+
name = baseName,
137+
buildSourceSetsDir = buildSourceSetsDir,
138+
buildSourceSetsProtoDir = buildSourceSetsProtoDir,
139+
buildSourceSetsImportDir = buildSourceSetsImportDir,
140+
withImport = pairSourceSet != null,
141+
) {
142+
dependsOn(processProtoTask)
143+
if (processImportProtoTask != null) {
144+
dependsOn(processImportProtoTask)
145+
}
146+
}
147+
148+
val generateBufGenYamlTask = registerGenerateBufGenYamlTask(
149+
name = baseName,
150+
buildSourceSetsDir = buildSourceSetsDir,
151+
protocPlugins = includedProtocPlugins,
152+
) {
153+
dependsOn(generateBufYamlTask)
154+
}
155+
147156
val out = protoBuildDirGenerated.resolve(baseName)
148157

149158
val destinationFileTree = fileTree(buildSourceSetsProtoDir)
@@ -281,6 +290,10 @@ internal open class DefaultGrpcExtension @Inject constructor(
281290
})
282291
}
283292

293+
// ignore for bufGenerate task caching
294+
project.normalization.runtimeClasspath.ignore("**/protoc-gen-kotlin-multiplatform.log")
295+
project.normalization.runtimeClasspath.ignore("**/.keep")
296+
284297
protocPlugins.create(GRPC_JAVA) {
285298
isJava.set(true)
286299

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal open class DefaultProtoSourceSet @Inject constructor(
6363
}
6464

6565
internal fun Project.createProtoExtensions() {
66-
fun findOrCreateAndConfigure(languageSourceSetName: String, languageSourceSet: Any) {
66+
fun findOrCreateAndConfigure(languageSourceSetName: String, languageSourceSet: Any?) {
6767
val container = project.findOrCreate(PROTO_SOURCE_SETS) {
6868
val container = objects.domainObjectContainer(
6969
ProtoSourceSet::class.java,
@@ -77,10 +77,13 @@ internal fun Project.createProtoExtensions() {
7777

7878
val protoSourceSet = container.maybeCreate(languageSourceSetName) as DefaultProtoSourceSet
7979

80-
protoSourceSet.languageSourceSets.add(languageSourceSet)
80+
languageSourceSet?.let { protoSourceSet.languageSourceSets.add(it) }
8181
}
8282

8383
project.withKotlinJvmExtension {
84+
findOrCreateAndConfigure("main", null)
85+
findOrCreateAndConfigure("test", null)
86+
8487
sourceSets.configureEach {
8588
if (name == SourceSet.MAIN_SOURCE_SET_NAME || name == SourceSet.TEST_SOURCE_SET_NAME) {
8689
findOrCreateAndConfigure(name, this)
@@ -97,6 +100,9 @@ internal fun Project.createProtoExtensions() {
97100
}
98101

99102
project.withKotlinKmpExtension {
103+
findOrCreateAndConfigure("jvmMain", null)
104+
findOrCreateAndConfigure("jvmTest", null)
105+
100106
sourceSets.configureEach {
101107
if (name == "jvmMain" || name == "jvmTest") {
102108
findOrCreateAndConfigure(name, this)

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44

55
package kotlinx.rpc.proto
66

7-
import kotlinx.rpc.util.ensureDirectoryExists
87
import kotlinx.rpc.util.ensureRegularFileExists
98
import org.gradle.api.Project
109
import org.gradle.api.file.SourceDirectorySet
1110
import org.gradle.api.tasks.Copy
1211
import org.gradle.api.tasks.TaskProvider
1312
import org.gradle.kotlin.dsl.register
1413
import java.io.File
15-
import java.nio.file.Files
1614

1715
/**
1816
* Copy proto files to a temporary directory for Buf to process.
@@ -32,19 +30,12 @@ internal fun Project.registerProcessProtoFilesTask(
3230
val capitalName = name.replaceFirstChar { it.uppercase() }
3331

3432
return tasks.register<ProcessProtoFiles>("process${capitalName}ProtoFiles") {
35-
val allFiles = protoFiles.files
36-
3733
// this task deletes the destination directory if it results in NO-SOURCE,
3834
// which breaks the configuration cache for bufGenerate
3935
// so we prevent No-SOURCE by creating a .keep file in the destination directory
4036
val keep = protoBuildDirSourceSetsKeep.ensureRegularFileExists()
4137
from(keep)
42-
43-
from(protoFiles.srcDirs) {
44-
include {
45-
it.file in allFiles
46-
}
47-
}
38+
from(protoFiles)
4839

4940
into(destination)
5041

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public open class ProtocPlugin(
9393
*
9494
* @see <a href="https://buf.build/docs/configuration/v2/buf-gen-yaml/#opt">Buf documentation - opt</a>
9595
*/
96-
public val options: MapProperty<String, Any?> = project.objects
97-
.mapProperty<String, Any?>()
96+
public val options: MapProperty<String, Any> = project.objects
97+
.mapProperty<String, Any>()
9898
.convention(emptyMap())
9999

100100
/**

gradle-plugin/src/main/kotlin/kotlinx/rpc/util/ProcessRunner.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ internal class ProcessRunner : Closeable {
5555
) {
5656
fun formattedOutput() = buildString {
5757
appendLine("Process $name finished:")
58-
appendLine(" - Arguments: $args")
58+
appendLine(" - Buf Arguments: $args")
5959
appendLine(" - Exit code: $exitCode")
6060
val perStream = { name: String, content: ByteArray ->
6161
val string = content.toString(StandardCharsets.UTF_8)

0 commit comments

Comments
 (0)