micronaut-projects / micronaut-gradle-plugin

A Gradle Plugin for Micronaut
Apache License 2.0
65 stars 43 forks source link

Make the Micronaut Platform Catalog plugin compatible with BuildSrc #985

Open FrogDevelopper opened 2 months ago

FrogDevelopper commented 2 months ago

Feature description

In case of projects using precompiled buildSrc, the platform plugin is not working, as it will try to find gradle.properties or libs.versions.toml from it directory (/buildSrc/gradle.properties or /buildSrc/.gradle/libs/versions.toml) instead of the files from root directory (/gradle.properties and /.gradle/libs/versions.toml)

That would be nice to be able to apply it at the root settings as well as in the buildSrc to fully use this feature I don't know what will be the best solution, have 1 plugin for root settings and an other for the buildSrc, or be able to manage it directly in the plugin when retrieving from the properties or the toml file

Here is my current workaround to have it: in /buildSrc/settings.gradle.kts

import io.micronaut.gradle.catalog.LenientVersionCatalogParser
import io.micronaut.gradle.catalog.MicronautCatalogSettingsPlugin.MN_OVERRIDE_VERSIONS_TOML_FILE
import io.micronaut.gradle.catalog.VersionCatalogTomlModel
import org.gradle.internal.management.VersionCatalogBuilderInternal
import java.util.Properties
import java.util.function.Consumer

val properties = Properties()
file("../gradle.properties").inputStream().use { fis -> properties.load(fis) }

val micronautVersion = properties["micronautVersion"] as String

dependencyResolutionManagement {
    repositories {
        mavenCentral()
    }
    versionCatalogs {
        create("mn") {
            description = "Micronaut Catalog"
            from("io.micronaut.platform:micronaut-platform:$micronautVersion")

            val catalogOverrideFile = file("../gradle/$MN_OVERRIDE_VERSIONS_TOML_FILE")
            if (catalogOverrideFile.exists()) {
                val parser = LenientVersionCatalogParser()
                catalogOverrideFile.inputStream().use { fis ->
                    parser.parse(fis)
                    val model = parser.model
                    fixupMicronautCatalogWith(this, model)
                }
            }
        }
    }
}

fun fixupMicronautCatalogWith(catalog: VersionCatalogBuilder, model: VersionCatalogTomlModel) {
    model.versionsTable.forEach { versionModel ->
        val version = versionModel.version
        val reference = versionModel.reference
        if (reference != null) {
            catalog.version(reference) {
                val strictly = version!!.strictly
                if (strictly != null) {
                    strictly(strictly)
                } else {
                    val require = version.require
                    if (require != null) {
                        require(require)
                    }
                }
                val prefer = version.prefer
                if (prefer != null) {
                    prefer(prefer)
                }
                if (version.isRejectAll) {
                    rejectAll()
                } else {
                    val rejectedVersions = version.rejectedVersions
                    rejectedVersions?.forEach(Consumer { versions: String? ->
                        reject(
                            versions
                        )
                    })
                }
            }
        }
    }
}

rootProject.name = "buildSrc"

Of course, there is this issue about using the type-safe version available in the precompiled script plugin, but the work-around work pretty well

  1. Add in /buildSrc/build.gradle.kts

    dependencies {
    implementation(files(mn.javaClass.superclass.protectionDomain.codeSource.location))
    }
  2. Create new ProjectExt.kt file

    
    import org.gradle.accessors.dm.LibrariesForMn
    import org.gradle.api.Project
    import org.gradle.kotlin.dsl.the

// https://github.com/gradle/gradle/issues/15383 val Project.mn get() = the()



Then, the `mn` catalog will be available in the precompiled script plugins