Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the Micronaut Platform Catalog plugin compatible with BuildSrc #985

Open
FrogDevelopper opened this issue Apr 15, 2024 · 0 comments
Open

Comments

@FrogDevelopper
Copy link

FrogDevelopper commented Apr 15, 2024

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))
}
  1. 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<LibrariesForMn>()

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant