Skip to content

Commit

Permalink
feat(mustache): Additional file handling functions. Resolves #893
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Aug 14, 2022
1 parent b5abade commit a3eda47
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,27 @@
import org.commonmark.renderer.html.HtmlRenderer;
import org.jreleaser.bundle.RB;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;

import static java.nio.file.Files.readAllBytes;
import static org.jreleaser.util.ChecksumUtils.checksum;
import static org.jreleaser.util.StringUtils.isNotBlank;

/**
Expand Down Expand Up @@ -137,6 +144,10 @@ public static void applyFunctions(Map<String, Object> props) {
props.put("f_capitalize", new CapitalizeFunction());
props.put("f_uncapitalize", new UncapitalizeFunction());
props.put("f_md2html", new MarkdownToHtmlFunction());
props.put("f_file_read", new FileReadFunction());
props.put("f_file_size", new FileSizeFunction());
EnumSet.allOf(Algorithm.class)
.forEach(algorithm -> props.put("f_checksum_" + algorithm.formatted(), new FileChecksumFunction(algorithm)));
}

private static class MyMustacheFactory extends DefaultMustacheFactory {
Expand Down Expand Up @@ -232,4 +243,67 @@ public String apply(String input) {
return renderer.render(document);
}
}

public static class FileReadFunction implements Function<Object, String> {
@Override
public String apply(Object input) {
try {
if (input instanceof Path) {
return new String(Files.readAllBytes((Path) input));
} else if (input instanceof File) {
return new String(Files.readAllBytes(((File) input).toPath()));
} else if (input instanceof CharSequence) {
return new String(Files.readAllBytes(Paths.get(String.valueOf(input).trim())));
}
} catch (IOException e) {
throw new IllegalStateException(RB.$("ERROR_unexpected_file_read", input), e);
}

throw new IllegalStateException(RB.$("ERROR_invalid_file_input", input));
}
}

public static class FileSizeFunction implements Function<Object, Long> {
@Override
public Long apply(Object input) {
try {
if (input instanceof Path) {
return Files.size((Path) input);
} else if (input instanceof File) {
return Files.size(((File) input).toPath());
} else if (input instanceof CharSequence) {
return Files.size(Paths.get(String.valueOf(input).trim()));
}
} catch (IOException e) {
throw new IllegalStateException(RB.$("ERROR_unexpected_file_read", input), e);
}

throw new IllegalStateException(RB.$("ERROR_invalid_file_input", input));
}
}

public static class FileChecksumFunction implements Function<Object, String> {
private final Algorithm algorithm;

public FileChecksumFunction(Algorithm algorithm) {
this.algorithm = algorithm;
}

@Override
public String apply(Object input) {
try {
if (input instanceof Path) {
return checksum(algorithm, readAllBytes((Path) input));
} else if (input instanceof File) {
return checksum(algorithm, readAllBytes(((File) input).toPath()));
} else if (input instanceof CharSequence) {
return checksum(algorithm, readAllBytes(Paths.get(String.valueOf(input).trim())));
}
} catch (IOException e) {
throw new IllegalStateException(RB.$("ERROR_unexpected_file_read", input), e);
}

throw new IllegalStateException(RB.$("ERROR_invalid_file_input", input));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ ERROR_files_create = Unable to create: {}
ERROR_files_cycle = Cycle detected: {}
ERROR_files_copy_attributes = Unable to copy all attributes to: {}
ERROR_mustache_write_value = Failed to write value:
ERROR_invalid_file_input = Input {} is not a File, Path, nor CharSequence.
ERROR_unexpected_file_read = Unexpected error when reading file {}
files.copy = copying files from {} to {}

ERROR_unexpected_release_announce = Unexpected error when announcing release
Expand Down

0 comments on commit a3eda47

Please sign in to comment.