Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set extension capability from gradle plugin #24034

Merged
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 @@ -2,8 +2,12 @@

import java.util.List;

import org.gradle.api.Action;
import org.gradle.api.Project;

import io.quarkus.extension.gradle.dsl.Capabilities;
import io.quarkus.extension.gradle.dsl.Capability;

public class QuarkusExtensionConfiguration {

private boolean disableValidation;
Expand All @@ -15,6 +19,7 @@ public class QuarkusExtensionConfiguration {
private List<String> lesserPriorityArtifacts;
private List<String> conditionalDependencies;
private List<String> dependencyCondition;
private Capabilities capabilities = new Capabilities();

private Project project;

Expand Down Expand Up @@ -94,6 +99,14 @@ public void setDependencyConditions(List<String> dependencyCondition) {
this.dependencyCondition = dependencyCondition;
}

public List<Capability> getCapabilities() {
return capabilities.getCapabilities();
}

public void capabilities(Action<Capabilities> capabilitiesAction) {
capabilitiesAction.execute(this.capabilities);
}

public String getDefaultDeployementArtifactName() {
String projectName = project.getName();
if (project.getParent() != null && projectName.equals("runtime")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkus.extension.gradle.dsl;

import java.util.ArrayList;
import java.util.List;

public class Capabilities {

private List<Capability> capabilities = new ArrayList<>(0);

public Capability capability(String name) {
Capability capability = new Capability(name);
capabilities.add(capability);
return capability;
}

public List<Capability> getCapabilities() {
return capabilities;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.quarkus.extension.gradle.dsl;

import java.util.ArrayList;
import java.util.List;

public class Capability {

private String name;

private List<String> onlyIf = new ArrayList<>(0);

private List<String> onlyIfNot = new ArrayList<>(0);

public Capability(String name) {
this.name = name;
}

public String getName() {
return name;
}

public Capability onlyIf(List<String> conditions) {
onlyIf.addAll(conditions);
return this;
}

public List<String> getOnlyIf() {
return onlyIf;
}

public Capability onlyIfNot(List<String> conditions) {
onlyIfNot.addAll(conditions);
return this;
}

public List<String> getOnlyIfNot() {
return onlyIfNot;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.quarkus.bootstrap.model.AppArtifactKey;
import io.quarkus.bootstrap.model.AppModel;
import io.quarkus.extension.gradle.QuarkusExtensionConfiguration;
import io.quarkus.extension.gradle.dsl.Capability;
import io.quarkus.fs.util.ZipUtils;
import io.quarkus.maven.dependency.GACT;

Expand Down Expand Up @@ -140,6 +141,16 @@ private void generateQuarkusExtensionProperties(Path metaInfDir) {
props.put(AppModel.LESSER_PRIORITY_ARTIFACTS, val);
}

List<Capability> capabilities = quarkusExtensionConfiguration.getCapabilities();
if (!capabilities.isEmpty()) {
final StringBuilder buf = new StringBuilder();
appendCapability(capabilities.get(0), buf);
for (int i = 1; i < capabilities.size(); ++i) {
appendCapability(capabilities.get(i), buf.append(','));
}
props.setProperty(BootstrapConstants.PROP_PROVIDES_CAPABILITIES, buf.toString());
}

try {
Files.createDirectories(metaInfDir);
try (BufferedWriter writer = Files
Expand Down Expand Up @@ -263,6 +274,20 @@ private void computeQuarkusCoreVersion(ObjectNode extObject) {
}
}

private static void appendCapability(Capability capability, StringBuilder buf) {
buf.append(capability.getName());
if (!capability.getOnlyIf().isEmpty()) {
for (String onlyIf : capability.getOnlyIf()) {
buf.append('?').append(onlyIf);
}
}
if (!capability.getOnlyIfNot().isEmpty()) {
for (String onlyIfNot : capability.getOnlyIfNot()) {
buf.append("?!").append(onlyIfNot);
}
}
}

private void computeQuarkusExtensions(ObjectNode extObject) {
ObjectNode metadataNode = getMetadataNode(extObject);
Set<ResolvedArtifact> extensions = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void setupProject() throws IOException {

@Test
public void jarShouldContainsExtensionPropertiesFile() throws IOException {
TestUtils.writeFile(buildFile, TestUtils.getDefaultGradleBuildFileContent(true, Collections.emptyList()));
TestUtils.writeFile(buildFile, TestUtils.getDefaultGradleBuildFileContent(true, Collections.emptyList(), ""));

BuildResult jarResult = GradleRunner.create()
.withPluginClasspath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

public class TestUtils {

public static String getDefaultGradleBuildFileContent(boolean disableValidation, List<String> implementationDependencies)
public static String getDefaultGradleBuildFileContent(boolean disableValidation, List<String> implementationDependencies,
String customPluginConfiguration)
throws IOException {
StringBuilder implementationBuilder = new StringBuilder();
for (String implementationDependency : implementationDependencies) {
Expand All @@ -43,7 +44,7 @@ public static String getDefaultGradleBuildFileContent(boolean disableValidation,
"}\n" +
QuarkusExtensionPlugin.EXTENSION_CONFIGURATION_NAME + " { \n" +
"disableValidation = " + disableValidation + "\n" +

customPluginConfiguration +
"}\n" +
"dependencies { \n" +
"implementation enforcedPlatform(\"io.quarkus:quarkus-bom:" + getCurrentQuarkusVersion() + "\")\n" +
Expand Down Expand Up @@ -93,7 +94,7 @@ public static void createExtensionProject(File testProjectDir, boolean disableVa
File runtimeModule = new File(testProjectDir, "runtime");
runtimeModule.mkdir();
writeFile(new File(runtimeModule, "build.gradle"),
getDefaultGradleBuildFileContent(disableValidation, runtimeDependencies));
getDefaultGradleBuildFileContent(disableValidation, runtimeDependencies, ""));
File runtimeTestFile = new File(runtimeModule, "src/main/java/runtime/Test.java");
runtimeTestFile.getParentFile().mkdirs();
writeFile(runtimeTestFile, "package runtime; public class Test {}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import io.quarkus.extension.gradle.QuarkusExtensionPlugin;
import io.quarkus.extension.gradle.TestUtils;

public class ExtensionDescriptorTaskTest {
Expand All @@ -37,7 +36,7 @@ public void setupProject() throws IOException {

@Test
public void shouldCreateFilesWithDefaultValues() throws IOException {
TestUtils.writeFile(buildFile, TestUtils.getDefaultGradleBuildFileContent(true, Collections.emptyList()));
TestUtils.writeFile(buildFile, TestUtils.getDefaultGradleBuildFileContent(true, Collections.emptyList(), ""));
TestUtils.runExtensionDescriptorTask(testProjectDir);

File extensionPropertiesFile = new File(testProjectDir, "build/resources/main/META-INF/quarkus-extension.properties");
Expand Down Expand Up @@ -75,10 +74,8 @@ public void shouldCreateFilesWithDefaultValues() throws IOException {

@Test
public void shouldUseCustomDeploymentArtifactName() throws IOException {
String buildFileContent = TestUtils.getDefaultGradleBuildFileContent(true, Collections.emptyList())
+ QuarkusExtensionPlugin.EXTENSION_CONFIGURATION_NAME + " { " +
"deploymentArtifact = 'custom.group:custom-deployment-artifact:0.1.0'" +
"}";
String buildFileContent = TestUtils.getDefaultGradleBuildFileContent(true, Collections.emptyList(),
"deploymentArtifact = 'custom.group:custom-deployment-artifact:0.1.0'");
TestUtils.writeFile(buildFile, buildFileContent);
TestUtils.runExtensionDescriptorTask(testProjectDir);

Expand All @@ -91,10 +88,8 @@ public void shouldUseCustomDeploymentArtifactName() throws IOException {

@Test
public void shouldContainsConditionalDependencies() throws IOException {
String buildFileContent = TestUtils.getDefaultGradleBuildFileContent(true, Collections.emptyList())
+ QuarkusExtensionPlugin.EXTENSION_CONFIGURATION_NAME + " { " +
"conditionalDependencies= ['org.acme:ext-a:0.1.0', 'org.acme:ext-b:0.1.0']" +
"}";
String buildFileContent = TestUtils.getDefaultGradleBuildFileContent(true, Collections.emptyList(),
"conditionalDependencies= ['org.acme:ext-a:0.1.0', 'org.acme:ext-b:0.1.0']");
TestUtils.writeFile(buildFile, buildFileContent);
TestUtils.runExtensionDescriptorTask(testProjectDir);

Expand All @@ -109,10 +104,9 @@ public void shouldContainsConditionalDependencies() throws IOException {

@Test
public void shouldContainsParentFirstArtifacts() throws IOException {
String buildFileContent = TestUtils.getDefaultGradleBuildFileContent(true, Collections.emptyList())
+ QuarkusExtensionPlugin.EXTENSION_CONFIGURATION_NAME + " { " +
"parentFirstArtifacts = ['org.acme:ext-a:0.1.0', 'org.acme:ext-b:0.1.0']" +
"}";
String buildFileContent = TestUtils.getDefaultGradleBuildFileContent(true, Collections.emptyList(),
"parentFirstArtifacts = ['org.acme:ext-a:0.1.0', 'org.acme:ext-b:0.1.0']");

TestUtils.writeFile(buildFile, buildFileContent);
TestUtils.runExtensionDescriptorTask(testProjectDir);

Expand All @@ -126,7 +120,7 @@ public void shouldContainsParentFirstArtifacts() throws IOException {

@Test
public void shouldGenerateDescriptorBasedOnExistingFile() throws IOException {
TestUtils.writeFile(buildFile, TestUtils.getDefaultGradleBuildFileContent(true, Collections.emptyList()));
TestUtils.writeFile(buildFile, TestUtils.getDefaultGradleBuildFileContent(true, Collections.emptyList(), ""));
File metaInfDir = new File(testProjectDir, "src/main/resources/META-INF");
metaInfDir.mkdirs();
String description = "name: extension-name\n" +
Expand All @@ -143,4 +137,24 @@ public void shouldGenerateDescriptorBasedOnExistingFile() throws IOException {
assertThat(extensionDescriptor.has("description")).isTrue();
assertThat(extensionDescriptor.get("description").asText()).isEqualTo("this is a sample extension");
}

@Test
public void shouldGenerateDescriptorWithCapabilities() throws IOException {
String buildFileContent = TestUtils.getDefaultGradleBuildFileContent(true, Collections.emptyList(),
"capabilities { \n" +
" capability 'org.acme:ext-a:0.1.0' \n" +
" capability 'org.acme:ext-b:0.1.0' onlyIf(['org.acme:ext-b:0.1.0']) onlyIfNot(['org.acme:ext-c:0.1.0']) \n"
+
"}\n");

TestUtils.writeFile(buildFile, buildFileContent);
TestUtils.runExtensionDescriptorTask(testProjectDir);

File extensionPropertiesFile = new File(testProjectDir, "build/resources/main/META-INF/quarkus-extension.properties");
assertThat(extensionPropertiesFile).exists();

Properties extensionProperty = TestUtils.readPropertyFile(extensionPropertiesFile.toPath());
assertThat(extensionProperty).containsEntry("provides-capabilities",
"org.acme:ext-a:0.1.0,org.acme:ext-b:0.1.0?org.acme:ext-b:0.1.0?!org.acme:ext-c:0.1.0");
}
}