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

com.spirytusz.spi.weaver.transform.ServiceProviderTransform.kt Maven / Gradle / Ivy

There is a newer version: 0.0.2
Show newest version
package com.spirytusz.spi.weaver.transform

import com.android.build.api.transform.QualifiedContent
import com.android.build.api.transform.Transform
import com.android.build.api.transform.TransformInvocation
import com.android.build.gradle.internal.pipeline.TransformManager
import com.google.gson.GsonBuilder
import com.spirytusz.spi.weaver.log.Logger
import com.spirytusz.spi.weaver.transform.global.TransformContext
import com.spirytusz.spi.weaver.transform.scan.InputScanner
import com.spirytusz.spi.weaver.transform.weave.CodeGenerator

class ServiceProviderTransform(
    private val transformContext: TransformContext
) : Transform() {

    companion object {
        private const val TAG = "ServiceProviderTransform"
    }

    override fun getName(): String {
        return "ServiceProviderTransform"
    }

    override fun getInputTypes(): MutableSet {
        return TransformManager.CONTENT_CLASS
    }

    override fun getScopes(): MutableSet {
        return TransformManager.SCOPE_FULL_PROJECT
    }

    override fun isIncremental(): Boolean {
        return true
    }

    override fun transform(transformInvocation: TransformInvocation?) {
        val start = System.currentTimeMillis()
        if (transformInvocation == null) {
            Logger.e(TAG) { "transform() >>> transformInvocation is null" }
            throw IllegalArgumentException("transformInvocation is null")
        }

        val configProvider = transformContext.configProvider
        Logger.isDebuggable = configProvider.debuggable
        Logger.i(TAG) { "transform() >>> config=$configProvider" }

        val inputScanner = InputScanner(transformContext)
        inputScanner.onReceiveInput(transformInvocation)
        val serviceMapping = inputScanner.serviceMapping
        Logger.d(TAG) {
            val gson = GsonBuilder().enableComplexMapKeySerialization().setPrettyPrinting().create()
            "serviceMapping: ${gson.toJson(serviceMapping)}"
        }
        CodeGenerator(transformInvocation, serviceMapping).generate()
        val end = System.currentTimeMillis()
        Logger.i(TAG) { "transform time cost [${end - start}]ms" }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy