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

com.codingfeline.buildkonfig.gradle.BuildKonfigExtension.kt Maven / Gradle / Ivy

The newest version!
package com.codingfeline.buildkonfig.gradle

import com.codingfeline.buildkonfig.compiler.DEFAULT_KONFIG_OBJECT_NAME
import org.gradle.api.Action
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project

open class BuildKonfigExtension(
    private val project: Project
) {

    private val configFactory = TargetConfigFactory(project.objects, project.logger)

    var packageName: String? = null
    var objectName: String = DEFAULT_KONFIG_OBJECT_NAME
    var exposeObjectWithName: String? = null

    val defaultConfigs = mutableMapOf()
    val targetConfigs = mutableMapOf>()

    @Suppress("unused")
    fun defaultConfigs(config: Action) {
        defaultConfigs.computeIfAbsent("") { createNewTargetConfig() }
            .let {
                config.execute(it)
                it.flavor = ""
            }
    }

    @Suppress("unused")
    fun defaultConfigs(flavor: String, config: Action) {
        defaultConfigs.computeIfAbsent(flavor) { createNewTargetConfig() }
            .let {
                config.execute(it)
                it.flavor = flavor
            }
    }

    @Suppress("unused")
    fun targetConfigs(config: Action>) {
        val container = targetConfigs.computeIfAbsent("") { createTargetConfigContainer() }
        config.execute(container)
        container.forEach { it.flavor = "" }
    }

    @Suppress("unused")
    fun targetConfigs(flavor: String, config: Action>) {
        val container = targetConfigs.computeIfAbsent(flavor) { createTargetConfigContainer() }
        config.execute(container)
        container.forEach { it.flavor = flavor }
    }

    private fun createNewTargetConfig(): TargetConfigDsl {
        return project.objects.newInstance(
            TargetConfigDsl::class.java,
            "defaults",
            project.logger
        )
    }

    private fun createTargetConfigContainer(): NamedDomainObjectContainer {
        return project.container(TargetConfigDsl::class.java, configFactory)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy