How can i accpept as list in SubpluginOption of Ko...
# compiler
f
How can i accpept as list in SubpluginOption of KotlinCompilerPluginSupportPlugin
r
You mean pass a list from the gradle plugin to the compiler plugin? I suspect you just construct your
CliOption
in the plugin with
repeated = true
and pass repeated options. Haven't tested it myself though
f
Yes, But in kotlin 2.1.10 i don't see any repeated = true
Copy code
class CliOption(
    override val optionName: String,
    override val valueDescription: String,
    override val description: String,
    override val required: Boolean = true,
    override val allowMultipleOccurrences: Boolean = false
) : AbstractCliOption
Copy code
override fun applyToCompilation(kotlinCompilation: KotlinCompilation<*>): Provider<List<SubpluginOption>> {
        val project = kotlinCompilation.target.project
        val extension = project.extensions.findByType(SemanticsExtension::class.java) ?:
        throw IllegalStateException("SemanticsExtension not found in project ${project.name}")
        val arg = extension.whiteListedUiComponents.joinToString(", ") { it.trim() }
        return project.provider {
            listOf(
                SubpluginOption(key = OPTION_ENABLED, value = extension.enabled.toString()),
                SubpluginOption(key = OPTION_TEST_TAG_PREFIX, value = extension.testTagPrefix),
                SubpluginOption(key = OPTION_PACKAGE_NAME, value = extension.packageName.toString()),
                SubpluginOption(
                    key = OPTION_WHITELISTED_UI_COMPONENTS,
                    value = extension.whiteListedUiComponents.joinToString(", ") { it.trim() }
                )
            )
        }
    }
open class SemanticsExtension {
    /**
     *  @property enabled Enables or disables the semantics plugin. Default is true.
     */
    var enabled: Boolean = true

    /**
     * @property whiteListedUiComponents List of UI component to generate Ids.
     * By default it is empty, meaning all components are whitelisted. Ids for all components will be generated.
     * Example: listOf("Button", "Text", "Image")
     */
    var whiteListedUiComponents: List<String> = emptyList()
}
In command line processor
Copy code
override val pluginOptions: Collection<CliOption> = listOf(
    CliOption(
        optionName = OPTION_ENABLED,
        valueDescription = "true|false",
        description = "Enable semantics plugin"
    ),
    CliOption(
        optionName = OPTION_WHITELISTED_UI_COMPONENTS,
        valueDescription = "string",
        description = "Comma-separated list of UI components",
    )
)

override fun processOption(
    option: AbstractCliOption,
    value: String,
    configuration: CompilerConfiguration
) {
    when (option.optionName) {
        OPTION_ENABLED -> configuration.put(ARG_ENABLED, value.toBoolean())
        OPTION_WHITELISTED_UI_COMPONENTS -> {
            val components = value.split(", ").map { it.trim() }.filter { it.isNotEmpty() }
            configuration.put(ARG_WHITELISTED_UI_COMPONENTS, components)
        }
        else -> throw IllegalArgumentException("Unknown option: ${option.optionName}")
    }
}
This whiteListedUiComponents list is not working
Trying with allowMultipleOccurrences = true
Not working.
Copy code
SubpluginOption(
    key = OPTION_WHITELISTED_UI_COMPONENTS,
    value = "Text,Button"
)
Passing something like this is throwing same error
Wrong plugin option format: null, should be plugin:<pluginId>:<optionName>=<value>
r
If you're passing a comma separated list I doubt you need
allowMultipleOccurrences
at all, I would expect it to work. Is
OPTION_WHITELISTED_UI_COMPONENTS
initialized and doesn't have any nulls or whitespace or
:
, etc? And does your value have spaces? Your
processOption
is attempting to split on
", "
when it should probably be
","
f
Ok, Thanks Ryan Nett. At last i created an entry of every string-element present in list with
SubpluginOption
keeping the key name same. This worked for me.
list.map{ SubpluginOptions(key, it) },
kept key name same