Allow excluding Kotlin source sets from transformation

This commit is contained in:
Brian Norman
2022-03-13 16:00:10 -05:00
parent ee05aaa7e3
commit ee1370925f
6 changed files with 53 additions and 1 deletions
+27
View File
@@ -119,6 +119,33 @@ kotlinPowerAssert {
} }
``` ```
You can also exclude Gradle source sets from being transformed by the plugin,
where those source sets can be specified by name.
```kotlin
// Kotlin DSL
configure<com.bnorm.power.PowerAssertGradleExtension> {
excludedSourceSets = listOf(
"commonMain",
"jvmMain",
"jsMain",
"nativeMain"
)
}
```
```groovy
// Groovy
kotlinPowerAssert {
excludedSourceSets = [
"commonMain",
"jvmMain",
"jsMain",
"nativeMain"
]
}
```
## Compatibility ## Compatibility
The Kotlin compiler plugin API is unstable and each new version of Kotlin can The Kotlin compiler plugin API is unstable and each new version of Kotlin can
@@ -18,4 +18,5 @@ package com.bnorm.power
open class PowerAssertGradleExtension { open class PowerAssertGradleExtension {
var functions: List<String> = listOf("kotlin.assert") var functions: List<String> = listOf("kotlin.assert")
var excludedSourceSets: List<String> = listOf()
} }
@@ -28,7 +28,11 @@ class PowerAssertGradlePlugin : KotlinCompilerPluginSupportPlugin {
extensions.create("kotlinPowerAssert", PowerAssertGradleExtension::class.java) extensions.create("kotlinPowerAssert", PowerAssertGradleExtension::class.java)
} }
override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean = true override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean {
val project = kotlinCompilation.target.project
val extension = project.extensions.getByType(PowerAssertGradleExtension::class.java)
return extension.excludedSourceSets.none { it == kotlinCompilation.defaultSourceSet.name }
}
override fun getCompilerPluginId(): String = "com.bnorm.kotlin-power-assert" override fun getCompilerPluginId(): String = "com.bnorm.kotlin-power-assert"
+6
View File
@@ -66,4 +66,10 @@ configure<com.bnorm.power.PowerAssertGradleExtension> {
"com.bnorm.power.assert", "com.bnorm.power.assert",
"com.bnorm.power.dbg" "com.bnorm.power.dbg"
) )
excludedSourceSets = listOf(
"commonMain",
"jvmMain",
"jsMain",
"nativeMain"
)
} }
@@ -20,6 +20,11 @@ data class Person(
val firstName: String, val firstName: String,
val lastName: String, val lastName: String,
) { ) {
init {
require(firstName.isNotBlank())
require(lastName.isNotBlank())
}
companion object { companion object {
val UNKNOWN = listOf(Person("John", "Doe"), Person("Jane", "Doe")) val UNKNOWN = listOf(Person("John", "Doe"), Person("Jane", "Doe"))
override fun toString() = "Person.Companion" override fun toString() = "Person.Companion"
@@ -63,6 +63,15 @@ class PowerAssertTest {
) )
} }
@Test
fun excludedRequire() {
val error = assertFailsWith<IllegalArgumentException> { Person("", "") }
assertEquals(
actual = error.message,
expected = "Failed requirement."
)
}
@Test @Test
fun softAssert() { fun softAssert() {
val unknown: List<Person>? = Person.UNKNOWN val unknown: List<Person>? = Person.UNKNOWN