Skip to content

Instantly share code, notes, and snippets.

@jkschneider
Created December 7, 2021 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkschneider/1a39146049165f82f9953a5042166187 to your computer and use it in GitHub Desktop.
Save jkschneider/1a39146049165f82f9953a5042166187 to your computer and use it in GitHub Desktop.
package org.openrewrite.java;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.SourceFile;
import org.openrewrite.Tree;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.tree.J;
import org.openrewrite.marker.Markers;
import org.openrewrite.text.PlainText;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class GenerateDocs extends Recipe {
@Override
public String getDisplayName() {
return "Generic docs";
}
@Override
public String getDescription() {
return "Generate docs based on some things.";
}
@Override
protected List<SourceFile> visit(List<SourceFile> before, ExecutionContext ctx) {
List<String> classNames = new ArrayList<>();
for (SourceFile sourceFile : before) {
new JavaIsoVisitor<Integer>() {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Integer integer) {
classNames.add(classDecl.getType().getFullyQualifiedName());
return super.visitClassDeclaration(classDecl, integer);
}
}.visit(sourceFile, 0);
}
PlainText doc = new PlainText(Tree.randomId(), Paths.get("docs/class-list.asciidoc"), Markers.EMPTY,
"= A list of classes" + classNames.stream().map(name -> "* " + name).collect(Collectors.joining("\n", "\n", "")));
return ListUtils.concat(before, doc);
}
}
@jkschneider
Copy link
Author

Relativization of the doc path relative to the project root will be handled for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment