Skip to content

Commit

Permalink
merge together modules that get the same name automatically
Browse files Browse the repository at this point in the history
Signed-off-by: cpw <[email protected]>
  • Loading branch information
cpw committed Jun 21, 2022
1 parent 9c64802 commit 780bc67
Showing 1 changed file with 21 additions and 35 deletions.
56 changes: 21 additions & 35 deletions src/main/java/cpw/mods/bootstraplauncher/BootstrapLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.*;
import java.util.function.BiPredicate;
import java.util.function.Consumer;

Expand All @@ -60,10 +52,14 @@ public static void main(String... args) {
var previousPackages = new HashSet<String>();
// The list of all SecureJars, which represent one module
var jars = new ArrayList<SecureJar>();
// path to name lookup
var pathLookup = new HashMap<Path, String>();
// Map of filenames to their 'module number', where all filenames sharing the same 'module number' is combined into one
var filenameMap = getMergeFilenameMap();
// Map of 'module number' to the list of paths which are combined into that module
var mergeMap = new HashMap<Integer, List<Path>>();
var mergeMap = new LinkedHashMap<String, List<Path>>();

var order = new ArrayList<String>();

outer:
for (var legacy : legacyClasspath) {
Expand All @@ -83,47 +79,37 @@ public static void main(String... args) {
System.out.println("bsl: encountered path '" + legacy + "'");
}

if (filenameMap.containsKey(filename)) {
if (DEBUG) {
System.out.println("bsl: path is contained with module #" + filenameMap.get(filename) + ", skipping for now");
}
mergeMap.computeIfAbsent(filenameMap.get(filename), k -> new ArrayList<>()).add(path);
continue;
}

if (Files.notExists(path)) continue;

var jar = SecureJar.from(new PackageTracker(Set.copyOf(previousPackages), path), path);
// This computes the name of the artifact for detecting collisions
var jar = SecureJar.from(path);
if ("".equals(jar.name())) continue;
var packages = jar.getPackages();

if (DEBUG) {
System.out.println("bsl: list of packages for file '" + legacy + "'");
packages.forEach(p -> System.out.println("bsl: " + p));
}

previousPackages.addAll(packages);
jars.add(jar);
var jarname = pathLookup.computeIfAbsent(path, k -> filenameMap.getOrDefault(filename, jar.name()));
order.add(jarname);
mergeMap.computeIfAbsent(jarname, k -> new ArrayList<>()).add(path);
}


// Iterate over merged modules map and combine them into one SecureJar each
mergeMap.forEach((idx, paths) -> {
mergeMap.entrySet().stream().sorted(Comparator.comparingInt(e-> order.indexOf(e.getKey()))).forEach(e -> {
// skip empty paths
var name = e.getKey();
var paths = e.getValue();
if (paths.size() == 1 && Files.notExists(paths.get(0))) return;
var pathsArray = paths.toArray(Path[]::new);
var jar = SecureJar.from(new PackageTracker(Set.copyOf(previousPackages), pathsArray), pathsArray);
var packages = jar.getPackages();

if (DEBUG) {
System.out.println("bsl: the following paths are merged together in module #" + idx);
System.out.println("bsl: the following paths are merged together in module " + name);
paths.forEach(path -> System.out.println("bsl: " + path));
System.out.println("bsl: list of packages for module #" + idx);
System.out.println("bsl: list of packages for module " + name);
packages.forEach(p -> System.out.println("bsl: " + p));
}

previousPackages.addAll(packages);
jars.add(jar);
});

var secureJarsArray = jars.toArray(SecureJar[]::new);

// Gather all the module names from the SecureJars
Expand Down Expand Up @@ -155,20 +141,20 @@ public static void main(String... args) {
((Consumer<String[]>) loader.stream().findFirst().orElseThrow().get()).accept(args);
}

private static Map<String, Integer> getMergeFilenameMap() {
private static Map<String, String> getMergeFilenameMap() {
var mergeModules = System.getProperty("mergeModules");
if (mergeModules == null)
return Map.of();
// `mergeModules` is a semicolon-separated set of comma-separated set of paths, where each (comma) set of paths is
// combined into a single modules
// example: filename1.jar,filename2.jar;filename2.jar,filename3.jar

Map<String, Integer> filenameMap = new HashMap<>();
Map<String, String> filenameMap = new HashMap<>();
int i = 0;
for (var merge : mergeModules.split(";")) {
var targets = merge.split(",");
for (String target : targets) {
filenameMap.put(target, i);
filenameMap.put(target, String.valueOf(i));
}
i++;
}
Expand Down

0 comments on commit 780bc67

Please sign in to comment.