buildSrc/src/main/kotlin/VersionAlignment.kt
changeset 68 e3215d4393f1
parent 67 882d271b5297
child 69 1167496633b2
equal deleted inserted replaced
67:882d271b5297 68:e3215d4393f1
     1 /*
       
     2  * Geekdroid is a utility library for development on the Android
       
     3  * Platform.
       
     4  *
       
     5  * Copyright (C) 2017-2024 by Frederic-Charles Barthelery.
       
     6  *
       
     7  * This file is part of Geekdroid.
       
     8  *
       
     9  * Geekdroid is free software: you can redistribute it and/or modify
       
    10  * it under the terms of the GNU General Public License as published by
       
    11  * the Free Software Foundation, either version 3 of the License, or
       
    12  * (at your option) any later version.
       
    13  *
       
    14  * Geekdroid is distributed in the hope that it will be useful,
       
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    17  * GNU General Public License for more details.
       
    18  *
       
    19  * You should have received a copy of the GNU General Public License
       
    20  * along with Geekdroid.  If not, see <http://www.gnu.org/licenses/>.
       
    21  */
       
    22 package com.geekorum.build
       
    23 
       
    24 import org.gradle.api.artifacts.ComponentMetadataContext
       
    25 import org.gradle.api.artifacts.ComponentMetadataRule
       
    26 import org.gradle.api.artifacts.Dependency
       
    27 import org.gradle.api.artifacts.dsl.ComponentMetadataHandler
       
    28 import org.gradle.api.artifacts.dsl.DependencyHandler
       
    29 
       
    30 private val componentsPlatform = mutableMapOf<ComponentMetadataHandler, MutableSet<String>>()
       
    31 
       
    32 fun DependencyHandler.createComponentsPlatforms() {
       
    33     components.apply {
       
    34         getOrCreatePlatform(DaggerPlatform)
       
    35     }
       
    36 }
       
    37 
       
    38 private fun ComponentMetadataHandler.getOrCreatePlatform(platformFactory: PlatformFactory): String {
       
    39     val componentsSet = componentsPlatform.getOrPut(this) { mutableSetOf() }
       
    40     if (!componentsSet.contains(platformFactory.platformName)) {
       
    41         componentsSet.add(platformFactory.createPlatform(this))
       
    42     }
       
    43     return platformFactory.platformName
       
    44 }
       
    45 
       
    46 internal class DaggerPlatform {
       
    47     companion object : PlatformFactory("com.google.dagger:dagger-platform",
       
    48         AlignmentRule::class.java)
       
    49 
       
    50     open class AlignmentRule : SameGroupAlignmentRule(platformName, "com.google.dagger")
       
    51 }
       
    52 
       
    53 fun DependencyHandler.daggerPlatform(version: String): Dependency {
       
    54     return platform("${components.getOrCreatePlatform(DaggerPlatform)}:$version")
       
    55 }
       
    56 
       
    57 open class PlatformFactory(
       
    58     internal val platformName: String,
       
    59     private val alignmentRule: Class<out ComponentMetadataRule>
       
    60 ) {
       
    61     fun createPlatform(components: ComponentMetadataHandler): String {
       
    62         components.all(alignmentRule)
       
    63         return platformName
       
    64     }
       
    65 }
       
    66 
       
    67 internal open class SameGroupAlignmentRule(
       
    68     private val platformName: String,
       
    69     private val group: String
       
    70 ) : ComponentMetadataRule {
       
    71 
       
    72     override fun execute(ctx: ComponentMetadataContext) {
       
    73         ctx.details.run {
       
    74             if (id.group == group) {
       
    75                 belongsTo("$platformName:${id.version}")
       
    76             }
       
    77         }
       
    78     }
       
    79 
       
    80 }