From b333868b8c5e5bd7db5df864da2f4002786847eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Mon, 24 Jun 2024 17:26:55 +0300 Subject: [PATCH] fix. --- .../net/infumia/pack/PackReadFilters.java | 24 +++++++++++++++ .../java/net/infumia/pack/PackReader.java | 3 ++ .../net/infumia/pack/PackReaderSettings.java | 29 ++++++++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 generator/src/main/java/net/infumia/pack/PackReadFilters.java diff --git a/generator/src/main/java/net/infumia/pack/PackReadFilters.java b/generator/src/main/java/net/infumia/pack/PackReadFilters.java new file mode 100644 index 0000000..d67edb7 --- /dev/null +++ b/generator/src/main/java/net/infumia/pack/PackReadFilters.java @@ -0,0 +1,24 @@ +package net.infumia.pack; + +import java.nio.file.Path; +import java.util.function.Predicate; + +/** + * Utility class for creating read filters for pack reading. + */ +public final class PackReadFilters { + + /** + * Creates a predicate that filters paths by the specified file extension. + * + * @param extension the file extension to filter by. Cannot be null. + * @return a predicate that returns true for paths with the specified extension. + */ + public static Predicate withExtension(final String extension) { + return path -> path.getFileName().toString().endsWith(extension); + } + + private PackReadFilters() { + throw new IllegalStateException("Utility class"); + } +} diff --git a/generator/src/main/java/net/infumia/pack/PackReader.java b/generator/src/main/java/net/infumia/pack/PackReader.java index 492beb3..3d4d21c 100644 --- a/generator/src/main/java/net/infumia/pack/PackReader.java +++ b/generator/src/main/java/net/infumia/pack/PackReader.java @@ -44,7 +44,10 @@ PackGeneratorContext read() throws IOException { private PackGeneratorContext read0(@NotNull final Stream walking) throws IOException { final PackReferenceMeta packReference = this.packReader.readValue(this.packReferenceFile); final Collection packPartReferences = walking + .filter(Files::isRegularFile) + .filter(this.settings.readFilter()) .map(Path::toFile) + .filter(file -> !this.packReferenceFile.equals(file)) .map(file -> { try ( final MappingIterator iterator = diff --git a/generator/src/main/java/net/infumia/pack/PackReaderSettings.java b/generator/src/main/java/net/infumia/pack/PackReaderSettings.java index 3ed0d63..f0f4f22 100644 --- a/generator/src/main/java/net/infumia/pack/PackReaderSettings.java +++ b/generator/src/main/java/net/infumia/pack/PackReaderSettings.java @@ -2,9 +2,11 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.nio.file.FileVisitOption; +import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; import java.util.StringJoiner; +import java.util.function.Predicate; import net.kyori.adventure.text.serializer.ComponentSerializer; /** @@ -18,6 +20,7 @@ public final class PackReaderSettings { private final Path outputDirectory; private final Path outputFile; private final ObjectMapper mapper; + private final Predicate readFilter; private final ComponentSerializer serializer; /** @@ -29,6 +32,7 @@ public final class PackReaderSettings { * @param outputDirectory the directory name. Can be null. * @param outputFile the zip file name. Can be null. * @param mapper the object mapper to read pack and pack part reference files. Cannot be null. + * @param readFilter the read filter for {@link Files#walk(Path, FileVisitOption...)}. * @param serializer the serializer to serialize components when needed. Cannot be null. */ public PackReaderSettings( @@ -38,6 +42,7 @@ public PackReaderSettings( final Path outputDirectory, final Path outputFile, final ObjectMapper mapper, + final Predicate readFilter, final ComponentSerializer serializer ) { this.root = root; @@ -46,6 +51,7 @@ public PackReaderSettings( this.outputDirectory = outputDirectory; this.outputFile = outputFile; this.mapper = mapper; + this.readFilter = readFilter; this.serializer = serializer; } @@ -57,6 +63,7 @@ public PackReaderSettings( * @param outputDirectory the directory name. Can be null. * @param outputFile the zip file name. Can be null. * @param mapper the object mapper to read pack and pack part reference files. Cannot be null. + * @param readFilter the read filter for {@link Files#walk(Path, FileVisitOption...)}. */ public PackReaderSettings( final Path root, @@ -64,9 +71,19 @@ public PackReaderSettings( final Path outputDirectory, final Path outputFile, final ObjectMapper mapper, + final Predicate readFilter, final ComponentSerializer serializer ) { - this(root, null, packReferenceFileName, outputDirectory, outputFile, mapper, serializer); + this( + root, + null, + packReferenceFileName, + outputDirectory, + outputFile, + mapper, + readFilter, + serializer + ); } /** @@ -123,6 +140,15 @@ public ObjectMapper mapper() { return this.mapper; } + /** + * Returns the read filter. + * + * @return the read filter. + */ + public Predicate readFilter() { + return this.readFilter; + } + /** * Returns the component serializer. * @@ -141,6 +167,7 @@ public String toString() { .add("outputDirectory=" + this.outputDirectory) .add("outputFile=" + this.outputFile) .add("mapper=" + this.mapper) + .add("readFilter=" + this.readFilter) .add("serializer=" + this.serializer) .toString(); }