Skip to content

Commit 38c054b

Browse files
committed
Added pb comments generation KRPC-149
1 parent 68887e1 commit 38c054b

File tree

16 files changed

+540
-155
lines changed

16 files changed

+540
-155
lines changed

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+
}

protoc-gen/common/src/main/kotlin/kotlinx/rpc/protoc/gen/core/AModelToKotlinCommonGenerator.kt

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

55
package kotlinx.rpc.protoc.gen.core
66

7-
import kotlinx.rpc.protoc.gen.core.model.EnumDeclaration
87
import kotlinx.rpc.protoc.gen.core.model.FieldDeclaration
98
import kotlinx.rpc.protoc.gen.core.model.FieldType
109
import kotlinx.rpc.protoc.gen.core.model.FileDeclaration
@@ -21,9 +20,8 @@ const val INTERNAL_RPC_API_ANNO = "kotlinx.rpc.internal.utils.InternalRpcApi"
2120
const val WITH_CODEC_ANNO = "kotlinx.rpc.grpc.codec.WithCodec"
2221

2322
abstract class AModelToKotlinCommonGenerator(
23+
protected val config: Config,
2424
protected val model: Model,
25-
protected val logger: Logger,
26-
private val explicitApiModeEnabled: Boolean,
2725
) {
2826
protected abstract fun CodeGenerator.generatePublicDeclaredEntities(fileDeclaration: FileDeclaration)
2927
protected abstract fun CodeGenerator.generateInternalDeclaredEntities(fileDeclaration: FileDeclaration)
@@ -52,10 +50,11 @@ abstract class AModelToKotlinCommonGenerator(
5250
private fun FileDeclaration.generatePublicKotlinFile(): FileGenerator {
5351
currentPackage = packageName
5452

55-
return file(logger = logger, explicitApiModeEnabled = explicitApiModeEnabled) {
53+
return file(config) {
5654
filename = this@generatePublicKotlinFile.name
5755
packageName = this@generatePublicKotlinFile.packageName.safeFullName()
5856
packagePath = this@generatePublicKotlinFile.packageName.safeFullName()
57+
comments = this@generatePublicKotlinFile.doc
5958

6059
dependencies.forEach { dependency ->
6160
importPackage(dependency.packageName.safeFullName())
@@ -76,7 +75,7 @@ abstract class AModelToKotlinCommonGenerator(
7675
private fun FileDeclaration.generateInternalKotlinFile(): FileGenerator {
7776
currentPackage = packageName
7877

79-
return file(logger = logger, explicitApiModeEnabled = explicitApiModeEnabled) {
78+
return file(config) {
8079
filename = this@generateInternalKotlinFile.name
8180
packageName = this@generateInternalKotlinFile.packageName.safeFullName()
8281
packagePath =
@@ -106,7 +105,7 @@ abstract class AModelToKotlinCommonGenerator(
106105
type.dec.value.name.safeFullName()
107106
}
108107

109-
is FieldType.Enum -> type.dec.name.safeFullName()
108+
is FieldType.Enum -> type.dec.value.name.safeFullName()
110109

111110
is FieldType.OneOf -> type.dec.name.safeFullName()
112111

@@ -118,7 +117,7 @@ abstract class AModelToKotlinCommonGenerator(
118117
val fqValue = when (val value = type.value) {
119118
is FieldType.Message -> value.dec.value.name
120119
is FieldType.IntegralType -> value.fqName
121-
is FieldType.Enum -> value.dec.name
120+
is FieldType.Enum -> value.dec.value.name
122121
else -> error("Unsupported type: $value")
123122
}
124123

@@ -137,7 +136,7 @@ abstract class AModelToKotlinCommonGenerator(
137136
val fqValue = when (val value = entry.value) {
138137
is FieldType.Message -> value.dec.value.name
139138
is FieldType.IntegralType -> value.fqName
140-
is FieldType.Enum -> value.dec.name
139+
is FieldType.Enum -> value.dec.value.name
141140
else -> error("Unsupported type: $value")
142141
}
143142

0 commit comments

Comments
 (0)