|
1 | 1 | package com.github.jengelman.gradle.plugins.shadow.tasks |
2 | 2 |
|
3 | 3 | import java.io.Serializable |
| 4 | +import org.gradle.api.Project |
4 | 5 | import org.gradle.api.artifacts.Configuration |
| 6 | +import org.gradle.api.artifacts.Dependency |
| 7 | +import org.gradle.api.artifacts.ProjectDependency |
| 8 | +import org.gradle.api.artifacts.ResolvedArtifact |
5 | 9 | import org.gradle.api.artifacts.ResolvedDependency |
6 | 10 | import org.gradle.api.file.FileCollection |
| 11 | +import org.gradle.api.provider.Provider |
7 | 12 | import org.gradle.api.specs.Spec |
8 | 13 |
|
9 | 14 | // DependencyFilter is used as Gradle Input in ShadowJar, so it must be Serializable. |
@@ -37,4 +42,73 @@ public interface DependencyFilter : Serializable { |
37 | 42 | * Create a [Spec] that matches the provided [dependencyNotation]. |
38 | 43 | */ |
39 | 44 | public fun dependency(dependencyNotation: Any): Spec<ResolvedDependency> |
| 45 | + |
| 46 | + public abstract class AbstractDependencyFilter( |
| 47 | + @Transient private val project: Project, |
| 48 | + @Transient protected val includeSpecs: MutableList<Spec<ResolvedDependency>> = mutableListOf(), |
| 49 | + @Transient protected val excludeSpecs: MutableList<Spec<ResolvedDependency>> = mutableListOf(), |
| 50 | + ) : DependencyFilter { |
| 51 | + |
| 52 | + protected abstract fun resolve( |
| 53 | + dependencies: Set<ResolvedDependency>, |
| 54 | + includedDependencies: MutableSet<ResolvedDependency>, |
| 55 | + excludedDependencies: MutableSet<ResolvedDependency>, |
| 56 | + ) |
| 57 | + |
| 58 | + override fun resolve(configuration: Configuration): FileCollection { |
| 59 | + val included = mutableSetOf<ResolvedDependency>() |
| 60 | + val excluded = mutableSetOf<ResolvedDependency>() |
| 61 | + resolve(configuration.resolvedConfiguration.firstLevelModuleDependencies, included, excluded) |
| 62 | + return project.files(configuration.files) - |
| 63 | + project.files(excluded.flatMap { it.moduleArtifacts.map(ResolvedArtifact::getFile) }) |
| 64 | + } |
| 65 | + |
| 66 | + override fun resolve(configurations: Collection<Configuration>): FileCollection { |
| 67 | + return configurations.map { resolve(it) } |
| 68 | + .reduceOrNull { acc, fileCollection -> acc + fileCollection } |
| 69 | + ?: project.files() |
| 70 | + } |
| 71 | + |
| 72 | + override fun exclude(spec: Spec<ResolvedDependency>) { |
| 73 | + excludeSpecs.add(spec) |
| 74 | + } |
| 75 | + |
| 76 | + override fun include(spec: Spec<ResolvedDependency>) { |
| 77 | + includeSpecs.add(spec) |
| 78 | + } |
| 79 | + |
| 80 | + override fun project(notation: Any): Spec<ResolvedDependency> { |
| 81 | + @Suppress("UNCHECKED_CAST") |
| 82 | + val realNotation = when (notation) { |
| 83 | + is ProjectDependency -> return notation.toSpec() |
| 84 | + is Provider<*> -> mapOf("path" to notation.get()) |
| 85 | + is String -> mapOf("path" to notation) |
| 86 | + is Map<*, *> -> notation as Map<String, Any> |
| 87 | + else -> throw IllegalArgumentException("Unsupported notation type: ${notation::class.java}") |
| 88 | + } |
| 89 | + return project.dependencies.project(realNotation).toSpec() |
| 90 | + } |
| 91 | + |
| 92 | + override fun dependency(dependencyNotation: Any): Spec<ResolvedDependency> { |
| 93 | + val realNotation = when (dependencyNotation) { |
| 94 | + is Provider<*> -> dependencyNotation.get() |
| 95 | + else -> dependencyNotation |
| 96 | + } |
| 97 | + return project.dependencies.create(realNotation).toSpec() |
| 98 | + } |
| 99 | + |
| 100 | + protected fun ResolvedDependency.isIncluded(): Boolean { |
| 101 | + val include = includeSpecs.isEmpty() || includeSpecs.any { it.isSatisfiedBy(this) } |
| 102 | + val exclude = excludeSpecs.isNotEmpty() && excludeSpecs.any { it.isSatisfiedBy(this) } |
| 103 | + return include && !exclude |
| 104 | + } |
| 105 | + |
| 106 | + private fun Dependency.toSpec(): Spec<ResolvedDependency> { |
| 107 | + return Spec<ResolvedDependency> { resolvedDependency -> |
| 108 | + (group == null || resolvedDependency.moduleGroup.matches(group!!.toRegex())) && |
| 109 | + resolvedDependency.moduleName.matches(name.toRegex()) && |
| 110 | + (version == null || resolvedDependency.moduleVersion.matches(version!!.toRegex())) |
| 111 | + } |
| 112 | + } |
| 113 | + } |
40 | 114 | } |
0 commit comments