Skip to content

Commit

Permalink
Simplify extension adding (#201)
Browse files Browse the repository at this point in the history
Dealing with extension points manually appears to be problematic and we don't necessarily need to run first
  • Loading branch information
ZacSweers authored Jan 13, 2022
1 parent 324d99a commit bec40fe
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ 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
import org.jetbrains.kotlin.name.FqName
Expand All @@ -50,15 +48,7 @@ public class MoshiComponentRegistrar : ComponentRegistrar {
val resourceOutputDir =
configuration.get(KEY_RESOURCES_OUTPUT_DIR)?.let(::File)
?: error("No resources dir provided for proguard rule generation")
// It's important to register our extension at the first position. The compiler calls each
// extension one by one. If an extension returns a result, then the compiler won't call any
// other extension. That usually happens with Kapt in the stub generating task.
//
// It's not dangerous for our extension to run first, because we generate code, restart the
// analysis phase and then don't return a result anymore. That means the next extension can
// take over. If we wouldn't do this and any other extension won't let ours run, then we
// couldn't generate any code.
AnalysisHandlerExtension.registerExtensionFirst(
AnalysisHandlerExtension.registerExtension(
project,
ProguardRuleGenerationExtension(
messageCollector = messageCollector,
Expand All @@ -72,18 +62,3 @@ public class MoshiComponentRegistrar : ComponentRegistrar {
MoshiIrGenerationExtension(messageCollector, fqGeneratedAnnotation, enableSealed, debug))
}
}

private fun AnalysisHandlerExtension.Companion.registerExtensionFirst(
project: MockProject,
extension: AnalysisHandlerExtension
) {
// 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 @@ -68,7 +68,8 @@ class MoshiGradleSubplugin : KotlinCompilerPluginSupportPlugin {

val enableSealed = extension.enableSealed.get()
if (enableSealed) {
project.dependencies.add("implementation", "dev.zacsweers.moshix:moshi-sealed-runtime:$VERSION")
project.dependencies.add(
"implementation", "dev.zacsweers.moshix:moshi-sealed-runtime:$VERSION")
}

if (generateProguardRules) {
Expand Down

0 comments on commit bec40fe

Please sign in to comment.