Skip to content

Commit 866279b

Browse files
Merge pull request #37 from chriswininger/add-json-report
Add Json Report
2 parents eb56987 + b2ef08b commit 866279b

File tree

8 files changed

+191
-57
lines changed

8 files changed

+191
-57
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@
153153
</exclusion>
154154
</exclusions>
155155
</dependency>
156+
157+
<dependency>
158+
<groupId>com.fasterxml.jackson.core</groupId>
159+
<artifactId>jackson-databind</artifactId>
160+
<version>2.13.2.2</version>
161+
</dependency>
156162
<!--
157163
<dependency>
158164
<groupId>com.github.mauricioaniche</groupId>

refactor-first-maven-plugin/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
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+
</dependency>
3439
</dependencies>
3540

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

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

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package org.hjug.mavenreport;
22

3-
import java.io.BufferedWriter;
3+
import static org.hjug.mavenreport.ReportWriter.writeReportToDisk;
4+
45
import java.io.File;
5-
import java.io.FileWriter;
6-
import java.io.IOException;
76
import java.time.Instant;
87
import java.time.ZoneId;
98
import java.time.format.DateTimeFormatter;
@@ -101,7 +100,7 @@ public void execute() {
101100
.append(projectVersion)
102101
.append(". ");
103102
contentBuilder.append("Please initialize a Git repository and perform an initial commit.");
104-
writeReportToDisk(filename, contentBuilder);
103+
writeReportToDisk(project, filename, contentBuilder);
105104
return;
106105
}
107106

@@ -133,7 +132,7 @@ public void execute() {
133132
.append(" has no God classes!");
134133
log.info("Done! No God classes found!");
135134

136-
writeReportToDisk(filename, contentBuilder);
135+
writeReportToDisk(project, filename, contentBuilder);
137136
return;
138137
}
139138

@@ -153,7 +152,7 @@ public void execute() {
153152

154153
log.info(contentBuilder.toString());
155154

156-
writeReportToDisk(filename, contentBuilder);
155+
writeReportToDisk(project, filename, contentBuilder);
157156
}
158157

159158
private DateTimeFormatter createFileDateTimeFormatter() {
@@ -238,28 +237,4 @@ private void addsRow(StringBuilder contentBuilder, String[] rankedDisharmonyData
238237
contentBuilder.append(rowData).append(",");
239238
}
240239
}
241-
242-
private void writeReportToDisk(String filename, StringBuilder stringBuilder) {
243-
String reportOutputDirectory = project.getModel().getReporting().getOutputDirectory();
244-
File reportOutputDir = new File(reportOutputDirectory);
245-
if (!reportOutputDir.exists()) {
246-
reportOutputDir.mkdirs();
247-
}
248-
String pathname = reportOutputDirectory + File.separator + filename;
249-
250-
File reportFile = new File(pathname);
251-
try {
252-
reportFile.createNewFile();
253-
} catch (IOException e) {
254-
log.error("Failure creating chart script file", e);
255-
}
256-
257-
try (BufferedWriter writer = new BufferedWriter(new FileWriter(reportFile))) {
258-
writer.write(stringBuilder.toString());
259-
} catch (IOException e) {
260-
log.error("Error writing chart script file", e);
261-
}
262-
263-
log.info("Done! View the report at target/site/{}", filename);
264-
}
265240
}

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
@@ -1,5 +1,7 @@
11
package org.hjug.mavenreport;
22

3+
import static org.hjug.mavenreport.ReportWriter.writeReportToDisk;
4+
35
import java.io.BufferedWriter;
46
import java.io.File;
57
import java.io.FileWriter;
@@ -212,7 +214,7 @@ public void execute() {
212214
.append(". ");
213215
stringBuilder.append("Please initialize a Git repository and perform an initial commit.");
214216
stringBuilder.append(THE_END);
215-
writeReportToDisk(filename, stringBuilder);
217+
writeReportToDisk(project, filename, stringBuilder);
216218
return;
217219
}
218220

@@ -243,7 +245,7 @@ public void execute() {
243245
.append(" has no God classes!");
244246
log.info("Done! No God classes found!");
245247
stringBuilder.append(THE_END);
246-
writeReportToDisk(filename, stringBuilder);
248+
writeReportToDisk(project, filename, stringBuilder);
247249
return;
248250
}
249251

@@ -310,31 +312,7 @@ public void execute() {
310312

311313
log.info(stringBuilder.toString());
312314

313-
writeReportToDisk(filename, stringBuilder);
314-
}
315-
316-
private void writeReportToDisk(String filename, StringBuilder stringBuilder) {
317-
String reportOutputDirectory = project.getModel().getReporting().getOutputDirectory();
318-
File reportOutputDir = new File(reportOutputDirectory);
319-
if (!reportOutputDir.exists()) {
320-
reportOutputDir.mkdirs();
321-
}
322-
String pathname = reportOutputDirectory + File.separator + "refactor-first-report.html";
323-
324-
File reportFile = new File(pathname);
325-
try {
326-
reportFile.createNewFile();
327-
} catch (IOException e) {
328-
log.error("Failure creating chart script file", e);
329-
}
330-
331-
try (BufferedWriter writer = new BufferedWriter(new FileWriter(reportFile))) {
332-
writer.write(stringBuilder.toString());
333-
} catch (IOException e) {
334-
log.error("Error writing chart script file", e);
335-
}
336-
337-
log.info("Done! View the report at target/site/{}", filename);
315+
writeReportToDisk(project, filename, stringBuilder);
338316
}
339317

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

0 commit comments

Comments
 (0)