Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ package com.netflix.nebula.archrules.gradle
import com.tngtech.archunit.lang.Priority
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.result.ResolvedArtifactResult
import org.gradle.api.attributes.Category
import org.gradle.api.attributes.VerificationType
import org.gradle.api.model.ObjectFactory
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.project
import org.gradle.kotlin.dsl.register
import javax.inject.Inject

class ArchrulesAggregateConsoleReportPlugin @Inject constructor(val objects: ObjectFactory) : Plugin<Project> {

class ArchrulesAggregateConsoleReportPlugin : Plugin<Project> {
override fun apply(project: Project) {
val ext = project.extensions.create("archRulesAggregate", ArchrulesAggregateExtension::class.java)
val verification: Category = objects.named(Category.VERIFICATION)
val archRulesVerificationType: VerificationType = objects.named("arch-rules")
val ext = project.extensions.create("archRulesAggregate", ArchrulesAggregateExtension::class.java)
ext.skipPassingSummaries.set(false)
ext.consoleDetailsThreshold(Priority.MEDIUM)
val archRulesAggregateDependencies = project.configurations.dependencyScope("archRulesAggregateDependencies") {
Expand All @@ -20,21 +26,36 @@ class ArchrulesAggregateConsoleReportPlugin : Plugin<Project> {
val archRulesDataFiles = project.configurations.resolvable("archRulesDataFiles") {
extendsFrom(archRulesAggregateDependencies.get())
attributes {
attribute(Category.CATEGORY_ATTRIBUTE, project.objects.named(Category.VERIFICATION))
attribute(VerificationType.VERIFICATION_TYPE_ATTRIBUTE, project.objects.named("arch-rules"))
attribute(Category.CATEGORY_ATTRIBUTE, verification)
attribute(VerificationType.VERIFICATION_TYPE_ATTRIBUTE, archRulesVerificationType)
}
}

project.subprojects {
project.dependencies.add("archRulesAggregateDependencies", project.dependencies.project(":$name"))
}

project.tasks.register<PrintConsoleReportTask>("archRulesAggregateConsoleReport") {
dataFiles.from(archRulesDataFiles.map {
dataFiles.from(archRulesDataFiles.flatMap {
it.incoming.artifactView {
lenient(true) // to handle the case where a subproject doesn't have archrules runner
}.files
lenient(true) // to handle the case where a subproject doesn't have archrules runner
}.artifacts.resolvedArtifacts.map {
// filter for only artifacts that match the attributes
it.filter {
it.hasCategory(verification) && it.hasVerificationType(archRulesVerificationType)
}.map {
it.file
}
}
})
summaryForPassingDisabled.set(ext.skipPassingSummaries)
detailsThreshold.set(ext.consoleDetailsThreshold)
}
}

fun ResolvedArtifactResult.hasCategory(category: Category): Boolean =
variant.attributes.getAttribute(Category.CATEGORY_ATTRIBUTE)?.name == category.name

fun ResolvedArtifactResult.hasVerificationType(type: VerificationType): Boolean =
variant.attributes.getAttribute(VerificationType.VERIFICATION_TYPE_ATTRIBUTE)?.name == type.name
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import nebula.test.dsl.subProject
import nebula.test.dsl.testProject
import org.gradle.kotlin.dsl.findByType
import org.gradle.testfixtures.ProjectBuilder
import org.gradle.testkit.runner.TaskOutcome
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.io.File
Expand All @@ -22,9 +23,11 @@ class ArchrulesAggregateConsoleReportPluginTest {
@TempDir
lateinit var projectDir: File

private fun TestProjectBuilder.setup(withFailures : Boolean = true) {
private fun TestProjectBuilder.setup(withFailures: Boolean = true) {
properties {
buildCache(true)
configurationCache(true)
property("org.gradle.unsafe.isolated-projects","true")
}
rootProject {
plugins {
Expand Down Expand Up @@ -55,7 +58,7 @@ class ArchrulesAggregateConsoleReportPluginTest {
)
src {
main {
if(withFailures) {
if (withFailures) {
exampleDeprecatedUsage("FailingCode1")
}
}
Expand All @@ -66,7 +69,7 @@ class ArchrulesAggregateConsoleReportPluginTest {
id("java")
id("com.netflix.nebula.archrules.runner")
}
repositories{
repositories {
mavenCentral()
}
dependencies(
Expand All @@ -75,7 +78,7 @@ class ArchrulesAggregateConsoleReportPluginTest {
)
src {
main {
if(withFailures) {
if (withFailures) {
exampleDeprecatedUsage("FailingCode2")
}
}
Expand All @@ -96,11 +99,13 @@ class ArchrulesAggregateConsoleReportPluginTest {
@Test
fun test() {
val runner = testProject(projectDir) {
setup()
setup()
}
val result = runner.run("archRulesAggregateConsoleReport") {
forwardOutput()
}
assertThat(result.task(":sub1:checkArchRulesMain"))
.hasOutcome(TaskOutcome.SUCCESS, TaskOutcome.FROM_CACHE)
assertThat(result.output)
.contains("deprecatedForRemoval MEDIUM (2 failures)")
.contains("deprecated LOW (4 failures)")
Expand All @@ -110,8 +115,9 @@ class ArchrulesAggregateConsoleReportPluginTest {
fun `test passing skip`() {
val runner = testProject(projectDir) {
setup(withFailures = false)
rootProject{
rawBuildScript("""
rootProject {
rawBuildScript(
"""
archRulesAggregate {
skipPassingSummaries = true
}
Expand All @@ -132,8 +138,9 @@ archRulesAggregate {
fun `test details threshold`() {
val runner = testProject(projectDir) {
setup()
rootProject{
rawBuildScript("""
rootProject {
rawBuildScript(
"""
archRulesAggregate {
consoleDetailsThreshold("LOW")
}
Expand All @@ -147,4 +154,29 @@ archRulesAggregate {
assertThat(result.output)
.contains("Method <com.example.consumer.FailingCode1.aMethod()> calls method <com.example.library.LibraryClass.deprecatedApi()>")
}

@Test
fun `test empty subproject`() {
val runner = testProject(projectDir) {
setup()
subProject("empty") {
plugins {
id("base")
}
rawBuildScript(
// language=kotlin
"""
val jarTask = tasks.register<Jar>("someJar")
artifacts {
add("default", jarTask)
}
"""
)
}
}
val result = runner.run("archRulesAggregateConsoleReport")
assertThat(result.task(":archRulesAggregateConsoleReport"))
.hasOutcome(TaskOutcome.SUCCESS)
assertThat(result.output).doesNotContain("Archrules data read failed")
}
}