From dfa15d72388572ec23a310596d0dcfd7be1dd73b Mon Sep 17 00:00:00 2001 From: xiaobailong24 Date: Fri, 1 Sep 2023 01:08:02 +0800 Subject: [PATCH] fix(example): add Ksp Ktorfit plugin to test sourceSets (#404) * fix(example): add Ksp Ktorfit plugin to test sourceSets, fix commonTest to support all platforms --- .../MultiplatformExample/gradle.properties | 1 - .../shared/build.gradle.kts | 88 ++++++------------- .../com/example/ktorfittest/KtorfitTest.kt | 19 ++++ .../kotlin/com/example/ktorfittest/TestApi.kt | 15 ++++ .../com/example/ktorfittest/Platform.kt | 2 +- .../com/example/ktorfittest/Platform.kt | 3 +- 6 files changed, 65 insertions(+), 63 deletions(-) create mode 100644 example/MultiplatformExample/shared/src/commonTest/kotlin/com/example/ktorfittest/KtorfitTest.kt create mode 100644 example/MultiplatformExample/shared/src/commonTest/kotlin/com/example/ktorfittest/TestApi.kt diff --git a/example/MultiplatformExample/gradle.properties b/example/MultiplatformExample/gradle.properties index 99a764c54..5f2d1af36 100644 --- a/example/MultiplatformExample/gradle.properties +++ b/example/MultiplatformExample/gradle.properties @@ -6,5 +6,4 @@ kotlin.code.style=official android.useAndroidX=true #MPP kotlin.mpp.enableCInteropCommonization=true -kotlin.native.binary.memoryModel=experimental kotlin.mpp.androidSourceSetLayoutVersion=2 diff --git a/example/MultiplatformExample/shared/build.gradle.kts b/example/MultiplatformExample/shared/build.gradle.kts index e6dcdf31e..c15f7345c 100644 --- a/example/MultiplatformExample/shared/build.gradle.kts +++ b/example/MultiplatformExample/shared/build.gradle.kts @@ -11,29 +11,20 @@ version = "1.0" val ktorVersion = "2.3.3" val ktorfitVersion = "1.6.0" -configure { +ktorfit { version = ktorfitVersion + logging = true } -tasks.withType { - kotlinOptions.jvmTarget = "1.8" -} - -java { - toolchain { - languageVersion.set(JavaLanguageVersion.of(8)) - } -} kotlin { - android() + jvmToolchain(8) + targetHierarchy.default() + + jvm() + androidTarget() iosX64() iosArm64() iosSimulatorArm64() - jvm(){ - compilations.all { - kotlinOptions.jvmTarget = "1.8" - } - } macosX64() js(IR) { this.nodejs() @@ -67,43 +58,11 @@ kotlin { implementation(kotlin("test")) } } - val androidMain by getting { - dependencies { - //implementation("io.ktor:ktor-client-cio-jvm:$ktorVersion") - } - } - val jvmMain by getting { - dependencies { - - - } - } - val jsMain by getting { - dependencies { - - } - } - val iosX64Main by getting - val iosArm64Main by getting - val iosSimulatorArm64Main by getting - val iosMain by creating { - dependsOn(commonMain) - iosX64Main.dependsOn(this) - iosArm64Main.dependsOn(this) - iosSimulatorArm64Main.dependsOn(this) - dependencies{ - // implementation("io.ktor:ktor-client-ios:$ktorVersion") - } - } - val iosX64Test by getting - val iosArm64Test by getting - val iosSimulatorArm64Test by getting - val iosTest by creating { - dependsOn(commonTest) - iosX64Test.dependsOn(this) - iosArm64Test.dependsOn(this) - iosSimulatorArm64Test.dependsOn(this) - } + val androidMain by getting + val jvmMain by getting + val jsMain by getting + val iosMain by getting + val macosX64Main by getting } } @@ -117,11 +76,22 @@ android { } dependencies { - add("kspCommonMainMetadata", "de.jensklingenberg.ktorfit:ktorfit-ksp:$ktorfitVersion") - add("kspJvm", "de.jensklingenberg.ktorfit:ktorfit-ksp:$ktorfitVersion") - add("kspAndroid", "de.jensklingenberg.ktorfit:ktorfit-ksp:$ktorfitVersion") - add("kspIosX64", "de.jensklingenberg.ktorfit:ktorfit-ksp:$ktorfitVersion") - add("kspJs", "de.jensklingenberg.ktorfit:ktorfit-ksp:$ktorfitVersion") - add("kspIosSimulatorArm64", "de.jensklingenberg.ktorfit:ktorfit-ksp:$ktorfitVersion") + with("de.jensklingenberg.ktorfit:ktorfit-ksp:$ktorfitVersion") { + add("kspCommonMainMetadata", this) + add("kspJvm", this) + add("kspJvmTest", this) + add("kspAndroid", this) + add("kspAndroidTest", this) + add("kspIosX64", this) + add("kspIosX64Test", this) + add("kspIosArm64", this) + add("kspIosArm64Test", this) + add("kspIosSimulatorArm64", this) + add("kspIosSimulatorArm64Test", this) + add("kspMacosX64", this) + add("kspMacosX64Test", this) + add("kspJs", this) + add("kspJsTest", this) + } } diff --git a/example/MultiplatformExample/shared/src/commonTest/kotlin/com/example/ktorfittest/KtorfitTest.kt b/example/MultiplatformExample/shared/src/commonTest/kotlin/com/example/ktorfittest/KtorfitTest.kt new file mode 100644 index 000000000..0ed1c29e4 --- /dev/null +++ b/example/MultiplatformExample/shared/src/commonTest/kotlin/com/example/ktorfittest/KtorfitTest.kt @@ -0,0 +1,19 @@ +package com.example.ktorfittest + +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch +import kotlin.test.Test +import kotlin.test.assertEquals + +class KtorfitTest { + @Test + fun test() { + GlobalScope.launch { + ktorfit.create() + .getPersonByIdResponse(3) + .collect { + assertEquals(it.name, "R2-D2") + } + } + } +} \ No newline at end of file diff --git a/example/MultiplatformExample/shared/src/commonTest/kotlin/com/example/ktorfittest/TestApi.kt b/example/MultiplatformExample/shared/src/commonTest/kotlin/com/example/ktorfittest/TestApi.kt new file mode 100644 index 000000000..47d56ac5e --- /dev/null +++ b/example/MultiplatformExample/shared/src/commonTest/kotlin/com/example/ktorfittest/TestApi.kt @@ -0,0 +1,15 @@ +package com.example.ktorfittest + +import de.jensklingenberg.ktorfit.http.GET +import de.jensklingenberg.ktorfit.http.Path +import kotlinx.coroutines.flow.Flow + +interface TestApi { + companion object { + const val baseUrl = "https://swapi.dev/api/" + } + + @GET("people/{id}/") + suspend fun getPersonByIdResponse(@Path("id") peopleId: Int): Flow +} + diff --git a/example/MultiplatformExample/shared/src/jsMain/kotlin/com/example/ktorfittest/Platform.kt b/example/MultiplatformExample/shared/src/jsMain/kotlin/com/example/ktorfittest/Platform.kt index 9cea8fe58..5e5c53329 100644 --- a/example/MultiplatformExample/shared/src/jsMain/kotlin/com/example/ktorfittest/Platform.kt +++ b/example/MultiplatformExample/shared/src/jsMain/kotlin/com/example/ktorfittest/Platform.kt @@ -2,5 +2,5 @@ package com.example.ktorfittest actual class Platform actual constructor() { actual val platform: String - get() = "JVM" + get() = "JS" } \ No newline at end of file diff --git a/example/MultiplatformExample/shared/src/macosX64Main/kotlin/com/example/ktorfittest/Platform.kt b/example/MultiplatformExample/shared/src/macosX64Main/kotlin/com/example/ktorfittest/Platform.kt index 51d449bf0..0520dfaf3 100644 --- a/example/MultiplatformExample/shared/src/macosX64Main/kotlin/com/example/ktorfittest/Platform.kt +++ b/example/MultiplatformExample/shared/src/macosX64Main/kotlin/com/example/ktorfittest/Platform.kt @@ -1,7 +1,6 @@ package com.example.ktorfittest -import platform.UIKit.UIDevice actual class Platform actual constructor() { - actual val platform: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion + actual val platform: String = "macOS" } \ No newline at end of file