-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'checkPlatformVersionConsistency' task
- Loading branch information
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
60 changes: 60 additions & 0 deletions
60
...plugins/src/main/kotlin/org/example/dependencyanalysis/PlatformVersionConsistencyCheck.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.example.dependencyanalysis | ||
|
||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.artifacts.component.ModuleComponentSelector | ||
import org.gradle.api.artifacts.result.ResolvedComponentResult | ||
import org.gradle.api.attributes.Attribute | ||
import org.gradle.api.attributes.Category | ||
import org.gradle.api.provider.SetProperty | ||
import org.gradle.api.tasks.Internal | ||
import org.gradle.api.tasks.TaskAction | ||
|
||
abstract class PlatformVersionConsistencyCheck : DefaultTask() { | ||
|
||
@get:Internal | ||
abstract val productClasspath: SetProperty<ResolvedComponentResult> | ||
|
||
@get:Internal | ||
abstract val classpathFromPlatform: SetProperty<ResolvedComponentResult> | ||
|
||
@TaskAction | ||
fun check() { | ||
val publishedCategory = Attribute.of( Category.CATEGORY_ATTRIBUTE.name, String::class.java) | ||
val resolvedToDeclaredVersions = | ||
productClasspath.get().filter { it.moduleVersion?.group != "org.example.product" }.associate { cpEntry -> | ||
cpEntry.moduleVersion to cpEntry.dependents.find { | ||
it.from.variants.any { variant -> | ||
variant.attributes.getAttribute(Category.CATEGORY_ATTRIBUTE)?.name == Category.REGULAR_PLATFORM | ||
|| variant.attributes.getAttribute(publishedCategory) == Category.REGULAR_PLATFORM | ||
} | ||
}?.let { | ||
(it.requested as ModuleComponentSelector).version | ||
} | ||
} | ||
|
||
|
||
val unnecessaryEntries = classpathFromPlatform.get().filter { platformEntry -> | ||
productClasspath.get().none { platformEntry.moduleVersion?.module == it.moduleVersion?.module } | ||
} | ||
val missingEntries = resolvedToDeclaredVersions.filter { it.value == null }.map { it.key } | ||
val wrongEntries = resolvedToDeclaredVersions.filter { it.value != null && it.key?.version != it.value } | ||
|
||
if (unnecessaryEntries.isNotEmpty() || missingEntries.isNotEmpty() || wrongEntries.isNotEmpty()) { | ||
throw RuntimeException(""" | ||
The following entries are not used in production code: | ||
${unnecessaryEntries.joinToString("\n ") { "api(\"${it.moduleVersion}\")" }} | ||
The following transitive dependencies are not listed in the platform: | ||
${missingEntries.joinToString("\n ") { "api(\"${it}\")" }} | ||
The following dependencies should be updated to the resolved versions: | ||
${wrongEntries.keys.joinToString("\n ") { "api(\"${it}\") - currently declared '${wrongEntries[it]}'" }} | ||
""".trimIndent()) | ||
} | ||
} | ||
} | ||
|