Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
charphi committed Aug 30, 2024
1 parent a99a891 commit 9d76248
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 57 deletions.
11 changes: 11 additions & 0 deletions heylogs-api/src/main/java/internal/heylogs/FlexmarkIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.vladsch.flexmark.formatter.Formatter;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Document;
import nbbrd.io.text.TextFormatter;
import nbbrd.io.text.TextParser;

public final class FlexmarkIO {

Expand All @@ -13,9 +16,17 @@ public static Parser newParser() {
return Parser.builder().build();
}

public static TextParser<Document> newTextParser() {
return TextParser.onParsingReader(newParser()::parseReader);
}

public static Formatter newFormatter() {
Formatter.Builder result = Formatter.builder();
result.set(Formatter.MAX_TRAILING_BLANK_LINES, 0);
return result.build();
}

public static TextFormatter<Document> newTextFormatter() {
return TextFormatter.onFormattingWriter(newFormatter()::render);
}
}
17 changes: 8 additions & 9 deletions heylogs-api/src/test/java/nbbrd/heylogs/FilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.time.LocalDate;
import java.util.function.Function;

import static nbbrd.heylogs.Filter.builder;
import static nbbrd.heylogs.Filter.parseLocalDate;
import static nbbrd.heylogs.Version.HYPHEN;
import static org.assertj.core.api.Assertions.*;
Expand All @@ -15,43 +14,43 @@ public class FilterTest {

@Test
public void testRef() {
assertThat(builder().build())
assertThat(Filter.builder().build())
.describedAs("Empty reference")
.is(containing(unreleased))
.is(containing(v1_1_0))
.is(containing(v1_0_0));

assertThat(builder().ref("Unreleased").build())
assertThat(Filter.builder().ref("Unreleased").build())
.describedAs("Full reference")
.is(containing(unreleased))
.isNot(containing(v1_1_0))
.isNot(containing(v1_0_0));

assertThat(builder().ref("1.1.0").build())
assertThat(Filter.builder().ref("1.1.0").build())
.describedAs("Full reference")
.isNot(containing(unreleased))
.is(containing(v1_1_0))
.isNot(containing(v1_0_0));

assertThat(builder().ref("rel").build())
assertThat(Filter.builder().ref("rel").build())
.describedAs("Partial reference")
.is(containing(unreleased))
.isNot(containing(v1_1_0))
.isNot(containing(v1_0_0));

assertThat(builder().ref("1.").build())
assertThat(Filter.builder().ref("1.").build())
.describedAs("Partial reference")
.isNot(containing(unreleased))
.is(containing(v1_1_0))
.is(containing(v1_0_0));

assertThat(builder().ref("other").build())
assertThat(Filter.builder().ref("other").build())
.describedAs("Unknown reference")
.isNot(containing(unreleased))
.isNot(containing(v1_1_0))
.isNot(containing(v1_0_0));

assertThat(builder().ref("other-SNAPSHOT").build())
assertThat(Filter.builder().ref("other-SNAPSHOT").build())
.describedAs("Matching unreleased pattern reference")
.is(containing(unreleased))
.isNot(containing(v1_1_0))
Expand All @@ -60,7 +59,7 @@ public void testRef() {

@Test
public void testTimeRange() {
Function<TimeRange, Filter> onTimeRange = o -> builder().timeRange(o).build();
Function<TimeRange, Filter> onTimeRange = o -> Filter.builder().timeRange(o).build();

assertThat(TimeRange.ALL)
.extracting(onTimeRange)
Expand Down
21 changes: 11 additions & 10 deletions heylogs-api/src/test/java/nbbrd/heylogs/HeylogsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

import static internal.heylogs.URLExtractor.urlOf;
import static java.util.Collections.singletonList;
import static nbbrd.heylogs.Filter.builder;
import static nbbrd.heylogs.Heylogs.FIRST_FORMAT_AVAILABLE;
import static nbbrd.heylogs.spi.RuleSeverity.ERROR;
import static nbbrd.io.function.IOFunction.unchecked;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.InstanceOfAssertFactories.list;
import static tests.heylogs.api.Sample.using;
Expand Down Expand Up @@ -62,13 +62,9 @@ public void testCheckFormat() {
public void testExtractVersions() {
Heylogs x = Heylogs.ofServiceLoader();

Function<Filter, String> usingMain = extractor -> {
Document doc = using("/Main.md");
x.extractVersions(doc, extractor);
return FlexmarkIO.newFormatter().render(doc);
};
Function<Filter, String> usingMain = extractor -> extractVersionsToString(x, using("/Main.md"), extractor);

assertThat(builder().ref("1.1.0").build())
assertThat(Filter.builder().ref("1.1.0").build())
.extracting(usingMain, STRING)
.isEqualTo(
"## [1.1.0] - 2019-02-15\n" +
Expand All @@ -86,14 +82,14 @@ public void testExtractVersions() {
"\n" +
"[1.1.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...v1.1.0\n");

assertThat(builder().ref("1.1.0").ignoreContent(true).build())
assertThat(Filter.builder().ref("1.1.0").ignoreContent(true).build())
.extracting(usingMain, STRING)
.isEqualTo(
"## [1.1.0] - 2019-02-15\n" +
"\n" +
"[1.1.0]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...v1.1.0\n");

assertThat(builder().ref("zzz").build())
assertThat(Filter.builder().ref("zzz").build())
.extracting(usingMain, STRING)
.isEmpty();
}
Expand Down Expand Up @@ -260,9 +256,14 @@ public void testFormatStatus() throws IOException {
);
}

private static String extractVersionsToString(Heylogs heylogs, Document doc, Filter extractor) {
heylogs.extractVersions(doc, extractor);
return unchecked(FlexmarkIO.newTextFormatter()::formatToString).apply(doc);
}

private static String releaseChangesToString(Heylogs heylogs, Document doc, Version version) {
heylogs.releaseChanges(doc, version, "v");
return FlexmarkIO.newFormatter().render(doc);
return unchecked(FlexmarkIO.newTextFormatter()::formatToString).apply(doc);
}

private static final class MockedRule implements Rule {
Expand Down
26 changes: 11 additions & 15 deletions heylogs-api/src/test/java/tests/heylogs/api/Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,32 @@
import nbbrd.io.function.IOConsumer;
import org.assertj.core.util.URLs;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.Objects;

import static java.nio.charset.StandardCharsets.UTF_8;
import static nbbrd.heylogs.spi.RuleSeverity.ERROR;
import static nbbrd.io.function.IOFunction.unchecked;

public class Sample {

public static Document using(String name) {
try (InputStream stream = Sample.class.getResourceAsStream(name)) {
if (stream == null) {
throw new IllegalArgumentException("Missing resource '" + name + "'");
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
return FlexmarkIO.newParser().parseReader(reader);
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
return unchecked((String x) -> FlexmarkIO.newTextParser().parseResource(Sample.class, x, UTF_8))
.apply(name);
}

public static Heading asHeading(String text) {
return (Heading) FlexmarkIO.newParser().parse(text).getChildOfType(Heading.class);
return unchecked(FlexmarkIO.newTextParser()::parseChars)
.andThen(doc -> (Heading) doc.getChildOfType(Heading.class))
.apply(text);
}

public static String asText(Heading heading) {
Document doc = new Document(null, BasedSequence.NULL);
doc.appendChild(heading);
return FlexmarkIO.newFormatter().render(doc).trim();
return unchecked(FlexmarkIO.newTextFormatter()::formatToString)
.andThen(String::trim)
.apply(doc);
}

public static final Problem PROBLEM1 = Problem.builder().id("rule1").severity(ERROR).issue(RuleIssue.builder().message("boom").line(5).column(18).build()).build();
Expand Down Expand Up @@ -72,6 +68,6 @@ public static String writing(IOConsumer<? super Appendable> content) {
}

public static String contentOf(Class<?> anchor, String resourceName) {
return URLs.contentOf(Objects.requireNonNull(anchor.getResource(resourceName)), StandardCharsets.UTF_8);
return URLs.contentOf(Objects.requireNonNull(anchor.getResource(resourceName)), UTF_8);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package internal.heylogs.cli;

import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Document;
import internal.heylogs.FlexmarkIO;
import lombok.AccessLevel;
Expand All @@ -10,7 +9,6 @@
import nbbrd.design.StaticFactoryMethod;

import java.io.IOException;
import java.io.Reader;
import java.nio.file.DirectoryStream;
import java.nio.file.Path;
import java.util.Locale;
Expand All @@ -26,12 +24,8 @@ public class MarkdownInputSupport extends TextInputSupport implements DirectoryS
return CommandSupporter.create(MarkdownInputSupport::new, supporters);
}

private @NonNull Parser parser = FlexmarkIO.newParser();

public Document readDocument(Path file) throws IOException {
try (Reader reader = newBufferedReader(file)) {
return parser.parseReader(reader);
}
return FlexmarkIO.newTextParser().parseReader(() -> newBufferedReader(file));
}

public String getName(Path file) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package internal.heylogs.cli;

import com.vladsch.flexmark.formatter.Formatter;
import com.vladsch.flexmark.util.ast.Document;
import internal.heylogs.FlexmarkIO;
import lombok.AccessLevel;
Expand All @@ -10,7 +9,6 @@
import nbbrd.design.StaticFactoryMethod;

import java.io.IOException;
import java.io.Writer;
import java.nio.file.Path;

@lombok.Getter
Expand All @@ -24,11 +22,7 @@ public class MarkdownOutputSupport extends TextOutputSupport {
return CommandSupporter.create(MarkdownOutputSupport::new, supporters);
}

private @NonNull Formatter formatter = FlexmarkIO.newFormatter();

public void writeDocument(Path file, Document document) throws IOException {
try (Writer writer = newBufferedWriter(file)) {
formatter.render(document, writer);
}
FlexmarkIO.newTextFormatter().formatWriter(document, () -> newBufferedWriter(file));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package nbbrd.heylogs.maven.plugin;

import com.vladsch.flexmark.formatter.Formatter;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Document;
import internal.heylogs.FlexmarkIO;
import nbbrd.design.MightBePromoted;
Expand All @@ -16,6 +14,7 @@
import java.util.function.Consumer;

import static internal.heylogs.maven.plugin.HeylogsParameters.isMojoLogFile;
import static java.nio.charset.StandardCharsets.UTF_8;
import static nbbrd.console.picocli.text.TextOutputSupport.newTextOutputSupport;

abstract class HeylogsMojo extends AbstractMojo {
Expand Down Expand Up @@ -44,22 +43,18 @@ protected void notifyMissingChangelog() {

protected Document readChangelog(File inputFile) throws MojoExecutionException {
getLog().info("Reading changelog " + inputFile);
Parser parser = FlexmarkIO.newParser();
try (Reader reader = Files.newBufferedReader(inputFile.toPath())) {
return parser.parseReader(reader);
try {
return FlexmarkIO.newTextParser().parseFile(inputFile, UTF_8);
} catch (IOException ex) {
throw new MojoExecutionException("Failed to read changelog", ex);
}
}

protected void writeChangelog(Document document, File outputFile) throws MojoExecutionException {
getLog().info("Writing changelog " + outputFile);
Formatter formatter = FlexmarkIO.newFormatter();
try {
Files.createDirectories(outputFile.getParentFile().toPath());
try (Writer writer = Files.newBufferedWriter(outputFile.toPath())) {
formatter.render(document, writer);
}
FlexmarkIO.newTextFormatter().formatFile(document, outputFile, UTF_8);
} catch (IOException ex) {
throw new MojoExecutionException("Failed to write changelog", ex);
}
Expand Down

0 comments on commit 9d76248

Please sign in to comment.