Skip to content

Commit ddefade

Browse files
feat(plugin): add per-file transformation report output
1 parent 1fb9fce commit ddefade

4 files changed

Lines changed: 110 additions & 0 deletions

File tree

bpj-maven-plugin/src/main/java/io/github/bpj/maven/BpjPrepareMojo.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.nio.charset.StandardCharsets;
99
import java.nio.file.Files;
1010
import java.nio.file.Path;
11+
import java.time.OffsetDateTime;
12+
import java.util.ArrayList;
1113
import java.util.List;
1214
import java.util.stream.Stream;
1315
import org.apache.maven.plugin.AbstractMojo;
@@ -43,6 +45,12 @@ public class BpjPrepareMojo extends AbstractMojo {
4345
@Parameter(defaultValue = "false")
4446
private boolean verbose;
4547

48+
@Parameter(defaultValue = "false")
49+
private boolean writeReport;
50+
51+
@Parameter(defaultValue = "${project.build.directory}/reports/bpj-transform-report.txt")
52+
private File reportFile;
53+
4654
@Override
4755
public void execute() throws MojoExecutionException {
4856
if (inputDirectory == null || !inputDirectory.exists()) {
@@ -56,6 +64,7 @@ public void execute() throws MojoExecutionException {
5664

5765
int changedCalls = 0;
5866
int filesProcessed = 0;
67+
List<FileTransformResult> transformedFiles = new ArrayList<>();
5968

6069
try {
6170
List<Path> sources;
@@ -79,13 +88,18 @@ public void execute() throws MojoExecutionException {
7988
String original = Files.readString(source, StandardCharsets.UTF_8);
8089
TransformationResult result = transformer.transform(source, original, failOnUnresolved);
8190
changedCalls += result.replacements();
91+
transformedFiles.add(new FileTransformResult(relative.toString(), result.replacements()));
8292

8393
Files.writeString(target, result.source(), StandardCharsets.UTF_8);
8494
if (verbose && result.replacements() > 0) {
8595
getLog().info("BPJ transformed " + relative + " (" + result.replacements() + " call(s))");
8696
}
8797
}
8898

99+
if (writeReport) {
100+
writeTransformReport(filesProcessed, changedCalls, transformedFiles);
101+
}
102+
89103
configureCompileRoots();
90104
getLog().info(
91105
"BPJ prepare completed. Processed " + filesProcessed
@@ -133,4 +147,39 @@ private String stackTrace(Exception exception) {
133147
}
134148
return sb.toString();
135149
}
150+
151+
private void writeTransformReport(
152+
int filesProcessed,
153+
int changedCalls,
154+
List<FileTransformResult> transformedFiles
155+
) throws IOException {
156+
if (reportFile == null) {
157+
return;
158+
}
159+
160+
Path reportPath = reportFile.toPath();
161+
Path parent = reportPath.getParent();
162+
if (parent != null) {
163+
Files.createDirectories(parent);
164+
}
165+
166+
List<String> lines = new ArrayList<>();
167+
lines.add("BPJ Transformation Report");
168+
lines.add("generatedAt: " + OffsetDateTime.now());
169+
lines.add("project: " + project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion());
170+
lines.add("filesProcessed: " + filesProcessed);
171+
lines.add("callsTransformed: " + changedCalls);
172+
lines.add("");
173+
lines.add("files:");
174+
175+
for (FileTransformResult result : transformedFiles) {
176+
lines.add(result.path() + " -> " + result.replacements() + " call(s)");
177+
}
178+
179+
Files.write(reportPath, lines, StandardCharsets.UTF_8);
180+
getLog().info("BPJ report written to " + reportPath);
181+
}
182+
183+
private record FileTransformResult(String path, int replacements) {
184+
}
136185
}

bpj-starter-smoke/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,26 @@
2525
<scope>test</scope>
2626
</dependency>
2727
</dependencies>
28+
29+
<build>
30+
<plugins>
31+
<plugin>
32+
<groupId>io.github.oriontheprogrammer</groupId>
33+
<artifactId>bpj-maven-plugin</artifactId>
34+
<version>${project.version}</version>
35+
<executions>
36+
<execution>
37+
<id>bpj-prepare</id>
38+
<goals>
39+
<goal>prepare</goal>
40+
</goals>
41+
</execution>
42+
</executions>
43+
<configuration>
44+
<writeReport>true</writeReport>
45+
<reportFile>${project.build.directory}/reports/bpj-transform-report.txt</reportFile>
46+
</configuration>
47+
</plugin>
48+
</plugins>
49+
</build>
2850
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.github.bpj.smoke;
2+
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
import java.io.IOException;
6+
import java.nio.charset.StandardCharsets;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import org.junit.jupiter.api.Test;
10+
11+
class TransformationReportTest {
12+
13+
@Test
14+
void shouldGenerateTransformationReport() throws IOException {
15+
Path report = Path.of("target", "reports", "bpj-transform-report.txt");
16+
assertTrue(Files.exists(report), "Expected BPJ report to exist");
17+
18+
String content = Files.readString(report, StandardCharsets.UTF_8).replace('\\', '/');
19+
20+
assertTrue(content.contains("BPJ Transformation Report"));
21+
assertTrue(content.contains("callsTransformed: 3"));
22+
assertTrue(content.contains("io/github/bpj/smoke/GreetingService.java -> 1 call(s)"));
23+
assertTrue(content.contains("io/github/bpj/smoke/CheckoutService.java -> 1 call(s)"));
24+
assertTrue(content.contains("io/github/bpj/smoke/WelcomeNotifier.java -> 1 call(s)"));
25+
}
26+
}

docs/MAVEN_PLUGIN.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ Default: `false`
114114

115115
Logs transformed files and replacement counts.
116116

117+
### `writeReport`
118+
119+
Default: `false`
120+
121+
When enabled, the plugin writes a per-file transformation report.
122+
123+
### `reportFile`
124+
125+
Default: `${project.build.directory}/reports/bpj-transform-report.txt`
126+
127+
Destination file for the transformation report when `writeReport=true`.
128+
117129
## Configuration
118130

119131
```xml
@@ -133,6 +145,7 @@ Logs transformed files and replacement counts.
133145
<configuration>
134146
<verbose>true</verbose>
135147
<failOnUnresolved>true</failOnUnresolved>
148+
<writeReport>true</writeReport>
136149
</configuration>
137150
</plugin>
138151
</plugins>

0 commit comments

Comments
 (0)