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

commonMain.co.touchlab.skie.gradle.KotlinCompilerVersion.kt Maven / Gradle / Ivy

There is a newer version: 0.9.0-RC.5
Show newest version
package co.touchlab.skie.gradle

import org.gradle.api.Named
import org.gradle.api.artifacts.dsl.DependencyHandler
import org.gradle.api.attributes.Attribute
import org.gradle.api.attributes.AttributeDisambiguationRule
import org.gradle.api.attributes.MultipleCandidatesDetails
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import javax.inject.Inject

interface KotlinCompilerVersion : Named {

    companion object {

        val attribute: Attribute = Attribute.of("co.touchlab.skie.kotlin.compiler.version", KotlinCompilerVersion::class.java)

        fun registerIn(dependencies: DependencyHandler, currentKotlinVersion: String) {
            dependencies.attributesSchema.attribute(attribute) {
                disambiguationRules.add(DisambiguationRule::class.java) {
                    params(currentKotlinVersion)
                }
            }
        }
    }

    class DisambiguationRule @Inject constructor(
        private val currentKotlinVersion: String,
    ) : AttributeDisambiguationRule {

        override fun execute(details: MultipleCandidatesDetails) {
            val correctCandidate = details.candidateValues.lastOrNull {
                it.name == currentKotlinVersion
            }

            if (correctCandidate != null) {
                details.closestMatch(correctCandidate)
            } else {
                // This should've already been caught by SKIE Plugin Loader, but we'll let the user know just in case.
                log.error("Could not find a Kotlin compiler version matching the current Kotlin version ($currentKotlinVersion)!")
            }
        }

        companion object {

            val log: Logger = Logging.getLogger("KotlinCompilerVersion.DisambiguationRule")
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy