-
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
8 changed files
with
134 additions
and
95 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
53 changes: 22 additions & 31 deletions
53
...dency-analysis-plugins/src/main/kotlin/org.example.dependency-analysis-project.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 |
---|---|---|
@@ -1,49 +1,40 @@ | ||
import com.autonomousapps.DependencyAnalysisSubExtension | ||
import org.example.dependencyanalysis.DependencyFormatCheck | ||
import org.example.dependencyanalysis.DependencyScopeCheck | ||
import org.example.dependencyanalysis.configurationPrefixesToSkip | ||
|
||
plugins { | ||
id("base") | ||
id("org.example.dependency-analysis") | ||
id("java") | ||
} | ||
|
||
// Check that dependencies are always declared without version. | ||
configurations.all { | ||
if (configurationPrefixesToSkip.any { name.startsWith(it) }) { | ||
return@all | ||
} | ||
val checkDependencyFormatting = tasks.register<DependencyFormatCheck>("checkDependencyFormatting") { | ||
group = LifecycleBasePlugin.VERIFICATION_GROUP | ||
|
||
val configuration = this | ||
withDependencies { | ||
forEach { dependency -> | ||
if (dependency is ExternalModuleDependency && !dependency.version.isNullOrEmpty()) { | ||
throw RuntimeException(""" | ||
${project.name}/build.gradle.kts | ||
Dependencies with versions are not allowed. Please declare the dependency as follows: | ||
${configuration.name}("${dependency.group}:${dependency.name}") | ||
All versions must be declared in 'gradle/platform'. | ||
If the version is not yet defined there, add the following to 'gradle/build.gradle.kts': | ||
api("${dependency.group}:${dependency.name}:${dependency.version}") | ||
""".trimIndent()) | ||
} | ||
} | ||
buildFilePath.set(project.buildFile.absolutePath) | ||
shouldNotHaveVersions.set(true) | ||
sourceSets.all { | ||
declaredDependencies.put(implementationConfigurationName, provider { configurations.getByName(implementationConfigurationName).dependencies.map { d -> d.toDeclaredString() } }) | ||
declaredDependencies.put(runtimeOnlyConfigurationName, provider { configurations.getByName(runtimeOnlyConfigurationName).dependencies.map { d -> d.toDeclaredString() } }) | ||
declaredDependencies.put(compileOnlyConfigurationName, provider { configurations.getByName(compileOnlyConfigurationName).dependencies.map { d -> d.toDeclaredString() } }) | ||
declaredDependencies.put(apiConfigurationName, provider { configurations.findByName(apiConfigurationName)?.dependencies?.map { d -> d.toDeclaredString() } ?: emptyList() }) | ||
declaredDependencies.put(compileOnlyApiConfigurationName, provider { configurations.findByName(compileOnlyApiConfigurationName)?.dependencies?.map { d -> d.toDeclaredString() } ?: emptyList() }) | ||
} | ||
} | ||
|
||
// Configure a 'checkDependencyScopes' tasks that uses the 'com.autonomousapps.dependency-analysis' plugin. | ||
// To find unused dependencies and check 'api' vs. 'implementation' scopes. | ||
val dependencyScopesCheck = tasks.register<DependencyScopeCheck>("checkDependencyScopes") { | ||
val checkDependencyScopes = tasks.register<DependencyScopeCheck>("checkDependencyScopes") { | ||
group = LifecycleBasePlugin.VERIFICATION_GROUP | ||
shouldRunAfter(checkDependencyFormatting) | ||
} | ||
|
||
tasks.check { | ||
dependsOn(dependencyScopesCheck) | ||
dependsOn(checkDependencyFormatting) | ||
dependsOn(checkDependencyScopes) | ||
} | ||
|
||
plugins.withId("com.autonomousapps.dependency-analysis") { | ||
extensions.getByType<DependencyAnalysisSubExtension>().registerPostProcessingTask(dependencyScopesCheck) | ||
extensions.getByType<DependencyAnalysisSubExtension>().registerPostProcessingTask(checkDependencyScopes) | ||
} | ||
|
||
fun Dependency.toDeclaredString() = when(this) { | ||
is ProjectDependency -> ":$name" | ||
else -> "$group:$name${if (version == null) "" else ":$version"}" | ||
} |
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
48 changes: 0 additions & 48 deletions
48
...ns/dependency-analysis-plugins/src/main/kotlin/org.example.dependency-analysis.gradle.kts
This file was deleted.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
...-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,81 @@ | ||
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' | ||
|
||
@get:Input | ||
abstract val shouldNotHaveVersions : Property<Boolean> | ||
|
||
@TaskAction | ||
fun check() { | ||
declaredDependencies.get().forEach { (scope, dependencies) -> | ||
if (shouldNotHaveVersions.get()) { | ||
dependencies.forEach { coordinates -> | ||
if (coordinates.count { it == ':' } == 2 && !coordinates.startsWith("org.jetbrains.kotlin:kotlin-stdlib:")) { | ||
throw RuntimeException(""" | ||
${buildFilePath.get()} | ||
Dependencies with versions are not allowed. Please declare the dependency as follows: | ||
${scope}("${coordinates.substring(0, coordinates.lastIndexOf(':'))}") | ||
All versions must be declared in 'gradle/platform'. | ||
If the version is not yet defined there, add the following to 'gradle/platform/build.gradle.kts': | ||
api("$coordinates") | ||
""".trimIndent()) | ||
} | ||
} | ||
} | ||
|
||
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()} | ||
$scope dependencies 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 ") {"${scope}(\"${it}\")"}} | ||
""".trimIndent()) | ||
} | ||
} | ||
} | ||
} |
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
15 changes: 0 additions & 15 deletions
15
...ncy-analysis-plugins/src/main/kotlin/org/example/dependencyanalysis/dependencyAnalysis.kt
This file was deleted.
Oops, something went wrong.