builder:CheckstyeWriter: restructure and write file with default options

Flip if block inside-out and add some logging
This commit is contained in:
Paul Campbell 2017-01-06 21:57:59 +00:00
parent ab0a95e395
commit 13e813b558

View file

@ -27,6 +27,7 @@ package net.kemitix.checkstyle.ruleset.builder;
import com.speedment.common.mapstream.MapStream; import com.speedment.common.mapstream.MapStream;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val; import lombok.val;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -34,7 +35,6 @@ import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -45,6 +45,7 @@ import java.util.stream.Stream;
* *
* @author Paul Campbell (pcampbell@kemitix.net) * @author Paul Campbell (pcampbell@kemitix.net)
*/ */
@Slf4j
@Component @Component
@RequiredArgsConstructor @RequiredArgsConstructor
class CheckstyleWriter implements CommandLineRunner { class CheckstyleWriter implements CommandLineRunner {
@ -82,19 +83,18 @@ class CheckstyleWriter implements CommandLineRunner {
.filter(rule -> ruleLevel.compareTo(rule.getLevel()) >= 0) .filter(rule -> ruleLevel.compareTo(rule.getLevel()) >= 0)
.map(this::formatRuleAsModule) .map(this::formatRuleAsModule)
.collect(Collectors.joining(NEWLINE)); .collect(Collectors.joining(NEWLINE));
try { try {
val checkstyleXmlTemplate = templateProperties.getCheckstyleXml(); val checkstyleXmlTemplate = templateProperties.getCheckstyleXml();
if (!checkstyleXmlTemplate.toFile() if (checkstyleXmlTemplate.toFile()
.exists()) { .exists()) {
throw new IOException(checkstyleXmlTemplate.toString()); val template = new String(Files.readAllBytes(checkstyleXmlTemplate));
val output = Arrays.asList(String.format(template, checkerRules, treeWalkerRules)
.split(NEWLINE));
log.info("Writing xmlFile: {}", xmlFile);
Files.write(xmlFile, output, StandardCharsets.UTF_8);
} else {
throw new IOException("Missing template: " + checkstyleXmlTemplate.toString());
} }
val bytes = Files.readAllBytes(checkstyleXmlTemplate);
val template = new String(bytes);
val output = Arrays.asList(String.format(template, checkerRules, treeWalkerRules)
.split(NEWLINE));
Files.write(xmlFile, output, StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }