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

com.grab.grazel.gradle.variant.AndroidVariantsExtractor.kt Maven / Gradle / Ivy

The newest version!
package com.grab.grazel.gradle.variant

import com.android.build.gradle.AppExtension
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.api.BaseVariant
import com.android.builder.model.BuildType
import com.android.builder.model.ProductFlavor
import com.grab.grazel.gradle.isAndroidApplication
import com.grab.grazel.gradle.isAndroidDynamicFeature
import com.grab.grazel.gradle.isAndroidLibrary
import org.gradle.api.DomainObjectSet
import org.gradle.api.Project
import org.gradle.kotlin.dsl.the
import javax.inject.Inject
import javax.inject.Singleton

internal interface AndroidVariantsExtractor {
    fun allVariants(project: Project): DomainObjectSet
    fun allVariants(project: Project, variantAction: (BaseVariant) -> Unit)
    fun getUnitTestVariants(project: Project): DomainObjectSet
    fun getTestVariants(project: Project): DomainObjectSet
    fun getVariants(project: Project): DomainObjectSet
    fun getFlavors(project: Project): Set
    fun getBuildTypes(project: Project): Set
}

@Singleton
internal class DefaultAndroidVariantsExtractor
@Inject
constructor() : AndroidVariantsExtractor {

    private val Project.isAndroidAppOrDynFeature get() = project.isAndroidApplication || project.isAndroidDynamicFeature

    override fun allVariants(project: Project): DomainObjectSet {
        return project.objects.domainObjectSet(BaseVariant::class.java).apply {
            addAll(getVariants(project) + getTestVariants(project) + getUnitTestVariants(project))
        }
    }

    override fun allVariants(project: Project, variantAction: (BaseVariant) -> Unit) {
        getVariants(project).all(variantAction)
        getTestVariants(project).all(variantAction)
        getUnitTestVariants(project).all(variantAction)
    }


    override fun getVariants(project: Project): DomainObjectSet {
        return when {
            project.isAndroidAppOrDynFeature -> project.the().applicationVariants
            project.isAndroidLibrary -> project.the().libraryVariants
            else -> project.objects.domainObjectSet(BaseVariant::class.java)
        }
    }

    override fun getTestVariants(project: Project): DomainObjectSet {
        return when {
            project.isAndroidAppOrDynFeature -> project.the().testVariants
            project.isAndroidLibrary -> project.the().testVariants
            else -> project.objects.domainObjectSet(BaseVariant::class.java)
        }
    }

    override fun getUnitTestVariants(project: Project): DomainObjectSet {
        return when {
            project.isAndroidAppOrDynFeature -> project.the().unitTestVariants
            project.isAndroidLibrary -> project.the().unitTestVariants
            else -> project.objects.domainObjectSet(BaseVariant::class.java)
        }
    }

    override fun getFlavors(project: Project): Set {
        return when {
            project.isAndroidAppOrDynFeature -> project.the().productFlavors
            project.isAndroidLibrary -> project.the().productFlavors
            else -> emptySet()
        }
    }

    override fun getBuildTypes(project: Project): Set {
        return when {
            project.isAndroidAppOrDynFeature -> project.the().buildTypes.toSet()
            project.isAndroidLibrary -> project.the().buildTypes.toSet()
            else -> emptySet()
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy