Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/main/java/me/itzg/helpers/files/Manifests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -121,13 +125,29 @@ public static boolean allFilesPresent(Path basePath, BaseManifest manifest) {
* @param ignoreMissingFiles relative paths of files to ignore if they're missing
*/
public static boolean allFilesPresent(Path basePath, BaseManifest manifest, @Nullable List<String> ignoreMissingFiles) {
if (ignoreMissingFiles == null || ignoreMissingFiles.isEmpty()) {
return manifest.getFiles().stream()
.allMatch(p -> Files.exists(basePath.resolve(p)));
}

if (ignoreMissingFiles.stream().anyMatch(s -> s.equals("*"))) {
return true;
}

FileSystem fs = FileSystems.getDefault();
List<PathMatcher> matchers = ignoreMissingFiles.stream()
.map(pattern -> fs.getPathMatcher("glob:" + pattern))
.collect(Collectors.toList());

return manifest.getFiles().stream()
.allMatch(p ->
(ignoreMissingFiles != null && ignoreMissingFiles.contains(p))
|| Files.exists(basePath.resolve(p))
);
.allMatch(p -> {
Path file = Paths.get(p);
boolean ignored = matchers.stream().anyMatch(m -> m.matches(file));
return ignored || Files.exists(basePath.resolve(p));
});
}


/**
*
* @param outputDir directory where manifest and other module files are based
Expand Down
41 changes: 38 additions & 3 deletions src/test/java/me/itzg/helpers/files/ManifestsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
Expand All @@ -13,14 +14,13 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

class ManifestsTest {
public class ManifestsTest {

@TempDir
Path tempDir;

@Getter @SuperBuilder @Jacksonized
static class EmptyManifest extends BaseManifest {

}

@Test
Expand All @@ -33,4 +33,39 @@ void loadFailsGracefullyWhenInvalid() throws IOException {
final EmptyManifest manifest = Manifests.load(tempDir, id, EmptyManifest.class);
assertThat(manifest).isNull();
}
}

@Test
void allFilesPresent_withWildcardIgnoreAll() {
EmptyManifest manifest = EmptyManifest.builder()
.files(Arrays.asList("a.jar", "b.jar"))
.build();

boolean result = Manifests.allFilesPresent(tempDir, manifest, Collections.singletonList("*"));
assertThat(result).isTrue();
}

@Test
void allFilesPresent_withGlobPattern() throws IOException {
Files.createDirectories(tempDir.resolve("mods"));
Files.createFile(tempDir.resolve("mods/present.jar"));

EmptyManifest manifest = EmptyManifest.builder()
.files(Arrays.asList("mods/present.jar", "mods/missing.jar"))
.build();

boolean result = Manifests.allFilesPresent(tempDir, manifest, Collections.singletonList("mods/*.jar"));
assertThat(result).isTrue();
}

@Test
void allFilesPresent_withExplicitFileNames() throws IOException {
Files.createFile(tempDir.resolve("keep.jar"));

EmptyManifest manifest = EmptyManifest.builder()
.files(Arrays.asList("keep.jar", "remove.jar"))
.build();

boolean result = Manifests.allFilesPresent(tempDir, manifest, Collections.singletonList("remove.jar"));
assertThat(result).isTrue();
}
}