Skip to content

Commit a284423

Browse files
authored
grpc: Add KMP sample project (#474)
1 parent c791fe7 commit a284423

File tree

39 files changed

+1510
-1
lines changed

39 files changed

+1510
-1
lines changed

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/internal/utils.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ internal fun grpc_status_code.toKotlin(): StatusCode = when (this) {
189189
grpc_status_code.GRPC_STATUS_UNAVAILABLE -> StatusCode.UNAVAILABLE
190190
grpc_status_code.GRPC_STATUS_DATA_LOSS -> StatusCode.DATA_LOSS
191191
grpc_status_code.GRPC_STATUS_UNAUTHENTICATED -> StatusCode.UNAUTHENTICATED
192-
grpc_status_code.GRPC_STATUS__DO_NOT_USE -> error("Invalid status code: ${this.ordinal}")
192+
grpc_status_code.GRPC_STATUS__DO_NOT_USE -> error("Invalid status code: $this")
193+
else -> error("Invalid status code: $this")
193194
}
194195

195196
internal fun StatusCode.toRawCallAllocation(): grpc_status_code = when (this) {

protobuf/protobuf-core/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,14 @@ tasks.withType<BufGenerateTask>().configureEach {
8686
outputDirectory = generatedCodeDir
8787
}
8888
}
89+
90+
// TODO: What is the correct way to declare this dependency? (KRPC-223)
91+
// (without it fails when executing "publishAllPublicationsToBuildRepository")"
92+
val bufGenerateCommonMain = tasks.named("bufGenerateCommonMain")
93+
94+
tasks.withType<org.gradle.jvm.tasks.Jar>().configureEach {
95+
// Only for sources jars
96+
if (archiveClassifier.orNull == "sources" || name.endsWith("SourcesJar")) {
97+
dependsOn(bufGenerateCommonMain)
98+
}
99+
}

samples/grpc-kmp-app/.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
*.iml
2+
.kotlin
3+
.gradle
4+
**/build/
5+
xcuserdata
6+
!src/**/build/
7+
local.properties
8+
.idea
9+
.DS_Store
10+
captures
11+
.externalNativeBuild
12+
.cxx
13+
*.xcodeproj/*
14+
!*.xcodeproj/project.pbxproj
15+
!*.xcodeproj/xcshareddata/
16+
!*.xcodeproj/project.xcworkspace/
17+
!*.xcworkspace/contents.xcworkspacedata
18+
**/xcshareddata/WorkspaceSettings.xcsettings

samples/grpc-kmp-app/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
This is a Kotlin Multiplatform project targeting iOS, Desktop (JVM), Server, demonstrating the `kotlinx.rpc` library
2+
for gRPC.
3+
4+
* [/composeApp](./composeApp/src) is for code that will be shared across Compose Multiplatform applications.
5+
- [commonMain](./composeApp/src/commonMain/kotlin) contains all the relevant gRPC client application code for all platforms.
6+
7+
* [/server](./server/src/main/kotlin) is for the gRPC server application.
8+
9+
* [/shared](./shared/src) is for the code that will be shared between all targets in the project.
10+
It contains the proto files and the generated code used by both, the server and the client applications.
11+
12+
### Build and Run Desktop (JVM) Application
13+
14+
To build and run the development version of the desktop app, use the run configuration from the run widget
15+
in your IDE’s toolbar or run it directly from the terminal:
16+
- on macOS/Linux
17+
```shell
18+
./gradlew :composeApp:run
19+
```
20+
- on Windows
21+
```shell
22+
.\gradlew.bat :composeApp:run
23+
```
24+
25+
### Build and Run Server
26+
27+
To build and run the server, use the run configuration from the run widget
28+
in your IDE’s toolbar or run it directly from the terminal:
29+
- on macOS/Linux
30+
```shell
31+
./gradlew :server:run
32+
```
33+
- on Windows
34+
```shell
35+
.\gradlew.bat :server:run
36+
```
37+
38+
### Build and Run iOS Application
39+
40+
To build and run the development version of the iOS app, use the run configuration from the run widget
41+
in your IDE’s toolbar or open the [/iosApp](./iosApp) directory in Xcode and run it from there.
42+
43+
---
44+
45+
Learn more about [Kotlin Multiplatform](https://www.jetbrains.com/help/kotlin-multiplatform-dev/get-started.html)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
plugins {
2+
// this is necessary to avoid the plugins to be loaded multiple times
3+
// in each subproject's classloader
4+
alias(libs.plugins.composeHotReload) apply false
5+
alias(libs.plugins.composeMultiplatform) apply false
6+
alias(libs.plugins.composeCompiler) apply false
7+
alias(libs.plugins.kotlinJvm) apply false
8+
alias(libs.plugins.kotlinMultiplatform) apply false
9+
alias(libs.plugins.kotlinxRpc) apply false
10+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
2+
3+
plugins {
4+
alias(libs.plugins.kotlinMultiplatform)
5+
alias(libs.plugins.composeMultiplatform)
6+
alias(libs.plugins.composeCompiler)
7+
alias(libs.plugins.composeHotReload)
8+
}
9+
10+
kotlin {
11+
listOf(
12+
iosArm64(),
13+
iosSimulatorArm64()
14+
).forEach { iosTarget ->
15+
iosTarget.binaries.framework {
16+
baseName = "ComposeApp"
17+
isStatic = true
18+
}
19+
}
20+
21+
jvm()
22+
23+
sourceSets {
24+
commonMain.dependencies {
25+
implementation(compose.runtime)
26+
implementation(compose.foundation)
27+
implementation(compose.material3)
28+
implementation(compose.ui)
29+
implementation(compose.components.resources)
30+
implementation(compose.components.uiToolingPreview)
31+
implementation(libs.androidx.lifecycle.viewmodelCompose)
32+
implementation(libs.androidx.lifecycle.runtimeCompose)
33+
implementation(libs.kotlinx.datetime)
34+
implementation(libs.kotlinx.rpc.protobuf.core)
35+
implementation(libs.kotlinx.rpc.grpc.core)
36+
implementation(libs.grpc.netty)
37+
implementation(projects.shared)
38+
}
39+
commonTest.dependencies {
40+
implementation(libs.kotlin.test)
41+
}
42+
jvmMain.dependencies {
43+
implementation(compose.desktop.currentOs)
44+
implementation(libs.kotlinx.coroutinesSwing)
45+
}
46+
}
47+
}
48+
49+
50+
compose.desktop {
51+
application {
52+
mainClass = "kotlinx.rpc.sample.MainKt"
53+
54+
nativeDistributions {
55+
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
56+
packageName = "kotlinx.rpc.sample"
57+
packageVersion = "1.0.0"
58+
}
59+
}
60+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<vector
2+
xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:aapt="http://schemas.android.com/aapt"
4+
android:width="450dp"
5+
android:height="450dp"
6+
android:viewportWidth="64"
7+
android:viewportHeight="64">
8+
<path
9+
android:pathData="M56.25,18V46L32,60 7.75,46V18L32,4Z"
10+
android:fillColor="#6075f2"/>
11+
<path
12+
android:pathData="m41.5,26.5v11L32,43V60L56.25,46V18Z"
13+
android:fillColor="#6b57ff"/>
14+
<path
15+
android:pathData="m32,43 l-9.5,-5.5v-11L7.75,18V46L32,60Z">
16+
<aapt:attr name="android:fillColor">
17+
<gradient
18+
android:centerX="23.131"
19+
android:centerY="18.441"
20+
android:gradientRadius="42.132"
21+
android:type="radial">
22+
<item android:offset="0" android:color="#FF5383EC"/>
23+
<item android:offset="0.867" android:color="#FF7F52FF"/>
24+
</gradient>
25+
</aapt:attr>
26+
</path>
27+
<path
28+
android:pathData="M22.5,26.5 L32,21 41.5,26.5 56.25,18 32,4 7.75,18Z">
29+
<aapt:attr name="android:fillColor">
30+
<gradient
31+
android:startX="44.172"
32+
android:startY="4.377"
33+
android:endX="17.973"
34+
android:endY="34.035"
35+
android:type="linear">
36+
<item android:offset="0" android:color="#FF33C3FF"/>
37+
<item android:offset="0.878" android:color="#FF5383EC"/>
38+
</gradient>
39+
</aapt:attr>
40+
</path>
41+
<path
42+
android:pathData="m32,21 l9.526,5.5v11L32,43 22.474,37.5v-11z"
43+
android:fillColor="#000000"/>
44+
</vector>

0 commit comments

Comments
 (0)