Skip to content

Commit

Permalink
Declare input and output for quarkusGenerateCode gradle task
Browse files Browse the repository at this point in the history
  • Loading branch information
glefloch committed Apr 23, 2021
1 parent 65b8819 commit ee80391
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
27 changes: 11 additions & 16 deletions devtools/gradle/src/main/java/io/quarkus/gradle/QuarkusPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,9 @@ public void execute(Task test) {
compileJavaTask.getOptions().setAnnotationProcessorPath(annotationProcessorConfig);

compileJavaTask.dependsOn(quarkusGenerateCode);
quarkusGenerateCode.setSourceRegistrar(compileJavaTask::source);

JavaCompile compileTestJavaTask = (JavaCompile) tasks.getByName(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME);
compileTestJavaTask.dependsOn(quarkusGenerateCodeTests);
quarkusGenerateCodeTests.setSourceRegistrar(compileTestJavaTask::source);

Task classesTask = tasks.getByName(JavaPlugin.CLASSES_TASK_NAME);
Task resourcesTask = tasks.getByName(JavaPlugin.PROCESS_RESOURCES_TASK_NAME);
Expand Down Expand Up @@ -204,27 +202,24 @@ public void execute(Task test) {
tasks.withType(Test.class).forEach(configureTestTask);
tasks.withType(Test.class).whenTaskAdded(configureTestTask::accept);

sourceSets.create(QuarkusGenerateCode.QUARKUS_GENERATED_SOURCES).getOutput()
SourceSet generatedSourceSet = sourceSets.create(QuarkusGenerateCode.QUARKUS_GENERATED_SOURCES);
generatedSourceSet.getOutput()
.dir(QuarkusGenerateCode.QUARKUS_GENERATED_SOURCES);
sourceSets.create(QuarkusGenerateCode.QUARKUS_TEST_GENERATED_SOURCES).getOutput()
SourceSet generatedTestSourceSet = sourceSets.create(QuarkusGenerateCode.QUARKUS_TEST_GENERATED_SOURCES);
generatedTestSourceSet.getOutput()
.dir(QuarkusGenerateCode.QUARKUS_TEST_GENERATED_SOURCES);

// Register the quarkus-generated-code
for (String provider : QuarkusGenerateCode.CODE_GENERATION_PROVIDER) {
mainSourceSet.getJava().srcDir(new File(generatedSourceSet.getJava().getOutputDir(), provider));
testSourceSet.getJava().srcDir(new File(generatedTestSourceSet.getJava().getOutputDir(), provider));
}

});

project.getPlugins().withId("org.jetbrains.kotlin.jvm", plugin -> {
tasks.getByName("compileKotlin").dependsOn(quarkusGenerateCode);
tasks.getByName("compileTestKotlin").dependsOn(quarkusGenerateCodeTests);

// Register the quarkus-generated-code
SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class)
.getSourceSets();
SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
SourceSet testSourceSet = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME);
SourceSet generatedSourceSet = sourceSets.getByName(QuarkusGenerateCode.QUARKUS_GENERATED_SOURCES);
SourceSet generatedTestSourceSet = sourceSets.getByName(QuarkusGenerateCode.QUARKUS_TEST_GENERATED_SOURCES);
for (String provider : QuarkusGenerateCode.CODE_GENERATION_PROVIDER) {
mainSourceSet.getJava().srcDir(new File(generatedSourceSet.getJava().getOutputDir(), provider));
testSourceSet.getJava().srcDir(new File(generatedTestSourceSet.getJava().getOutputDir(), provider));
}
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.quarkus.gradle.tasks;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
Expand All @@ -16,6 +19,8 @@
import org.gradle.api.plugins.Convention;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.CompileClasspath;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.OutputDirectories;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskAction;

Expand All @@ -33,10 +38,12 @@ public class QuarkusGenerateCode extends QuarkusTask {
public static final String QUARKUS_TEST_GENERATED_SOURCES = "quarkus-test-generated-sources";
// TODO dynamically load generation provider, or make them write code directly in quarkus-generated-sources
public static final String[] CODE_GENERATION_PROVIDER = new String[] { "grpc", "avdl", "avpr", "avsc" };
public static final String[] CODE_GENERATION_INPUT = new String[] { "proto", "avro" };

public static final String INIT_AND_RUN = "initAndRun";
private Set<Path> sourcesDirectories;
private Consumer<Path> sourceRegistrar;
private Consumer<Path> sourceRegistrar = (p) -> {
};
private boolean test = false;

public QuarkusGenerateCode() {
Expand All @@ -53,6 +60,29 @@ public FileCollection getClasspath() {
return QuarkusGradleUtils.getSourceSet(getProject(), SourceSet.MAIN_SOURCE_SET_NAME).getCompileClasspath();
}

@InputFiles
public Set<File> getInputDirectory() {
Set<File> inputDirectories = new HashSet<>();

final String inputSourceSetName = test ? SourceSet.TEST_SOURCE_SET_NAME : SourceSet.MAIN_SOURCE_SET_NAME;
Path src = getProject().getProjectDir().toPath().resolve("src").resolve(inputSourceSetName);

for (String input : CODE_GENERATION_INPUT) {
Path providerSrcDir = src.resolve(input);
if (Files.exists(providerSrcDir)) {
inputDirectories.add(providerSrcDir.toFile());
}
}

return inputDirectories;
}

@OutputDirectories
public FileCollection getGeneratedOutputDirectory() {
final String generatedSourceSetName = test ? QUARKUS_TEST_GENERATED_SOURCES : QUARKUS_GENERATED_SOURCES;
return QuarkusGradleUtils.getSourceSet(getProject(), generatedSourceSetName).getOutput().getDirs();
}

@TaskAction
public void prepareQuarkus() {
getLogger().lifecycle("preparing quarkus application");
Expand Down Expand Up @@ -118,10 +148,6 @@ public void setSourcesDirectories(Set<Path> sourcesDirectories) {
this.sourcesDirectories = sourcesDirectories;
}

public void setSourceRegistrar(Consumer<Path> sourceRegistrar) {
this.sourceRegistrar = sourceRegistrar;
}

public void setTest(boolean test) {
this.test = test;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void canDetectUpToDateBuild() throws Exception {
assertThat(firstBuild.getTasks().get(":quarkusBuild")).isEqualTo(BuildResult.SUCCESS_OUTCOME);

BuildResult secondBuild = runGradleWrapper(projectRoot, "quarkusBuild", "--stacktrace");
assertThat(secondBuild.getTasks().get(":quarkusGenerateCode")).isEqualTo(BuildResult.UPTODATE_OUTCOME);
assertThat(secondBuild.getTasks().get(":quarkusBuild")).isEqualTo(BuildResult.UPTODATE_OUTCOME);
}

Expand Down

0 comments on commit ee80391

Please sign in to comment.