Skip to content

Commit

Permalink
Merge pull request #390 from eclipse-vertx/remove-codegen-output
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
vietj authored Sep 9, 2024
2 parents c137984 + c4fba68 commit 817b4af
Show file tree
Hide file tree
Showing 26 changed files with 1,585 additions and 1,051 deletions.
550 changes: 2 additions & 548 deletions vertx-codegen-api/README.md

Large diffs are not rendered by default.

68 changes: 0 additions & 68 deletions vertx-codegen-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,6 @@
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<executions>
<execution>
<id>default-clean</id>
<configuration>
<filesets>
<fileset>
<directory>${project.basedir}/src/converters/generated</directory>
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down Expand Up @@ -145,62 +130,9 @@
<executions>
<execution>
<id>package-docs</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<attach>true</attach>
<descriptors>
<descriptor>src/main/assembly/docs.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>

<!-- Documentation generation : activate with -Pdocs -->
<profile>
<id>docs</id>
<build>
<plugins>
<!-- Transform the Asciidoctor to html -->
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>process-asciidoc</goal>
</goals>
<configuration>
<sourceDirectory>${project.build.directory}/asciidoc</sourceDirectory>
</configuration>
<phase>package</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>javadoc</goal>
</goals>
<configuration>
<reportOutputDirectory>${project.build.directory}/docs</reportOutputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
15 changes: 0 additions & 15 deletions vertx-codegen-json/src/main/assembly/docs.xml

This file was deleted.

48 changes: 7 additions & 41 deletions vertx-codegen-processor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ There can be as many generators as you like.

## Generated output

A generator can create 3 different kinds of output: Java classes, resources and anything else
A generator can create 2 different kinds of output: Java classes or resources

### Generated Java classes

Expand All @@ -24,32 +24,10 @@ The following generators use it:

### Generated resources

A generator declaring a filename prefixed by `resources/` will have its content generated as a compiler resource. This resource will be stored in the generated sources directory and the generated class directory.
Any other file is considered as a java resource, its content generated is considered as a compiler resource. This resource will be stored in the generated sources directory and the generated class directory.

Generated files are handled by the Java compiler (`-s` option), usually build tools configures the compiler to store them in a specific build location, for instance Maven by default uses the `target/generated-sources/annotations` directory.

### Other generated files

Anything else will be stored in the file system using the filename, when the `filename` is relative (it usually is) the target path will be resolved against the `codegen.output` directory.

The following generators use it:

- Scala generator
- Kotlin extension methods

When the `codegen.output` is not specified, generated files are discarded.

### Relocation

Sometimes you want to have a generator to output its files in another directory, you can do that with the
`codegen.output.generator-name` compiler option:

```
<codegen.output.data_object_converters>generated</codegen.output.data_object_converters>
```

Generators will store its content in the `codegen.output/generated` directory instead as a Java class.

## Processor configuration

You can configure the `CodeGenProcessor` as any Java annotation processor, here is how to do with Maven:
Expand All @@ -62,8 +40,7 @@ You can configure the `CodeGenProcessor` as any Java annotation processor, here
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<release>11</release>
<encoding>${project.build.sourceEncoding}</encoding>
<!-- Important: there are issues with apt and incremental compilation in the maven-compiler-plugin -->
<useIncrementalCompilation>false</useIncrementalCompilation>
Expand All @@ -84,9 +61,6 @@ You can configure the `CodeGenProcessor` as any Java annotation processor, here
</path>
<!-- ... more path such as vertx-service-proxy/vertx-rx-java2 depends on what you want to generate ... -->
</annotationProcessorPaths>
<compilerArgs>
<arg>-Acodegen.output=${project.basedir}/src/main</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down Expand Up @@ -114,8 +88,7 @@ task annotationProcessing(type: JavaCompile, group: 'other') { // codegen
options.annotationProcessorPath = configurations.compileClasspath
options.compilerArgs = [
"-proc:only",
"-processor", "io.vertx.codegen.CodeGenProcessor",
"-Acodegen.output=${project.projectDir}/src/main"
"-processor", "io.vertx.codegen.CodeGenProcessor"
]
}
Expand Down Expand Up @@ -150,8 +123,7 @@ tasks.register<JavaCompile>("annotationProcessing") {
options.annotationProcessorPath = configurations.compileClasspath.get()
options.compilerArgs = listOf(
"-proc:only",
"-processor", "io.vertx.codegen.CodeGenProcessor",
"-Acodegen.output=${project.projectDir}/src/main"
"-processor", "io.vertx.codegen.CodeGenProcessor"
)
}
Expand Down Expand Up @@ -179,14 +151,8 @@ Besides you can use the `processor` classified dependency that declares the anno
</dependency>
```

You still need to configure the `codegen.output` for generating files non resources/classes as the processors
requires this option to know where to place them.

The processor is configured by a few options

- `codegen.output` : where the non Java classes / non resources are stored
- `codegen.output.<generator-name>` : relocate the output of _<generator-name>_ to another directory
- `codegen.generators` : a comma separated list of generators, each expression is a regex, allow to filter undesired generators
The processor is configured by a the `codegen.generators` option, a comma separated list of generators, each expression
is a regex, allow to filter undesired generators

## API constraints

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ public void load(ProcessingEnvironment processingEnv) {
* When the returned value
* <ul>
* <li>is {@code null}, no file is created</li>
* <li>does not contain {@code /} and ends with {@code .java}, the file is created as a source file by the annotation processor
* and the class will be compiled by the current compilation process</li>
* <li>starts with the {@code resources/} prefix, the file is created as a resource file by the annotation processor using the
* remaining suffix value and the file will likely end in the classes directory</li>
* <li>otherwise the resource will be created as a file on the filesystem, a value not starting with
* {@code /} is created relative to the {@code codegen.output} directory</li>
* <li>ends with {@code .java} suffix, the file is created as a Java source file by the annotation processor,
* the class will be compiled by the current compilation process</li>
* <li>otherwise, the file is created as a resource file by the annotation processor and the file will likely end
* in the classes directory</li>
* </ul>
*
* @param model the model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.annotation.processing.FilerException;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
Expand All @@ -16,7 +17,6 @@
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
Expand All @@ -30,23 +30,19 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
@javax.annotation.processing.SupportedOptions({"codegen.output","codegen.generators"})
@javax.annotation.processing.SupportedSourceVersion(javax.lang.model.SourceVersion.RELEASE_8)
@javax.annotation.processing.SupportedOptions({"codegen.generators"})
@javax.annotation.processing.SupportedSourceVersion(SourceVersion.RELEASE_11)
public class Processor extends AbstractProcessor {

private static final int JAVA= 0, RESOURCE = 1, OTHER = 2;
private static final String JSON_MAPPERS_PROPERTIES_PATH = "META-INF/vertx/json-mappers.properties";
public static final Logger log = Logger.getLogger(Processor.class.getName());
private File outputDirectory;
private List<? extends Generator<?>> codeGenerators;
private Map<String, GeneratedFile> generatedFiles = new HashMap<>();
private Map<String, GeneratedFile> generatedResources = new HashMap<>();
private Map<String, String> relocations = new HashMap<>();
private Set<Class<? extends Annotation>> supportedAnnotation = new HashSet<>();
private List<CodeGen.Converter> mappers;

Expand All @@ -58,7 +54,6 @@ public Set<String> getSupportedAnnotationTypes() {
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
generatedFiles.clear();
generatedResources.clear();
supportedAnnotation = new HashSet<>(Arrays.asList(DataObject.class, VertxGen.class));
getCodeGenerators()
Expand Down Expand Up @@ -88,18 +83,6 @@ private Predicate<Generator> filterGenerators() {

private Collection<? extends Generator<?>> getCodeGenerators() {
if (codeGenerators == null) {
String outputDirectoryOption = processingEnv.getOptions().get("codegen.output");
if (outputDirectoryOption != null) {
outputDirectory = new File(outputDirectoryOption);
if (!outputDirectory.exists()) {
if (!outputDirectory.mkdirs()) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Output directory " + outputDirectoryOption + " does not exist");
}
}
if (!outputDirectory.isDirectory()) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Output directory " + outputDirectoryOption + " is not a directory");
}
}
// load GeneratorLoader by ServiceLoader
ServiceLoader<GeneratorLoader> genLoaders = ServiceLoader.load(GeneratorLoader.class, Processor.class.getClassLoader());
Iterator<GeneratorLoader> it = genLoaders.iterator();
Expand All @@ -126,15 +109,6 @@ private Collection<? extends Generator<?>> getCodeGenerators() {
}
}

relocations = processingEnv.getOptions()
.entrySet()
.stream()
.filter(e -> e.getKey().startsWith("codegen.output."))
.collect(Collectors.toMap(
e -> e.getKey().substring("codegen.output.".length()),
Map.Entry::getValue)
);

codeGenerators = generators;
}
return codeGenerators;
Expand Down Expand Up @@ -278,22 +252,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
if (codeGenerator.kinds.contains(model.getKind())) {
String relativeName = codeGenerator.filename(model);
if (relativeName != null) {
int kind;
if (relativeName.endsWith(".java") && !relativeName.contains("/")) {
String relocation = relocations.get(codeGenerator.name);
if (relocation != null) {
kind = OTHER;
relativeName = relocation + '/' +
relativeName.substring(0, relativeName.length() - ".java".length()).replace('.', '/') + ".java";
} else {
kind = JAVA;
}
} else if (relativeName.startsWith("resources/")) {
kind = RESOURCE;
} else {
kind = OTHER;
}
if (kind == JAVA) {
if (relativeName.endsWith(".java")) {
// Special handling for .java
String fqn = relativeName.substring(0, relativeName.length() - ".java".length());
// Avoid to recreate the same file (this may happen as we unzip and recompile source trees)
Expand All @@ -302,12 +261,9 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
}
List<ModelProcessing> processings = generatedClasses.computeIfAbsent(fqn, GeneratedFile::new);
processings.add(new ModelProcessing(model, codeGenerator));
} else if (kind == RESOURCE) {
relativeName = relativeName.substring("resources/".length());
List<ModelProcessing> processings = generatedResources.computeIfAbsent(relativeName, GeneratedFile::new);
processings.add(new ModelProcessing(model, codeGenerator));
} else {
List<ModelProcessing> processings = generatedFiles.computeIfAbsent(relativeName, GeneratedFile::new);
// RESOURCE
List<ModelProcessing> processings = generatedResources.computeIfAbsent(relativeName, GeneratedFile::new);
processings.add(new ModelProcessing(model, codeGenerator));
}
}
Expand All @@ -322,7 +278,6 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment

// Generate classes
generatedClasses.values().forEach(generated -> {
boolean shouldWarningsBeSuppressed = false;
try {
String content = generated.generate();
if (content.length() > 0) {
Expand All @@ -343,11 +298,11 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment

// Generate resources
for (GeneratedFile generated : generatedResources.values()) {
boolean shouldWarningsBeSuppressed = false;
try {
String content = generated.generate();
if (content.length() > 0) {
try (Writer w = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", generated.uri).openWriter()) {
if (!content.isEmpty()) {
try (Writer w = processingEnv.getFiler()
.createResource(StandardLocation.CLASS_OUTPUT, "", generated.uri).openWriter()) {
w.write(content);
}
boolean createSource;
Expand All @@ -371,30 +326,6 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
reportException(e, generated.get(0).model.getElement());
}
}
// Generate files
generatedFiles.values().forEach(generated -> {
Path path = new File(generated.uri).toPath();
if (path.isAbsolute()) {
// Nothing to do
} else if (outputDirectory != null) {
path = outputDirectory.toPath().resolve(path);
} else {
return;
}
File file = path.toFile();
Helper.ensureParentDir(file);
String content = generated.generate();
if (content.length() > 0) {
try (FileWriter fileWriter = new FileWriter(file)) {
fileWriter.write(content);
} catch (GenException e) {
reportGenException(e);
} catch (Exception e) {
reportException(e, generated.get(0).model.getElement());
}
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Generated model " + generated.get(0).model.getFqn() + ": " + generated.uri);
}
});
}
return true;
}
Expand Down
Loading

0 comments on commit 817b4af

Please sign in to comment.