Skip to content

Commit 5bb4b1e

Browse files
authored
feat: move gradle build to kts (#120)
1 parent 04522f3 commit 5bb4b1e

File tree

10 files changed

+393
-399
lines changed

10 files changed

+393
-399
lines changed

examples/build.gradle

Lines changed: 0 additions & 115 deletions
This file was deleted.

examples/build.gradle.kts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import com.google.protobuf.gradle.generateProtoTasks
2+
import com.google.protobuf.gradle.id
3+
import com.google.protobuf.gradle.ofSourceSet
4+
import com.google.protobuf.gradle.plugins
5+
import com.google.protobuf.gradle.protobuf
6+
import com.google.protobuf.gradle.protoc
7+
8+
val grpcVersion = "1.30.0"
9+
val grpcKotlinVersion = "0.1.3"
10+
val protobufVersion = "3.12.2"
11+
val coroutinesVersion = "1.3.7"
12+
13+
plugins {
14+
application
15+
kotlin("jvm") version "1.3.72"
16+
id("com.google.protobuf") version "0.8.12"
17+
id("org.jlleitschuh.gradle.ktlint") version "9.2.1"
18+
}
19+
20+
repositories {
21+
mavenLocal()
22+
google()
23+
jcenter()
24+
mavenCentral()
25+
}
26+
27+
dependencies {
28+
implementation(kotlin("stdlib"))
29+
implementation("javax.annotation:javax.annotation-api:1.2")
30+
implementation("com.google.protobuf:protobuf-java-util:$protobufVersion")
31+
implementation("io.grpc:grpc-protobuf:$grpcVersion")
32+
implementation("io.grpc:grpc-stub:$grpcVersion")
33+
implementation("io.grpc:grpc-kotlin-stub:$grpcKotlinVersion")
34+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
35+
runtimeOnly("io.grpc:grpc-netty-shaded:$grpcVersion")
36+
}
37+
38+
protobuf {
39+
protoc {
40+
artifact = "com.google.protobuf:protoc:$protobufVersion"
41+
}
42+
plugins {
43+
id("grpc") {
44+
artifact = "io.grpc:protoc-gen-grpc-java:$grpcVersion"
45+
}
46+
id("grpckt") {
47+
artifact = "io.grpc:protoc-gen-grpc-kotlin:$grpcKotlinVersion"
48+
}
49+
}
50+
generateProtoTasks {
51+
ofSourceSet("main").forEach {
52+
it.plugins {
53+
id("grpc")
54+
id("grpckt")
55+
}
56+
}
57+
}
58+
}
59+
60+
java {
61+
sourceCompatibility = JavaVersion.VERSION_1_8
62+
}
63+
64+
application {
65+
mainClassName = "io.grpc.examples.helloworld.HelloWorldServerKt"
66+
}
67+
68+
tasks.register<JavaExec>("HelloWorldServer") {
69+
dependsOn("classes")
70+
classpath = sourceSets["main"].runtimeClasspath
71+
main = "io.grpc.examples.helloworld.HelloWorldServerKt"
72+
}
73+
74+
tasks.register<JavaExec>("HelloWorldClient") {
75+
dependsOn("classes")
76+
classpath = sourceSets["main"].runtimeClasspath
77+
main = "io.grpc.examples.helloworld.HelloWorldClientKt"
78+
}
79+
80+
tasks.register<JavaExec>("RouteGuideServerKt") {
81+
dependsOn("classes")
82+
classpath = sourceSets["main"].runtimeClasspath
83+
main = "io.grpc.examples.routeguide.RouteGuideServerKt"
84+
}
85+
86+
tasks.register<JavaExec>("RouteGuideClientKt") {
87+
dependsOn("classes")
88+
classpath = sourceSets["main"].runtimeClasspath
89+
main = "io.grpc.examples.routeguide.RouteGuideClientKt"
90+
}
91+
92+
val helloWorldServerStartScripts = tasks.register<CreateStartScripts>("helloWorldServerStartScripts") {
93+
mainClassName = "io.grpc.examples.helloworld.HelloWorldServerKt"
94+
applicationName = "hello-world-server"
95+
outputDir = tasks.named<CreateStartScripts>("startScripts").get().outputDir
96+
classpath = tasks.named<CreateStartScripts>("startScripts").get().classpath
97+
}
98+
99+
val helloWorldClientStartScripts = tasks.register<CreateStartScripts>("helloWorldClientStartScripts") {
100+
mainClassName = "io.grpc.examples.helloworld.HelloWorldClientKt"
101+
applicationName = "hello-world-client"
102+
outputDir = tasks.named<CreateStartScripts>("startScripts").get().outputDir
103+
classpath = tasks.named<CreateStartScripts>("startScripts").get().classpath
104+
}
105+
106+
val routeGuideServerStartScripts = tasks.register<CreateStartScripts>("routeGuideServerStartScripts") {
107+
mainClassName = "io.grpc.examples.routeguide.RouteGuideServerKt"
108+
applicationName = "route-guide-server"
109+
outputDir = tasks.named<CreateStartScripts>("startScripts").get().outputDir
110+
classpath = tasks.named<CreateStartScripts>("startScripts").get().classpath
111+
}
112+
113+
val routeGuideClientStartScripts = tasks.register<CreateStartScripts>("routeGuideClientStartScripts") {
114+
mainClassName = "io.grpc.examples.routeguide.RouteGuideClientKt"
115+
applicationName = "route-guide-client"
116+
outputDir = tasks.named<CreateStartScripts>("startScripts").get().outputDir
117+
classpath = tasks.named<CreateStartScripts>("startScripts").get().classpath
118+
}
119+
120+
tasks.named("startScripts") {
121+
dependsOn(helloWorldServerStartScripts)
122+
dependsOn(helloWorldClientStartScripts)
123+
dependsOn(routeGuideServerStartScripts)
124+
dependsOn(routeGuideClientStartScripts)
125+
}

examples/settings.gradle

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = "examples"

examples/src/main/kotlin/io/grpc/examples/helloworld/HelloWorldClient.kt

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,41 @@ package io.grpc.examples.helloworld
1919
import io.grpc.ManagedChannel
2020
import io.grpc.ManagedChannelBuilder
2121
import io.grpc.examples.helloworld.GreeterGrpcKt.GreeterCoroutineStub
22+
import java.io.Closeable
23+
import java.util.concurrent.TimeUnit
2224
import kotlinx.coroutines.Dispatchers
2325
import kotlinx.coroutines.asExecutor
2426
import kotlinx.coroutines.async
2527
import kotlinx.coroutines.coroutineScope
2628
import kotlinx.coroutines.runBlocking
27-
import java.io.Closeable
28-
import java.util.concurrent.TimeUnit
2929

30-
class HelloWorldClient constructor(
31-
private val channel: ManagedChannel
32-
) : Closeable {
33-
private val stub: GreeterCoroutineStub = GreeterCoroutineStub(channel)
30+
class HelloWorldClient constructor(private val channel: ManagedChannel) : Closeable {
31+
private val stub: GreeterCoroutineStub = GreeterCoroutineStub(channel)
3432

35-
suspend fun greet(name: String) = coroutineScope {
36-
val request = HelloRequest.newBuilder().setName(name).build()
37-
val response = async { stub.sayHello(request) }
38-
println("Received: ${response.await().message}")
39-
}
33+
suspend fun greet(name: String) = coroutineScope {
34+
val request = HelloRequest.newBuilder().setName(name).build()
35+
val response = async { stub.sayHello(request) }
36+
println("Received: ${response.await().message}")
37+
}
4038

41-
override fun close() {
42-
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS)
43-
}
39+
override fun close() {
40+
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS)
41+
}
4442
}
4543

46-
4744
/**
4845
* Greeter, uses first argument as name to greet if present;
4946
* greets "world" otherwise.
5047
*/
5148
fun main(args: Array<String>) = runBlocking {
52-
val port = 50051
49+
val port = 50051
5350

54-
val client = HelloWorldClient(
55-
ManagedChannelBuilder.forAddress("localhost", port)
56-
.usePlaintext()
57-
.executor(Dispatchers.Default.asExecutor())
58-
.build())
51+
val client = HelloWorldClient(
52+
ManagedChannelBuilder.forAddress("localhost", port)
53+
.usePlaintext()
54+
.executor(Dispatchers.Default.asExecutor())
55+
.build())
5956

60-
val user = args.singleOrNull() ?: "world"
61-
client.greet(user)
57+
val user = args.singleOrNull() ?: "world"
58+
client.greet(user)
6259
}

examples/src/main/kotlin/io/grpc/examples/helloworld/HelloWorldServer.kt

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,43 @@ package io.grpc.examples.helloworld
1919
import io.grpc.Server
2020
import io.grpc.ServerBuilder
2121

22-
class HelloWorldServer constructor(
23-
private val port: Int
24-
) {
25-
val server: Server = ServerBuilder
26-
.forPort(port)
27-
.addService(HelloWorldService())
28-
.build()
29-
30-
fun start() {
31-
server.start()
32-
println("Server started, listening on $port")
33-
Runtime.getRuntime().addShutdownHook(
34-
Thread {
35-
println("*** shutting down gRPC server since JVM is shutting down")
36-
this@HelloWorldServer.stop()
37-
println("*** server shut down")
38-
}
39-
)
40-
}
41-
42-
private fun stop() {
43-
server.shutdown()
44-
}
45-
46-
fun blockUntilShutdown() {
47-
server.awaitTermination()
48-
}
49-
50-
private class HelloWorldService : GreeterGrpcKt.GreeterCoroutineImplBase() {
51-
override suspend fun sayHello(request: HelloRequest) = HelloReply
52-
.newBuilder()
53-
.setMessage("Hello ${request.name}")
54-
.build()
55-
}
22+
class HelloWorldServer constructor(private val port: Int) {
23+
val server: Server = ServerBuilder
24+
.forPort(port)
25+
.addService(HelloWorldService())
26+
.build()
27+
28+
fun start() {
29+
server.start()
30+
println("Server started, listening on $port")
31+
Runtime.getRuntime().addShutdownHook(
32+
Thread {
33+
println("*** shutting down gRPC server since JVM is shutting down")
34+
this@HelloWorldServer.stop()
35+
println("*** server shut down")
36+
}
37+
)
38+
}
39+
40+
private fun stop() {
41+
server.shutdown()
42+
}
43+
44+
fun blockUntilShutdown() {
45+
server.awaitTermination()
46+
}
47+
48+
private class HelloWorldService : GreeterGrpcKt.GreeterCoroutineImplBase() {
49+
override suspend fun sayHello(request: HelloRequest) = HelloReply
50+
.newBuilder()
51+
.setMessage("Hello ${request.name}")
52+
.build()
53+
}
5654
}
5755

5856
fun main() {
59-
val port = 50051
60-
val server = HelloWorldServer(port)
61-
server.start()
62-
server.blockUntilShutdown()
57+
val port = 50051
58+
val server = HelloWorldServer(port)
59+
server.start()
60+
server.blockUntilShutdown()
6361
}

0 commit comments

Comments
 (0)