From 7ff8c2d1d32a1220bb85dc016f9bdc0a45fd6f18 Mon Sep 17 00:00:00 2001 From: Nicolas Vervelle Date: Mon, 13 Nov 2023 19:02:54 +0100 Subject: [PATCH] :art: Use Extension instead of Convention because the latest will be removed in gradle 9 --- src/main/java/com/bisnode/opa/OpaPlugin.java | 24 +++---- .../java/com/bisnode/opa/StartOpaTask.java | 10 +-- .../com/bisnode/opa/TestRegoCoverageTask.java | 16 ++--- .../java/com/bisnode/opa/TestRegoTask.java | 8 +-- .../DefaultOpaConfiguration.java | 62 ------------------- ...nvention.java => DefaultOpaExtension.java} | 30 +-------- ...paConfiguration.java => OpaExtension.java} | 2 +- .../configuration/OpaPluginConvention.java | 33 ---------- src/test/java/com/bisnode/opa/OpaIOTest.java | 8 ++- .../com/bisnode/opa/StartOpaTaskTest.java | 8 ++- .../DefaultOpaConfigurationTest.java | 8 +-- 11 files changed, 43 insertions(+), 166 deletions(-) delete mode 100644 src/main/java/com/bisnode/opa/configuration/DefaultOpaConfiguration.java rename src/main/java/com/bisnode/opa/configuration/{DefaultOpaPluginConvention.java => DefaultOpaExtension.java} (56%) rename src/main/java/com/bisnode/opa/configuration/{OpaConfiguration.java => OpaExtension.java} (92%) delete mode 100644 src/main/java/com/bisnode/opa/configuration/OpaPluginConvention.java diff --git a/src/main/java/com/bisnode/opa/OpaPlugin.java b/src/main/java/com/bisnode/opa/OpaPlugin.java index 59baa7c..8e375c6 100644 --- a/src/main/java/com/bisnode/opa/OpaPlugin.java +++ b/src/main/java/com/bisnode/opa/OpaPlugin.java @@ -6,12 +6,10 @@ import java.util.List; import java.util.Set; -import com.bisnode.opa.configuration.DefaultOpaConfiguration; -import com.bisnode.opa.configuration.DefaultOpaPluginConvention; +import com.bisnode.opa.configuration.DefaultOpaExtension; import com.bisnode.opa.configuration.ExecutableMode; -import com.bisnode.opa.configuration.OpaConfiguration; +import com.bisnode.opa.configuration.OpaExtension; import com.bisnode.opa.configuration.OpaPlatform; -import com.bisnode.opa.configuration.OpaPluginConvention; import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.Task; @@ -23,9 +21,7 @@ public class OpaPlugin implements Plugin { @Override public void apply(Project project) { - OpaPluginConvention convention = new DefaultOpaPluginConvention(project); - project.getConvention().getPlugins().put("opa", convention); - project.getExtensions().create(OpaConfiguration.class, "opa", DefaultOpaConfiguration.class, convention); + project.getExtensions().create(OpaExtension.class, "opa", DefaultOpaExtension.class); TaskContainer tasks = project.getTasks(); List addedTasks = new ArrayList<>(); @@ -38,14 +34,14 @@ public void apply(Project project) { } private void applyToRootProject(Project project, List dependentTasks) { - OpaConfiguration opaConfiguration = project.getExtensions().findByType(OpaConfiguration.class); - if (opaConfiguration == null) { + OpaExtension opaExtension = project.getExtensions().findByType(OpaExtension.class); + if (opaExtension == null) { return; } - if (!ExecutableMode.DOWNLOAD.equals(opaConfiguration.getMode())) { + if (!ExecutableMode.DOWNLOAD.equals(opaExtension.getMode())) { return; } - String version = opaConfiguration.getVersion(); + String version = opaExtension.getVersion(); if (version == null || version.trim().isEmpty()) { throw new IllegalStateException("You must specify OPA version in DOWNLOAD mode"); } @@ -76,9 +72,9 @@ private synchronized void applyDownloadTask(Project project, String version, Lis } downloadTasks.forEach(downloadTask -> dependentTasks.forEach(task -> { task.dependsOn(downloadTask); - final OpaConfiguration opaConfiguration = task.getExtensions().findByType(OpaConfiguration.class); - if (opaConfiguration != null) { - opaConfiguration.setLocation(opaExecutable.getParent()); + final OpaExtension opaExtension = task.getExtensions().findByType(OpaExtension.class); + if (opaExtension != null) { + opaExtension.setLocation(opaExecutable.getParent()); } })); } diff --git a/src/main/java/com/bisnode/opa/StartOpaTask.java b/src/main/java/com/bisnode/opa/StartOpaTask.java index c4d7d98..a45bdb5 100644 --- a/src/main/java/com/bisnode/opa/StartOpaTask.java +++ b/src/main/java/com/bisnode/opa/StartOpaTask.java @@ -1,6 +1,6 @@ package com.bisnode.opa; -import com.bisnode.opa.configuration.OpaPluginConvention; +import com.bisnode.opa.configuration.OpaExtension; import com.bisnode.opa.process.OpaOutputConsumer; import org.gradle.api.DefaultTask; import org.gradle.api.tasks.TaskAction; @@ -11,6 +11,7 @@ import java.io.InputStreamReader; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -29,9 +30,10 @@ public StartOpaTask() { @TaskAction public void startOpa() { - OpaPluginConvention convention = getProject().getConvention().getPlugin(OpaPluginConvention.class); - String location = convention.getLocation(); - String srcDir = convention.getSrcDir(); + OpaExtension extension = Objects.requireNonNull(getProject().getExtensions().findByType( + OpaExtension.class), "opa extension"); + String location = extension.getLocation(); + String srcDir = extension.getSrcDir(); String srcAbsolutePath = OpaPluginUtils.toAbsoluteProjectPath(getProject(), srcDir); getLogger().debug("Starting OPA from {} with srcDir set to {}", "opa".equals(location) ? "$PATH" : location, srcDir); diff --git a/src/main/java/com/bisnode/opa/TestRegoCoverageTask.java b/src/main/java/com/bisnode/opa/TestRegoCoverageTask.java index cb2b1fe..ac84865 100644 --- a/src/main/java/com/bisnode/opa/TestRegoCoverageTask.java +++ b/src/main/java/com/bisnode/opa/TestRegoCoverageTask.java @@ -1,6 +1,6 @@ package com.bisnode.opa; -import com.bisnode.opa.configuration.OpaPluginConvention; +import com.bisnode.opa.configuration.OpaExtension; import org.gradle.api.DefaultTask; import org.gradle.api.tasks.InputDirectory; import org.gradle.api.tasks.TaskAction; @@ -13,6 +13,7 @@ import java.nio.file.Paths; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.Optional; import static java.nio.charset.StandardCharsets.UTF_8; @@ -33,11 +34,12 @@ public TestRegoCoverageTask() { @TaskAction public void testRegoCoverage() { - OpaPluginConvention convention = getProject().getConvention().getPlugin(OpaPluginConvention.class); - String location = convention.getLocation(); + OpaExtension extension = Objects.requireNonNull(getProject().getExtensions().findByType( + OpaExtension.class), "opa extension"); + String location = extension.getLocation(); - String src = Optional.ofNullable(srcDir).orElse(convention.getSrcDir()); - String test = Optional.ofNullable(testDir).orElse(convention.getTestDir()); + String src = Optional.ofNullable(srcDir).orElse(extension.getSrcDir()); + String test = Optional.ofNullable(testDir).orElse(extension.getTestDir()); List command = Arrays.asList(location, "test", src, test, "--coverage"); @@ -62,13 +64,13 @@ public void testRegoCoverage() { @InputDirectory public String getSrcDir() { return Optional.ofNullable(srcDir) - .orElse(getProject().getConvention().getPlugin(OpaPluginConvention.class).getSrcDir()); + .orElse(getProject().getExtensions().getByType(OpaExtension.class).getSrcDir()); } @InputDirectory public String getTestDir() { return Optional.ofNullable(testDir) - .orElse(getProject().getConvention().getPlugin(OpaPluginConvention.class).getTestDir()); + .orElse(getProject().getExtensions().getByType(OpaExtension.class).getTestDir()); } public void setSrcDir(String srcDir) { diff --git a/src/main/java/com/bisnode/opa/TestRegoTask.java b/src/main/java/com/bisnode/opa/TestRegoTask.java index af2ba16..8f347f7 100644 --- a/src/main/java/com/bisnode/opa/TestRegoTask.java +++ b/src/main/java/com/bisnode/opa/TestRegoTask.java @@ -1,6 +1,6 @@ package com.bisnode.opa; -import com.bisnode.opa.configuration.OpaPluginConvention; +import com.bisnode.opa.configuration.OpaExtension; import com.bisnode.opa.process.OpaTestProcess; import com.bisnode.opa.process.ProcessConfiguration; import com.bisnode.opa.process.ProcessExecutionResult; @@ -80,17 +80,17 @@ private String asJUnitXml(ProcessExecutionResult processExecutionResult) { @InputDirectory public String getSrcDir() { return Optional.ofNullable(srcDir) - .orElse(getProject().getConvention().getPlugin(OpaPluginConvention.class).getSrcDir()); + .orElse(getProject().getExtensions().getByType(OpaExtension.class).getSrcDir()); } @InputDirectory public String getTestDir() { return Optional.ofNullable(testDir) - .orElse(getProject().getConvention().getPlugin(OpaPluginConvention.class).getTestDir()); + .orElse(getProject().getExtensions().getByType(OpaExtension.class).getTestDir()); } private String getLocation() { - return getProject().getConvention().getPlugin(OpaPluginConvention.class).getLocation(); + return getProject().getExtensions().getByType(OpaExtension.class).getLocation(); } public void setSrcDir(String srcDir) { diff --git a/src/main/java/com/bisnode/opa/configuration/DefaultOpaConfiguration.java b/src/main/java/com/bisnode/opa/configuration/DefaultOpaConfiguration.java deleted file mode 100644 index b47f9cd..0000000 --- a/src/main/java/com/bisnode/opa/configuration/DefaultOpaConfiguration.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.bisnode.opa.configuration; - -import javax.annotation.Nullable; - -public class DefaultOpaConfiguration implements OpaConfiguration { - - private final OpaPluginConvention opaPluginConvention; - - public DefaultOpaConfiguration(OpaPluginConvention opaPluginConvention) { - this.opaPluginConvention = opaPluginConvention; - } - - @Override - public ExecutableMode getMode() { - return opaPluginConvention.getMode(); - } - - @Override - public void setMode(ExecutableMode mode) { - opaPluginConvention.setMode(mode); - } - - @Override - public String getLocation() { - return opaPluginConvention.getLocation(); - } - - @Override - public void setLocation(String location) { - opaPluginConvention.setLocation(location); - } - - @Override - public @Nullable String getVersion() { - return opaPluginConvention.getVersion(); - } - - @Override - public void setVersion(String version) { - opaPluginConvention.setVersion(version); - } - - @Override - public String getSrcDir() { - return opaPluginConvention.getSrcDir(); - } - - @Override - public void setSrcDir(String srcDir) { - opaPluginConvention.setSrcDir(srcDir); - } - - @Override - public String getTestDir() { - return opaPluginConvention.getTestDir(); - } - - @Override - public void setTestDir(String testDir) { - opaPluginConvention.setTestDir(testDir); - } -} diff --git a/src/main/java/com/bisnode/opa/configuration/DefaultOpaPluginConvention.java b/src/main/java/com/bisnode/opa/configuration/DefaultOpaExtension.java similarity index 56% rename from src/main/java/com/bisnode/opa/configuration/DefaultOpaPluginConvention.java rename to src/main/java/com/bisnode/opa/configuration/DefaultOpaExtension.java index 906718e..4c70903 100644 --- a/src/main/java/com/bisnode/opa/configuration/DefaultOpaPluginConvention.java +++ b/src/main/java/com/bisnode/opa/configuration/DefaultOpaExtension.java @@ -2,13 +2,7 @@ import javax.annotation.Nullable; -import org.gradle.api.Project; -import org.gradle.api.reflect.HasPublicType; -import org.gradle.api.reflect.TypeOf; - -public class DefaultOpaPluginConvention extends OpaPluginConvention implements HasPublicType { - - private final Project project; +public class DefaultOpaExtension implements OpaExtension { private ExecutableMode mode = ExecutableMode.LOCAL; private String location = "opa"; @@ -16,10 +10,6 @@ public class DefaultOpaPluginConvention extends OpaPluginConvention implements H private String srcDir = "src/main/rego"; private String testDir = "src/test/rego"; - public DefaultOpaPluginConvention(Project project) { - this.project = project; - } - @Override public ExecutableMode getMode() { return mode; @@ -46,7 +36,7 @@ public void setLocation(String location) { } @Override - public void setVersion(@Nullable String version) { + public void setVersion(String version) { this.version = version; } @@ -69,20 +59,4 @@ public String getTestDir() { public void setTestDir(String testDir) { this.testDir = testDir; } - - @Override - public TypeOf getPublicType() { - return TypeOf.typeOf(OpaPluginConvention.class); - } - - @Override - public String toString() { - return "DefaultOpaPluginConvention{" + - "project=" + project + - ", mode='" + mode + '\'' + - ", location='" + location + '\'' + - ", srcDir='" + srcDir + '\'' + - ", testDir='" + testDir + '\'' + - '}'; - } } diff --git a/src/main/java/com/bisnode/opa/configuration/OpaConfiguration.java b/src/main/java/com/bisnode/opa/configuration/OpaExtension.java similarity index 92% rename from src/main/java/com/bisnode/opa/configuration/OpaConfiguration.java rename to src/main/java/com/bisnode/opa/configuration/OpaExtension.java index 64e1de4..11d37f2 100644 --- a/src/main/java/com/bisnode/opa/configuration/OpaConfiguration.java +++ b/src/main/java/com/bisnode/opa/configuration/OpaExtension.java @@ -3,7 +3,7 @@ import javax.annotation.Nullable; @SuppressWarnings("unused") -public interface OpaConfiguration { +public interface OpaExtension { ExecutableMode getMode(); void setMode(ExecutableMode mode); diff --git a/src/main/java/com/bisnode/opa/configuration/OpaPluginConvention.java b/src/main/java/com/bisnode/opa/configuration/OpaPluginConvention.java deleted file mode 100644 index 05ca418..0000000 --- a/src/main/java/com/bisnode/opa/configuration/OpaPluginConvention.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.bisnode.opa.configuration; - -import javax.annotation.Nullable; - -import com.bisnode.opa.OpaPlugin; -import org.gradle.api.plugins.Convention; - -/** - *

The {@link Convention} used for configuring the {@link OpaPlugin}.

- */ -public abstract class OpaPluginConvention { - - public abstract ExecutableMode getMode(); - - public abstract void setMode(ExecutableMode mode); - - public abstract String getLocation(); - - public abstract void setLocation(String location); - - @Nullable public abstract String getVersion(); - - public abstract void setVersion(String version); - - public abstract String getSrcDir(); - - public abstract void setSrcDir(String srcDir); - - public abstract String getTestDir(); - - public abstract void setTestDir(String testDir); - -} diff --git a/src/test/java/com/bisnode/opa/OpaIOTest.java b/src/test/java/com/bisnode/opa/OpaIOTest.java index 4d1a133..1760bbd 100644 --- a/src/test/java/com/bisnode/opa/OpaIOTest.java +++ b/src/test/java/com/bisnode/opa/OpaIOTest.java @@ -1,6 +1,6 @@ package com.bisnode.opa; -import com.bisnode.opa.configuration.OpaPluginConvention; +import com.bisnode.opa.configuration.OpaExtension; import org.gradle.api.Project; import org.gradle.internal.impldep.org.apache.http.client.methods.CloseableHttpResponse; import org.gradle.internal.impldep.org.apache.http.client.methods.HttpGet; @@ -16,6 +16,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.util.Objects; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -39,8 +40,9 @@ public void after() { @Test void shouldNotHangOnOPAOutputBufferOverflow() throws IOException { //given - OpaPluginConvention convention = project.getConvention().getPlugin(OpaPluginConvention.class); - convention.setSrcDir(getPathToTmpFolder()); + OpaExtension extension = Objects.requireNonNull(project.getExtensions().findByType( + OpaExtension.class), "opa extension"); + extension.setSrcDir(getPathToTmpFolder()); StartOpaTask startOpaTask = (StartOpaTask) project.getTasks().getByName("startOpa"); //when diff --git a/src/test/java/com/bisnode/opa/StartOpaTaskTest.java b/src/test/java/com/bisnode/opa/StartOpaTaskTest.java index 6b83057..bb8b0c9 100644 --- a/src/test/java/com/bisnode/opa/StartOpaTaskTest.java +++ b/src/test/java/com/bisnode/opa/StartOpaTaskTest.java @@ -1,6 +1,6 @@ package com.bisnode.opa; -import com.bisnode.opa.configuration.OpaPluginConvention; +import com.bisnode.opa.configuration.OpaExtension; import org.gradle.api.Project; import org.gradle.internal.impldep.org.junit.rules.TemporaryFolder; import org.gradle.testfixtures.ProjectBuilder; @@ -10,6 +10,7 @@ import javax.annotation.Nullable; import java.io.IOException; +import java.util.Objects; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -37,8 +38,9 @@ public void taskIsInOpaGroup() { @Test public void opaPluginStartTaskSavesProcessInExtProperties() throws IOException { - OpaPluginConvention convention = project.getConvention().getPlugin(OpaPluginConvention.class); - convention.setSrcDir(getPathToTmpFolder()); + OpaExtension extension = Objects.requireNonNull(project.getExtensions().findByType( + OpaExtension.class), "opa extension"); + extension.setSrcDir(getPathToTmpFolder()); StartOpaTask startOpaTask = (StartOpaTask) project.getTasks().getByName("startOpa"); startOpaTask.startOpa(); diff --git a/src/test/java/com/bisnode/opa/configuration/DefaultOpaConfigurationTest.java b/src/test/java/com/bisnode/opa/configuration/DefaultOpaConfigurationTest.java index f024d01..cd3b32e 100644 --- a/src/test/java/com/bisnode/opa/configuration/DefaultOpaConfigurationTest.java +++ b/src/test/java/com/bisnode/opa/configuration/DefaultOpaConfigurationTest.java @@ -1,13 +1,11 @@ package com.bisnode.opa.configuration; import org.gradle.api.Project; -import org.gradle.api.reflect.TypeOf; import org.gradle.testfixtures.ProjectBuilder; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; public class DefaultOpaConfigurationTest { @@ -21,8 +19,7 @@ public void before() { @Test public void allConfigurationSettingsReturnExpectedValues() { - DefaultOpaPluginConvention pluginConvention = new DefaultOpaPluginConvention(project); - DefaultOpaConfiguration configuration = new DefaultOpaConfiguration(pluginConvention); + DefaultOpaExtension configuration = new DefaultOpaExtension(); configuration.setLocation("/tmp/location"); configuration.setSrcDir("/tmp/src"); @@ -31,9 +28,6 @@ public void allConfigurationSettingsReturnExpectedValues() { assertEquals("/tmp/location", configuration.getLocation()); assertEquals("/tmp/src", configuration.getSrcDir()); assertEquals("/tmp/test", configuration.getTestDir()); - - assertEquals(TypeOf.typeOf(OpaPluginConvention.class), pluginConvention.getPublicType()); - assertTrue(pluginConvention.toString().startsWith("DefaultOpaPluginConvention{")); } }