All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.freeletics.gradle.setup.ProcessingSetup.kt Maven / Gradle / Ivy

There is a newer version: 0.20.0
Show newest version
package com.freeletics.gradle.setup

import com.freeletics.gradle.util.android
import com.google.devtools.ksp.gradle.KspExtension
import org.gradle.api.Project
import org.gradle.process.CommandLineArgumentProvider
import org.jetbrains.kotlin.gradle.plugin.KaptExtension

internal fun Project.configureProcessing(
    useKsp: Boolean,
    vararg arguments: ProcessingArgument,
): String {
    if (useKsp) {
        plugins.apply("com.google.devtools.ksp")

        if (arguments.isNotEmpty()) {
            extensions.configure(KspExtension::class.java) { extension ->
                arguments.forEach { arg ->
                    when (arg) {
                        is BasicArgument ->
                            extension.arg(arg.key, arg.value)

                        is CliArgumentProvider ->
                            extension.arg(arg.provider)
                    }
                }
            }
        }

        return "ksp"
    } else {
        plugins.apply("org.jetbrains.kotlin.kapt")

        val basicArguments = arguments.filterIsInstance()
        if (basicArguments.isNotEmpty()) {
            extensions.configure(KaptExtension::class.java) { extension ->
                extension.mapDiagnosticLocations = true
                extension.correctErrorTypes = true

                extension.arguments {
                    basicArguments.forEach { (key, value) ->
                        arg(key, value)
                    }
                }
            }
        }
        val cliArguments = arguments.filterIsInstance()
            .map { KaptArgProviderWrapper(it.provider) }
        if (cliArguments.isNotEmpty()) {
            project.android {
                defaultConfig {
                    javaCompileOptions {
                        annotationProcessorOptions {
                            compilerArgumentProviders.addAll(cliArguments)
                        }
                    }
                }
            }
        }

        return "kapt"
    }
}

internal sealed interface ProcessingArgument

internal fun basicArgument(pair: Pair): ProcessingArgument = BasicArgument(pair.first, pair.second)

private data class BasicArgument(
    val key: String,
    val value: String,
) : ProcessingArgument

internal fun argumentProvider(provider: CommandLineArgumentProvider): ProcessingArgument = CliArgumentProvider(provider)

private data class CliArgumentProvider(
    val provider: CommandLineArgumentProvider,
) : ProcessingArgument

private class KaptArgProviderWrapper(
    val provider: CommandLineArgumentProvider,
) : CommandLineArgumentProvider {
    override fun asArguments(): Iterable {
        // prepends arguments with "-A" for kapt or javac
        return provider.asArguments().map { "-A$it" }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy