Small grammar and naming followups for proguard gen (#1365)

This commit is contained in:
Zac Sweers
2021-07-16 03:06:43 -04:00
committed by GitHub
parent 72f464bbdc
commit ce45a68cc3
3 changed files with 9 additions and 10 deletions

View File

@@ -61,11 +61,12 @@ public class JsonClassCodegenProcessor : AbstractProcessor() {
public const val OPTION_GENERATED: String = "moshi.generated" public const val OPTION_GENERATED: String = "moshi.generated"
/** /**
* This annotation processing argument disables proguard rule generation. * This boolean processing option can control proguard rule generation.
* Normally, this is not recommended unless end-users build their own JsonAdapter look-up tool. * Normally, this is not recommended unless end-users build their own JsonAdapter look-up tool.
* This is enabled by default * This is enabled by default.
*/ */
public const val OPTION_ENABLE_PROGUARD_RULE_GENERATION: String = "moshi.enabledProguardGenerated" public const val OPTION_GENERATE_PROGUARD_RULES: String = "moshi.generateProguardRules"
private val POSSIBLE_GENERATED_NAMES = arrayOf( private val POSSIBLE_GENERATED_NAMES = arrayOf(
ClassName("javax.annotation.processing", "Generated"), ClassName("javax.annotation.processing", "Generated"),
ClassName("javax.annotation", "Generated") ClassName("javax.annotation", "Generated")
@@ -96,7 +97,7 @@ public class JsonClassCodegenProcessor : AbstractProcessor() {
) )
} }
generateProguardRules = processingEnv.options[OPTION_ENABLE_PROGUARD_RULE_GENERATION]?.toBooleanStrictOrNull() ?: true generateProguardRules = processingEnv.options[OPTION_GENERATE_PROGUARD_RULES]?.toBooleanStrictOrNull() ?: true
this.types = processingEnv.typeUtils this.types = processingEnv.typeUtils
this.elements = processingEnv.elementUtils this.elements = processingEnv.elementUtils
@@ -145,9 +146,7 @@ public class JsonClassCodegenProcessor : AbstractProcessor() {
} }
preparedAdapter.spec.writeTo(filer) preparedAdapter.spec.writeTo(filer)
if (generateProguardRules) { preparedAdapter.proguardConfig?.writeTo(filer, type)
preparedAdapter.proguardConfig?.writeTo(filer, type)
}
} }
} }

View File

@@ -163,7 +163,7 @@ internal class AdapterGenerator(
.initializer("null") .initializer("null")
.build() .build()
fun prepare(generatedProguard: Boolean, typeHook: (TypeSpec) -> TypeSpec = { it }): PreparedAdapter { fun prepare(generateProguardRules: Boolean, typeHook: (TypeSpec) -> TypeSpec = { it }): PreparedAdapter {
val reservedSimpleNames = mutableSetOf<String>() val reservedSimpleNames = mutableSetOf<String>()
for (property in nonTransientProperties) { for (property in nonTransientProperties) {
// Allocate names for simple property types first to avoid collisions // Allocate names for simple property types first to avoid collisions
@@ -181,7 +181,7 @@ internal class AdapterGenerator(
result.addComment("Code generated by moshi-kotlin-codegen. Do not edit.") result.addComment("Code generated by moshi-kotlin-codegen. Do not edit.")
result.addAnnotation(COMMON_SUPPRESS) result.addAnnotation(COMMON_SUPPRESS)
result.addType(generatedAdapter) result.addType(generatedAdapter)
val proguardConfig = if (generatedProguard) { val proguardConfig = if (generateProguardRules) {
generatedAdapter.createProguardRule() generatedAdapter.createProguardRule()
} else { } else {
null null

View File

@@ -368,7 +368,7 @@ class JsonClassCodegenProcessorTest {
""" """
) )
).apply { ).apply {
kaptArgs[JsonClassCodegenProcessor.OPTION_ENABLE_PROGUARD_RULE_GENERATION] = "false" kaptArgs[JsonClassCodegenProcessor.OPTION_GENERATE_PROGUARD_RULES] = "false"
}.compile() }.compile()
assertThat(result.generatedFiles.filter { it.endsWith(".pro") }).isEmpty() assertThat(result.generatedFiles.filter { it.endsWith(".pro") }).isEmpty()
} }