Skip to content

Commit b11dd69

Browse files
committed
Improve workspace dumper implementation
1 parent 65db113 commit b11dd69

File tree

8 files changed

+141
-62
lines changed

8 files changed

+141
-62
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
}

java-compiler-testing/src/main/java/io/github/ascopes/jct/workspaces/impl/WorkspaceImpl.java

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,13 @@
2020

2121
import io.github.ascopes.jct.ex.JctIllegalInputException;
2222
import io.github.ascopes.jct.filemanagers.ModuleLocation;
23-
import io.github.ascopes.jct.utils.IoExceptionUtils;
2423
import io.github.ascopes.jct.utils.ToStringBuilder;
2524
import io.github.ascopes.jct.workspaces.ManagedDirectory;
2625
import io.github.ascopes.jct.workspaces.PathRoot;
2726
import io.github.ascopes.jct.workspaces.PathStrategy;
2827
import io.github.ascopes.jct.workspaces.Workspace;
29-
import java.io.IOException;
30-
import java.nio.file.FileVisitResult;
3128
import java.nio.file.Files;
3229
import java.nio.file.Path;
33-
import java.nio.file.SimpleFileVisitor;
34-
import java.nio.file.attribute.BasicFileAttributes;
3530
import java.util.ArrayList;
3631
import java.util.HashMap;
3732
import java.util.List;
@@ -100,60 +95,7 @@ public void close() {
10095

10196
@Override
10297
public void dump(Appendable appendable) {
103-
IoExceptionUtils.uncheckedIo(() -> {
104-
appendable.append(toString()).append("\n");
105-
for (var location : locations.keySet().stream().sorted().toList()) {
106-
appendable.append(" Location ").append(location.toString()).append(": \n");
107-
108-
for (var pathRoot : locations.get(location)) {
109-
appendable.append(" - ").append(pathRoot.getUri().toString()).append(" contents:\n");
110-
111-
var baseIndent = 8;
112-
var basePath = pathRoot.getPath();
113-
114-
Files.walkFileTree(basePath, new SimpleFileVisitor<>() {
115-
private int indent = 0;
116-
117-
@Override
118-
public FileVisitResult preVisitDirectory(
119-
Path dir,
120-
BasicFileAttributes attrs
121-
) throws IOException {
122-
if (!dir.equals(basePath)) {
123-
appendIndent();
124-
appendable.append(dir.getFileName().toString()).append("/\n");
125-
indent += 2;
126-
}
127-
128-
return FileVisitResult.CONTINUE;
129-
}
130-
131-
@Override
132-
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
133-
indent -= 2;
134-
return FileVisitResult.CONTINUE;
135-
}
136-
137-
@Override
138-
public FileVisitResult visitFile(
139-
Path file,
140-
BasicFileAttributes attrs
141-
) throws IOException {
142-
appendIndent();
143-
appendable.append(file.getFileName().toString()).append("\n");
144-
return FileVisitResult.CONTINUE;
145-
}
146-
147-
private void appendIndent() throws IOException {
148-
appendable.append(" ".repeat(baseIndent))
149-
.append("·".repeat(indent));
150-
}
151-
});
152-
}
153-
154-
appendable.append("\n");
155-
}
156-
});
98+
new WorkspaceDumper(appendable).dump(toString(), locations);
15799
}
158100

159101
@Override

java-compiler-testing/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
requires static org.junit.jupiter.api;
130130
requires static org.junit.jupiter.params;
131131
requires org.slf4j;
132+
requires java.desktop;
132133

133134
//////////////////
134135
/// PUBLIC API ///

java-compiler-testing/src/test/java/io/github/ascopes/jct/integration/compilation/BasicLegacyCompilationIntegrationTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ void helloWorldJavacRamDirectory(JctCompiler compiler) {
4343
.copyContentsFrom(resourcesDirectory());
4444
var compilation = compiler.compile(workspace);
4545

46+
workspace.dump(System.err);
47+
4648
assertThatCompilation(compilation)
4749
.isSuccessfulWithoutWarnings();
4850

@@ -64,6 +66,8 @@ void helloWorldJavacTempDirectory(JctCompiler compiler) {
6466

6567
var compilation = compiler.compile(workspace);
6668

69+
workspace.dump(System.err);
70+
6771
assertThatCompilation(compilation)
6872
.isSuccessfulWithoutWarnings();
6973

java-compiler-testing/src/test/java/io/github/ascopes/jct/integration/compilation/BasicModuleCompilationIntegrationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ void helloWorldRamDisk(JctCompiler compiler) {
4444
// When
4545
var compilation = compiler.compile(workspace);
4646

47+
workspace.dump(System.err);
48+
4749
// Then
4850
assertThatCompilation(compilation)
4951
.isSuccessfulWithoutWarnings();
5052

51-
workspace.dump(System.out);
52-
5353
assertThatCompilation(compilation)
5454
.classOutputPackages()
5555
.fileExists("com", "example", "HelloWorld.class")
@@ -74,7 +74,7 @@ void helloWorldUsingTempDirectory(JctCompiler compiler) {
7474
// When
7575
var compilation = compiler.compile(workspace);
7676

77-
workspace.dump(System.out);
77+
workspace.dump(System.err);
7878

7979
// Then
8080
assertThatCompilation(compilation)

java-compiler-testing/src/test/java/io/github/ascopes/jct/integration/compilation/BasicMultiModuleCompilationIntegrationTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ void singleModuleInMultiModuleLayoutRamDisk(JctCompiler compiler) {
4545
// When
4646
var compilation = compiler.compile(workspace);
4747

48+
workspace.dump(System.err);
49+
4850
// Then
4951
assertThatCompilation(compilation)
5052
.isSuccessfulWithoutWarnings()
@@ -73,6 +75,8 @@ void singleModuleInMultiModuleLayoutTempDirectory(JctCompiler compiler) {
7375
// When
7476
var compilation = compiler.compile(workspace);
7577

78+
workspace.dump(System.err);
79+
7680
// Then
7781
assertThatCompilation(compilation)
7882
.isSuccessfulWithoutWarnings()
@@ -105,6 +109,8 @@ void multipleModulesInMultiModuleLayoutRamDisk(JctCompiler compiler) {
105109
// When
106110
var compilation = compiler.compile(workspace);
107111

112+
workspace.dump(System.err);
113+
108114
// Then
109115
assertThatCompilation(compilation)
110116
.isSuccessfulWithoutWarnings();
@@ -151,6 +157,8 @@ void multipleModulesInMultiModuleLayoutTempDirectory(JctCompiler compiler) {
151157
// When
152158
var compilation = compiler.compile(workspace);
153159

160+
workspace.dump(System.err);
161+
154162
assertThatCompilation(compilation)
155163
.isSuccessfulWithoutWarnings();
156164

java-compiler-testing/src/test/java/io/github/ascopes/jct/integration/compilation/CompilingSpecificClassesIntegrationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ void onlyTheClassesSpecifiedGetCompiled(JctCompiler compiler) {
4141

4242
var compilation = compiler.compile(workspace, "Fibonacci", "HelloWorld");
4343

44+
workspace.dump(System.err);
45+
4446
assertThat(compilation)
4547
.isSuccessfulWithoutWarnings()
4648
.classOutputPackages()

java-compiler-testing/src/test/java/io/github/ascopes/jct/integration/compilation/MultiTieredCompilationIntegrationTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ void compileSourcesToClassesAndProvideThemInClassPathToSecondCompilation(
4949
.copyContentsFrom(resourcesDirectory().resolve("first"));
5050

5151
var firstCompilation = compiler.compile(firstWorkspace);
52+
53+
firstWorkspace.dump(System.err);
54+
5255
assertThatCompilation(firstCompilation)
5356
.isSuccessfulWithoutWarnings()
5457
.classOutputPackages()
@@ -62,6 +65,9 @@ void compileSourcesToClassesAndProvideThemInClassPathToSecondCompilation(
6265
.copyContentsFrom(resourcesDirectory().resolve("second"));
6366

6467
var secondCompilation = compiler.compile(secondWorkspace);
68+
69+
secondWorkspace.dump(System.err);
70+
6571
assertThatCompilation(secondCompilation)
6672
.isSuccessfulWithoutWarnings()
6773
.classOutputPackages()
@@ -88,6 +94,7 @@ void compileSourcesToClassesAndProvideThemInClassPathToSecondCompilationWithinJa
8894
.copyContentsFrom(resourcesDirectory().resolve("first"));
8995

9096
var firstCompilation = compiler.compile(firstWorkspace);
97+
9198
assertThatCompilation(firstCompilation)
9299
.isSuccessfulWithoutWarnings()
93100
.classOutputPackages()
@@ -100,13 +107,18 @@ void compileSourcesToClassesAndProvideThemInClassPathToSecondCompilationWithinJa
100107
.createFile("first.jar")
101108
.asJarFrom(firstWorkspace.getClassOutputPackages().get(0));
102109

110+
firstWorkspace.dump(System.err);
111+
103112
var firstJar = firstWorkspace.getClassOutputPackages().get(1).getPath().resolve("first.jar");
104113
secondWorkspace.addClassPathPackage(firstJar);
105114
secondWorkspace
106115
.createSourcePathPackage()
107116
.copyContentsFrom(resourcesDirectory().resolve("second"));
108117

109118
var secondCompilation = compiler.compile(secondWorkspace);
119+
120+
secondWorkspace.dump(System.err);
121+
110122
assertThatCompilation(secondCompilation)
111123
.isSuccessfulWithoutWarnings()
112124
.classOutputPackages()

0 commit comments

Comments
 (0)