Skip to content

Commit 1048217

Browse files
authored
Pb comments (#488)
1 parent aec6379 commit 1048217

File tree

53 files changed

+1954
-187
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1954
-187
lines changed

.github/workflows/protobuf-conformance.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ jobs:
2323
run: |
2424
if [[ -n "$(git status --porcelain | grep tests/protobuf-conformance/)" ]]; then
2525
echo "Protobuf conformance test is not up to date. Please run './gradlew tests:protobuf-conformance:bufGenerateMain' and commit changes"
26+
git status --porcelain
27+
git diff --minimal | cat
2628
exit 1
2729
fi

.github/workflows/protobuf-well-known-types.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ jobs:
2323
run: |
2424
if [[ -n "$(git status --porcelain | grep protobuf/protobuf-core/)" ]]; then
2525
echo "Well-Known Types are not up-to-date. Please run './gradlew :protobuf:protobuf-core:bufGenerateCommonMain' and commit changes"
26+
git status --porcelain
27+
git diff --minimal | cat
2628
exit 1
2729
fi

gradle-plugin/api/gradle-plugin.api

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public final class kotlinx/rpc/VersionsKt {
4242
public static final field LIBRARY_VERSION Ljava/lang/String;
4343
}
4444

45+
public class kotlinx/rpc/buf/BufCommentsExtension {
46+
public fun <init> (Lorg/gradle/api/Project;)V
47+
public final fun getCopyComments ()Lorg/gradle/api/provider/Property;
48+
public final fun getIncludeFileLevelComments ()Lorg/gradle/api/provider/Property;
49+
}
50+
4551
public class kotlinx/rpc/buf/BufExtension {
4652
public fun <init> (Lorg/gradle/api/model/ObjectFactory;)V
4753
public final fun generate (Lorg/gradle/api/Action;)V
@@ -65,9 +71,12 @@ public final class kotlinx/rpc/buf/BufExtension$LogFormat : java/lang/Enum {
6571

6672
public class kotlinx/rpc/buf/BufGenerateExtension {
6773
public fun <init> (Lorg/gradle/api/Project;)V
74+
public final fun comments (Lorg/gradle/api/Action;)V
75+
public final fun getComments ()Lkotlinx/rpc/buf/BufCommentsExtension;
6876
public final fun getErrorFormat ()Lorg/gradle/api/provider/Property;
6977
public final fun getIncludeImports ()Lorg/gradle/api/provider/Property;
7078
public final fun getIncludeWkt ()Lorg/gradle/api/provider/Property;
79+
public final fun getIndentSize ()Lorg/gradle/api/provider/Property;
7180
}
7281

7382
public final class kotlinx/rpc/buf/BufGenerateExtension$ErrorFormat : java/lang/Enum {

gradle-plugin/src/main/kotlin/kotlinx/rpc/Extensions.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import org.gradle.kotlin.dsl.property
1919
import java.util.concurrent.atomic.AtomicBoolean
2020
import javax.inject.Inject
2121

22-
internal fun Project.rpcExtension(): RpcExtension = extensions.findByType<RpcExtension>()
22+
internal fun Project.rpcExtensionOrNull(): RpcExtension? = extensions.findByType<RpcExtension>()
23+
24+
internal fun Project.rpcExtension(): RpcExtension = rpcExtensionOrNull()
2325
?: error("Rpc extension not found. Please apply the plugin to the project")
2426

2527
public open class RpcExtension @Inject constructor(objects: ObjectFactory, private val project: Project) {

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public open class BufExtension @Inject constructor(objects: ObjectFactory) {
6868
*
6969
* @see <a href="https://buf.build/docs/reference/cli/buf/generate/">"buf generate" command</a>
7070
* @see [BUF_GEN_YAML]
71+
* @see [BufGenerateExtension]
7172
*/
7273
public val generate: BufGenerateExtension = objects.newInstance(BufGenerateExtension::class.java)
7374

@@ -76,6 +77,7 @@ public open class BufExtension @Inject constructor(objects: ObjectFactory) {
7677
*
7778
* @see <a href="https://buf.build/docs/reference/cli/buf/generate/">"buf generate" command</a>
7879
* @see [BUF_GEN_YAML]
80+
* @see [BufGenerateExtension]
7981
*/
8082
public fun generate(configure: Action<BufGenerateExtension>) {
8183
configure.execute(generate)
@@ -226,4 +228,45 @@ public open class BufGenerateExtension @Inject constructor(internal val project:
226228
Default(""),
227229
;
228230
}
231+
232+
/**
233+
* Option to configure the indent sized for the generated code.
234+
*
235+
* Default value: `4`.
236+
*/
237+
public val indentSize: Property<Int> = project.objects.property<Int>().convention(4)
238+
239+
/**
240+
* Extension for configuring comments in the generated code.
241+
*
242+
* @see [BufCommentsExtension].
243+
*/
244+
public val comments: BufCommentsExtension = project.objects.newInstance(BufCommentsExtension::class.java)
245+
246+
/**
247+
* Extension for configuring comments in the generated code.
248+
*
249+
* @see [BufCommentsExtension].
250+
*/
251+
public fun comments(configure: Action<BufCommentsExtension>) {
252+
configure.execute(comments)
253+
}
254+
}
255+
256+
/**
257+
* Extension for configuring comments in the generated code.
258+
*/
259+
public open class BufCommentsExtension @Inject constructor(internal val project: Project) {
260+
/**
261+
* Whether to copy comments from the original source files.
262+
*/
263+
public val copyComments: Property<Boolean> = project.objects.property<Boolean>().convention(true)
264+
265+
/**
266+
* Whether to include file-level comments. This includes:
267+
* - Comments on the `package` declaration.
268+
* - Comments on the `syntax` declaration.
269+
* - Comments on the `editions` declaration.
270+
*/
271+
public val includeFileLevelComments: Property<Boolean> = project.objects.property<Boolean>().convention(true)
229272
}

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,31 @@
44

55
package kotlinx.rpc.protoc
66

7+
import kotlinx.rpc.buf.BufCommentsExtension
8+
import kotlinx.rpc.buf.BufGenerateExtension
79
import kotlinx.rpc.buf.tasks.BufGenerateTask
810
import kotlinx.rpc.protoc.ProtocPlugin.Companion.GRPC_KOTLIN_MULTIPLATFORM
911
import kotlinx.rpc.protoc.ProtocPlugin.Companion.KOTLIN_MULTIPLATFORM
12+
import kotlinx.rpc.rpcExtension
13+
import kotlinx.rpc.rpcExtensionOrNull
1014
import kotlinx.rpc.util.findOrCreate
1115
import kotlinx.rpc.util.withKotlinJvmExtension
1216
import kotlinx.rpc.util.withKotlinKmpExtension
1317
import org.gradle.api.*
1418
import org.gradle.api.file.SourceDirectorySet
1519
import org.gradle.api.provider.ListProperty
1620
import org.gradle.api.provider.Property
21+
import org.gradle.api.provider.Provider
1722
import org.gradle.api.tasks.SourceSet
1823
import org.gradle.api.tasks.SourceSetContainer
1924
import org.gradle.kotlin.dsl.add
2025
import org.gradle.kotlin.dsl.listProperty
26+
import org.gradle.kotlin.dsl.newInstance
2127
import org.gradle.kotlin.dsl.property
2228
import org.gradle.kotlin.dsl.the
2329
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
2430
import org.jetbrains.kotlin.gradle.dsl.KotlinBaseExtension
31+
import org.jetbrains.kotlin.gradle.targets.js.npm.includedRange
2532
import javax.inject.Inject
2633

2734
@Suppress("UNCHECKED_CAST")
@@ -58,24 +65,33 @@ internal open class DefaultProtoSourceSet @Inject constructor(
5865
javaJar(project.kotlinMultiplatformProtocPluginJarPath)
5966
}
6067

61-
options.put("debugOutput", "protoc-gen-kotlin-multiplatform.log")
62-
63-
if (this@DefaultProtoSourceSet.name.lowercase().endsWith("main")) {
64-
options.put("explicitApiModeEnabled", explicitApiModeEnabled)
65-
}
68+
defaultOptions(explicitApiModeEnabled)
6669
}
6770

6871
plugins.create(GRPC_KOTLIN_MULTIPLATFORM) {
6972
local {
7073
javaJar(project.grpcKotlinMultiplatformProtocPluginJarPath)
7174
}
7275

73-
options.put("debugOutput", "protoc-gen-grpc-kotlin-multiplatform.log")
76+
defaultOptions(explicitApiModeEnabled)
77+
}
78+
}
7479

75-
if (this@DefaultProtoSourceSet.name.lowercase().endsWith("main")) {
76-
options.put("explicitApiModeEnabled", explicitApiModeEnabled)
77-
}
80+
private fun ProtocPlugin.defaultOptions(explicitApiModeEnabled: Provider<Boolean>) {
81+
options.put("debugOutput", "protoc-gen-$name.log")
82+
83+
if (this@DefaultProtoSourceSet.name.lowercase().endsWith("main")) {
84+
options.put("explicitApiModeEnabled", explicitApiModeEnabled)
85+
}
86+
87+
val comments: Provider<BufGenerateExtension> = project.provider {
88+
project.rpcExtensionOrNull()?.run { protoc.buf.generate }
89+
?: project.objects.newInstance<BufGenerateExtension>()
7890
}
91+
92+
options.put("generateComments", comments.flatMap { it.comments.copyComments })
93+
options.put("generateFileLevelComments", comments.flatMap { it.comments.includeFileLevelComments })
94+
options.put("indentSize", comments.flatMap { it.indentSize })
7995
}
8096

8197
val languageSourceSets: ListProperty<Any> = project.objects.listProperty<Any>()

gradle-plugin/src/main/kotlin/kotlinx/rpc/protoc/DefaultProtocExtension.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,15 @@ import org.gradle.api.tasks.TaskProvider
2525
import org.gradle.api.tasks.compile.JavaCompile
2626
import org.gradle.kotlin.dsl.findByType
2727
import org.gradle.kotlin.dsl.newInstance
28-
import org.gradle.kotlin.dsl.the
2928
import org.gradle.kotlin.dsl.withType
30-
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
31-
import org.jetbrains.kotlin.gradle.dsl.KotlinBaseExtension
3229
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
3330
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
3431
import java.io.File
3532
import javax.inject.Inject
3633

3734
internal open class DefaultProtocExtension @Inject constructor(
3835
objects: ObjectFactory,
39-
private val project: Project,
36+
project: Project,
4037
) : ProtocExtension {
4138
override val buf: BufExtension = project.objects.newInstance<BufExtension>()
4239
override fun buf(action: Action<BufExtension>) {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// detached syntax
2+
3+
4+
// leading syntax
5+
syntax = "proto3";
6+
7+
// detached package
8+
9+
// leading package
10+
package some;
11+
12+
// detached Some
13+
14+
// leading Some 1
15+
// leading Some 2
16+
/* leading
17+
18+
19+
Some
20+
21+
22+
3
23+
24+
funky indents:
25+
- option 1
26+
- suboption 2
27+
*/
28+
message Some {
29+
// trailing Some
30+
31+
// detached field1 1.1
32+
// detached field1 1.2
33+
34+
35+
// detached field1 2.1
36+
37+
// leading field1
38+
Nested.Nested.Nested field1 = 1;
39+
// trailing field1
40+
41+
SomeEnum field2 = 2; // trailing field2
42+
43+
// leading SomeEnum
44+
enum SomeEnum {
45+
// trailing SomeEnum
46+
47+
option allow_alias = true;
48+
49+
// leading SomeEnum.value
50+
value = 0;
51+
52+
// leading SomeEnum.value_alias
53+
value_alias = 0;
54+
}
55+
56+
// leading Nested
57+
message Nested {
58+
// leading Nested.Nested
59+
message Nested {
60+
message Nested { /* trailing Nested.Nested.Nested */
61+
// leading Nested.Nested.Nested.nested
62+
string nested = 1;
63+
}
64+
}
65+
}
66+
67+
// leading choice
68+
oneof choice {
69+
// leading red
70+
int32 red = 3;
71+
// leading blue
72+
int32 blue = 4;
73+
74+
int32 yellow = 5; // leading blue
75+
}
76+
}
77+
78+
// detached Service
79+
80+
// leading Service
81+
service Service {
82+
// trailing Service
83+
84+
// leading Method1
85+
rpc Method1(Some) returns (Some);
86+
// trailing Method1
87+
88+
// leading Method2
89+
rpc Method2(Some) returns (Some);
90+
// trailing Method2
91+
92+
rpc Method3(Some) returns (Some); // leading Method3
93+
}

protobuf/protobuf-core/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ protoSourceSets {
7272
}
7373
}
7474

75+
rpc {
76+
protoc.buf.generate.comments {
77+
includeFileLevelComments = false
78+
}
79+
}
80+
7581
configureLocalProtocGenDevelopmentDependency("Main", "Test")
7682

7783
val generatedCodeDir = layout.projectDirectory

0 commit comments

Comments
 (0)