Skip to content

Commit 5c75e76

Browse files
committed
Attempted new Bootstrap
1 parent 9d15a98 commit 5c75e76

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

src/main/java/org/mangorage/bootstrap/Bootstrap.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.mangorage.bootstrap;
22

33
import java.io.File;
4+
import java.io.IOException;
45
import java.lang.reflect.Method;
56
import java.net.MalformedURLException;
67
import java.net.URL;
@@ -9,7 +10,7 @@
910
import java.util.List;
1011

1112
public class Bootstrap {
12-
public static void main(final String[] args) {
13+
public static void main(final String[] args) throws IOException {
1314

1415
if (args.length > 0) {
1516
if (args[0].contains("--useModules")) {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.mangorage.bootstrap;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.lang.module.ModuleFinder;
6+
import java.nio.file.DirectoryStream;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import java.nio.file.StandardCopyOption;
11+
import java.util.Comparator;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.jar.JarFile;
15+
import java.util.zip.ZipEntry;
16+
17+
public class LibraryHandler {
18+
19+
public static void handle() throws IOException {
20+
Path source = Paths.get("libraries");
21+
Path target = Paths.get("sortedLibraries");
22+
23+
if (Files.exists(target)) {
24+
deleteDirectory(target);
25+
}
26+
27+
Files.createDirectories(target);
28+
29+
Map<String, Path> seenModules = new HashMap<>();
30+
31+
try (DirectoryStream<Path> stream = Files.newDirectoryStream(source, "*.jar")) {
32+
for (Path jar : stream) {
33+
String moduleName = resolveModuleName(jar);
34+
if (moduleName == null) {
35+
System.out.println("Skipping non-module JAR: " + jar);
36+
continue;
37+
}
38+
39+
if (!seenModules.containsKey(moduleName)) {
40+
Path dest = target.resolve(jar.getFileName());
41+
Files.copy(jar, dest, StandardCopyOption.REPLACE_EXISTING);
42+
seenModules.put(moduleName, jar);
43+
System.out.println("Added module: " + moduleName);
44+
} else {
45+
System.out.println("Duplicate module ignored: " + moduleName + " from " + jar);
46+
}
47+
}
48+
}
49+
50+
System.out.println("Finished deduplicating modules. Result at: " + target);
51+
}
52+
53+
private static void deleteDirectory(Path dir) throws IOException {
54+
Files.walk(dir)
55+
.sorted(Comparator.reverseOrder())
56+
.map(Path::toFile)
57+
.forEach(File::delete);
58+
}
59+
60+
private static String resolveModuleName(Path jarPath) {
61+
try (JarFile jarFile = new JarFile(jarPath.toFile())) {
62+
ZipEntry entry = jarFile.getEntry("module-info.class");
63+
if (entry != null) {
64+
// This is a proper JPMS module JAR
65+
return ModuleFinder.of(jarPath).findAll().iterator().next().descriptor().name();
66+
} else {
67+
// Fall back to heuristic based on filename (best effort)
68+
String filename = jarPath.getFileName().toString();
69+
return filename.replaceAll("-[\\d\\.]+.*\\.jar$", "").replaceAll("\\.jar$", "");
70+
}
71+
} catch (IOException e) {
72+
e.printStackTrace();
73+
return null;
74+
}
75+
}
76+
}

src/main/java/org/mangorage/bootstrap/Test.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
package org.mangorage.bootstrap;
22

33
import java.io.File;
4+
import java.io.IOException;
45
import java.lang.module.Configuration;
56
import java.lang.module.ModuleFinder;
67
import java.lang.reflect.Method;
78
import java.net.URLClassLoader;
89
import java.nio.file.Path;
10+
import java.nio.file.Paths;
911
import java.util.Set;
1012

1113
import static org.mangorage.bootstrap.Bootstrap.fetchJars;
1214

1315
public final class Test {
14-
public static void main(String[] args) {
16+
public static void main(String[] args) throws IOException {
1517
try {
1618
System.out.println("you have 15 seconds!");
1719
Thread.sleep(15_000);
1820

1921
} catch (Throwable ignored) {}
2022

21-
Path libsPath = Path.of("libraries");
23+
LibraryHandler.handle();
24+
25+
Path libsPath = Path.of("sortedLibraries");
2226
Path pluginsPath = Path.of("plugins");
2327

2428
ModuleFinder plugins = ModuleFinder.of(pluginsPath);

0 commit comments

Comments
 (0)