Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for property-based plugin arguments #466

Merged
merged 7 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,16 @@ class PokoBuildPlugin : Plugin<Project> {
buildConfigField("VERSION", project.pokoVersion)
buildConfigField("ANNOTATIONS_ARTIFACT", "poko-annotations")
buildConfigField("COMPILER_PLUGIN_ARTIFACT", "poko-compiler-plugin")

buildConfigField("POKO_ENABLED_OPTION_NAME", "enabled")
buildConfigField("DEFAULT_POKO_ENABLED", true)

buildConfigField("POKO_ANNOTATION_OPTION_NAME", "pokoAnnotation")
buildConfigField("DEFAULT_POKO_ANNOTATION", "dev/drewhamilton/poko/Poko")

buildConfigField("POKO_PLUGIN_ARGS_OPTION_NAME", "pokoPluginArgs")
buildConfigField("POKO_PLUGIN_ARGS_LIST_DELIMITER", ';')
buildConfigField("POKO_PLUGIN_ARGS_ITEM_DELIMITER", '=')
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dev.drewhamilton.poko
import org.jetbrains.kotlin.config.CompilerConfigurationKey

internal object CompilerOptions {
val ENABLED = CompilerConfigurationKey<Boolean>("enabled")
val POKO_ANNOTATION = CompilerConfigurationKey<String>("pokoAnnotation")
val ENABLED = CompilerConfigurationKey<Boolean>(BuildConfig.POKO_ENABLED_OPTION_NAME)
val POKO_ANNOTATION = CompilerConfigurationKey<String>(BuildConfig.POKO_ANNOTATION_OPTION_NAME)
val POKO_PLUGIN_ARGS =
CompilerConfigurationKey<Map<String, String>>(BuildConfig.POKO_PLUGIN_ARGS_OPTION_NAME)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,50 @@ public class PokoCommandLineProcessor : CommandLineProcessor {
override val pluginId: String = BuildConfig.COMPILER_PLUGIN_ARTIFACT

override val pluginOptions: Collection<AbstractCliOption> = listOf(
CliOption(CompilerOptions.ENABLED.toString(), "<true|false>", "", required = false),
CliOption(CompilerOptions.POKO_ANNOTATION.toString(), "Annotation class name", "", required = false),
CliOption(
optionName = CompilerOptions.ENABLED.toString(),
valueDescription = "<true|false>",
description = "",
required = false,
),
CliOption(
optionName = CompilerOptions.POKO_ANNOTATION.toString(),
valueDescription = "Annotation class name",
description = "",
required = false,
),
CliOption(
optionName = CompilerOptions.POKO_PLUGIN_ARGS.toString(),
valueDescription = "",
description = "Additional Poko compiler plugin arguments",
required = false,
),
)

override fun processOption(
option: AbstractCliOption,
value: String,
configuration: CompilerConfiguration
): Unit = when (option.optionName) {
CompilerOptions.ENABLED.toString() -> configuration.put(CompilerOptions.ENABLED, value.toBoolean())
CompilerOptions.POKO_ANNOTATION.toString() -> configuration.put(CompilerOptions.POKO_ANNOTATION, value)
else -> throw IllegalArgumentException("Unknown plugin option: ${option.optionName}")
CompilerOptions.ENABLED.toString() ->
configuration.put(CompilerOptions.ENABLED, value.toBoolean())
CompilerOptions.POKO_ANNOTATION.toString() ->
configuration.put(CompilerOptions.POKO_ANNOTATION, value)
CompilerOptions.POKO_PLUGIN_ARGS.toString() ->
configuration.put(
CompilerOptions.POKO_PLUGIN_ARGS,
value.split(BuildConfig.POKO_PLUGIN_ARGS_LIST_DELIMITER)
.associate { arg ->
arg.split(BuildConfig.POKO_PLUGIN_ARGS_ITEM_DELIMITER).let {
require(it.size == 2) {
"Invalid syntax for <${it.firstOrNull()}>: " +
"must be in `key=value` property format"
}
it.first() to it.last()
}
},
)
else ->
throw IllegalArgumentException("Unknown plugin option: ${option.optionName}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dev.drewhamilton.poko.BuildConfig.DEFAULT_POKO_ENABLED
import dev.drewhamilton.poko.fir.PokoFirExtensionRegistrar
import dev.drewhamilton.poko.ir.PokoIrGenerationExtension
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
Expand All @@ -20,17 +21,30 @@ public class PokoCompilerPluginRegistrar : CompilerPluginRegistrar() {

override val supportsK2: Boolean get() = true

private val knownPokoPluginArgs = emptySet<String>()

override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) {
if (!configuration.get(CompilerOptions.ENABLED, DEFAULT_POKO_ENABLED))
return

val pokoAnnotationString = configuration.get(CompilerOptions.POKO_ANNOTATION, DEFAULT_POKO_ANNOTATION)
val pokoAnnotationClassId = ClassId.fromString(pokoAnnotationString)

val messageCollector = configuration.get(
CommonConfigurationKeys.MESSAGE_COLLECTOR_KEY,
MessageCollector.NONE,
)

val pokoPluginArgs = configuration.get(CompilerOptions.POKO_PLUGIN_ARGS, emptyMap())
pokoPluginArgs.keys.forEach { pluginArgName ->
if (!knownPokoPluginArgs.contains(pluginArgName)) {
messageCollector.report(
severity = CompilerMessageSeverity.WARNING,
message = "Ignoring unknown Poko plugin arg: $pluginArgName",
)
}
}

IrGenerationExtension.registerExtension(
PokoIrGenerationExtension(pokoAnnotationClassId, messageCollector)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,26 +177,51 @@ class PokoCompilerPluginTest(
}
}

@Test fun `compilation with unknown pokoPluginArg yields warning`() {
testCompilation(
pokoPluginArgs = "poko.nothing=value",
) { result ->
val warning = "w: Ignoring unknown Poko plugin arg: poko.nothing"
assertThat(result.messages).contains(warning)
}
}

@Test fun `compilation with invalid pokoPluginArg fails`() {
testCompilation(
pokoPluginArgs = "poko.nothing",
expectedExitCode = KotlinCompilation.ExitCode.COMPILATION_ERROR,
) { result ->
val error = "Invalid syntax for <poko.nothing>: must be in `key=value` property format"
assertThat(result.messages).contains(error)
}
}

private inline fun testCompilation(
vararg sourceFileNames: String,
pokoAnnotationName: String = "dev/drewhamilton/poko/Poko",
pokoPluginArgs: String? = null,
expectedExitCode: KotlinCompilation.ExitCode = KotlinCompilation.ExitCode.OK,
additionalTesting: (JvmCompilationResult) -> Unit = {}
) = testCompilation(
*sourceFileNames.map { SourceFile.fromPath("src/test/resources/$it.kt") }.toTypedArray(),
pokoAnnotationName = pokoAnnotationName,
pokoPluginArgs = pokoPluginArgs,
expectedExitCode = expectedExitCode,
additionalTesting = additionalTesting
)

private inline fun testCompilation(
vararg sourceFiles: SourceFile,
pokoAnnotationName: String,
pokoPluginArgs: String? = null,
expectedExitCode: KotlinCompilation.ExitCode = KotlinCompilation.ExitCode.OK,
additionalTesting: (JvmCompilationResult) -> Unit = {}
) {
val result =
prepareCompilation(*sourceFiles, pokoAnnotationName = pokoAnnotationName).compile()
val result = prepareCompilation(
*sourceFiles,
pokoAnnotationName = pokoAnnotationName,
pokoPluginArgs = pokoPluginArgs,
).compile()
if (
expectedExitCode == KotlinCompilation.ExitCode.OK &&
result.exitCode != KotlinCompilation.ExitCode.OK
Expand Down Expand Up @@ -224,6 +249,7 @@ class PokoCompilerPluginTest(
private fun prepareCompilation(
vararg sourceFiles: SourceFile,
pokoAnnotationName: String,
pokoPluginArgs: String?,
) = KotlinCompilation().apply {
workingDir = temporaryFolder.root
compilerPluginRegistrars = listOf(PokoCompilerPluginRegistrar())
Expand All @@ -240,13 +266,19 @@ class PokoCompilerPluginTest(

val commandLineProcessor = PokoCommandLineProcessor()
commandLineProcessors = listOf(commandLineProcessor)
pluginOptions = listOf(
pluginOptions = listOfNotNull(
commandLineProcessor.option(CompilerOptions.ENABLED, true),
commandLineProcessor.option(CompilerOptions.POKO_ANNOTATION, pokoAnnotationName)
commandLineProcessor.option(CompilerOptions.POKO_ANNOTATION, pokoAnnotationName),
pokoPluginArgs?.let {
commandLineProcessor.option(CompilerOptions.POKO_PLUGIN_ARGS, it)
},
)
}

private fun CommandLineProcessor.option(key: CompilerConfigurationKey<*>, value: Any?) = PluginOption(
private fun CommandLineProcessor.option(
key: CompilerConfigurationKey<*>,
value: Any?,
) = PluginOption(
pluginId,
key.toString(),
value.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,28 @@ public class PokoGradlePlugin : KotlinCompilerPluginSupportPlugin {
val project = kotlinCompilation.target.project
val extension = project.extensions.getByType(PokoPluginExtension::class.java)

val pokoPluginArgs = project.properties
.filter { it.key.startsWith("poko.", ignoreCase = true) }
.map { (key, value) -> "$key${BuildConfig.POKO_PLUGIN_ARGS_ITEM_DELIMITER}$value" }
.joinToString(separator = BuildConfig.POKO_PLUGIN_ARGS_LIST_DELIMITER.toString())
.ifBlank { null }

return project.provider {
listOf(
SubpluginOption(key = "enabled", value = extension.enabled.get().toString()),
SubpluginOption(key = "pokoAnnotation", value = extension.pokoAnnotation.get()),
listOfNotNull(
SubpluginOption(
key = BuildConfig.POKO_ENABLED_OPTION_NAME,
value = extension.enabled.get().toString(),
),
SubpluginOption(
key = BuildConfig.POKO_ANNOTATION_OPTION_NAME,
value = extension.pokoAnnotation.get(),
),
pokoPluginArgs?.let {
SubpluginOption(
key = BuildConfig.POKO_PLUGIN_ARGS_OPTION_NAME,
value = it,
)
},
)
}
}
Expand Down
Loading