Skip to content

Commit a92c354

Browse files
committed
Add Json Report
This adds the ability to generate a json version of the report using :jsonreport
1 parent c1962e3 commit a92c354

File tree

7 files changed

+192
-54
lines changed

7 files changed

+192
-54
lines changed

refactor-first-maven-plugin/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
<version>3.6.1</version>
3232
<scope>provided</scope>
3333
</dependency>
34+
35+
<dependency>
36+
<groupId>com.fasterxml.jackson.core</groupId>
37+
<artifactId>jackson-databind</artifactId>
38+
<version>2.13.2.2</version>
39+
</dependency>
3440
</dependencies>
3541

3642
<build>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.hjug.mavenreport.RefactorFirstJsonReport;
2+
3+
import java.util.List;
4+
5+
import lombok.Builder;
6+
import lombok.Data;
7+
8+
@Data
9+
@Builder
10+
class JsonReport
11+
{
12+
private List<JsonReportDisharmonyEntry> rankedDisharmonies;
13+
private List<String> errors;
14+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.hjug.mavenreport.RefactorFirstJsonReport;
2+
3+
import java.time.ZoneId;
4+
import java.time.format.DateTimeFormatter;
5+
import java.time.format.FormatStyle;
6+
import java.util.Locale;
7+
8+
import lombok.Builder;
9+
import lombok.Data;
10+
import org.hjug.cbc.RankedDisharmony;
11+
12+
@Data
13+
@Builder
14+
class JsonReportDisharmonyEntry
15+
{
16+
private static final DateTimeFormatter formatter =
17+
DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT )
18+
.withLocale( Locale.getDefault() )
19+
.withZone( ZoneId.systemDefault() );
20+
21+
private final String className;
22+
private final Integer effortRank;
23+
private final Integer changePronenessRank;
24+
private final Integer priority;
25+
private final Integer weightedMethodCount;
26+
private final Integer commitCount;
27+
private final String mostRecentCommitTime;
28+
29+
public static JsonReportDisharmonyEntry fromRankedDisharmony(RankedDisharmony entry) {
30+
return JsonReportDisharmonyEntry.builder()
31+
.className(entry.getClassName())
32+
.effortRank(entry.getEffortRank())
33+
.changePronenessRank(entry.getChangePronenessRank())
34+
.priority(entry.getPriority())
35+
.weightedMethodCount(entry.getWmc())
36+
.commitCount(entry.getCommitCount())
37+
.mostRecentCommitTime(formatter.format(entry.getMostRecentCommitTime()))
38+
.build();
39+
}
40+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.hjug.mavenreport.RefactorFirstJsonReport;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
import com.fasterxml.jackson.core.JsonProcessingException;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import lombok.extern.slf4j.Slf4j;
11+
import org.apache.maven.plugin.AbstractMojo;
12+
import org.apache.maven.plugins.annotations.LifecyclePhase;
13+
import org.apache.maven.plugins.annotations.Mojo;
14+
import org.apache.maven.plugins.annotations.Parameter;
15+
import org.apache.maven.plugins.annotations.ResolutionScope;
16+
import org.apache.maven.project.MavenProject;
17+
import org.hjug.cbc.CostBenefitCalculator;
18+
import org.hjug.cbc.RankedDisharmony;
19+
20+
import static org.hjug.mavenreport.ReportWriter.writeReportToDisk;
21+
22+
@Slf4j
23+
@Mojo(
24+
name = "jsonreport",
25+
defaultPhase = LifecyclePhase.SITE,
26+
requiresDependencyResolution = ResolutionScope.RUNTIME,
27+
requiresProject = true,
28+
threadSafe = true,
29+
inheritByDefault = false
30+
)
31+
public class RefactorFirstMavenJsonReport extends AbstractMojo {
32+
private static final String FILE_NAME = "refactor-first-data.json";
33+
34+
private static final ObjectMapper MAPPER = new ObjectMapper();
35+
36+
@Parameter(readonly = true, defaultValue = "${project}")
37+
private MavenProject project;
38+
39+
@Override
40+
public void execute() {
41+
final String projectBaseDir = project.getBasedir().getPath();
42+
43+
final CostBenefitCalculator costBenefitCalculator = new CostBenefitCalculator();
44+
final List<RankedDisharmony> rankedDisharmonies = costBenefitCalculator.calculateCostBenefitValues(projectBaseDir);
45+
final List<JsonReportDisharmonyEntry> disharmonyEntries = rankedDisharmonies.stream()
46+
.map(JsonReportDisharmonyEntry::fromRankedDisharmony)
47+
.collect(Collectors.toList());
48+
49+
final JsonReport report = JsonReport
50+
.builder()
51+
.rankedDisharmonies(disharmonyEntries)
52+
.build();
53+
54+
try {
55+
final String reportJson = MAPPER.writeValueAsString(report);
56+
57+
writeReportToDisk(project, FILE_NAME, new StringBuilder(reportJson));
58+
} catch (final JsonProcessingException jsonProcessingException) {
59+
final String errorMessage = "Could not generate a json report: " + jsonProcessingException;
60+
61+
log.error(errorMessage);
62+
final JsonReport errorReport = JsonReport.builder()
63+
.errors(new ArrayList<>(Collections.singletonList(errorMessage)))
64+
.build();
65+
66+
writeErrorReport(errorReport);
67+
}
68+
}
69+
70+
private void writeErrorReport(final JsonReport errorReport) {
71+
try {
72+
writeReportToDisk(project, FILE_NAME, new StringBuilder(MAPPER.writeValueAsString(errorReport)));
73+
} catch (final JsonProcessingException jsonProcessingException) {
74+
log.error("failed to write error report: ", jsonProcessingException);
75+
}
76+
77+
}
78+
}

refactor-first-maven-plugin/src/main/java/org/hjug/mavenreport/RefactorFirstMavenCsvReport.java

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.Locale;
2525
import java.util.Optional;
2626

27+
import static org.hjug.mavenreport.ReportWriter.writeReportToDisk;
28+
2729
@Slf4j
2830
@Mojo(
2931
name = "csvreport",
@@ -100,7 +102,7 @@ public void execute() {
100102
.append(projectName).append(" ")
101103
.append(projectVersion).append(". ");
102104
contentBuilder.append("Please initialize a Git repository and perform an initial commit.");
103-
writeReportToDisk(filename, contentBuilder);
105+
writeReportToDisk(project, filename, contentBuilder);
104106
return;
105107
}
106108

@@ -129,7 +131,7 @@ public void execute() {
129131
.append(projectVersion).append(" has no God classes!");
130132
log.info("Done! No God classes found!");
131133

132-
writeReportToDisk(filename, contentBuilder);
134+
writeReportToDisk(project, filename, contentBuilder);
133135
return;
134136
}
135137

@@ -149,7 +151,7 @@ public void execute() {
149151

150152
log.info(contentBuilder.toString());
151153

152-
writeReportToDisk(filename, contentBuilder);
154+
writeReportToDisk(project, filename, contentBuilder);
153155
}
154156

155157
private DateTimeFormatter createFileDateTimeFormatter() {
@@ -230,28 +232,4 @@ private void addsRow(StringBuilder contentBuilder, String[] rankedDisharmonyData
230232
contentBuilder.append(rowData).append(",");
231233
}
232234
}
233-
234-
private void writeReportToDisk(String filename, StringBuilder stringBuilder) {
235-
String reportOutputDirectory = project.getModel().getReporting().getOutputDirectory();
236-
File reportOutputDir = new File(reportOutputDirectory);
237-
if(!reportOutputDir.exists()) {
238-
reportOutputDir.mkdirs();
239-
}
240-
String pathname = reportOutputDirectory + File.separator + filename;
241-
242-
File reportFile = new File(pathname);
243-
try {
244-
reportFile.createNewFile();
245-
} catch (IOException e) {
246-
log.error("Failure creating chart script file", e);
247-
}
248-
249-
try(BufferedWriter writer = new BufferedWriter(new FileWriter(reportFile))) {
250-
writer.write(stringBuilder.toString());
251-
} catch (IOException e) {
252-
log.error("Error writing chart script file", e);
253-
}
254-
255-
log.info("Done! View the report at target/site/{}", filename);
256-
}
257235
}

refactor-first-maven-plugin/src/main/java/org/hjug/mavenreport/RefactorFirstMavenReport.java

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.Locale;
2626
import java.util.Optional;
2727

28+
import static org.hjug.mavenreport.ReportWriter.writeReportToDisk;
29+
2830
@Slf4j
2931
@Mojo(
3032
name = "report",
@@ -206,7 +208,7 @@ public void execute() {
206208
stringBuilder.append("No Git repository found in project ").append(projectName).append(" ").append(projectVersion).append(". ");
207209
stringBuilder.append("Please initialize a Git repository and perform an initial commit.");
208210
stringBuilder.append(THE_END);
209-
writeReportToDisk(filename, stringBuilder);
211+
writeReportToDisk(project, filename, stringBuilder);
210212
return;
211213
}
212214

@@ -234,7 +236,7 @@ public void execute() {
234236
.append(projectVersion).append(" has no God classes!");
235237
log.info("Done! No God classes found!");
236238
stringBuilder.append(THE_END);
237-
writeReportToDisk(filename, stringBuilder);
239+
writeReportToDisk(project, filename, stringBuilder);
238240
return;
239241
}
240242

@@ -299,31 +301,7 @@ public void execute() {
299301

300302
log.info(stringBuilder.toString());
301303

302-
writeReportToDisk(filename, stringBuilder);
303-
}
304-
305-
private void writeReportToDisk(String filename, StringBuilder stringBuilder) {
306-
String reportOutputDirectory = project.getModel().getReporting().getOutputDirectory();
307-
File reportOutputDir = new File(reportOutputDirectory);
308-
if(!reportOutputDir.exists()) {
309-
reportOutputDir.mkdirs();
310-
}
311-
String pathname = reportOutputDirectory + File.separator + "refactor-first-report.html";
312-
313-
File reportFile = new File(pathname);
314-
try {
315-
reportFile.createNewFile();
316-
} catch (IOException e) {
317-
log.error("Failure creating chart script file", e);
318-
}
319-
320-
try(BufferedWriter writer = new BufferedWriter(new FileWriter(reportFile))) {
321-
writer.write(stringBuilder.toString());
322-
} catch (IOException e) {
323-
log.error("Error writing chart script file", e);
324-
}
325-
326-
log.info("Done! View the report at target/site/{}", filename);
304+
writeReportToDisk(project, filename, stringBuilder);
327305
}
328306

329307
//TODO: Move to another class to allow use by Gradle plugin
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.hjug.mavenreport;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.File;
5+
import java.io.FileWriter;
6+
import java.io.IOException;
7+
8+
import lombok.extern.slf4j.Slf4j;
9+
import org.apache.maven.project.MavenProject;
10+
11+
@Slf4j
12+
public class ReportWriter
13+
{
14+
public static void writeReportToDisk(
15+
final MavenProject project,
16+
final String filename,
17+
final StringBuilder stringBuilder
18+
) {
19+
final String reportOutputDirectory = project.getModel().getReporting().getOutputDirectory();
20+
final File reportOutputDir = new File(reportOutputDirectory);
21+
22+
if(!reportOutputDir.exists()) {
23+
reportOutputDir.mkdirs();
24+
}
25+
26+
final String pathname = reportOutputDirectory + File.separator + filename;
27+
28+
final File reportFile = new File(pathname);
29+
30+
try {
31+
reportFile.createNewFile();
32+
} catch (IOException e) {
33+
log.error("Failure creating chart script file", e);
34+
}
35+
36+
try(BufferedWriter writer = new BufferedWriter(new FileWriter(reportFile))) {
37+
writer.write(stringBuilder.toString());
38+
} catch (IOException e) {
39+
log.error("Error writing chart script file", e);
40+
}
41+
42+
log.info("Done! View the report at target/site/{}", filename);
43+
}
44+
}

0 commit comments

Comments
 (0)