From 20bdb46d372e11e07915dbaacc56a8c747a2c1c2 Mon Sep 17 00:00:00 2001 From: Abdulahi Osoble Date: Wed, 4 Oct 2023 21:57:50 +0400 Subject: [PATCH] Fix ksp code generation for non-jvm target When jvm isn't a target, ksp code generation fails due to the wrong srcDir being generated and the wrong dependency and task being set --- CHANGELOG.md | 1 + .../MockingbirdPluginKspDelegate.kt | 34 +++++++++++++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93b3831..fcf45e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. --- ## master +* Fix ksp code generation for non-jvm target ## 2.13.0 * Refactoring publication and configuration logic diff --git a/mockingbird-compiler/src/main/kotlin/com/careem/mockingbird/MockingbirdPluginKspDelegate.kt b/mockingbird-compiler/src/main/kotlin/com/careem/mockingbird/MockingbirdPluginKspDelegate.kt index ec87139..c7fef44 100644 --- a/mockingbird-compiler/src/main/kotlin/com/careem/mockingbird/MockingbirdPluginKspDelegate.kt +++ b/mockingbird-compiler/src/main/kotlin/com/careem/mockingbird/MockingbirdPluginKspDelegate.kt @@ -34,18 +34,38 @@ class MockingbirdPluginKspDelegate { // in commonTest. The plugin will add this the code generated at point 1 as source set for common test so that // this code will be available for each platform and resolvable by the IDE target.extensions.configure(KotlinMultiplatformExtension::class.java) { - val firstTargetName = targets.first { it.targetName != "metadata" }.targetName - val selectedTargetName = - targets.firstOrNull { it.targetName == "jvm" }?.targetName ?: firstTargetName - sourceSets.getByName("commonTest") { - kotlin.srcDir("build/generated/ksp/$selectedTargetName/${selectedTargetName}Test/kotlin") + val selectedTarget = + targets.firstOrNull { it.preset?.name == "jvm" } + ?: targets.firstOrNull { it.preset?.name == "android" } + ?: targets.first { it.preset?.name != "metadata" } + + val srcDir = if (selectedTarget.preset!!.name == "android") { + "build/generated/ksp/${selectedTarget.name}/${selectedTarget.name}UnitTestRelease/kotlin" + } else { + "build/generated/ksp/${selectedTarget.name}/${selectedTarget.name}Test/kotlin" + } + + sourceSets.getByName("commonTest") { kotlin.srcDir(srcDir) } + + val kspConfiguration = if (selectedTarget.preset!!.name == "android") { + "ksp${selectedTarget.name.capitalized()}TestRelease" + } else { + "ksp${selectedTarget.name.capitalized()}Test" } + target.dependencies { - "ksp${selectedTargetName.capitalized()}Test"("com.careem.mockingbird:mockingbird-processor:${BuildConfig.VERSION}") + kspConfiguration("com.careem.mockingbird:mockingbird-processor:${BuildConfig.VERSION}") + } + + val kspTask = if (selectedTarget.preset!!.name == "android") { + "kspReleaseUnitTestKotlin${selectedTarget.name.capitalized()}" + } else { + "kspTestKotlin${selectedTarget.name.capitalized()}" } + tasks.forEach { task -> if (task.name.contains("Test") && (task is KotlinCompile<*>)) { - task.dependsOn("kspTestKotlin${selectedTargetName.capitalized()}") + task.dependsOn(kspTask) } } }