-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Ksp mocking to MockingBird plugin (#125)
Add Ksp mocking to MockingBird plugin This patch is to let the mockingbird plugin take care of the configurations and workaround the current ksp limitations that are present for commonTest code generation.
- Loading branch information
1 parent
b951415
commit 50dbe57
Showing
9 changed files
with
221 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
mockingbird-compiler/src/main/kotlin/com/careem/mockingbird/MockingbirdPluginKspDelegate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.careem.mockingbird | ||
|
||
import com.careem.mockingbird.mockingbird_compiler.BuildConfig | ||
import org.gradle.api.Project | ||
import org.gradle.configurationcache.extensions.capitalized | ||
import org.gradle.kotlin.dsl.dependencies | ||
import org.gradle.kotlin.dsl.get | ||
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile | ||
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension | ||
|
||
class MockingbirdPluginKspDelegate { | ||
fun apply(target: Project) { | ||
target.afterEvaluate { | ||
if (hasKspPlugin(this)) { | ||
// TODO revisit this whole logic once Ksp will be able to generate code for commonTest ( not the case today see: https://github.com/google/ksp/issues/567 ) | ||
// The following code will workaround the current ksp limitations doing the follow: | ||
// 1. To avoid running ksp for each target the plugin will run ksp for a single target jvm will be prefered if the target is available otherwise it will pick the first target | ||
// 2. Since current multiplatform ksp implementation can target specific targets, mocks generated will not be resolved | ||
// 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.filter { it.targetName != "metadata" }.first().targetName | ||
val selectedTargetName = | ||
targets.filter { it.targetName == "jvm" }.firstOrNull()?.targetName ?: firstTargetName | ||
sourceSets.getByName("commonTest") { | ||
kotlin.srcDir("build/generated/ksp/$selectedTargetName/${selectedTargetName}Test/kotlin") | ||
} | ||
target.dependencies { | ||
"ksp${selectedTargetName.capitalized()}Test"("com.careem.mockingbird:mockingbird-processor:${BuildConfig.VERSION}") | ||
} | ||
tasks.forEach { task -> | ||
if (task.name.contains("Test") && (task is KotlinCompile<*>)) { | ||
task.dependsOn("kspTestKotlin${selectedTargetName.capitalized()}") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun hasKspPlugin(target: Project): Boolean { | ||
return target.plugins.findPlugin("com.google.devtools.ksp") != null | ||
} | ||
} |
Oops, something went wrong.