Skip to content

Commit

Permalink
Better target source set configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
SalomonBrys committed Jan 31, 2023
1 parent 866c924 commit 3f39037
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
13 changes: 13 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,19 @@ plugins {
----
====

==== Applying to main source sets

In some cases, you may need to apply the processor to the common-main source-set instead of common-test.

[source,kotlin,subs="verbatim,attributes"]
.build.gradle.kts
----
mockmp {
targetSourceSet = CommonMain
}
----


=== With KSP and its incomplete multiplatform support

KSP for multiplatform is in beta, and *https://github.com/google/ksp/issues/567[KSP for common tests is not supported]* (yet).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
// Come on Google, having KSP work in multiplatform correctly must be very important!
class MocKMPGradlePlugin : Plugin<Project> {

@Suppress("EnumEntryName")
enum class SourceSetTarget {
CommonMain,
CommonTest_Via_JVM
}

@Suppress("PropertyName")
class Extension(
var usesHelper: Boolean = false,
var throwErrors: Boolean = false,
var public: Boolean = false,
var sourceSet: SourceSet = SourceSet.CommonTest_Via_JVM
var targetSourceSet: SourceSetTarget = SourceSetTarget.CommonTest_Via_JVM
) {
@Suppress("ClassName")
sealed class SourceSet {
object CommonMain : SourceSet()
object CommonTest_Via_JVM : SourceSet()
}
val CommonMain get() = SourceSetTarget.CommonMain
val CommonTest_Via_JVM get() = SourceSetTarget.CommonTest_Via_JVM
}

private val Project.kotlinExtension get() = project.extensions.findByType<KotlinMultiplatformExtension>() ?: throw GradleException("Could not find Kotlin/Multiplatform plugin")
Expand Down Expand Up @@ -105,9 +108,9 @@ class MocKMPGradlePlugin : Plugin<Project> {
target.extensions.add("mockmp", ext)

target.afterEvaluate {
when (ext.sourceSet) {
Extension.SourceSet.CommonMain -> executeOnMain(target, ext)
Extension.SourceSet.CommonTest_Via_JVM -> executeOnTests(target, ext)
when (ext.targetSourceSet) {
SourceSetTarget.CommonMain -> executeOnMain(target, ext)
SourceSetTarget.CommonTest_Via_JVM -> executeOnTests(target, ext)
}

}
Expand Down

0 comments on commit 3f39037

Please sign in to comment.