diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultReadmeIndexBuilder.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultReadmeIndexBuilder.java new file mode 100644 index 0000000..1c018f6 --- /dev/null +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultReadmeIndexBuilder.java @@ -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 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); + } +} diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/ReadmeIndexBuilder.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/ReadmeIndexBuilder.java new file mode 100644 index 0000000..004cccd --- /dev/null +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/ReadmeIndexBuilder.java @@ -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(); +} diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriter.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriter.java index 4a4a273..673f313 100644 --- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriter.java +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/ReadmeWriter.java @@ -33,9 +33,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; 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.stream.Collectors; @@ -58,48 +55,23 @@ class ReadmeWriter implements CommandLineRunner { private final RuleReadmeLoader ruleReadmeLoader; + private final ReadmeIndexBuilder indexBuilder; + @Override public void run(final String... args) throws Exception { 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 enabledSevntu = readmeRules(this::isEnabledSevntuRule); final String disabledCheckstyle = readmeRules(this::isDisabledCheckstyleRule); final String disabledSevntu = readmeRules(this::isDisabledSevntuRule); final byte[] readme = - String.format(readmeTemplate, indexHeader + index, enabledCheckstyle, enabledSevntu, disabledCheckstyle, + String.format(readmeTemplate, indexBuilder.build(), enabledCheckstyle, enabledSevntu, disabledCheckstyle, disabledSevntu ) .getBytes(StandardCharsets.UTF_8); 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) { return rule.isEnabled() && RuleSource.SEVNTU.equals(rule.getSource()); }