Skip to content

Commit

Permalink
feat: create custom merger task (#45)
Browse files Browse the repository at this point in the history
* feat: create custom merger task

* pr remarks
  • Loading branch information
paullatzelsperger committed Nov 15, 2022
1 parent 2da8545 commit 1fca9c5
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 7 deletions.
1 change: 1 addition & 0 deletions plugins/edc-build/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {
implementation(project(":plugins:autodoc:autodoc-plugin"))
implementation(project(":plugins:test-summary"))
implementation(project(":plugins:module-names"))
implementation(project(":plugins:openapi-merger"))

implementation("com.autonomousapps:dependency-analysis-gradle-plugin:1.13.1")
implementation("io.github.gradle-nexus:publish-plugin:1.1.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
package org.eclipse.edc.plugins.edcbuild;

import com.autonomousapps.DependencyAnalysisPlugin;
import com.rameshkp.openapi.merger.gradle.plugin.OpenApiMergerGradlePlugin;
import io.github.gradlenexus.publishplugin.NexusPublishPlugin;
import org.eclipse.edc.plugins.autodoc.AutodocPlugin;
import org.eclipse.edc.plugins.modulenames.ModuleNamesPlugin;
import org.eclipse.edc.plugins.openapimerger.OpenApiMergerPlugin;
import org.eclipse.edc.plugins.testsummary.TestSummaryPlugin;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
Expand Down Expand Up @@ -49,7 +49,7 @@ private static void defineCapabilities(Project target) {
if (target == target.getRootProject()) {
target.getPlugins().apply(ChecksumPlugin.class);
target.getPlugins().apply(NexusPublishPlugin.class);
target.getPlugins().apply(OpenApiMergerGradlePlugin.class);
target.getPlugins().apply(OpenApiMergerPlugin.class);
target.getPlugins().apply(ModuleNamesPlugin.class);
target.getPlugins().apply(DependencyAnalysisPlugin.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public void apply(Project target) {
dependencyAnalysis(),
tests(),
jar(),

swagger()
).forEach(c -> c.apply(project));
});


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import static java.util.Optional.ofNullable;

class ConventionFunctions {
public class ConventionFunctions {

/**
* Supplies a generic {@link GradleException} with the given message.
Expand All @@ -33,11 +33,11 @@ public static Supplier<GradleException> gradleException(String message) {
/**
* Supplies a {@link GradleException} that has a specific message indicating that a particular extension is missing.
*/
public static Supplier<GradleException> extensionException(Class<?> extensionClass) {
static Supplier<GradleException> extensionException(Class<?> extensionClass) {
return gradleException(extensionClass.getSimpleName() + " expected but was null");
}

static <A> A requireExtension(Project target, Class<A> extensionClass) {
public static <A> A requireExtension(Project target, Class<A> extensionClass) {
return ofNullable(target.getExtensions().findByType(extensionClass))
.orElseThrow(extensionException(extensionClass));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import io.swagger.v3.plugins.gradle.tasks.ResolveTask;
import org.eclipse.edc.plugins.edcbuild.extensions.BuildExtension;
import org.eclipse.edc.plugins.edcbuild.tasks.PrintApiGroupTask;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginExtension;
Expand All @@ -42,6 +43,9 @@ public void apply(Project target) {
}

target.getPluginManager().withPlugin("io.swagger.core.v3.swagger-gradle-plugin", appliedPlugin -> {

target.getTasks().register("apiGroups", PrintApiGroupTask.class);

target.getDependencies().add(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME,
"io.swagger.core.v3:swagger-jaxrs2-jakarta:2.2.2");
target.getDependencies().add(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2022 Microsoft Corporation
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*
*/

package org.eclipse.edc.plugins.edcbuild.tasks;

import org.eclipse.edc.plugins.edcbuild.extensions.BuildExtension;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;

import static org.eclipse.edc.plugins.edcbuild.conventions.ConventionFunctions.requireExtension;


public class PrintApiGroupTask extends DefaultTask {

@TaskAction
public void printApiGroup() {
var buildExt = requireExtension(getProject(), BuildExtension.class);
getProject().getLogger().lifecycle("API Group: {}", buildExt.getSwagger().getApiGroup().getOrElse(""));
}
}
1 change: 1 addition & 0 deletions plugins/openapi-merger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module contains a gradle plugin that allows to merge partial OpenAPI spec files into one file.
35 changes: 35 additions & 0 deletions plugins/openapi-merger/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
plugins {
`java-gradle-plugin`
id("org.gradle.crypto.checksum") version "1.4.0"
}

val jupiterVersion: String by project
val assertj: String by project
val groupId: String by project

dependencies {
// contains the actual merger task
implementation("com.rameshkp:openapi-merger-gradle-plugin:1.0.4")
// needed for the OpenApiDataInvalidException:
implementation("com.rameshkp:openapi-merger-app:1.0.4")
}

gradlePlugin {
// Define the plugin
plugins {
create("openapi-merger") {
displayName = "openapi-merger"
description =
"Plugin to several OpenAPI spec files into one"
id = "${groupId}.openapi-merger"
implementationClass = "org.eclipse.edc.plugins.openapimerger.OpenApiMergerPlugin"
}
}
}

pluginBundle {
website = "https://projects.eclipse.org/proposals/eclipse-dataspace-connector"
vcsUrl = "https://github.com/eclipse-dataspaceconnector/GradlePlugins.git"
version = version
tags = listOf("build", "openapi", "merge", "documentation")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2022 Microsoft Corporation
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*
*/

package org.eclipse.edc.plugins.openapimerger;

import com.rameshkp.openapi.merger.gradle.plugin.OpenApiMergerGradlePlugin;
import org.eclipse.edc.plugins.openapimerger.tasks.MergeApiSpecByPathTask;
import org.gradle.api.Plugin;
import org.gradle.api.Project;

/**
* Custom grade plugin to avoid module name duplications.
* Checks between modules with a gradle build file that their names are unique in the whole project.
* `samples` and `system-tests` modules are excluded.
* <p>
* Ref: <a href="https://github.com/gradle/gradle/issues/847">Github Issue</a>
*/
public class OpenApiMergerPlugin implements Plugin<Project> {

@Override
public void apply(Project project) {

if (project == project.getRootProject()) {
project.getPlugins().apply(OpenApiMergerGradlePlugin.class);
project.getTasks().register(MergeApiSpecByPathTask.NAME, MergeApiSpecByPathTask.class);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2022 Microsoft Corporation
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*
*/

package org.eclipse.edc.plugins.openapimerger.tasks;

import com.rameshkp.openapi.merger.gradle.task.OpenApiMergerTask;
import org.gradle.api.tasks.options.Option;

import java.io.File;

/**
* Customization of the {@link OpenApiMergerTask}, which allows to pass in the input and output directories via command line.
*/
public class MergeApiSpecByPathTask extends OpenApiMergerTask {

public static final String NAME = "mergeApiSpec";

@Option(option = "output", description = "Output directory where the merged spec file is stores. Optional.")
public void setOutputDir(String outputDirectory) {
// inject the command line arg into the merger task's config
getOutputFileProperty().set(new File(outputDirectory));
}

@Option(option = "input", description = "Input directory where to look for partial specs. Required")
public void setInputDir(String inputDir) {
// inject the command line arg into the merger task's config
getInputDirectory().set(new File(inputDir));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2022 Microsoft Corporation
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*
*/

package org.eclipse.edc.plugins.modulenames;

import org.eclipse.edc.plugins.openapimerger.OpenApiMergerPlugin;
import org.eclipse.edc.plugins.openapimerger.tasks.MergeApiSpecByPathTask;
import org.gradle.api.Project;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class OpenApiMergerPluginTest {

private Project project;

@BeforeEach
void setUp() {
project = ProjectBuilder.builder().build();
project.getPlugins().apply(OpenApiMergerPlugin.class);
}

@Test
void verify_hasMergerTask() {
assertThat(project.getTasks().findByName(MergeApiSpecByPathTask.NAME)).isNotNull();
}


@Test
void verify_pluginIsOnlyAppliedToRootProject() {
var subproj = ProjectBuilder.builder().withParent(project).build();

subproj.getPlugins().apply(OpenApiMergerPlugin.class);

assertThat(subproj.getTasks().findByName(MergeApiSpecByPathTask.NAME)).isNull();
}

}
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
rootProject.name = "edcPlugins"
include("plugins:module-names")
include("plugins:test-summary")
include("plugins:openapi-merger")
include("plugins:edc-build")
include("plugins:autodoc:autodoc-plugin")
include("plugins:autodoc:autodoc-processor")
include("runtime-metamodel")
include("version-catalog")
include("version-catalog")

0 comments on commit 1fca9c5

Please sign in to comment.