Skip to content

Commit

Permalink
Fix proguard rule output location (#463)
Browse files Browse the repository at this point in the history
Resolves #461
  • Loading branch information
ZacSweers authored Jul 20, 2023
1 parent 1d3ea75 commit 8bc4b08
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ public class MoshiProguardGenSymbolProcessor(
targetConstructorHasDefaults = false,
targetConstructorParams = emptyList()
)
val fileName = "moshi-${targetType.canonicalName}.pro"
environment.logger.info("MOSHI: Writing rules for $fileName: $config", clazz)
environment.logger.info(
"MOSHI: Writing proguard rules for ${targetType.canonicalName}: $config",
clazz
)
val fileName = config.outputFilePathWithoutExtension(targetType.canonicalName)
environment.codeGenerator
.createNewFile(
Dependencies(
Expand All @@ -110,7 +113,7 @@ public class MoshiProguardGenSymbolProcessor(
),
packageName = "",
fileName = fileName,
extensionName = "",
extensionName = "pro",
)
.bufferedWriter()
.use { writer ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.tschuchort.compiletesting.kspSourcesDir
import com.tschuchort.compiletesting.symbolProcessorProviders
import dev.zacsweers.moshix.proguardgen.MoshiProguardGenSymbolProcessor.Companion.OPTION_GENERATE_MOSHI_CORE_PROGUARD_RULES
import dev.zacsweers.moshix.proguardgen.MoshiProguardGenSymbolProcessor.Companion.OPTION_GENERATE_PROGUARD_RULES
import java.io.File
import org.junit.Test

class MoshiSealedSymbolProcessorProviderTest {
Expand Down Expand Up @@ -61,18 +62,19 @@ class MoshiSealedSymbolProcessorProviderTest {
val generatedSourcesDir = compilation.kspSourcesDir
val proguardFiles = generatedSourcesDir.walkTopDown().filter { it.extension == "pro" }.toList()
check(proguardFiles.isNotEmpty())
proguardFiles.forEach { generatedFile ->
for (generatedFile in proguardFiles) {
generatedFile.assertInCorrectPath()
when (generatedFile.nameWithoutExtension) {
"moshi-test.BaseType" ->
assertThat(generatedFile.readText())
.contains(
"""
# Conditionally keep this adapter for every possible nested subtype that uses it.
-if class test.BaseType.TypeC
-keep class test.BaseTypeJsonAdapter {
public <init>(com.squareup.moshi.Moshi);
}
"""
# Conditionally keep this adapter for every possible nested subtype that uses it.
-if class test.BaseType.TypeC
-keep class test.BaseTypeJsonAdapter {
public <init>(com.squareup.moshi.Moshi);
}
"""
.trimIndent()
)
else -> error("Unrecognized proguard file: $generatedFile")
Expand Down Expand Up @@ -139,26 +141,27 @@ class MoshiSealedSymbolProcessorProviderTest {
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
val generatedSourcesDir = compilation.kspSourcesDir
val proguardFiles = generatedSourcesDir.walkTopDown().filter { it.extension == "pro" }.toList()
check(proguardFiles.isNotEmpty())
proguardFiles.forEach { generatedFile ->
check(proguardFiles.isNotEmpty()) { "No generated proguard files found" }
for (generatedFile in proguardFiles) {
generatedFile.assertInCorrectPath()
when (generatedFile.nameWithoutExtension) {
"moshi-test.BaseType" ->
assertThat(generatedFile.readText())
.contains(
"""
-if class test.BaseType
-keepnames class test.BaseType
-if class test.BaseType
-keep class test.BaseTypeJsonAdapter {
public <init>(com.squareup.moshi.Moshi);
}
-if class test.BaseType
-keepnames class test.BaseType
-if class test.BaseType
-keep class test.BaseTypeJsonAdapter {
public <init>(com.squareup.moshi.Moshi);
}
# Conditionally keep this adapter for every possible nested subtype that uses it.
-if class test.BaseType.TypeC
-keep class test.BaseTypeJsonAdapter {
public <init>(com.squareup.moshi.Moshi);
}
"""
# Conditionally keep this adapter for every possible nested subtype that uses it.
-if class test.BaseType.TypeC
-keep class test.BaseTypeJsonAdapter {
public <init>(com.squareup.moshi.Moshi);
}
"""
.trimIndent()
)
else -> error("Unrecognized proguard file: $generatedFile")
Expand All @@ -176,4 +179,10 @@ class MoshiSealedSymbolProcessorProviderTest {
symbolProcessorProviders = listOf(MoshiProguardGenSymbolProcessor.Provider())
block()
}

private fun File.assertInCorrectPath() {
// Ensure the proguard file is in the right place
// https://github.com/ZacSweers/MoshiX/issues/461
check(absolutePath.contains("META-INF/proguard/"))
}
}

0 comments on commit 8bc4b08

Please sign in to comment.