Skip to content

Commit f85f8b8

Browse files
committed
Simplified grpc gradle config (#281)
* Simplified grpc gradle config * Enabled explicit API for the Gradle Plugin
1 parent 7860ced commit f85f8b8

File tree

10 files changed

+342
-59
lines changed

10 files changed

+342
-59
lines changed

gradle-plugin/build.gradle.kts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ group = "org.jetbrains.kotlinx"
1616
version = rootProject.libs.versions.kotlinx.rpc.get()
1717

1818
kotlin {
19-
explicitApi = ExplicitApiMode.Disabled
19+
explicitApi = ExplicitApiMode.Strict
2020
}
2121

2222
dependencies {
23+
implementation(libs.protobuf.gradle.plugin)
2324
compileOnly(libs.kotlin.gradle.plugin)
2425
}
2526

@@ -46,18 +47,28 @@ gradlePlugin {
4647
}
4748

4849
abstract class GeneratePluginVersionTask @Inject constructor(
49-
@get:Input val pluginVersion: String,
50+
@get:Input val libraryVersion: String,
51+
@get:Input val protobufVersion: String,
52+
@get:Input val grpcVersion: String,
53+
@get:Input val grpcKotlinVersion: String,
5054
@get:OutputDirectory val sourcesDir: File
5155
) : DefaultTask() {
5256
@TaskAction
5357
fun generate() {
54-
val sourceFile = File(sourcesDir, "PluginVersion.kt")
58+
val sourceFile = File(sourcesDir, "Versions.kt")
5559

5660
sourceFile.writeText(
5761
"""
5862
package kotlinx.rpc
5963
60-
const val PLUGIN_VERSION = "$pluginVersion"
64+
public const val LIBRARY_VERSION: String = "$libraryVersion"
65+
66+
@Deprecated("Use kotlinx.rpc.LIBRARY_VERSION instead", ReplaceWith("kotlinx.rpc.LIBRARY_VERSION"))
67+
public const val PLUGIN_VERSION: String = LIBRARY_VERSION
68+
69+
public const val PROTOBUF_VERSION: String = "$protobufVersion"
70+
public const val GRPC_VERSION: String = "$grpcVersion"
71+
public const val GRPC_KOTLIN_VERSION: String = "$grpcKotlinVersion"
6172
6273
""".trimIndent()
6374
)
@@ -66,8 +77,14 @@ abstract class GeneratePluginVersionTask @Inject constructor(
6677

6778
val sourcesDir = File(project.layout.buildDirectory.asFile.get(), "generated-sources/pluginVersion")
6879

69-
val generatePluginVersionTask =
70-
tasks.register<GeneratePluginVersionTask>("generatePluginVersion", version.toString(), sourcesDir)
80+
val generatePluginVersionTask = tasks.register<GeneratePluginVersionTask>(
81+
"generatePluginVersion",
82+
version.toString(),
83+
libs.versions.protobuf.asProvider().get().toString(),
84+
libs.versions.grpc.asProvider().get().toString(),
85+
libs.versions.grpc.kotlin.get().toString(),
86+
sourcesDir,
87+
)
7188

7289
kotlin {
7390
sourceSets {

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,77 @@
77
package kotlinx.rpc
88

99
import org.gradle.api.Action
10+
import org.gradle.api.Project
1011
import org.gradle.api.model.ObjectFactory
1112
import org.gradle.api.provider.Property
13+
import org.gradle.kotlin.dsl.findByType
1214
import org.gradle.kotlin.dsl.newInstance
1315
import org.gradle.kotlin.dsl.property
1416
import javax.inject.Inject
1517

16-
open class RpcExtension @Inject constructor(objects: ObjectFactory) {
18+
public fun Project.rpcExtension(): RpcExtension = extensions.findByType<RpcExtension>() ?: RpcExtension(objects)
19+
20+
public open class RpcExtension @Inject constructor(objects: ObjectFactory) {
1721
/**
1822
* Controls `@Rpc` [annotation type-safety](https://github.com/Kotlin/kotlinx-rpc/pull/240) compile-time checkers.
1923
*
2024
* CAUTION: Disabling is considered unsafe.
2125
* This option is only needed to prevent cases where type-safety analysis fails and valid code can't be compiled.
2226
*/
2327
@RpcDangerousApi
24-
val annotationTypeSafetyEnabled = objects.property<Boolean>().convention(true)
28+
public val annotationTypeSafetyEnabled: Property<Boolean> = objects.property<Boolean>().convention(true)
2529

2630
/**
2731
* Strict mode settings.
2832
* Allows configuring the reporting state of deprecated features.
2933
*/
30-
val strict: RpcStrictModeExtension = objects.newInstance<RpcStrictModeExtension>()
34+
public val strict: RpcStrictModeExtension = objects.newInstance<RpcStrictModeExtension>()
3135

3236
/**
3337
* Strict mode settings.
3438
* Allows configuring the reporting state of deprecated features.
3539
*/
36-
fun strict(configure: Action<RpcStrictModeExtension>) {
40+
public fun strict(configure: Action<RpcStrictModeExtension>) {
3741
configure.execute(strict)
3842
}
43+
44+
/**
45+
* Grpc settings.
46+
*/
47+
public val grpc: GrpcExtension = objects.newInstance<GrpcExtension>()
48+
49+
/**
50+
* Grpc settings.
51+
*/
52+
public fun grpc(configure: Action<GrpcExtension>) {
53+
configure.execute(grpc)
54+
}
3955
}
4056

41-
open class RpcStrictModeExtension @Inject constructor(objects: ObjectFactory) {
57+
public open class RpcStrictModeExtension @Inject constructor(objects: ObjectFactory) {
4258
/**
4359
* `StateFlow`s in RPC services are deprecated,
4460
* due to their error-prone nature.
4561
*
4662
* Consider using plain flows and converting them to state on the application side.
4763
*/
48-
val stateFlow: Property<RpcStrictMode> = objects.strictModeProperty()
64+
public val stateFlow: Property<RpcStrictMode> = objects.strictModeProperty()
4965

5066
/**
5167
* `SharedFlow`s in RPC services are deprecated,
5268
* due to their error-prone nature.
5369
*
5470
* Consider using plain flows and converting them to state on the application side.
5571
*/
56-
val sharedFlow: Property<RpcStrictMode> = objects.strictModeProperty()
72+
public val sharedFlow: Property<RpcStrictMode> = objects.strictModeProperty()
5773

5874
/**
5975
* Nested flows in RPC services are deprecated,
6076
* due to their error-prone nature.
6177
*
6278
* Consider using plain flows and converting them to state on the application side.
6379
*/
64-
val nestedFlow: Property<RpcStrictMode> = objects.strictModeProperty()
80+
public val nestedFlow: Property<RpcStrictMode> = objects.strictModeProperty()
6581

6682
/**
6783
* StreamScoped functions are deprecated.
@@ -80,15 +96,15 @@ open class RpcStrictModeExtension @Inject constructor(objects: ObjectFactory) {
8096
*
8197
* Consider returning a Flow and requesting other data in a different method.
8298
*/
83-
val notTopLevelServerFlow: Property<RpcStrictMode> = objects.strictModeProperty()
99+
public val notTopLevelServerFlow: Property<RpcStrictMode> = objects.strictModeProperty()
84100

85101
/**
86102
* Fields in RPC services are deprecated,
87103
* due to its error-prone nature.
88104
*
89105
* Consider using regular streaming.
90106
*/
91-
val fields: Property<RpcStrictMode> = objects.strictModeProperty()
107+
public val fields: Property<RpcStrictMode> = objects.strictModeProperty()
92108

93109
private fun ObjectFactory.strictModeProperty(
94110
default: RpcStrictMode = RpcStrictMode.WARNING,
@@ -97,8 +113,12 @@ open class RpcStrictModeExtension @Inject constructor(objects: ObjectFactory) {
97113
}
98114
}
99115

100-
enum class RpcStrictMode {
116+
/**
117+
* Strict mode inspections levels.
118+
* Correspond to same compiler levels of messages.
119+
*/
120+
public enum class RpcStrictMode {
101121
NONE, WARNING, ERROR;
102122

103-
fun toCompilerArg(): String = name.lowercase()
123+
internal fun toCompilerArg(): String = name.lowercase()
104124
}

0 commit comments

Comments
 (0)