Skip to content

Commit

Permalink
Merge pull request #105 from nvervelle/Replace-convention-by-extension
Browse files Browse the repository at this point in the history
🎨 Use Extension instead of Convention
  • Loading branch information
irodzik authored Dec 21, 2023
2 parents c47b783 + 7ff8c2d commit fcd67af
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 166 deletions.
24 changes: 10 additions & 14 deletions src/main/java/com/bisnode/opa/OpaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,9 +21,7 @@ public class OpaPlugin implements Plugin<Project> {

@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<Task> addedTasks = new ArrayList<>();
Expand All @@ -38,14 +34,14 @@ public void apply(Project project) {
}

private void applyToRootProject(Project project, List<Task> 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");
}
Expand Down Expand Up @@ -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());
}
}));
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/bisnode/opa/StartOpaTask.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/com/bisnode/opa/TestRegoCoverageTask.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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<String> command = Arrays.asList(location, "test", src, test, "--coverage");

Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/bisnode/opa/TestRegoTask.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,14 @@

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";
@Nullable private String version;
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;
Expand All @@ -46,7 +36,7 @@ public void setLocation(String location) {
}

@Override
public void setVersion(@Nullable String version) {
public void setVersion(String version) {
this.version = version;
}

Expand All @@ -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 + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import javax.annotation.Nullable;

@SuppressWarnings("unused")
public interface OpaConfiguration {
public interface OpaExtension {

ExecutableMode getMode();
void setMode(ExecutableMode mode);
Expand Down

This file was deleted.

8 changes: 5 additions & 3 deletions src/test/java/com/bisnode/opa/OpaIOTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/com/bisnode/opa/StartOpaTaskTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit fcd67af

Please sign in to comment.