Skip to content

Commit

Permalink
Add maxTransitiveDepth config (#24)
Browse files Browse the repository at this point in the history
* Add feature to ignoreTransitive dependencies

* rename to maxTransitiveDepth

* Use consistent naming

* Set reasonable default for maxTransitiveDependency and add to README
  • Loading branch information
Breefield authored Oct 16, 2024
1 parent 379814d commit 7b8b53c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ libyear {

// alternatively:
validator = singleArtifactMustNotBeOlderThan(2.days)

// optional: limit the depth of the dependency traversal, default is 0 = only the root level, null = no limit
maxTransitiveDepth = 5
}
```

Expand All @@ -45,6 +48,7 @@ libyear {
configurations = ['compileClasspath']
failOnError = true
validator = allArtifactsCombinedMustNotBeOlderThan(days(5))
maxTransitiveDepth = 5
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ open class LibYearExtension {

var validator: DependencyValidatorSpec = defaultValidator

var maxTransitiveDepth: Int? = 0

var configurations: List<String> = defaultConfigurations

// DSL for build script authors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class LibYearPlugin : Plugin<Project> {
val ageOracle = createOracle(project, extension)
val validator = createValidator(project, extension)
val visitor = ValidatingVisitor(project.logger, ageOracle, validator, ValidationConfig(failOnError = extension.failOnError))
DependencyTraversal.visit(resolvableDependencies.resolutionResult.root, visitor)
DependencyTraversal.visit(resolvableDependencies.resolutionResult.root, visitor, extension.maxTransitiveDepth)
maybeReportFailure(project.logger, validator)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ open class LibYearReportTask : DefaultTask() {
.forEach {
val ageOracle = createOracle(project, extension)
val visitor = ReportingVisitor(project.logger, ageOracle)
DependencyTraversal.visit(it.incoming.resolutionResult.root, visitor)
DependencyTraversal.visit(it.incoming.resolutionResult.root, visitor, extension.maxTransitiveDepth)
visitor.print()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import org.gradle.api.artifacts.result.ResolvedComponentResult
import org.gradle.api.artifacts.result.ResolvedDependencyResult

class DependencyTraversal private constructor(
private val visitor: DependencyVisitor
private val visitor: DependencyVisitor,
private val maxTransitiveDepth: Int?
) {

private val seen = mutableSetOf<ComponentIdentifier>()

private fun visit(component: ComponentResult) {
private fun visit(component: ComponentResult, depth: Int = 0) {
if (!seen.add(component.id)) return

visitor.visitComponentResult(component)
Expand All @@ -23,12 +24,15 @@ class DependencyTraversal private constructor(
if (!visitor.canContinue()) return

if (dependency is ResolvedDependencyResult) {
if (maxTransitiveDepth != null && depth > maxTransitiveDepth) {
continue
}
nextComponents.add(dependency.selected)
}
}

for (nextComponent in nextComponents) {
visit(nextComponent)
visit(nextComponent, depth + 1)
if (!visitor.canContinue()) break
}
}
Expand All @@ -37,7 +41,8 @@ class DependencyTraversal private constructor(

fun visit(
root: ResolvedComponentResult,
visitor: DependencyVisitor
): Unit = DependencyTraversal(visitor).visit(root)
visitor: DependencyVisitor,
maxTransitiveDepth: Int? = null
): Unit = DependencyTraversal(visitor, maxTransitiveDepth).visit(root, depth = 0)
}
}

0 comments on commit 7b8b53c

Please sign in to comment.