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

ru.tinkoff.plugins.buildmetrics.android.labels.AndroidSdkLabelsFactory.kt Maven / Gradle / Ivy

package ru.tinkoff.plugins.buildmetrics.android.labels

import ru.tinkoff.plugins.buildmetrics.api.factories.Factory
import ru.tinkoff.plugins.buildmetrics.api.labels.Label
import java.io.File
import java.util.Properties

/**
 * Labels:
 * - android_sdk_revision;
 */
class AndroidSdkLabelsFactory(
    private val compileSdkVersion: Int,
    private val localPropertiesFile: File,
) : Factory.Labels {

    override fun create(): List> = mutableListOf>().apply {
        addAll(androidSdkPlatformLabels())
    }

    private fun androidSdkPlatformLabels(): List> {
        val androidSdkPath = System.getenv("ANDROID_HOME") ?: localProperties().getProperty("sdk.dir")
        check(androidSdkPath != null) {
            "Failed to get android sdk path. " +
                    "Check that the 'local.properties' file exists and the 'sdk.dir' property is set in it. " +
                    "Or set the ANDROID_HOME environment variable. " +
                    "See https://developer.android.com/tools/variables#android_home"
        }
        val androidPlatformDir = File(androidSdkPath, "platforms/android-$compileSdkVersion")
        check(androidPlatformDir.isDirectory) {
            "Failed to get android platform dir '$androidPlatformDir'."
        }
        val sourcePropertiesFile = File(androidPlatformDir, "source.properties")
        check(sourcePropertiesFile.isFile) {
            "Failed to get 'source.properties' file '$sourcePropertiesFile'."
        }
        val sourceProperties = propertiesFromFile(fileProperties = sourcePropertiesFile)
        return listOf(
            androidSdkRevisionLabel(sourceProperties = sourceProperties),
        )
    }

    private fun androidSdkRevisionLabel(
        sourceProperties: Properties,
    ): Label = Label(
        name = "android_sdk_revision",
        value = sourceProperties.getProperty("Pkg.Revision") ?: error("Failed to get property 'Pkg.Revision'.")
    )

    private fun localProperties(): Properties = propertiesFromFile(
        fileProperties = localPropertiesFile,
    )

    private fun propertiesFromFile(fileProperties: File): Properties = Properties().apply {
        if (fileProperties.isFile) {
            fileProperties.inputStream().buffered().use {
                load(it)
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy