Skip to content

Commit 84229af

Browse files
committed
Support importing main declarations in test source sets
1 parent 7053b01 commit 84229af

File tree

12 files changed

+115
-60
lines changed

12 files changed

+115
-60
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.gradle.api.provider.ListProperty
1414
import org.gradle.api.provider.Property
1515
import org.gradle.api.provider.Provider
1616
import org.gradle.api.tasks.Input
17+
import org.gradle.api.tasks.InputDirectory
1718
import org.gradle.api.tasks.InputFile
1819
import org.gradle.api.tasks.Optional
1920
import org.gradle.api.tasks.TaskAction
@@ -36,7 +37,7 @@ public abstract class BufExecTask : DefaultTask() {
3637
@get:Input
3738
public abstract val args: ListProperty<String>
3839

39-
@get:Input
40+
@get:InputDirectory
4041
public abstract val workingDir: Property<File>
4142

4243
@get:InputFile

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

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

77
import kotlinx.rpc.buf.BUF_GEN_YAML
8+
import kotlinx.rpc.proto.PROTO_FILES_DIR
89
import kotlinx.rpc.proto.PROTO_GROUP
910
import kotlinx.rpc.proto.ProtocPlugin
1011
import kotlinx.rpc.proto.protoBuildDirSourceSets
@@ -26,7 +27,7 @@ internal data class ResolvedGrpcPlugin(
2627
val type: Type,
2728
val locator: List<String>,
2829
val out: String,
29-
val options: Map<String, String?>,
30+
val options: Map<String, Any?>,
3031
val strategy: String?,
3132
val includeImports: Boolean?,
3233
val includeWrk: Boolean?,
@@ -59,7 +60,7 @@ public abstract class BufGenYamlUpdate : DefaultTask() {
5960
file.createNewFile()
6061
}
6162

62-
bufGenFile.get().bufferedWriter(Charsets.UTF_8).use { writer ->
63+
file.bufferedWriter(Charsets.UTF_8).use { writer ->
6364
writer.appendLine("version: v2")
6465
writer.appendLine("clean: true")
6566
writer.appendLine("plugins:")
@@ -107,6 +108,9 @@ public abstract class BufGenYamlUpdate : DefaultTask() {
107108
}
108109
}
109110

111+
writer.appendLine("inputs:")
112+
writer.appendLine(" - directory: $PROTO_FILES_DIR")
113+
110114
writer.flush()
111115
}
112116
}
@@ -155,9 +159,7 @@ internal fun Project.registerBufGenYamlUpdateTask(
155159
val bufGenYamlFile = project.protoBuildDirSourceSets
156160
.resolve(dir)
157161
.resolve(BUF_GEN_YAML)
158-
.apply {
159-
ensureRegularFileExists()
160-
}
162+
.ensureRegularFileExists()
161163

162164
bufGenFile.set(bufGenYamlFile)
163165

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ public abstract class BufGenerateTask : BufExecTask() {
2424
@get:Optional
2525
public abstract val additionalArgs: ListProperty<String>
2626

27+
@get:InputFiles
28+
internal abstract val protoFiles: ListProperty<File>
29+
2730
/** Whether to include imports. */
2831
@get:Input
2932
internal abstract val includeImports: Property<Boolean>
3033

31-
/** The input proto files. */
32-
@get:InputFiles
33-
internal abstract val inputFiles: ConfigurableFileCollection
34-
3534
/** The directory to output generated files. */
3635
@get:OutputDirectory
3736
internal abstract val outputDirectory: Property<File>
@@ -57,8 +56,8 @@ public abstract class BufGenerateTask : BufExecTask() {
5756
internal fun Project.registerBufGenerateTask(
5857
name: String,
5958
workingDir: Provider<File>,
60-
inputFiles: Provider<FileCollection>,
6159
outputDirectory: Provider<File>,
60+
protoFiles: Provider<FileCollection>,
6261
configure: BufGenerateTask.() -> Unit = {},
6362
): TaskProvider<BufGenerateTask> {
6463
return registerBufExecTask<BufGenerateTask>(name, workingDir) {
@@ -68,8 +67,8 @@ internal fun Project.registerBufGenerateTask(
6867
val generate = project.rpcExtension().grpc.buf.generate
6968

7069
includeImports.set(generate.includeImports)
71-
this.inputFiles.setFrom(inputFiles)
7270
this.outputDirectory.set(outputDirectory)
71+
this.protoFiles.set(protoFiles)
7372

7473
configure()
7574
}

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

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

77
import kotlinx.rpc.buf.BUF_YAML
8+
import kotlinx.rpc.proto.IMPORT_PROTO_FILES_DIR
89
import kotlinx.rpc.proto.PROTO_FILES_DIR
910
import kotlinx.rpc.proto.PROTO_GROUP
1011
import kotlinx.rpc.proto.protoBuildDirSourceSets
1112
import kotlinx.rpc.util.ensureDirectoryExists
1213
import kotlinx.rpc.util.ensureRegularFileExists
1314
import org.gradle.api.DefaultTask
1415
import org.gradle.api.Project
15-
import org.gradle.api.provider.ListProperty
1616
import org.gradle.api.provider.Property
17-
import org.gradle.api.tasks.Input
1817
import org.gradle.api.tasks.InputDirectory
18+
import org.gradle.api.tasks.Optional
1919
import org.gradle.api.tasks.OutputFile
2020
import org.gradle.api.tasks.TaskAction
2121
import org.gradle.api.tasks.TaskProvider
@@ -26,6 +26,10 @@ public abstract class BufYamlUpdate : DefaultTask() {
2626
@get:InputDirectory
2727
internal abstract val protoSourceDir: Property<File>
2828

29+
@get:Optional
30+
@get:InputDirectory
31+
internal abstract val importSourceDir: Property<File>
32+
2933
@get:OutputFile
3034
public abstract val bufFile: Property<File>
3135

@@ -59,6 +63,12 @@ public abstract class BufYamlUpdate : DefaultTask() {
5963
writer.appendLine(" - path: $modulePath")
6064
}
6165

66+
val importDir = importSourceDir.orNull
67+
if (importDir != null && importDir.exists()) {
68+
val modulePath = importDir.relativeTo(file.parentFile)
69+
writer.appendLine(" - path: $modulePath")
70+
}
71+
6272
writer.flush()
6373
}
6474
}
@@ -71,21 +81,29 @@ public abstract class BufYamlUpdate : DefaultTask() {
7181
internal fun Project.registerBufYamlUpdateTask(
7282
name: String,
7383
dir: String,
84+
withImport: Boolean,
7485
configure: BufYamlUpdate.() -> Unit = {},
7586
): TaskProvider<BufYamlUpdate> {
7687
val capitalizeName = name.replaceFirstChar { it.uppercase() }
7788
return tasks.register<BufYamlUpdate>("${BufYamlUpdate.PREFIX_NAME}$capitalizeName") {
78-
val protoDir = project.protoBuildDirSourceSets.resolve(dir).resolve(PROTO_FILES_DIR)
79-
protoDir.ensureDirectoryExists()
89+
val baseDir = project.protoBuildDirSourceSets.resolve(dir)
90+
val protoDir = baseDir
91+
.resolve(PROTO_FILES_DIR)
92+
.ensureDirectoryExists()
8093

8194
protoSourceDir.set(protoDir)
8295

83-
val bufYamlFile = project.protoBuildDirSourceSets
84-
.resolve(dir)
96+
if (withImport) {
97+
val importDir = baseDir
98+
.resolve(IMPORT_PROTO_FILES_DIR)
99+
.ensureDirectoryExists()
100+
101+
importSourceDir.set(importDir)
102+
}
103+
104+
val bufYamlFile = baseDir
85105
.resolve(BUF_YAML)
86-
.apply {
87-
ensureRegularFileExists()
88-
}
106+
.ensureRegularFileExists()
89107

90108
bufFile.set(bufYamlFile)
91109

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

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import kotlinx.rpc.buf.tasks.BufGenerateTask
1717
import kotlinx.rpc.buf.tasks.registerBufGenYamlUpdateTask
1818
import kotlinx.rpc.buf.tasks.registerBufGenerateTask
1919
import kotlinx.rpc.buf.tasks.registerBufYamlUpdateTask
20+
import kotlinx.rpc.proto.IMPORT_PROTO_FILES_DIR
2021
import kotlinx.rpc.proto.KXRPC_PLUGIN_JAR_CONFIGURATION
22+
import kotlinx.rpc.proto.PROTO_FILES_DIR
2123
import kotlinx.rpc.proto.ProtoSourceSet
2224
import kotlinx.rpc.proto.ProtocPlugin
2325
import kotlinx.rpc.proto.configureKxRpcPluginJarConfiguration
@@ -89,7 +91,10 @@ public open class GrpcExtension @Inject constructor(
8991
val baseName = protoSourceSet.baseName.get()
9092
val baseGenDir = project.protoBuildDirSourceSets.resolve(baseName)
9193

92-
val protocPluginNames = protoSourceSet.collectProtocPlugins().distinct()
94+
val pairSourceSet = protoSourceSet.correspondingMainSourceSetOrNull()
95+
96+
val mainProtocPlugins = pairSourceSet?.protocPlugins?.get().orEmpty()
97+
val protocPluginNames = (protoSourceSet.protocPlugins.get() + mainProtocPlugins).distinct()
9398

9499
val includedProtocPlugins = protocPluginNames.map {
95100
protocPlugins.findByName(it)
@@ -99,46 +104,81 @@ public open class GrpcExtension @Inject constructor(
99104
val protoFiles = protoSourceSet.proto
100105
val hasFiles = !protoFiles.isEmpty
101106

102-
val bufGenUpdateTask = project.registerBufGenYamlUpdateTask(
107+
val bufUpdateTask = registerBufYamlUpdateTask(
108+
name = protoSourceSet.name,
109+
dir = baseName,
110+
withImport = pairSourceSet != null,
111+
)
112+
113+
val bufGenUpdateTask = registerBufGenYamlUpdateTask(
103114
name = protoSourceSet.name,
104115
dir = baseName,
105116
protocPlugins = includedProtocPlugins,
106117
) {
107-
onlyIf { hasFiles }
108-
}
109-
110-
val bufUpdateTask = project.registerBufYamlUpdateTask(protoSourceSet.name, baseName) {
111-
onlyIf { hasFiles }
118+
dependsOn(bufUpdateTask)
112119
}
113120

114-
val processProtoTask = project.registerProcessProtoFilesTask(
121+
val processProtoTask = registerProcessProtoFilesTask(
115122
name = protoSourceSet.name,
116-
baseGenDir = project.provider { baseGenDir },
123+
baseGenDir = provider { baseGenDir },
117124
protoFiles = protoFiles,
125+
toDir = PROTO_FILES_DIR,
118126
) {
119-
onlyIf { hasFiles }
120-
121-
dependsOn(bufGenUpdateTask)
122127
dependsOn(bufUpdateTask)
128+
dependsOn(bufGenUpdateTask)
129+
}
130+
131+
val processImportProtoTask = if (pairSourceSet != null) {
132+
val importProtoFiles = pairSourceSet.proto
133+
134+
registerProcessProtoFilesTask(
135+
name = "${protoSourceSet.name}Import",
136+
baseGenDir = provider { baseGenDir },
137+
protoFiles = importProtoFiles,
138+
toDir = IMPORT_PROTO_FILES_DIR,
139+
) {
140+
dependsOn(bufUpdateTask)
141+
dependsOn(bufGenUpdateTask)
142+
dependsOn(processProtoTask)
143+
}
144+
} else {
145+
null
123146
}
124147

125-
val out = project.protoBuildDirGenerated.resolve(baseName)
148+
val out = protoBuildDirGenerated.resolve(baseName)
126149

127150
val capitalName = protoSourceSet.name.replaceFirstChar { it.uppercase() }
128-
val bufGenerateTask = project.registerBufGenerateTask(
151+
val bufGenerateTask = registerBufGenerateTask(
129152
name = "${BufGenerateTask.NAME_PREFIX}$capitalName",
130153
workingDir = provider { baseGenDir },
131-
inputFiles = processProtoTask.map { it.outputs.files },
132154
outputDirectory = provider { out },
155+
protoFiles = provider {
156+
protoFiles.asFileTree.let {
157+
if (pairSourceSet != null) {
158+
it + pairSourceSet.proto
159+
} else {
160+
it
161+
}
162+
}
163+
},
133164
) {
134165
dependsOn(bufGenUpdateTask)
135166
dependsOn(bufUpdateTask)
136167
dependsOn(processProtoTask)
168+
if (processImportProtoTask != null) {
169+
dependsOn(processImportProtoTask)
170+
}
171+
172+
if (pairSourceSet != null) {
173+
dependsOn(pairSourceSet.generateTask)
174+
}
137175

138176
onlyIf { hasFiles }
139177
}
140178

141-
project.tasks.withType<KotlinCompile>().configureEach {
179+
protoSourceSet.generateTask.set(bufGenerateTask)
180+
181+
tasks.withType<KotlinCompile>().configureEach {
142182
// compileKotlin - main
143183
// compileTestKotlin - test
144184
// compileKotlinJvm - jvmMain
@@ -147,8 +187,8 @@ public open class GrpcExtension @Inject constructor(
147187
// compileTestKotlinIosArm64 - iosArm64Test
148188
val taskNameAsSourceSet = name
149189
.removePrefix("compile").let {
150-
val suffix = it.substringBefore("Kotlin").takeIf {
151-
prefix -> prefix.isNotEmpty()
190+
val suffix = it.substringBefore("Kotlin").takeIf { prefix ->
191+
prefix.isNotEmpty()
152192
} ?: "Main"
153193

154194
(it.substringAfter("Kotlin")
@@ -162,7 +202,7 @@ public open class GrpcExtension @Inject constructor(
162202
}
163203
}
164204

165-
project.tasks.withType<JavaCompile>().configureEach {
205+
tasks.withType<JavaCompile>().configureEach {
166206
// compileJvmTestJava - test (java, kmp)
167207
// compileJvmMainJava - main (java, kmp)
168208
// compileJava - main (java)
@@ -183,7 +223,7 @@ public open class GrpcExtension @Inject constructor(
183223

184224
includedProtocPlugins.forEach { plugin ->
185225
// locates correctly jvmMain, main jvmTest, test
186-
val javaSourceSet = project.extensions.findByType<JavaPluginExtension>()
226+
val javaSourceSet = extensions.findByType<JavaPluginExtension>()
187227
?.sourceSets?.findByName(baseName)?.java
188228

189229
if (plugin.isJava.get() && javaSourceSet != null) {
@@ -237,17 +277,17 @@ public open class GrpcExtension @Inject constructor(
237277
}
238278
}
239279

240-
private fun ProtoSourceSet.collectProtocPlugins(): List<String> {
280+
private fun ProtoSourceSet.correspondingMainSourceSetOrNull(): ProtoSourceSet? {
241281
return when {
242282
name.endsWith("Main") -> {
243-
protocPlugins.get()
283+
null
244284
}
245285

246286
name.endsWith("Test") -> {
247287
val main = project.protoSourceSets
248288
.getByName(correspondingMainName())
249289

250-
main.collectProtocPlugins() + protocPlugins.get()
290+
main
251291
}
252292

253293
else -> throw GradleException("Unknown source set name: $name")

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ internal fun Project.registerProcessProtoFilesTask(
2222
name: String,
2323
baseGenDir: Provider<File>,
2424
protoFiles: SourceDirectorySet,
25+
toDir: String,
2526
configure: ProcessProtoFiles.() -> Unit = {},
2627
): TaskProvider<ProcessProtoFiles> {
2728
val capitalName = name.replaceFirstChar { it.uppercase() }
2829

2930
return tasks.register<ProcessProtoFiles>("process${capitalName}ProtoFiles") {
30-
val protoGenDir = baseGenDir.map { it.resolve(PROTO_FILES_DIR) }
31+
val protoGenDir = baseGenDir.map { it.resolve(toDir) }
3132

3233
val allFiles = protoFiles.files
3334

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package kotlinx.rpc.proto
66

7+
import kotlinx.rpc.buf.tasks.BufGenerateTask
78
import kotlinx.rpc.util.findOrCreate
89
import kotlinx.rpc.util.withKotlinJvmExtension
910
import kotlinx.rpc.util.withKotlinKmpExtension
@@ -40,6 +41,7 @@ public open class ProtoSourceSet @Inject constructor(internal val project: Proje
4041
internal val baseName: Property<String> = project.objects.property<String>()
4142
internal val languageSourceSets: ListProperty<Any> = project.objects.listProperty<Any>()
4243
internal val protocPlugins: ListProperty<String> = project.objects.listProperty<String>().convention(emptyList())
44+
internal val generateTask: Property<BufGenerateTask> = project.objects.property<BufGenerateTask>()
4345

4446
public fun protocPlugin(plugin: NamedDomainObjectProvider<ProtocPlugin>) {
4547
protocPlugins.add(plugin.name)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.gradle.api.Project
88

99
public const val PROTO_GROUP: String = "proto"
1010
public const val PROTO_FILES_DIR: String = "proto"
11+
public const val IMPORT_PROTO_FILES_DIR: String = "import"
1112
public const val PROTO_BUILD_DIR: String = "protoBuild"
1213
public const val PROTO_SOURCE_DIRECTORY_NAME: String = "proto"
1314
public const val PROTO_SOURCE_SETS: String = "protoSourceSets"

0 commit comments

Comments
 (0)