Created
February 24, 2023 20:19
-
-
Save gabrielfeo/9d32a23a3dcb71bc19b9914e5761939d to your computer and use it in GitHub Desktop.
check-inconsistent-versions.init.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Checks for inconsistent versions in project dependencies. | |
* USAGE: | |
* ./gradlew help --init-script check-inconsistent-versions.init.gradle.kts | |
* | |
* | |
* For example, a project using Okio 3.0.0 in production, but 3.3.0 in tests: | |
* | |
* project ':': inconsistencies found | |
* com.squareup.okio:okio: | |
* 3.0.0 (compileClasspath) | |
* 3.0.0 (runtimeClasspath) | |
* 3.3.0 (testCompileClasspath) | |
* 3.3.0 (testRuntimeClasspath) | |
*/ | |
projectsEvaluated { | |
allprojects { | |
val configurations = getAllCompileOrRuntimeConfigurations(project).map { it.getInfo() } | |
val diff = diffProjectConfigurations(configurations) | |
val output = when { | |
diff.isEmpty() -> "project '$path': OK" | |
else -> "\nproject '$path': inconsistencies found\n$diff" | |
} | |
println(output) | |
} | |
} | |
fun getAllCompileOrRuntimeConfigurations(project: Project): Set<Configuration> { | |
return project.configurations.matching { | |
val name = it.name.toLowerCase() | |
name.endsWith("compileclasspath") || name.endsWith("runtimeclasspath") | |
} | |
} | |
fun Configuration.getInfo() = ConfigurationInfo( | |
name = name, | |
versionsByArtifact = incoming.resolutionResult.allComponents | |
.filter { it.moduleVersion != null && it.moduleVersion?.version != "unspecified" } | |
.associate { it.moduleVersion!!.module to it.moduleVersion!!.version }, | |
) | |
data class ConfigurationInfo( | |
val name: String, | |
val versionsByArtifact: Map<ModuleIdentifier, String>, | |
) | |
fun diffProjectConfigurations( | |
configurations: List<ConfigurationInfo>, | |
) = buildString { | |
val allArtifactNames = configurations.flatMapTo(HashSet()) { it.versionsByArtifact.keys } | |
for (artifactName in allArtifactNames) { | |
val firstVersion = configurations | |
.mapNotNull { it.versionsByArtifact[artifactName] } | |
.first() | |
val anyInconsistentVersion = configurations | |
.filter { it.versionsByArtifact[artifactName] != null } | |
.any { it.versionsByArtifact[artifactName] != firstVersion } | |
if (anyInconsistentVersion) { | |
val versions = configurations.joinToString( | |
separator = "\n " | |
) { | |
val version = it.versionsByArtifact[artifactName] | |
"$version (${it.name})" | |
} | |
appendLine(" $artifactName:\n $versions") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment