Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add excludedPackages config #25

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libyear-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ tasks.withType(Test::class) {
testLogging {
events("started")
showExceptions = true
showStandardStreams = true
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,23 @@ internal class LibYearPluginTest {
.extracting { it?.outcome }.isEqualTo(TaskOutcome.FAILED)
}

private fun withGradleRunner() = GradleRunner.create().apply {
@Test
fun testExcludedPackages() {
setUpProject("excludedPackages.gradle.kts")

val result = withGradleRunner("reportLibyears").build()
val output = result.output

assertThat(output)
.contains("from 3 dependencies")
.contains("org.apache.commons:commons-text")
.contains("org.apache.commons:commons-collections")
assertThat(output).doesNotContain("unknown.package")
}

private fun withGradleRunner(command: String = "dependencies") = GradleRunner.create().apply {
withProjectDir(project.toFile())
withArguments("dependencies")
withArguments(command)
withPluginClasspath()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
id("com.libyear.libyear-gradle-plugin")
java
}

repositories {
mavenCentral()
}

dependencies {
implementation("org.apache.commons:commons-text:1.9")
implementation("org.apache.commons:commons-collections4:4.4")
implementation("org.slf4j:slf4j-api:2.0.9")
}

libyear {
failOnError = false
validator = singleArtifactMustNotBeOlderThan(100.years)
excludedPackages = setOf("org.slf4j")
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ open class LibYearExtension {

var configurations: List<String> = defaultConfigurations

var excludedPackages: Set<String> = emptySet()

// DSL for build script authors

val defaultConfigurations: List<String> get() = listOf("compileClasspath")
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, extension.maxTransitiveDepth)
DependencyTraversal.visit(resolvableDependencies.resolutionResult.root, visitor, extension.maxTransitiveDepth, extension.excludedPackages)
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, extension.maxTransitiveDepth)
DependencyTraversal.visit(it.incoming.resolutionResult.root, visitor, extension.maxTransitiveDepth, extension.excludedPackages)
visitor.print()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import org.gradle.api.artifacts.result.ResolvedDependencyResult

class DependencyTraversal private constructor(
private val visitor: DependencyVisitor,
private val maxTransitiveDepth: Int?
private val maxTransitiveDepth: Int?,
private val excludedPackages: Set<String>
) {

private val seen = mutableSetOf<ComponentIdentifier>()
Expand All @@ -27,7 +28,13 @@ class DependencyTraversal private constructor(
if (maxTransitiveDepth != null && depth > maxTransitiveDepth) {
continue
}

if (excludedPackages.any { selected.moduleVersion.toString().contains(it, ignoreCase = true) }) {
continue
}

nextComponents.add(dependency.selected)

}
}

Expand All @@ -42,7 +49,8 @@ class DependencyTraversal private constructor(
fun visit(
root: ResolvedComponentResult,
visitor: DependencyVisitor,
maxTransitiveDepth: Int? = null
): Unit = DependencyTraversal(visitor, maxTransitiveDepth).visit(root, depth = 0)
maxTransitiveDepth: Int? = null,
excludedPackages: Set<String> = emptySet()
): Unit = DependencyTraversal(visitor, maxTransitiveDepth, excludedPackages).visit(root, depth = 0)
}
}