Skip to content

Commit

Permalink
introduce extension (=config)
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Nov 28, 2024
1 parent 0cd5401 commit 12c54d6
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.eclipse.dataspacetck.gradle.plugins.tckgen;

import org.gradle.api.provider.Property;

public abstract class TckGeneratorExtension {
private String imageFormat = "svg";
private boolean forceConversion = true;

public void setImageFormat(String imageFormat) {
this.imageFormat = imageFormat;
}

public void setForceConversion(boolean forceConversion) {
this.forceConversion = forceConversion;
}

/**
* SemVer string that indicates the version of the Test Plan Generator module (org.eclipse.dataspacetck:test-plan-generator)
* to use. This should typically be the same version as the other TCK libraries
* <p>
* The default is the same version as the plugin
*/
public abstract Property<String> getGeneratorVersion();

/**
* Whether Mermaid/PlantUML diagrams should be converted - and embedded as - images in the Test Plan document.
* Note that this may involve remote calls to third-party services!
* <p>
* The default is {@code true}
*/
public boolean forceConversion() {
return forceConversion;
}

/**
* The image format for conversion. Can be "svg" or "png" and is only relevant if {@code forceConversion} is set to {@code true}
* <p>
* The default is "svg"
*/
public String getImageFormat() {
return imageFormat;
}

/**
* Output directory of the generated Test Plan.
* <p>
* The default is {@code {rootProject}/build}
*/
public abstract Property<String> getOutputDirectory();

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,80 @@


import org.eclipse.dataspacetck.gradle.tasks.GenerateTestPlanTask;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.DependencyResolutionListener;
import org.gradle.api.artifacts.ResolvableDependencies;
import org.jetbrains.annotations.NotNull;

import static java.util.Optional.ofNullable;

/**
* Gradle plugin that adds a task to generate a test plan
*/
public class TckGeneratorPlugin implements org.gradle.api.Plugin<Project> {
public class TckGeneratorPlugin implements Plugin<Project> {

private static final String GENERATOR_GROUP_ARTIFACT = "org.eclipse.dataspacetck:test-plan-generator";

@Override
public void apply(@NotNull Project target) {
System.out.println("YYEEEAHH " + target.getName());
// register the annotation processor task to all modules that declare a dependency onto the annotation processor,
// except the annotation processor module itself

ofNullable(target.getConfigurations().findByName("annotationProcessor"))
.ifPresent(c -> c.getDependencies().add(target.getDependencies().create("org.eclipse.dataspacetck:test-plan-generator:0.0.1")));

target.getTasks().register(GenerateTestPlanTask.NAME, GenerateTestPlanTask.class).configure(generateTestPlanTask -> {
generateTestPlanTask.setGroup("documentation");
// normally one would use getOrDefault, but the wildcard map prevents that
generateTestPlanTask.setImageFormat(ofNullable(target.getProperties().get("cvf.conversion.format"))
.map(Object::toString)
.orElse(("svg")));

generateTestPlanTask.setForceConversion(ofNullable(target.getProperties().get("cvf.conversion.force"))
.map(Object::toString)
.map(Boolean::parseBoolean)
.orElse(true));
// generateTestPlanTask.setOutputDirectory("/path/to/directory");
});
var extension = target.getExtensions().create("generatorExtension", TckGeneratorExtension.class);

target.getGradle().addListener(new DependencyInjector(target, extension));

target.getTasks()
.register(GenerateTestPlanTask.NAME, GenerateTestPlanTask.class)
.configure(generateTestPlanTask -> {
generateTestPlanTask.setGroup("documentation");
// normally one would use getOrDefault, but the wildcard map prevents that
generateTestPlanTask.setImageFormat(extension.getImageFormat());

generateTestPlanTask.setForceConversion(extension.forceConversion());

if (extension.getOutputDirectory().isPresent()) {
generateTestPlanTask.setOutputDirectory(extension.getOutputDirectory().get());
}
});
}

private @NotNull String getProcessorVersion(TckGeneratorExtension extension, Project project) {
var processorVersion = extension.getGeneratorVersion();

if (processorVersion.isPresent()) {
var version = processorVersion.get();
project.getLogger().debug("{}: use configured version from AutodocExtension (override) [{}]", project.getName(), version);
return version;
} else {
var version = project.getVersion().toString();
project.getLogger().info("No explicit configuration value for the annotationProcessor version was found. Project version {} will be used", version);
return version;
}
}

/**
* callback that is invoked before dependency resolution begins. We need this hook to add the "annotationProcessor" version
*/
private class DependencyInjector implements DependencyResolutionListener {
private final Project target;
private final TckGeneratorExtension extension;

private DependencyInjector(Project target, TckGeneratorExtension extension) {
this.target = target;
this.extension = extension;
}

@Override
public void beforeResolve(@NotNull ResolvableDependencies dependencies) {
// register the annotation processor dependency. This has to happen _after_ the configuration phase is
// complete, e.g. before dependency resolution
ofNullable(target.getConfigurations().findByName("annotationProcessor"))
.ifPresent(c -> c.getDependencies().add(target.getDependencies().create(GENERATOR_GROUP_ARTIFACT + ":" + getProcessorVersion(extension, target))));
target.getGradle().removeListener(this);
}

@Override
public void afterResolve(@NotNull ResolvableDependencies dependencies) {
//noop
}
}
}

0 comments on commit 12c54d6

Please sign in to comment.