Skip to content

Commit

Permalink
feat: Check all versions are used
Browse files Browse the repository at this point in the history
  • Loading branch information
osipxd committed Oct 6, 2022
1 parent e90e375 commit 0bdb153
Showing 1 changed file with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import org.gradle.api.DefaultTask
import org.gradle.api.artifacts.Configuration
import org.gradle.api.attributes.Usage
import org.gradle.api.internal.catalog.DefaultVersionCatalog
import org.gradle.api.internal.catalog.DependencyModel
import org.gradle.api.internal.catalog.PluginModel
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
Expand All @@ -25,6 +27,8 @@ abstract class ValidateVersionCatalogTask : DefaultTask() {
configuration.resolvedConfiguration
.firstLevelModuleDependencies
.forEach { println("- ${it.name}") }

catalog.checkVersionsUsed()
}

private fun createConfiguration(): Configuration {
Expand All @@ -44,16 +48,32 @@ abstract class ValidateVersionCatalogTask : DefaultTask() {
}

private fun Configuration.addLibraries(catalog: DefaultVersionCatalog) {
catalog.libraryAliases
.asSequence()
.map(catalog::getDependencyData)
catalog.libraries
.forEach { dependency ->
val dependencyNotation = "${dependency.group}:${dependency.name}:${dependency.version}"
project.dependencies.add(name, dependencyNotation)
}
}

private fun DefaultVersionCatalog.checkVersionsUsed() {
val librariesVersions = libraries.map { it.versionRef }.toSet()
val pluginsVersions = plugins.map { it.versionRef }.toSet()
val unusedVersions = versionAliases - librariesVersions - pluginsVersions

if (unusedVersions.isNotEmpty()) logger.warn("[WARNING] These versions are not used: $unusedVersions")
}

private companion object {
const val CONFIGURATION_NAME = "libraries"
}
}

private val DefaultVersionCatalog.libraries: Sequence<DependencyModel>
get() = libraryAliases
.asSequence()
.map(::getDependencyData)

private val DefaultVersionCatalog.plugins: Sequence<PluginModel>
get() = pluginAliases
.asSequence()
.map(::getPlugin)

0 comments on commit 0bdb153

Please sign in to comment.