7: Create Formatters
This commit is contained in:
@@ -6,7 +6,7 @@ plugins {
|
|||||||
group 'com.bartek'
|
group 'com.bartek'
|
||||||
version '1.0-SNAPSHOT'
|
version '1.0-SNAPSHOT'
|
||||||
|
|
||||||
sourceCompatibility = 1.8
|
sourceCompatibility = 1.12
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
@@ -22,6 +22,8 @@ dependencies {
|
|||||||
compile "io.vavr:vavr:${vavrVersion}"
|
compile "io.vavr:vavr:${vavrVersion}"
|
||||||
compile "com.github.javaparser:javaparser-core:${javaParserVersion}"
|
compile "com.github.javaparser:javaparser-core:${javaParserVersion}"
|
||||||
compile "commons-io:commons-io:${commonsIoVersion}"
|
compile "commons-io:commons-io:${commonsIoVersion}"
|
||||||
|
compile "org.fusesource.jansi:jansi:${jansiVersion}"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
jar {
|
jar {
|
||||||
|
|||||||
@@ -5,4 +5,5 @@ ext {
|
|||||||
vavrVersion = '1.0.0-alpha-2'
|
vavrVersion = '1.0.0-alpha-2'
|
||||||
javaParserVersion = '3.13.4'
|
javaParserVersion = '3.13.4'
|
||||||
commonsIoVersion = '2.6'
|
commonsIoVersion = '2.6'
|
||||||
|
jansiVersion = '1.17.1'
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.bartek.esa.formatter.archetype;
|
||||||
|
|
||||||
|
import com.bartek.esa.core.model.object.Issue;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface Formatter {
|
||||||
|
void format(List<Issue> issues);
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.bartek.esa.formatter.di;
|
||||||
|
|
||||||
|
import com.bartek.esa.core.desc.provider.DescriptionProvider;
|
||||||
|
import com.bartek.esa.formatter.formatter.ColorFormatter;
|
||||||
|
import com.bartek.esa.formatter.formatter.SimpleFormatter;
|
||||||
|
import dagger.Module;
|
||||||
|
import dagger.Provides;
|
||||||
|
|
||||||
|
@Module
|
||||||
|
public class FormatterModule {
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
public SimpleFormatter simpleFormatter(DescriptionProvider descriptionProvider) {
|
||||||
|
return new SimpleFormatter(descriptionProvider);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
public ColorFormatter colorFormatter(DescriptionProvider descriptionProvider) {
|
||||||
|
return new ColorFormatter(descriptionProvider);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package com.bartek.esa.formatter.formatter;
|
||||||
|
|
||||||
|
import com.bartek.esa.core.desc.provider.DescriptionProvider;
|
||||||
|
import com.bartek.esa.core.model.object.Issue;
|
||||||
|
import com.bartek.esa.formatter.archetype.Formatter;
|
||||||
|
import org.fusesource.jansi.Ansi;
|
||||||
|
import org.fusesource.jansi.AnsiConsole;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static org.fusesource.jansi.Ansi.Color.*;
|
||||||
|
import static org.fusesource.jansi.Ansi.ansi;
|
||||||
|
|
||||||
|
public class ColorFormatter implements Formatter {
|
||||||
|
|
||||||
|
private final DescriptionProvider descriptionProvider;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public ColorFormatter(DescriptionProvider descriptionProvider) {
|
||||||
|
this.descriptionProvider = descriptionProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void format(List<Issue> issues) {
|
||||||
|
AnsiConsole.systemInstall();
|
||||||
|
String format = issues.stream()
|
||||||
|
.map(this::format)
|
||||||
|
.collect(Collectors.joining());
|
||||||
|
|
||||||
|
System.out.println(format.substring(0, format.length() - 2));
|
||||||
|
AnsiConsole.systemUninstall();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String format(Issue issue) {
|
||||||
|
Ansi ansi = ansi();
|
||||||
|
ansi = appendDescription(issue, ansi);
|
||||||
|
ansi = appendFile(issue, ansi);
|
||||||
|
ansi = appendLine(issue, ansi);
|
||||||
|
ansi.a("\n");
|
||||||
|
|
||||||
|
return ansi.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Ansi appendDescription(Issue issue, Ansi ansi) {
|
||||||
|
String description = descriptionProvider.getDescriptionForIssue(issue);
|
||||||
|
|
||||||
|
return ansi
|
||||||
|
.fg(getColorForSeverity(issue))
|
||||||
|
.a(issue.getSeverity().name())
|
||||||
|
.reset()
|
||||||
|
.a(": ").a(description)
|
||||||
|
.a("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
private Ansi appendFile(Issue issue, Ansi ansi) {
|
||||||
|
return ansi
|
||||||
|
.fg(GREEN)
|
||||||
|
.a("File: ")
|
||||||
|
.reset()
|
||||||
|
.a(issue.getFile().getAbsolutePath())
|
||||||
|
.a("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
private Ansi appendLine(Issue issue, Ansi ansi) {
|
||||||
|
Optional.ofNullable(issue.getLine())
|
||||||
|
.ifPresent(line -> {
|
||||||
|
ansi
|
||||||
|
.fg(BLUE)
|
||||||
|
.a("Line");
|
||||||
|
Optional.ofNullable(issue.getLineNumber()).ifPresentOrElse(
|
||||||
|
number -> ansi.a(" ").a(number).a(": "),
|
||||||
|
() -> ansi.a(": ")
|
||||||
|
);
|
||||||
|
ansi.reset().a(line).a("\n");
|
||||||
|
});
|
||||||
|
return ansi;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Ansi.Color getColorForSeverity(Issue issue) {
|
||||||
|
switch(issue.getSeverity()) {
|
||||||
|
case WARNING: return YELLOW;
|
||||||
|
case ERROR: return RED;
|
||||||
|
}
|
||||||
|
|
||||||
|
return RED;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package com.bartek.esa.formatter.formatter;
|
||||||
|
|
||||||
|
import com.bartek.esa.core.desc.provider.DescriptionProvider;
|
||||||
|
import com.bartek.esa.core.model.object.Issue;
|
||||||
|
import com.bartek.esa.formatter.archetype.Formatter;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class SimpleFormatter implements Formatter {
|
||||||
|
private final DescriptionProvider descriptionProvider;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public SimpleFormatter(DescriptionProvider descriptionProvider) {
|
||||||
|
this.descriptionProvider = descriptionProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void format(List<Issue> issues) {
|
||||||
|
String format = issues.stream()
|
||||||
|
.map(this::format)
|
||||||
|
.collect(Collectors.joining());
|
||||||
|
|
||||||
|
System.out.println(format.substring(0, format.length() - 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String format(Issue issue) {
|
||||||
|
StringBuilder format = new StringBuilder();
|
||||||
|
appendDescription(issue, format);
|
||||||
|
appendFile(issue, format);
|
||||||
|
appendLine(issue, format);
|
||||||
|
format.append("\n");
|
||||||
|
|
||||||
|
return format.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void appendDescription(Issue issue, StringBuilder format) {
|
||||||
|
String description = descriptionProvider.getDescriptionForIssue(issue);
|
||||||
|
format
|
||||||
|
.append(issue.getSeverity().name())
|
||||||
|
.append(": ").append(description)
|
||||||
|
.append("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void appendFile(Issue issue, StringBuilder format) {
|
||||||
|
format
|
||||||
|
.append("File: ")
|
||||||
|
.append(issue.getFile().getAbsolutePath())
|
||||||
|
.append("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void appendLine(Issue issue, StringBuilder format) {
|
||||||
|
Optional.ofNullable(issue.getLine())
|
||||||
|
.ifPresent(line -> {
|
||||||
|
format.append("Line");
|
||||||
|
Optional.ofNullable(issue.getLineNumber()).ifPresentOrElse(
|
||||||
|
number -> format.append(" ").append(number).append(": "),
|
||||||
|
() -> format.append(": ")
|
||||||
|
);
|
||||||
|
format.append(line).append("\n");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user