From fb02dc5f8836018173fe29df363de59ff28d7758 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 5 Jan 2017 16:31:27 +0000 Subject: [PATCH] builder:RuleReadmeLoader: added rule documentation fragment loader --- .../builder/DefaultRuleReadmeLoader.java | 36 +++++++++++++++++++ .../ruleset/builder/RuleReadmeLoader.java | 21 +++++++++++ 2 files changed, 57 insertions(+) create mode 100644 builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleReadmeLoader.java create mode 100644 builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleReadmeLoader.java diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleReadmeLoader.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleReadmeLoader.java new file mode 100644 index 0000000..fb8c2e0 --- /dev/null +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultRuleReadmeLoader.java @@ -0,0 +1,36 @@ +package net.kemitix.checkstyle.ruleset.builder; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.stream.Stream; + +/** + * Default README fragment loader. + * + * @author Paul Campbell (paul.campbell@hubio.com) + */ +@Slf4j +@Component +@RequiredArgsConstructor +class DefaultRuleReadmeLoader implements RuleReadmeLoader { + + private final TemplateProperties templateProperties; + + @Override + public Stream load(final Rule rule) { + try { + final Path resolve = templateProperties.getReadmeFragments() + .resolve(rule.getName() + ".md"); + log.info("Loading fragment: {}", resolve); + return Stream.concat(Stream.of(String.format("%n#### [%s](%s)", rule.getName(), rule.getUri())), + Files.lines(resolve)); + } catch (IOException e) { + return Stream.empty(); + } + } +} diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleReadmeLoader.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleReadmeLoader.java new file mode 100644 index 0000000..85dd161 --- /dev/null +++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/RuleReadmeLoader.java @@ -0,0 +1,21 @@ +package net.kemitix.checkstyle.ruleset.builder; + +import java.util.stream.Stream; + +/** + * Loads README fragment for rule. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +@FunctionalInterface +interface RuleReadmeLoader { + + /** + * Loads the README fragment for rule. + * + * @param rule The Rule. + * + * @return A Stream of the readme fragment. + */ + Stream load(Rule rule); +}