builder:ReadmeIndexBuilder: refactored out from ReadmeWriter
This commit is contained in:
parent
f6ee87e6dc
commit
c46668d1f1
3 changed files with 97 additions and 31 deletions
|
@ -0,0 +1,78 @@
|
||||||
|
package net.kemitix.checkstyle.ruleset.builder;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Rule Index Builder.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (paul.campbell@hubio.com)
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DefaultReadmeIndexBuilder implements ReadmeIndexBuilder {
|
||||||
|
|
||||||
|
private static final String NEWLINE = "\n";
|
||||||
|
|
||||||
|
private static final String HEADERROW = "Rule|Level|Source|Enabled|Suppressable\n";
|
||||||
|
|
||||||
|
private static final String SEPARATOR = "----|-----|------|-------|------------\n";
|
||||||
|
|
||||||
|
private static final Locale LOCALE = Locale.ENGLISH;
|
||||||
|
|
||||||
|
private final RulesProperties rulesProperties;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String build() {
|
||||||
|
return HEADERROW + SEPARATOR + rulesProperties.getRules()
|
||||||
|
.stream()
|
||||||
|
.sorted(Comparator.comparing(lowerCaseRuleName()))
|
||||||
|
.map(this::formatRuleRow)
|
||||||
|
.collect(Collectors.joining(NEWLINE));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Function<Rule, String> lowerCaseRuleName() {
|
||||||
|
return this::getLowerCaseRuleName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getLowerCaseRuleName(final Rule rule) {
|
||||||
|
return rule.getName()
|
||||||
|
.toLowerCase(LOCALE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String formatRuleRow(final Rule rule) {
|
||||||
|
return String.format(
|
||||||
|
"%s|%s|%s|%s|%s", link(rule), level(rule), source(rule), enabled(rule), suppressible(rule));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String suppressible(final Rule rule) {
|
||||||
|
return rule.isInsuppressible() ? "No" : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String enabled(final Rule rule) {
|
||||||
|
return rule.isEnabled() ? "Yes" : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String source(final Rule rule) {
|
||||||
|
return rule.getSource()
|
||||||
|
.toString()
|
||||||
|
.toLowerCase(LOCALE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String link(final Rule rule) {
|
||||||
|
return String.format("[%s](#%s)", rule.getName(), getLowerCaseRuleName(rule));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String level(final Rule rule) {
|
||||||
|
return Optional.ofNullable(rule.getLevel())
|
||||||
|
.orElse(RuleLevel.UNSPECIFIED)
|
||||||
|
.toString()
|
||||||
|
.toLowerCase(LOCALE);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package net.kemitix.checkstyle.ruleset.builder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the Rule Index for README.md in Markdown Format.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (paul.campbell@hubio.com)
|
||||||
|
*/
|
||||||
|
public interface ReadmeIndexBuilder {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds the Rule Index in Markdown Format.
|
||||||
|
*
|
||||||
|
* @return The rule index.
|
||||||
|
*/
|
||||||
|
String build();
|
||||||
|
}
|
|
@ -33,9 +33,6 @@ import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@ -58,48 +55,23 @@ class ReadmeWriter implements CommandLineRunner {
|
||||||
|
|
||||||
private final RuleReadmeLoader ruleReadmeLoader;
|
private final RuleReadmeLoader ruleReadmeLoader;
|
||||||
|
|
||||||
|
private final ReadmeIndexBuilder indexBuilder;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(final String... args) throws Exception {
|
public void run(final String... args) throws Exception {
|
||||||
final String readmeTemplate = readFile(templateProperties.getReadmeTemplate());
|
final String readmeTemplate = readFile(templateProperties.getReadmeTemplate());
|
||||||
final String indexHeader =
|
|
||||||
"Rule|Level|Source|Enabled|Suppressable\n" + "----|-----|------|-------|------------\n";
|
|
||||||
final String index = ruleIndex();
|
|
||||||
final String enabledCheckstyle = readmeRules(this::isEnabledCheckstyleRule);
|
final String enabledCheckstyle = readmeRules(this::isEnabledCheckstyleRule);
|
||||||
final String enabledSevntu = readmeRules(this::isEnabledSevntuRule);
|
final String enabledSevntu = readmeRules(this::isEnabledSevntuRule);
|
||||||
final String disabledCheckstyle = readmeRules(this::isDisabledCheckstyleRule);
|
final String disabledCheckstyle = readmeRules(this::isDisabledCheckstyleRule);
|
||||||
final String disabledSevntu = readmeRules(this::isDisabledSevntuRule);
|
final String disabledSevntu = readmeRules(this::isDisabledSevntuRule);
|
||||||
final byte[] readme =
|
final byte[] readme =
|
||||||
String.format(readmeTemplate, indexHeader + index, enabledCheckstyle, enabledSevntu, disabledCheckstyle,
|
String.format(readmeTemplate, indexBuilder.build(), enabledCheckstyle, enabledSevntu, disabledCheckstyle,
|
||||||
disabledSevntu
|
disabledSevntu
|
||||||
)
|
)
|
||||||
.getBytes(StandardCharsets.UTF_8);
|
.getBytes(StandardCharsets.UTF_8);
|
||||||
Files.write(outputProperties.getReadme(), readme, StandardOpenOption.TRUNCATE_EXISTING);
|
Files.write(outputProperties.getReadme(), readme, StandardOpenOption.TRUNCATE_EXISTING);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String ruleIndex() {
|
|
||||||
return rulesProperties.getRules()
|
|
||||||
.stream()
|
|
||||||
.sorted(Comparator.comparing(rule -> rule.getName()
|
|
||||||
.toLowerCase(Locale.ENGLISH)))
|
|
||||||
.map(this::formatRuleIndex)
|
|
||||||
.collect(Collectors.joining(NEWLINE));
|
|
||||||
}
|
|
||||||
|
|
||||||
private String formatRuleIndex(final Rule rule) {
|
|
||||||
final String ruleLink = rule.getName()
|
|
||||||
.toLowerCase(Locale.ENGLISH);
|
|
||||||
final String level = Optional.ofNullable(rule.getLevel())
|
|
||||||
.orElse(RuleLevel.UNSPECIFIED)
|
|
||||||
.toString()
|
|
||||||
.toLowerCase(Locale.ENGLISH);
|
|
||||||
final String source = rule.getSource()
|
|
||||||
.toString()
|
|
||||||
.toLowerCase(Locale.ENGLISH);
|
|
||||||
final String enabled = rule.isEnabled() ? "Yes" : "";
|
|
||||||
final String insuppressible = rule.isInsuppressible() ? "No" : "";
|
|
||||||
return String.format("[%s](#%s)|%s|%s|%s|%s", rule.getName(), ruleLink, level, source, enabled, insuppressible);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isEnabledSevntuRule(final Rule rule) {
|
private boolean isEnabledSevntuRule(final Rule rule) {
|
||||||
return rule.isEnabled() && RuleSource.SEVNTU.equals(rule.getSource());
|
return rule.isEnabled() && RuleSource.SEVNTU.equals(rule.getSource());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue