Skip to content

Commit

Permalink
Enable KSP proguard rule gen in ir tests + fix them! (#492)
Browse files Browse the repository at this point in the history
* Enable KSP proguard rule gen in ir tests

* Add a proguard rules validation check

* Add another included build for rule gen

* Don't add KSP deps in afterEvaluate

* Don't enable it on K2

* It's a string

* Use find

* Always add dep for now

* Fix test dir check
  • Loading branch information
ZacSweers committed Sep 6, 2023
1 parent 71fa63a commit 430cb44
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,27 @@ class MoshiGradleSubplugin : KotlinCompilerPluginSupportPlugin {
"Please apply the KSP Gradle plugin ('com.google.devtools.ksp') to your buildscript and try again."
)
}
target.afterEvaluate { project ->
// Add the KSP dependency to the appropriate configurations
// In KMP, we only add to androidJvm/jvm targets
val kExtension = project.kotlinExtension
val configurationsToAdd =
if (kExtension is KotlinMultiplatformExtension) {
kExtension.targets
.filter { it.platformType in SUPPORTED_PLATFORMS }
.map {
"ksp${it.targetName.replaceFirstChar { if (it.isLowerCase()) it.titlecase(US) else it.toString() }}"
}
} else {
listOf("ksp")

fun addKspDep(configurationName: String) {
target.dependencies.add(
configurationName,
"dev.zacsweers.moshix:moshi-proguard-rule-gen:$VERSION"
)
}

// Add the KSP dependency to the appropriate configurations
// In KMP, we only add to androidJvm/jvm targets
val kExtension = target.kotlinExtension
if (kExtension is KotlinMultiplatformExtension) {
kExtension.targets
.matching { it.platformType in SUPPORTED_PLATFORMS }
.configureEach {
addKspDep(
"ksp${it.targetName.replaceFirstChar { if (it.isLowerCase()) it.titlecase(US) else it.toString() }}"
)
}
for (config in configurationsToAdd) {
target.dependencies.add(config, "dev.zacsweers.moshix:moshi-proguard-rule-gen:$VERSION")
}
} else {
addKspDep("ksp")
}
target.extensions.configure(KspExtension::class.java) {
// Enable core moshi proguard rule gen
Expand Down
19 changes: 19 additions & 0 deletions moshi-ir/moshi-kotlin-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,29 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
alias(libs.plugins.kotlinJvm)
alias(libs.plugins.moshix)
alias(libs.plugins.ksp)
}

moshi { enableSealed.set(true) }

val proguardRuleValidator =
tasks.register("validateProguardRules") {
doNotTrackState("This is a validation task that should always run")
doLast {
logger.lifecycle("Validating proguard rules")
val proguardRulesDir = project.file("build/generated/ksp/test/resources/META-INF/proguard")
check(proguardRulesDir.exists() && proguardRulesDir.listFiles()!!.isNotEmpty()) {
"No proguard rules found! Did you forget to apply the KSP Gradle plugin?"
}
logger.lifecycle("Proguard rules properly generated ✅ ")
}
}

tasks.withType<KotlinCompile>().configureEach {
compilerOptions { freeCompilerArgs.addAll("-opt-in=kotlin.ExperimentalStdlibApi") }
if (name == "compileTestKotlin" && project.findProperty("kotlin.experimental.tryK2") != "true") {
finalizedBy(proguardRuleValidator)
}
}

dependencies {
Expand All @@ -43,5 +60,7 @@ configurations.configureEach {
.using(project(":moshi-ir:moshi-compiler-plugin"))
substitute(module("dev.zacsweers.moshix:moshi-sealed-runtime"))
.using(project(":moshi-sealed:runtime"))
substitute(module("dev.zacsweers.moshix:moshi-proguard-rule-gen"))
.using(project(":moshi-proguard-rule-gen"))
}
}
2 changes: 1 addition & 1 deletion moshi-ir/moshi-kotlin-tests/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
moshix.generateProguardRules=false
moshix.generateProguardRules=true

0 comments on commit 430cb44

Please sign in to comment.