-
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.
- Loading branch information
Showing
7 changed files
with
184 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ pluginManagement { | |
plugins { | ||
id("org.example.settings") | ||
} | ||
includeBuild("../..") |
1 change: 1 addition & 0 deletions
1
gradle/plugins/base-plugins/src/main/kotlin/org.example.platform.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
54 changes: 54 additions & 0 deletions
54
...ency-analysis-plugins/src/main/kotlin/org.example.dependency-analysis-platform.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import org.example.dependencyanalysis.DependencyFormatCheck | ||
import org.example.dependencyanalysis.PlatformVersionConsistencyCheck | ||
|
||
plugins { | ||
id("java-platform") | ||
id("org.gradlex.java-module-dependencies") | ||
} | ||
|
||
val checkDependencyFormatting = tasks.register<DependencyFormatCheck>("checkDependencyFormatting") { | ||
group = LifecycleBasePlugin.VERIFICATION_GROUP | ||
|
||
buildFilePath.set(project.buildFile.absolutePath) | ||
declaredDependencies.put("api", provider { configurations.api.get().dependencies.map { d -> d.toDeclaredString() } }) | ||
declaredDependencies.put("runtime", provider { configurations.runtime.get().dependencies.map { d -> d.toDeclaredString() } }) | ||
declaredConstraints.put("api", provider { configurations.api.get().dependencyConstraints.map { d -> d.toDeclaredString() } }) | ||
declaredConstraints.put("runtime", provider { configurations.runtime.get().dependencyConstraints.map { d -> d.toDeclaredString() } }) | ||
} | ||
|
||
// Install a task that checks the consistency of the platform against the resolution result of the product | ||
val product = configurations.dependencyScope("product").get() | ||
dependencies { | ||
product(platform(project(path))) | ||
} | ||
val purePlatformVersions = configurations.dependencyScope("purePlatformVersions") { | ||
withDependencies { | ||
add(project.dependencies.platform(project(project.path))) | ||
// Create a dependency for each constraint defined in the platform (this is to check for unused entries) | ||
configurations.api.get().dependencyConstraints.forEach { constraint -> | ||
add(project.dependencies.create("${constraint.group}:${constraint.name}") { isTransitive = false }) | ||
} | ||
} | ||
} | ||
val fullProductRuntimeClasspath = configurations.resolvable("fullProductRuntimeClasspath") { | ||
attributes.attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME)) | ||
attributes.attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.LIBRARY)) | ||
extendsFrom(product) | ||
} | ||
val purePlatformVersionsPath = configurations.resolvable("purePlatformVersionsPath") { | ||
attributes.attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME)) | ||
attributes.attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.LIBRARY)) | ||
extendsFrom(purePlatformVersions.get()) | ||
} | ||
tasks.register<PlatformVersionConsistencyCheck>("checkPlatformVersionConsistency") { | ||
group = HelpTasksPlugin.HELP_GROUP | ||
productClasspath.set(fullProductRuntimeClasspath.map { it.incoming.resolutionResult.allComponents }) | ||
classpathFromPlatform.set(purePlatformVersionsPath.map { it.incoming.resolutionResult.allComponents }) | ||
} | ||
|
||
tasks.check { | ||
dependsOn(checkDependencyFormatting) | ||
} | ||
|
||
fun Dependency.toDeclaredString() = "$group:$name:$version" | ||
fun DependencyConstraint.toDeclaredString() = "version(\"${javaModuleDependencies.moduleName("$group:$name").getOrElse("$group:$name")}\", \"$version\")" |
59 changes: 59 additions & 0 deletions
59
...-analysis-plugins/src/main/kotlin/org/example/dependencyanalysis/DependencyFormatCheck.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,59 @@ | ||
package org.example.dependencyanalysis | ||
|
||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.provider.MapProperty | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.TaskAction | ||
|
||
/** | ||
* Check that 'dependencies' are defined in alphabetical order and without version. | ||
*/ | ||
abstract class DependencyFormatCheck : DefaultTask() { | ||
|
||
@get:Input | ||
abstract val buildFilePath : Property<String> | ||
|
||
@get:Input | ||
abstract val declaredDependencies : MapProperty<String, List<String>> // Map of 'scope' to 'coordinates' | ||
|
||
@get:Input | ||
abstract val declaredConstraints : MapProperty<String, List<String>> // Map of 'scope' to 'coordinates' | ||
|
||
@TaskAction | ||
fun check() { | ||
declaredDependencies.get().forEach { (scope, dependencies) -> | ||
val declaredInBuildFile = dependencies.filter { | ||
// Ignore dependencies that are defined in our plugins | ||
it !in listOf( | ||
"org.example.product:platform", | ||
"org.slf4j:slf4j-simple", | ||
"org.junit.jupiter:junit-jupiter-engine", | ||
"org.junit.jupiter:junit-jupiter") | ||
} | ||
val sortedProject = declaredInBuildFile.filter { it.startsWith(":") }.sorted() | ||
val sortedExternal = declaredInBuildFile.filter { !it.startsWith(":") }.sorted() | ||
if (declaredInBuildFile != sortedProject + sortedExternal) { | ||
throw RuntimeException(""" | ||
${buildFilePath.get()} | ||
Module versions are not declared in alphabetical order. Please use this order: | ||
${sortedProject.joinToString("\n ") {"${scope}(project(\"${it}\"))"}} | ||
${sortedExternal.joinToString("\n ") {"${scope}(\"${it}\")"}} | ||
""".trimIndent()) | ||
} | ||
} | ||
|
||
declaredConstraints.get().forEach { (scope, constraints) -> | ||
val sortedConstraints = constraints.sorted() | ||
if (constraints != sortedConstraints) { | ||
throw RuntimeException(""" | ||
${buildFilePath.get()} | ||
$scope dependency constraints are not declared in alphabetical order. Please use this order: | ||
${sortedConstraints.joinToString("\n ")} | ||
""".trimIndent()) | ||
} | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...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,63 @@ | ||
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 | ||
|
||
/** | ||
* Checks that all versions declared in the platform are used and are consistent with the consistent resolution result. | ||
*/ | ||
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()) | ||
} | ||
} | ||
} | ||
|