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
2 changes: 2 additions & 0 deletions .github/workflows/protobuf-conformance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ jobs:
run: |
if [[ -n "$(git status --porcelain | grep tests/protobuf-conformance/)" ]]; then
echo "Protobuf conformance test is not up to date. Please run './gradlew tests:protobuf-conformance:bufGenerateMain' and commit changes"
git status --porcelain
git diff --minimal | cat
exit 1
fi
2 changes: 2 additions & 0 deletions .github/workflows/protobuf-well-known-types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ jobs:
run: |
if [[ -n "$(git status --porcelain | grep protobuf/protobuf-core/)" ]]; then
echo "Well-Known Types are not up-to-date. Please run './gradlew :protobuf:protobuf-core:bufGenerateCommonMain' and commit changes"
git status --porcelain
git diff --minimal | cat
exit 1
fi
9 changes: 9 additions & 0 deletions gradle-plugin/api/gradle-plugin.api
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public final class kotlinx/rpc/VersionsKt {
public static final field LIBRARY_VERSION Ljava/lang/String;
}

public class kotlinx/rpc/buf/BufCommentsExtension {
public fun <init> (Lorg/gradle/api/Project;)V
public final fun getCopyComments ()Lorg/gradle/api/provider/Property;
public final fun getIncludeFileLevelComments ()Lorg/gradle/api/provider/Property;
}

public class kotlinx/rpc/buf/BufExtension {
public fun <init> (Lorg/gradle/api/model/ObjectFactory;)V
public final fun generate (Lorg/gradle/api/Action;)V
Expand All @@ -65,9 +71,12 @@ public final class kotlinx/rpc/buf/BufExtension$LogFormat : java/lang/Enum {

public class kotlinx/rpc/buf/BufGenerateExtension {
public fun <init> (Lorg/gradle/api/Project;)V
public final fun comments (Lorg/gradle/api/Action;)V
public final fun getComments ()Lkotlinx/rpc/buf/BufCommentsExtension;
public final fun getErrorFormat ()Lorg/gradle/api/provider/Property;
public final fun getIncludeImports ()Lorg/gradle/api/provider/Property;
public final fun getIncludeWkt ()Lorg/gradle/api/provider/Property;
public final fun getIndentSize ()Lorg/gradle/api/provider/Property;
}

public final class kotlinx/rpc/buf/BufGenerateExtension$ErrorFormat : java/lang/Enum {
Expand Down
4 changes: 3 additions & 1 deletion gradle-plugin/src/main/kotlin/kotlinx/rpc/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import org.gradle.kotlin.dsl.property
import java.util.concurrent.atomic.AtomicBoolean
import javax.inject.Inject

internal fun Project.rpcExtension(): RpcExtension = extensions.findByType<RpcExtension>()
internal fun Project.rpcExtensionOrNull(): RpcExtension? = extensions.findByType<RpcExtension>()

internal fun Project.rpcExtension(): RpcExtension = rpcExtensionOrNull()
?: error("Rpc extension not found. Please apply the plugin to the project")

public open class RpcExtension @Inject constructor(objects: ObjectFactory, private val project: Project) {
Expand Down
43 changes: 43 additions & 0 deletions gradle-plugin/src/main/kotlin/kotlinx/rpc/buf/BufExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public open class BufExtension @Inject constructor(objects: ObjectFactory) {
*
* @see <a href="https://buf.build/docs/reference/cli/buf/generate/">"buf generate" command</a>
* @see [BUF_GEN_YAML]
* @see [BufGenerateExtension]
*/
public val generate: BufGenerateExtension = objects.newInstance(BufGenerateExtension::class.java)

Expand All @@ -76,6 +77,7 @@ public open class BufExtension @Inject constructor(objects: ObjectFactory) {
*
* @see <a href="https://buf.build/docs/reference/cli/buf/generate/">"buf generate" command</a>
* @see [BUF_GEN_YAML]
* @see [BufGenerateExtension]
*/
public fun generate(configure: Action<BufGenerateExtension>) {
configure.execute(generate)
Expand Down Expand Up @@ -226,4 +228,45 @@ public open class BufGenerateExtension @Inject constructor(internal val project:
Default(""),
;
}

/**
* Option to configure the indent sized for the generated code.
*
* Default value: `4`.
*/
public val indentSize: Property<Int> = project.objects.property<Int>().convention(4)

/**
* Extension for configuring comments in the generated code.
*
* @see [BufCommentsExtension].
*/
public val comments: BufCommentsExtension = project.objects.newInstance(BufCommentsExtension::class.java)

/**
* Extension for configuring comments in the generated code.
*
* @see [BufCommentsExtension].
*/
public fun comments(configure: Action<BufCommentsExtension>) {
configure.execute(comments)
}
}

/**
* Extension for configuring comments in the generated code.
*/
public open class BufCommentsExtension @Inject constructor(internal val project: Project) {
/**
* Whether to copy comments from the original source files.
*/
public val copyComments: Property<Boolean> = project.objects.property<Boolean>().convention(true)

/**
* Whether to include file-level comments. This includes:
* - Comments on the `package` declaration.
* - Comments on the `syntax` declaration.
* - Comments on the `editions` declaration.
*/
public val includeFileLevelComments: Property<Boolean> = project.objects.property<Boolean>().convention(true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@

package kotlinx.rpc.protoc

import kotlinx.rpc.buf.BufCommentsExtension
import kotlinx.rpc.buf.BufGenerateExtension
import kotlinx.rpc.buf.tasks.BufGenerateTask
import kotlinx.rpc.protoc.ProtocPlugin.Companion.GRPC_KOTLIN_MULTIPLATFORM
import kotlinx.rpc.protoc.ProtocPlugin.Companion.KOTLIN_MULTIPLATFORM
import kotlinx.rpc.rpcExtension
import kotlinx.rpc.rpcExtensionOrNull
import kotlinx.rpc.util.findOrCreate
import kotlinx.rpc.util.withKotlinJvmExtension
import kotlinx.rpc.util.withKotlinKmpExtension
import org.gradle.api.*
import org.gradle.api.file.SourceDirectorySet
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.kotlin.dsl.add
import org.gradle.kotlin.dsl.listProperty
import org.gradle.kotlin.dsl.newInstance
import org.gradle.kotlin.dsl.property
import org.gradle.kotlin.dsl.the
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
import org.jetbrains.kotlin.gradle.dsl.KotlinBaseExtension
import org.jetbrains.kotlin.gradle.targets.js.npm.includedRange
import javax.inject.Inject

@Suppress("UNCHECKED_CAST")
Expand Down Expand Up @@ -58,24 +65,33 @@ internal open class DefaultProtoSourceSet @Inject constructor(
javaJar(project.kotlinMultiplatformProtocPluginJarPath)
}

options.put("debugOutput", "protoc-gen-kotlin-multiplatform.log")

if ([email protected]().endsWith("main")) {
options.put("explicitApiModeEnabled", explicitApiModeEnabled)
}
defaultOptions(explicitApiModeEnabled)
}

plugins.create(GRPC_KOTLIN_MULTIPLATFORM) {
local {
javaJar(project.grpcKotlinMultiplatformProtocPluginJarPath)
}

options.put("debugOutput", "protoc-gen-grpc-kotlin-multiplatform.log")
defaultOptions(explicitApiModeEnabled)
}
}

if ([email protected]().endsWith("main")) {
options.put("explicitApiModeEnabled", explicitApiModeEnabled)
}
private fun ProtocPlugin.defaultOptions(explicitApiModeEnabled: Provider<Boolean>) {
options.put("debugOutput", "protoc-gen-$name.log")

if ([email protected]().endsWith("main")) {
options.put("explicitApiModeEnabled", explicitApiModeEnabled)
}

val comments: Provider<BufGenerateExtension> = project.provider {
project.rpcExtensionOrNull()?.run { protoc.buf.generate }
?: project.objects.newInstance<BufGenerateExtension>()
}

options.put("generateComments", comments.flatMap { it.comments.copyComments })
options.put("generateFileLevelComments", comments.flatMap { it.comments.includeFileLevelComments })
options.put("indentSize", comments.flatMap { it.indentSize })
}

val languageSourceSets: ListProperty<Any> = project.objects.listProperty<Any>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,15 @@ import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.newInstance
import org.gradle.kotlin.dsl.the
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
import org.jetbrains.kotlin.gradle.dsl.KotlinBaseExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
import java.io.File
import javax.inject.Inject

internal open class DefaultProtocExtension @Inject constructor(
objects: ObjectFactory,
private val project: Project,
project: Project,
) : ProtocExtension {
override val buf: BufExtension = project.objects.newInstance<BufExtension>()
override fun buf(action: Action<BufExtension>) {
Expand Down
93 changes: 93 additions & 0 deletions grpc/grpc-core/src/commonTest/proto/comments.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// detached syntax


// leading syntax
syntax = "proto3";

// detached package

// leading package
package some;

// detached Some

// leading Some 1
// leading Some 2
/* leading


Some


3

funky indents:
- option 1
- suboption 2
*/
message Some {
// trailing Some

// detached field1 1.1
// detached field1 1.2


// detached field1 2.1

// leading field1
Nested.Nested.Nested field1 = 1;
// trailing field1

SomeEnum field2 = 2; // trailing field2

// leading SomeEnum
enum SomeEnum {
// trailing SomeEnum

option allow_alias = true;

// leading SomeEnum.value
value = 0;

// leading SomeEnum.value_alias
value_alias = 0;
}

// leading Nested
message Nested {
// leading Nested.Nested
message Nested {
message Nested { /* trailing Nested.Nested.Nested */
// leading Nested.Nested.Nested.nested
string nested = 1;
}
}
}

// leading choice
oneof choice {
// leading red
int32 red = 3;
// leading blue
int32 blue = 4;

int32 yellow = 5; // leading blue
}
}

// detached Service

// leading Service
service Service {
// trailing Service

// leading Method1
rpc Method1(Some) returns (Some);
// trailing Method1

// leading Method2
rpc Method2(Some) returns (Some);
// trailing Method2

rpc Method3(Some) returns (Some); // leading Method3
}
6 changes: 6 additions & 0 deletions protobuf/protobuf-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ protoSourceSets {
}
}

rpc {
protoc.buf.generate.comments {
includeFileLevelComments = false
}
}

configureLocalProtocGenDevelopmentDependency("Main", "Test")

val generatedCodeDir = layout.projectDirectory
Expand Down
Loading
Loading