Skip to content

Commit 06e6bab

Browse files
authored
files: support globbing in Manifest allFilesPresent (#662)
1 parent e7d362d commit 06e6bab

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

src/main/java/me/itzg/helpers/files/Manifests.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import java.io.IOException;
5+
import java.nio.file.FileSystem;
6+
import java.nio.file.FileSystems;
57
import java.nio.file.Files;
68
import java.nio.file.Path;
9+
import java.nio.file.PathMatcher;
10+
import java.nio.file.Paths;
711
import java.util.Collection;
812
import java.util.Collections;
913
import java.util.HashSet;
@@ -121,13 +125,29 @@ public static boolean allFilesPresent(Path basePath, BaseManifest manifest) {
121125
* @param ignoreMissingFiles relative paths of files to ignore if they're missing
122126
*/
123127
public static boolean allFilesPresent(Path basePath, BaseManifest manifest, @Nullable List<String> ignoreMissingFiles) {
128+
if (ignoreMissingFiles == null || ignoreMissingFiles.isEmpty()) {
129+
return manifest.getFiles().stream()
130+
.allMatch(p -> Files.exists(basePath.resolve(p)));
131+
}
132+
133+
if (ignoreMissingFiles.stream().anyMatch(s -> s.equals("*"))) {
134+
return true;
135+
}
136+
137+
FileSystem fs = FileSystems.getDefault();
138+
List<PathMatcher> matchers = ignoreMissingFiles.stream()
139+
.map(pattern -> fs.getPathMatcher("glob:" + pattern))
140+
.collect(Collectors.toList());
141+
124142
return manifest.getFiles().stream()
125-
.allMatch(p ->
126-
(ignoreMissingFiles != null && ignoreMissingFiles.contains(p))
127-
|| Files.exists(basePath.resolve(p))
128-
);
143+
.allMatch(p -> {
144+
Path file = Paths.get(p);
145+
boolean ignored = matchers.stream().anyMatch(m -> m.matches(file));
146+
return ignored || Files.exists(basePath.resolve(p));
147+
});
129148
}
130149

150+
131151
/**
132152
*
133153
* @param outputDir directory where manifest and other module files are based

src/test/java/me/itzg/helpers/files/ManifestsTest.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.nio.file.Files;
77
import java.nio.file.Path;
8+
import java.util.Arrays;
89
import java.util.Collections;
910
import lombok.Getter;
1011
import lombok.experimental.SuperBuilder;
@@ -13,14 +14,13 @@
1314
import org.junit.jupiter.api.Test;
1415
import org.junit.jupiter.api.io.TempDir;
1516

16-
class ManifestsTest {
17+
public class ManifestsTest {
1718

1819
@TempDir
1920
Path tempDir;
2021

2122
@Getter @SuperBuilder @Jacksonized
2223
static class EmptyManifest extends BaseManifest {
23-
2424
}
2525

2626
@Test
@@ -33,4 +33,39 @@ void loadFailsGracefullyWhenInvalid() throws IOException {
3333
final EmptyManifest manifest = Manifests.load(tempDir, id, EmptyManifest.class);
3434
assertThat(manifest).isNull();
3535
}
36-
}
36+
37+
@Test
38+
void allFilesPresent_withWildcardIgnoreAll() {
39+
EmptyManifest manifest = EmptyManifest.builder()
40+
.files(Arrays.asList("a.jar", "b.jar"))
41+
.build();
42+
43+
boolean result = Manifests.allFilesPresent(tempDir, manifest, Collections.singletonList("*"));
44+
assertThat(result).isTrue();
45+
}
46+
47+
@Test
48+
void allFilesPresent_withGlobPattern() throws IOException {
49+
Files.createDirectories(tempDir.resolve("mods"));
50+
Files.createFile(tempDir.resolve("mods/present.jar"));
51+
52+
EmptyManifest manifest = EmptyManifest.builder()
53+
.files(Arrays.asList("mods/present.jar", "mods/missing.jar"))
54+
.build();
55+
56+
boolean result = Manifests.allFilesPresent(tempDir, manifest, Collections.singletonList("mods/*.jar"));
57+
assertThat(result).isTrue();
58+
}
59+
60+
@Test
61+
void allFilesPresent_withExplicitFileNames() throws IOException {
62+
Files.createFile(tempDir.resolve("keep.jar"));
63+
64+
EmptyManifest manifest = EmptyManifest.builder()
65+
.files(Arrays.asList("keep.jar", "remove.jar"))
66+
.build();
67+
68+
boolean result = Manifests.allFilesPresent(tempDir, manifest, Collections.singletonList("remove.jar"));
69+
assertThat(result).isTrue();
70+
}
71+
}

0 commit comments

Comments
 (0)