Skip to content

Commit 90d50eb

Browse files
library plugin WIP
Co-authored-by: Emily Yuan <eyuan@netflix.com>
1 parent 53cd7dc commit 90d50eb

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

nebula-archrules-gradle-plugin/src/main/kotlin/com/netflix/nebula/archrules/gradle/ArchrulesLibraryPlugin.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ package com.netflix.nebula.archrules.gradle
22

33
import org.gradle.api.Plugin
44
import org.gradle.api.Project
5+
import org.gradle.api.plugins.JavaPluginExtension
6+
import org.gradle.kotlin.dsl.getByType
57

68
class ArchrulesLibraryPlugin : Plugin<Project> {
7-
override fun apply(target: Project) {
9+
override fun apply(project: Project) {
10+
project.plugins.withId("java") {
11+
val ext = project.extensions.getByType<JavaPluginExtension>()
12+
ext.sourceSets.create("archRules")
13+
}
814
}
915
}

nebula-archrules-gradle-plugin/src/test/kotlin/com/netflix/nebula/archrules/gradle/IntegrationTest.kt

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,39 @@ public class LibraryClass {
3434
"""
3535
)
3636
}
37+
sourceSet("archRules") {
38+
java(
39+
"com/example/library/LibraryArchRules.java",
40+
//language=java
41+
"""
42+
package com.example.library;
43+
44+
import com.netflix.nebula.archrules.core.ArchRulesService;
45+
import com.tngtech.archunit.lang.Priority;
46+
import com.tngtech.archunit.lang.syntax.ArchRuleDefinition;
47+
48+
import static com.tngtech.archunit.core.domain.JavaAccess.Predicates.target;
49+
import static com.tngtech.archunit.core.domain.JavaAccess.Predicates.targetOwner;
50+
import static com.tngtech.archunit.core.domain.properties.CanBeAnnotated.Predicates.annotatedWith;
51+
52+
public class LibraryArchRules implements ArchRulesService {
53+
private final ArchRule noDeprecated = ArchRuleDefinition.priority(Priority.LOW)
54+
.noClasses()
55+
.should().accessTargetWhere(targetOwner(annotatedWith(Deprecated.class)))
56+
.orShould().accessTargetWhere(target(annotatedWith(Deprecated.class)))
57+
.orShould().dependOnClassesThat().areAnnotatedWith(Deprecated.class)
58+
.allowEmptyShould(true)
59+
.as("No code should reference deprecated APIs")
60+
.because("usage of deprecated APIs introduces risk that future upgrades and migrations will be blocked");
61+
62+
@Override
63+
public Map<String, ArchRule> getRules() {
64+
return Map.of("deprecated", noDeprecated);
65+
}
66+
}
67+
"""
68+
)
69+
}
3770
}
3871
}
3972
subProject("code-to-check") {
@@ -48,8 +81,11 @@ public class LibraryClass {
4881
}
4982
}
5083

51-
val result = runner.run("check")
84+
val result = runner.run("check", "compileArchRulesJava")
5285

86+
assertThat(result.task(":library-with-rules:compileArchRulesJava"))
87+
.`as`("compile task runs for the archRules source set")
88+
.hasOutcome(TaskOutcome.SUCCESS, TaskOutcome.UP_TO_DATE)
5389
assertThat(result.task(":library-with-rules:check"))
5490
.hasOutcome(TaskOutcome.SUCCESS, TaskOutcome.UP_TO_DATE)
5591
assertThat(result.task(":code-to-check:check"))

0 commit comments

Comments
 (0)