Skip to content

Commit

Permalink
feat: Add validateCatalog to check task
Browse files Browse the repository at this point in the history
  • Loading branch information
osipxd committed Oct 6, 2022
1 parent 0bdb153 commit 0d7881d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,29 @@ import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.named
import org.gradle.language.base.plugins.LifecycleBasePlugin

abstract class ValidateVersionCatalogTask : DefaultTask() {

@get:Input
abstract val dependenciesModel: Property<DefaultVersionCatalog>

init {
group = LifecycleBasePlugin.VERIFICATION_GROUP
description = "Validates version catalog"
}

@TaskAction
fun validate() {
val catalog = dependenciesModel.get()

// Check libraries in catalog are resolvable
val configuration = createConfiguration()
configuration.addLibraries(catalog)
println("Resolved dependencies:")
logger.debug("Resolved dependencies:")
configuration.resolvedConfiguration
.firstLevelModuleDependencies
.forEach { println("- ${it.name}") }
.forEach { logger.debug("- ${it.name}") }

catalog.checkVersionsUsed()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ package com.redmadrobot.gradle.catalog

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.catalog.VersionCatalogPlugin.GENERATE_CATALOG_FILE_TASKNAME
import org.gradle.api.plugins.catalog.internal.CatalogExtensionInternal
import org.gradle.kotlin.dsl.getByName
import org.gradle.kotlin.dsl.register
import org.gradle.language.base.plugins.LifecycleBasePlugin.CHECK_TASK_NAME

class VersionCatalogExtensionsPlugin : Plugin<Project> {

override fun apply(target: Project) {
val extension = target.extensions.getByName<CatalogExtensionInternal>("catalog")
target.tasks.register<ValidateVersionCatalogTask>("validateCatalog") {
override fun apply(target: Project) = with(target) {
val extension = extensions.getByName<CatalogExtensionInternal>("catalog")
val validateCatalog = tasks.register<ValidateVersionCatalogTask>(VALIDATE_CATALOG_TASK_NAME) {
dependenciesModel.set(extension.versionCatalog)
dependsOn(tasks.named(GENERATE_CATALOG_FILE_TASKNAME))
}
tasks.named(CHECK_TASK_NAME).configure { dependsOn(validateCatalog) }
}

companion object {
const val VALIDATE_CATALOG_TASK_NAME = "validateCatalog"
}
}

0 comments on commit 0d7881d

Please sign in to comment.