Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ package kotlinx.rpc.buf.tasks
import kotlinx.rpc.proto.PROTO_GROUP
import kotlinx.rpc.rpcExtension
import org.gradle.api.Project
import org.gradle.api.file.FileCollection
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskProvider
import java.io.File
import kotlinx.rpc.buf.BufGenerateExtension
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.InputDirectory

/**
* Buf `generate` command.
Expand All @@ -26,8 +25,12 @@ import kotlinx.rpc.buf.BufGenerateExtension
*/
public abstract class BufGenerateTask : BufExecTask() {
// unsued, but required for Gradle to properly recognize inputs
@get:InputFiles
internal abstract val protoFiles: ListProperty<File>
@get:InputDirectory
internal abstract val protoFilesDir: Property<File>

// unsued, but required for Gradle to properly recognize inputs
@get:InputDirectory
internal abstract val importFilesDir: Property<File>

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

internal fun Project.registerBufGenerateTask(
name: String,
workingDir: Provider<File>,
outputDirectory: Provider<File>,
protoFiles: Provider<FileCollection>,
workingDir: File,
outputDirectory: File,
protoFilesDir: File,
importFilesDir: File,
configure: BufGenerateTask.() -> Unit = {},
): TaskProvider<BufGenerateTask> {
val capitalName = name.replaceFirstChar { it.uppercase() }
val bufGenerateTaskName = "${BufGenerateTask.NAME_PREFIX}$capitalName"

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

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

this.outputDirectory.set(outputDirectory)
this.protoFiles.set(protoFiles)

this.protoFilesDir.set(protoFilesDir)
this.importFilesDir.set(importFilesDir)

configure()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import kotlinx.rpc.buf.BUF_GEN_YAML
import kotlinx.rpc.proto.PROTO_FILES_DIR
import kotlinx.rpc.proto.PROTO_GROUP
import kotlinx.rpc.proto.ProtocPlugin
import kotlinx.rpc.proto.protoBuildDirSourceSets
import kotlinx.rpc.util.ensureRegularFileExists
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
Expand Down Expand Up @@ -136,7 +135,7 @@ public abstract class GenerateBufGenYaml : DefaultTask() {

internal fun Project.registerGenerateBufGenYamlTask(
name: String,
dir: String,
buildSourceSetsDir: File,
protocPlugins: Iterable<ProtocPlugin>,
configure: GenerateBufGenYaml.() -> Unit = {},
): TaskProvider<GenerateBufGenYaml> {
Expand Down Expand Up @@ -176,8 +175,7 @@ internal fun Project.registerGenerateBufGenYamlTask(

plugins.set(pluginsProvider)

val bufGenYamlFile = project.protoBuildDirSourceSets
.resolve(dir)
val bufGenYamlFile = buildSourceSetsDir
.resolve(BUF_GEN_YAML)
.ensureRegularFileExists()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
package kotlinx.rpc.buf.tasks

import kotlinx.rpc.buf.BUF_YAML
import kotlinx.rpc.proto.PROTO_FILES_IMPORT_DIR
import kotlinx.rpc.proto.PROTO_FILES_DIR
import kotlinx.rpc.proto.PROTO_GROUP
import kotlinx.rpc.proto.protoBuildDirSourceSets
import kotlinx.rpc.util.ensureDirectoryExists
import kotlinx.rpc.util.ensureRegularFileExists
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.TaskProvider
Expand All @@ -29,10 +25,12 @@ public abstract class GenerateBufYaml : DefaultTask() {
@get:InputDirectory
internal abstract val protoSourceDir: Property<File>

@get:Optional
@get:InputDirectory
internal abstract val importSourceDir: Property<File>

@get:Input
internal abstract val withImport: Property<Boolean>

/**
* The `buf.yaml` file to generate/update.
*/
Expand Down Expand Up @@ -69,8 +67,8 @@ public abstract class GenerateBufYaml : DefaultTask() {
writer.appendLine(" - path: $modulePath")
}

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

internal fun Project.registerGenerateBufYamlTask(
name: String,
dir: String,
buildSourceSetsDir: File,
buildSourceSetsProtoDir: File,
buildSourceSetsImportDir: File,
withImport: Boolean,
configure: GenerateBufYaml.() -> Unit = {},
): TaskProvider<GenerateBufYaml> {
val capitalizeName = name.replaceFirstChar { it.uppercase() }
return tasks.register<GenerateBufYaml>("${GenerateBufYaml.NAME_PREFIX}$capitalizeName") {
val baseDir = project.protoBuildDirSourceSets.resolve(dir)
val protoDir = baseDir
.resolve(PROTO_FILES_DIR)
.ensureDirectoryExists()

protoSourceDir.set(protoDir)

if (withImport) {
val importDir = baseDir
.resolve(PROTO_FILES_IMPORT_DIR)
.ensureDirectoryExists()

importSourceDir.set(importDir)
}
protoSourceDir.set(buildSourceSetsProtoDir)
importSourceDir.set(buildSourceSetsImportDir)
this.withImport.set(withImport)

val bufYamlFile = baseDir
val bufYamlFile = buildSourceSetsDir
.resolve(BUF_YAML)
.ensureRegularFileExists()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import kotlinx.rpc.proto.ProtocPlugin.Companion.GRPC_JAVA
import kotlinx.rpc.proto.ProtocPlugin.Companion.GRPC_KOTLIN
import kotlinx.rpc.proto.ProtocPlugin.Companion.KXRPC
import kotlinx.rpc.proto.ProtocPlugin.Companion.PROTOBUF_JAVA
import kotlinx.rpc.util.ensureDirectoryExists
import org.gradle.api.Action
import org.gradle.api.GradleException
import org.gradle.api.NamedDomainObjectContainer
Expand Down Expand Up @@ -59,7 +60,7 @@ internal open class DefaultGrpcExtension @Inject constructor(

createDefaultProtocPlugins()

project.configureProtoExtensions { _, protoSourceSet ->
project.protoSourceSets.forEach { protoSourceSet ->
protoSourceSet.protocPlugin(protocPlugins.protobufJava)
protoSourceSet.protocPlugin(protocPlugins.grpcJava)
protoSourceSet.protocPlugin(protocPlugins.grpcKotlin)
Expand All @@ -80,7 +81,15 @@ internal open class DefaultGrpcExtension @Inject constructor(
@Suppress("detekt.LongMethod", "detekt.CyclomaticComplexMethod")
private fun Project.configureTasks(protoSourceSet: DefaultProtoSourceSet) {
val baseName = protoSourceSet.name
val baseGenDir = project.protoBuildDirSourceSets.resolve(baseName)

val buildSourceSetsDir = project.protoBuildDirSourceSets.resolve(baseName)
.ensureDirectoryExists()

val buildSourceSetsProtoDir = buildSourceSetsDir.resolve(PROTO_FILES_DIR)
.ensureDirectoryExists()

val buildSourceSetsImportDir = buildSourceSetsDir.resolve(PROTO_FILES_IMPORT_DIR)
.ensureDirectoryExists()

val pairSourceSet = protoSourceSet.correspondingMainSourceSetOrNull()

Expand All @@ -93,27 +102,27 @@ internal open class DefaultGrpcExtension @Inject constructor(
}

val protoFiles = protoSourceSet.proto
val hasFiles = !protoFiles.isEmpty

val generateBufYamlTask = registerGenerateBufYamlTask(
name = baseName,
dir = baseName,
buildSourceSetsDir = buildSourceSetsDir,
buildSourceSetsProtoDir = buildSourceSetsProtoDir,
buildSourceSetsImportDir = buildSourceSetsImportDir,
withImport = pairSourceSet != null,
)

val generateBufGenYamlTask = registerGenerateBufGenYamlTask(
name = baseName,
dir = baseName,
buildSourceSetsDir = buildSourceSetsDir,
protocPlugins = includedProtocPlugins,
) {
dependsOn(generateBufYamlTask)
}

val processProtoTask = registerProcessProtoFilesTask(
name = baseName,
baseGenDir = provider { baseGenDir },
destination = buildSourceSetsProtoDir,
protoFiles = protoFiles,
toDir = PROTO_FILES_DIR,
) {
dependsOn(generateBufYamlTask)
dependsOn(generateBufGenYamlTask)
Expand All @@ -124,9 +133,8 @@ internal open class DefaultGrpcExtension @Inject constructor(

registerProcessProtoFilesTask(
name = "${baseName}Import",
baseGenDir = provider { baseGenDir },
destination = buildSourceSetsImportDir,
protoFiles = importProtoFiles,
toDir = PROTO_FILES_IMPORT_DIR,
) {
dependsOn(generateBufYamlTask)
dependsOn(generateBufGenYamlTask)
Expand All @@ -138,19 +146,14 @@ internal open class DefaultGrpcExtension @Inject constructor(

val out = protoBuildDirGenerated.resolve(baseName)

val destinationFileTree = fileTree(buildSourceSetsProtoDir)

val bufGenerateTask = registerBufGenerateTask(
name = baseName,
workingDir = provider { baseGenDir },
outputDirectory = provider { out },
protoFiles = provider {
protoFiles.asFileTree.let {
if (pairSourceSet != null) {
it + pairSourceSet.proto
} else {
it
}
}
},
workingDir = buildSourceSetsDir,
outputDirectory = out,
protoFilesDir = buildSourceSetsProtoDir,
importFilesDir = buildSourceSetsImportDir,
) {
dependsOn(generateBufGenYamlTask)
dependsOn(generateBufYamlTask)
Expand All @@ -163,7 +166,7 @@ internal open class DefaultGrpcExtension @Inject constructor(
dependsOn(pairSourceSet.generateTask)
}

onlyIf { hasFiles }
onlyIf { !destinationFileTree.filter { it.extension == "proto" }.isEmpty }
}

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

val customTask = registerBufExecTask(
clazz = definition.kClass,
workingDir = provider { baseGenDir },
workingDir = provider { buildSourceSetsDir },
name = taskName,
) {
dependsOn(generateBufYamlTask)
Expand All @@ -246,7 +249,7 @@ internal open class DefaultGrpcExtension @Inject constructor(
dependsOn(processImportProtoTask)
}

onlyIf { hasFiles }
onlyIf { !destinationFileTree.filter { it.extension == "proto" }.isEmpty }
}

when {
Expand Down Expand Up @@ -310,7 +313,7 @@ internal open class DefaultGrpcExtension @Inject constructor(
}

name.lowercase().endsWith("test") -> {
project.protoSourceSets.getByName(correspondingMainName()) as DefaultProtoSourceSet
project.protoSourceSets.findByName(correspondingMainName()) as? DefaultProtoSourceSet
}

else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,15 @@ internal open class DefaultProtoSourceSet @Inject constructor(
PROTO_SOURCE_DIRECTORY_NAME,
"Proto sources",
).apply {
srcDirs("src/$name/proto")
srcDirs("src/${this@DefaultProtoSourceSet.name}/proto")
}

override fun proto(action: Action<SourceDirectorySet>) {
action.execute(proto)
}
}

internal fun Project.configureProtoExtensions(
configure: Project.(
languageSourceSet: Any,
protoSourceSet: DefaultProtoSourceSet,
) -> Unit
) {
internal fun Project.createProtoExtensions() {
fun findOrCreateAndConfigure(languageSourceSetName: String, languageSourceSet: Any) {
val container = project.findOrCreate(PROTO_SOURCE_SETS) {
val container = objects.domainObjectContainer(
Expand All @@ -82,7 +77,7 @@ internal fun Project.configureProtoExtensions(

val protoSourceSet = container.maybeCreate(languageSourceSetName) as DefaultProtoSourceSet

configure(languageSourceSet, protoSourceSet)
protoSourceSet.languageSourceSets.add(languageSourceSet)
}

project.withKotlinJvmExtension {
Expand All @@ -109,13 +104,3 @@ internal fun Project.configureProtoExtensions(
}
}
}

internal fun Project.createProtoExtensions() {
configureProtoExtensions { languageSourceSet, sourceSet ->
sourceSet.initExtension(languageSourceSet)
}
}

private fun DefaultProtoSourceSet.initExtension(languageSourceSet: Any) {
this.languageSourceSets.add(languageSourceSet)
}
Loading