Skip to content

Commit

Permalink
Two small IR fixes (#198)
Browse files Browse the repository at this point in the history
* Fix generateProguardRules not getting applied

* Always set it

* Try synchronizing extension access

* Add generateProguardRules regression test
  • Loading branch information
ZacSweers authored Jan 6, 2022
1 parent 6ff52f6 commit 74e6fd5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.com.intellij.mock.MockProject
import org.jetbrains.kotlin.com.intellij.openapi.extensions.Extensions
import org.jetbrains.kotlin.com.intellij.openapi.extensions.LoadingOrder
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
import org.jetbrains.kotlin.config.CompilerConfiguration
Expand Down Expand Up @@ -76,8 +77,13 @@ private fun AnalysisHandlerExtension.Companion.registerExtensionFirst(
project: MockProject,
extension: AnalysisHandlerExtension
) {
project
.extensionArea
.getExtensionPoint(AnalysisHandlerExtension.extensionPointName)
.registerExtension(extension, LoadingOrder.FIRST, project)
// See
// https://github.com/detekt/detekt/commit/a0d36e2ca4f6ca38cac0f9cb418df989ccf4f063#diff-1abfa33e705e9d1a139e397920c0a3b91cff3fe0d738291dd9bce517943290d0R24-R28
@Suppress("DEPRECATION")
synchronized(Extensions.getRootArea()) {
project
.extensionArea
.getExtensionPoint(AnalysisHandlerExtension.extensionPointName)
.registerExtension(extension, LoadingOrder.FIRST, project)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ class MoshiIrVisitorTest {
val result =
prepareCompilation(
"javax.annotation.GeneratedBlerg",
true,
kotlin(
"source.kt",
"""
Expand Down Expand Up @@ -745,12 +746,35 @@ class MoshiIrVisitorTest {
}
}

@Test
fun `Disabled proguard rules gen should generate no rules`() {
val compilation =
prepareCompilation(
null,
generateProguardRules = false,
kotlin(
"source.kt",
"""
package testPackage
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class Example(val firstName: String)
"""))
val result = compilation.compile()
assertThat(result.exitCode).isEqualTo(OK)

assertThat(resourcesDir.walkTopDown().filter { it.extension == "pro" }.toList()).isEmpty()
}

private fun prepareCompilation(vararg sourceFiles: SourceFile): KotlinCompilation {
return prepareCompilation(null, *sourceFiles)
return prepareCompilation(
generatedAnnotation = null, generateProguardRules = true, *sourceFiles)
}

private fun prepareCompilation(
generatedAnnotation: String? = null,
generateProguardRules: Boolean = true,
vararg sourceFiles: SourceFile
): KotlinCompilation {
return KotlinCompilation().apply {
Expand All @@ -762,7 +786,7 @@ class MoshiIrVisitorTest {
buildList {
add(processor.option(KEY_ENABLED, "true"))
add(processor.option(KEY_DEBUG, "false")) // Enable when needed for extra debugging
add(processor.option(KEY_GENERATE_PROGUARD_RULES, "true"))
add(processor.option(KEY_GENERATE_PROGUARD_RULES, generateProguardRules.toString()))
add(processor.option(KEY_RESOURCES_OUTPUT_DIR, resourcesDir))
if (generatedAnnotation != null) {
processor.option(KEY_GENERATED_ANNOTATION, generatedAnnotation)
Expand All @@ -780,13 +804,6 @@ class MoshiIrVisitorTest {
}

private fun compile(vararg sourceFiles: SourceFile): KotlinCompilation.Result {
return compile(null, *sourceFiles)
}

private fun compile(
generatedAnnotation: String? = null,
vararg sourceFiles: SourceFile
): KotlinCompilation.Result {
return prepareCompilation(generatedAnnotation, *sourceFiles).compile()
return prepareCompilation(null, true, *sourceFiles).compile()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class MoshiGradleSubplugin : KotlinCompilerPluginSupportPlugin {
if (generatedAnnotation != null) {
add(SubpluginOption(key = "generatedAnnotation", value = generatedAnnotation))
}
add(SubpluginOption("generateProguardRules", generateProguardRules.toString()))
if (generateProguardRules) {
val resourceOutputDir = getMoshiXResourceOutputDir(project, sourceSetName).path
add(SubpluginOption("resourcesOutputDir", resourceOutputDir))
Expand All @@ -107,14 +108,6 @@ class MoshiGradleSubplugin : KotlinCompilerPluginSupportPlugin {
}
}

// fun registerGeneratedJavaSources(
// project: Project,
// kotlinCompilation: KotlinJvmAndroidCompilation,
// resourcesOutputDir: FileCollection,
// ) {
// kotlinCompilation.androidVariant.registerPostJavacGeneratedBytecode(resourcesOutputDir)
// }

// Copied from kotlin-gradle-plugin, because they are internal.
internal inline fun <reified T : Task> Project.locateTask(name: String): TaskProvider<T>? =
try {
Expand Down

0 comments on commit 74e6fd5

Please sign in to comment.