Skip to content

Commit

Permalink
feat(ext): Lazy dependencies configuration (#119)
Browse files Browse the repository at this point in the history
BREAKING CHANGES: The `strictNullCheck.sources` property of the extension was removed to encourage a lazy configuration. The methods that used to be inside `strictNullCheck.sources` ar now at `strictNullCheck` level. Also, it's not possible to add custom dependencies from the plugin extension anymore. If you want to add custom annotations, use Gradle's `dependencies` DSL as usual and configure `strictNullCheck.packageInfo` accordingly.
  • Loading branch information
JoseLion authored Oct 20, 2023
1 parent 69595ff commit 45773c5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,5 @@ public void apply(final Project project) {
.srcDir(extension.getGeneratedDir().get().concat("/java/main"))
);
});

project.afterEvaluate(evaluated -> {
final var allCompileOnly = evaluated
.getConfigurations()
.matching(configuration -> {
final var name = configuration.getName();
return name.startsWith("compileOnly") || name.endsWith("CompileOnly");
});

allCompileOnly.configureEach(configuration ->
extension
.getSource()
.getDependencies()
.get()
.stream()
.map(evaluated.getDependencies()::create)
.forEach(configuration.getDependencies()::add)
);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import javax.inject.Inject;

import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;

import lombok.Getter;
Expand All @@ -23,14 +25,14 @@ public class StrictNullCheckExtension {
@Nested
private final PackageInfo packageInfo;

@Nested
private final Source source;
@Internal
private final Project project;

@Inject
public StrictNullCheckExtension(final ObjectFactory objects, final ProjectLayout layout) {
public StrictNullCheckExtension(final ObjectFactory objects, final ProjectLayout layout, final Project project) {
this.generatedDir = objects.property(String.class);
this.packageInfo = objects.newInstance(PackageInfo.class);
this.source = objects.newInstance(Source.class);
this.project = project;

this.generatedDir.convention(
layout
Expand All @@ -46,8 +48,44 @@ public void packageInfo(final Action<PackageInfo> action) {
action.execute(this.packageInfo);
}

public void source(final Action<Source> action) {
action.execute(this.source);
public void addFindBugs(final String version) {
this.addDependency("com.google.code.findbugs:jsr305:".concat(version));
}

public void addFindBugs() {
this.addFindBugs("3.0.2");
}

public void addSpotBugs(final String version) {
this.addDependency("com.github.spotbugs:spotbugs-annotations:".concat(version));
}

public void addSpotBugs() {
this.addSpotBugs("4.7.3");
}

public void addEclipse(final String version) {
this.addDependency("org.eclipse.jdt:org.eclipse.jdt.annotation:".concat(version));
}

public void addEclipse() {
this.addEclipse("2.2.700");
}

private void addDependency(final String notation) {
final var dependency = this.project.getDependencies().create(notation);
final var allCompileOnly = this.project
.getConfigurations()
.matching(config -> {
final var name = config.getName();
return name.startsWith("compileOnly") || name.endsWith("CompileOnly");
});

allCompileOnly.configureEach(config ->
config
.getDependencies()
.add(dependency)
);
}

@Getter
Expand Down Expand Up @@ -112,42 +150,4 @@ public void useEclipse() {
);
}
}

@Getter
public static class Source {

@Input
private final ListProperty<String> dependencies;

@Inject
public Source(final ObjectFactory objects) {
this.dependencies = objects.listProperty(String.class);

this.dependencies.convention(List.of());
}

public void addFindBugs(final String version) {
this.dependencies.add("com.google.code.findbugs:jsr305:".concat(version));
}

public void addFindBugs() {
this.addFindBugs("3.0.2");
}

public void addSpotBugs(final String version) {
this.dependencies.add("com.github.spotbugs:spotbugs-annotations:".concat(version));
}

public void addSpotBugs() {
this.addSpotBugs("4.7.3");
}

public void addEclipse(final String version) {
this.dependencies.add("org.eclipse.jdt:org.eclipse.jdt.annotation:".concat(version));
}

public void addEclipse() {
this.addEclipse("2.2.700");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
final var imports = extension.getPackageInfo().getImports().get();
final var annotations = extension.getPackageInfo().getAnnotations().get();
final var javadoc = extension.getPackageInfo().getJavadoc().get();
final var dependencies = extension.getSource().getDependencies().get();

assertThat(imports).containsExactly("javax.annotation.ParametersAreNonnullByDefault");
assertThat(annotations).containsExactly("@ParametersAreNonnullByDefault");
assertThat(generatedDir).isEqualTo(buildDir.concat("/generated/sources/strictNullCheck"));
assertThat(javadoc).isEmpty();
assertThat(dependencies).isEmpty();
}
}

Expand Down Expand Up @@ -82,45 +80,4 @@
}
}
}

@Nested class source {
@Nested class addFindBugs {
@Test void adds_the_FindBugs_dependency() {
final var project = ProjectBuilder.builder().build();
final var extension = project.getExtensions().create("strictNullCheck", StrictNullCheckExtension.class);

extension.getSource().addFindBugs();

final var dependencies = extension.getSource().getDependencies().get();

assertThat(dependencies).contains("com.google.code.findbugs:jsr305:3.0.2");
}
}

@Nested class addSpotBugs {
@Test void adds_the_SpotBugs_dependency() {
final var project = ProjectBuilder.builder().build();
final var extension = project.getExtensions().create("strictNullCheck", StrictNullCheckExtension.class);

extension.getSource().addSpotBugs();

final var dependencies = extension.getSource().getDependencies().get();

assertThat(dependencies).contains("com.github.spotbugs:spotbugs-annotations:4.7.3");
}
}

@Nested class addEclipse {
@Test void adds_the_SpotBugs_dependency() {
final var project = ProjectBuilder.builder().build();
final var extension = project.getExtensions().create("strictNullCheck", StrictNullCheckExtension.class);

extension.getSource().addEclipse();

final var dependencies = extension.getSource().getDependencies().get();

assertThat(dependencies).contains("org.eclipse.jdt:org.eclipse.jdt.annotation:2.2.700");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
}
strictNullCheck {
source {
addFindBugs()
}
addFindBugs()
}
repositories {
Expand Down Expand Up @@ -70,8 +68,7 @@
'my.custom.annotation.NullApi',
'my.custom.annotation.NullFields',
]
source.dependencies = ['my.custom:annotations:1.5.3']
source.addFindBugs()
addFindBugs()
}
repositories {
Expand All @@ -80,8 +77,9 @@
task showConfig() {
doLast {
def deps = configurations.compileOnly.dependencies.collect { "$it.group:$it.name:$it.version" }
println("*** packageInfo.annotations: ${strictNullCheck.packageInfo.annotations.get()}")
println("*** source.dependencies: ${strictNullCheck.source.dependencies.get()}")
println("*** dependencies: $deps")
}
}
"""
Expand All @@ -91,7 +89,7 @@ task showConfig() {

assertThat(result.getOutput())
.contains("*** packageInfo.annotations: [my.custom.annotation.NullApi, my.custom.annotation.NullFields]")
.contains("*** source.dependencies: [my.custom:annotations:1.5.3, com.google.code.findbugs:jsr305:3.0.2]")
.contains("*** dependencies: [com.google.code.findbugs:jsr305:3.0.2]")
.contains("BUILD SUCCESSFUL");
}
}
Expand All @@ -112,10 +110,7 @@ task showConfig() {
'my.custom.annotation.NullFields',
]
}
source {
dependencies = ['my.custom:annotations:1.5.3']
addFindBugs()
}
addFindBugs()
}
repositories {
Expand All @@ -124,8 +119,9 @@ task showConfig() {
task showConfig() {
doLast {
def deps = configurations.compileOnly.dependencies.collect { "$it.group:$it.name:$it.version" }
println("*** packageInfo.annotations: ${strictNullCheck.packageInfo.annotations.get()}")
println("*** source.dependencies: ${strictNullCheck.source.dependencies.get()}")
println("*** dependencies: $deps")
}
}
"""
Expand All @@ -135,7 +131,7 @@ task showConfig() {

assertThat(result.getOutput())
.contains("*** packageInfo.annotations: [my.custom.annotation.NullApi, my.custom.annotation.NullFields]")
.contains("*** source.dependencies: [my.custom:annotations:1.5.3, com.google.code.findbugs:jsr305:3.0.2]")
.contains("*** dependencies: [com.google.code.findbugs:jsr305:3.0.2]")
.contains("BUILD SUCCESSFUL");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
}
strictNullCheck {
source {
addFindBugs()
}
addFindBugs()
}
repositories {
Expand Down

0 comments on commit 45773c5

Please sign in to comment.