|
| 1 | +package io.github.ascopes.jct.workspaces.impl; |
| 2 | + |
| 3 | +import static io.github.ascopes.jct.utils.IoExceptionUtils.uncheckedIo; |
| 4 | + |
| 5 | +import io.github.ascopes.jct.workspaces.PathRoot; |
| 6 | +import java.io.Closeable; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.FileVisitResult; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.nio.file.SimpleFileVisitor; |
| 12 | +import java.nio.file.attribute.BasicFileAttributes; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import javax.tools.JavaFileManager.Location; |
| 16 | +import org.jspecify.annotations.Nullable; |
| 17 | + |
| 18 | +/** |
| 19 | + * Helper for dumping the structure of locations in a workspace for debugging purposes. |
| 20 | + * |
| 21 | + * <p>This is not thread-safe. |
| 22 | + * |
| 23 | + * @author Ashley Scopes |
| 24 | + * @since 5.0.0 |
| 25 | + */ |
| 26 | +final class WorkspaceDumper { |
| 27 | + |
| 28 | + private final Appendable appendable; |
| 29 | + |
| 30 | + WorkspaceDumper(Appendable appendable) { |
| 31 | + this.appendable = appendable; |
| 32 | + } |
| 33 | + |
| 34 | + void dump(String repr, Map<Location, List<PathRoot>> locations) { |
| 35 | + uncheckedIo(() -> appendable.append("Workspace ").append(repr).append(":\n")); |
| 36 | + locations.forEach(this::dumpLocation); |
| 37 | + } |
| 38 | + |
| 39 | + private void dumpLocation(Location location, List<PathRoot> pathRoots) { |
| 40 | + uncheckedIo(() -> appendable.append(" Location ").append(location.toString()).append(":\n")); |
| 41 | + pathRoots.forEach(this::dumpPath); |
| 42 | + } |
| 43 | + |
| 44 | + private void dumpPath(PathRoot pathRoot) { |
| 45 | + uncheckedIo(() -> { |
| 46 | + appendable.append(" - ").append(pathRoot.getUri().toString()).append("\n"); |
| 47 | + try (var walker = new Walker(7)) { |
| 48 | + Files.walkFileTree(pathRoot.getPath(), walker); |
| 49 | + } |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + private final class Walker extends SimpleFileVisitor<Path> implements AutoCloseable { |
| 54 | + |
| 55 | + private static final int INDENT_SIZE = 2; |
| 56 | + |
| 57 | + private int index; |
| 58 | + private int indent; |
| 59 | + |
| 60 | + private Walker(int indent) { |
| 61 | + index = 0; |
| 62 | + this.indent = indent; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void close() throws IOException { |
| 67 | + // Only 1 index implies an empty directory. |
| 68 | + if (index <= 1) { |
| 69 | + doIndent(); |
| 70 | + appendable.append(" [[empty]]\n"); |
| 71 | + } |
| 72 | + appendable.append("\n"); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public FileVisitResult preVisitDirectory( |
| 77 | + Path dir, |
| 78 | + BasicFileAttributes attrs |
| 79 | + ) throws IOException { |
| 80 | + if (index > 0) { |
| 81 | + doIndent(); |
| 82 | + appendable.append(dir.getFileName().toString()).append("/\n"); |
| 83 | + indent += INDENT_SIZE; |
| 84 | + } |
| 85 | + |
| 86 | + ++index; |
| 87 | + return super.preVisitDirectory(dir, attrs); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public FileVisitResult postVisitDirectory( |
| 92 | + Path dir, |
| 93 | + @Nullable IOException ex |
| 94 | + ) throws IOException { |
| 95 | + indent -= INDENT_SIZE; |
| 96 | + return super.postVisitDirectory(dir, ex); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 101 | + doIndent(); |
| 102 | + appendable.append(file.getFileName().toString()).append("\n"); |
| 103 | + return super.visitFile(file, attrs); |
| 104 | + } |
| 105 | + |
| 106 | + private void doIndent() throws IOException { |
| 107 | + appendable.append(" ".repeat(indent)); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments