mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
List a pack's modules grouped by host and pack, wrapped
This commit is contained in:
1 parent
bedaac5155
commit
6c4e4ef6f7
3 files changed
+84
-46
No files matched your search
@@ -1,13 +1,18 @@
|
||||
package com.volmit.host;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** The one log message a pack load writes: how many modules the pack uses, and each module the pack
|
||||
* brings itself with the jar it comes from. The host's modules are the same for every pack, so they
|
||||
* are only counted.
|
||||
/** The one log message a pack load writes: which modules the pack uses, grouped by whether the host
|
||||
* or the pack brings them. The host's modules are named only; the pack's own also carry their
|
||||
* version and jar.
|
||||
*/
|
||||
final class PackModulesSummary {
|
||||
private static final String INDENT = " ";
|
||||
private static final String HOST_LABEL = "Host: ";
|
||||
private static final String PACK_LABEL = "Pack: ";
|
||||
private static final String ENTRY_SEPARATOR = ",";
|
||||
private static final int MAX_LINE_WIDTH = 100;
|
||||
|
||||
private PackModulesSummary() {}
|
||||
|
||||
@@ -15,13 +20,19 @@ final class PackModulesSummary {
|
||||
if (modules.isEmpty()) {
|
||||
return "Pack " + packName + " uses no modules: no jars in its " + PackModules.MODULES_DIRECTORY + " directory";
|
||||
}
|
||||
List<DiscoveredModule> ownModules = modules.stream().filter(module -> !isFromHost(module)).toList();
|
||||
int hostModuleCount = modules.size() - ownModules.size();
|
||||
var text = new StringBuilder("Pack ").append(packName).append(" uses ").append(countOf(modules.size()))
|
||||
.append(": ").append(sourcesOf(hostModuleCount, ownModules.size()));
|
||||
for (var module : ownModules) {
|
||||
text.append('\n').append(INDENT).append(describe(module));
|
||||
List<String> hostModules = new ArrayList<>();
|
||||
List<String> ownModules = new ArrayList<>();
|
||||
for (var module : modules) {
|
||||
if (isFromHost(module)) {
|
||||
hostModules.add(module.info().name());
|
||||
} else {
|
||||
ownModules.add(describeOwn(module));
|
||||
}
|
||||
}
|
||||
var text = new StringBuilder("Pack ").append(packName).append(" uses ").append(modules.size())
|
||||
.append(modules.size() == 1 ? " module:" : " modules:");
|
||||
appendGroup(text, HOST_LABEL, hostModules);
|
||||
appendGroup(text, PACK_LABEL, ownModules);
|
||||
return text.toString();
|
||||
}
|
||||
|
||||
@@ -30,22 +41,31 @@ final class PackModulesSummary {
|
||||
|| module.origin().endsWith(PackModules.BUNDLED_ORIGIN_SUFFIX);
|
||||
}
|
||||
|
||||
private static String sourcesOf(int hostModuleCount, int ownModuleCount) {
|
||||
if (ownModuleCount == 0) {
|
||||
return "all from the host";
|
||||
}
|
||||
if (hostModuleCount == 0) {
|
||||
return "all from the pack";
|
||||
}
|
||||
return hostModuleCount + " from the host, " + ownModuleCount + " from the pack";
|
||||
}
|
||||
|
||||
private static String countOf(int moduleCount) {
|
||||
return moduleCount + (moduleCount == 1 ? " module" : " modules");
|
||||
}
|
||||
|
||||
private static String describe(DiscoveredModule discovered) {
|
||||
private static String describeOwn(DiscoveredModule discovered) {
|
||||
var info = discovered.info();
|
||||
return info.name() + " (" + info.id() + " " + info.version() + ") from " + discovered.origin();
|
||||
return info.name() + " " + info.version() + " (" + discovered.origin() + ")";
|
||||
}
|
||||
|
||||
private static void appendGroup(StringBuilder text, String label, List<String> entries) {
|
||||
if (entries.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
String continuation = INDENT + " ".repeat(label.length());
|
||||
var line = new StringBuilder(INDENT).append(label);
|
||||
int entriesOnLine = 0;
|
||||
for (int index = 0; index < entries.size(); index++) {
|
||||
String entry = entries.get(index) + (index < entries.size() - 1 ? ENTRY_SEPARATOR : "");
|
||||
if (entriesOnLine > 0 && line.length() + 1 + entry.length() > MAX_LINE_WIDTH) {
|
||||
text.append('\n').append(line);
|
||||
line = new StringBuilder(continuation);
|
||||
entriesOnLine = 0;
|
||||
}
|
||||
if (entriesOnLine > 0) {
|
||||
line.append(' ');
|
||||
}
|
||||
line.append(entry);
|
||||
entriesOnLine++;
|
||||
}
|
||||
text.append('\n').append(line);
|
||||
}
|
||||
}
|
||||
@@ -120,7 +120,7 @@ class LoadedPackTest {
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(List.of("INFO Pack " + pack.getFileName() + " uses 1 module: all from the pack"), log.levelsAndFirstLines());
|
||||
assertEquals(List.of("INFO Pack " + pack.getFileName() + " uses 1 module:"), log.levelsAndFirstLines());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -4,8 +4,10 @@ import com.volmit.IrisModule;
|
||||
import com.volmit.example.placement.MossModule;
|
||||
import com.volmit.example.placement.SnowModule;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PackModulesSummaryTest {
|
||||
private static DiscoveredModule pack(IrisModule module, String origin) {
|
||||
@@ -17,47 +19,63 @@ class PackModulesSummaryTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void listsNameIdVersionAndJarOfEveryModuleThePackBringsInLoadOrder() {
|
||||
void listsTheVersionAndJarOfEveryModuleThePackBringsInLoadOrder() {
|
||||
var modules = List.of(
|
||||
pack(new MossModule(), "modules/moss.jar"),
|
||||
pack(new SnowModule(), "modules/snow.jar"));
|
||||
|
||||
assertEquals("""
|
||||
Pack overworld uses 2 modules: all from the pack
|
||||
Moss (example:moss 1.0.0) from modules/moss.jar
|
||||
Snow (iris:snow test) from modules/snow.jar""",
|
||||
Pack overworld uses 2 modules:
|
||||
Pack: Moss 1.0.0 (modules/moss.jar), Snow test (modules/snow.jar)""",
|
||||
PackModulesSummary.describe("overworld", modules));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hostModulesAreOnlyCounted() {
|
||||
void namesHostModulesWithoutTheirJar() {
|
||||
var modules = List.of(
|
||||
bundled(new SnowModule()),
|
||||
DiscoveredModule.builtIn(new SnowModule()));
|
||||
|
||||
assertEquals("Pack overworld uses 2 modules: all from the host",
|
||||
PackModulesSummary.describe("overworld", modules));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMixedPackCountsBothAndListsOnlyItsOwn() {
|
||||
var modules = List.of(
|
||||
bundled(new SnowModule()),
|
||||
pack(new MossModule(), "modules/moss.jar"));
|
||||
DiscoveredModule.builtIn(new MossModule()));
|
||||
|
||||
assertEquals("""
|
||||
Pack overworld uses 2 modules: 1 from the host, 1 from the pack
|
||||
Moss (example:moss 1.0.0) from modules/moss.jar""",
|
||||
Pack overworld uses 2 modules:
|
||||
Host: Snow, Moss""",
|
||||
PackModulesSummary.describe("overworld", modules));
|
||||
}
|
||||
|
||||
@Test
|
||||
void groupsHostModulesBeforeThePacksOwn() {
|
||||
var modules = List.of(
|
||||
pack(new MossModule(), "modules/moss.jar"),
|
||||
bundled(new SnowModule()));
|
||||
|
||||
assertEquals("""
|
||||
Pack overworld uses 2 modules:
|
||||
Host: Snow
|
||||
Pack: Moss 1.0.0 (modules/moss.jar)""",
|
||||
PackModulesSummary.describe("overworld", modules));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wrapsALongGroupUnderItsFirstEntry() {
|
||||
var modules = Collections.nCopies(20, bundled(new SnowModule()));
|
||||
|
||||
String[] lines = PackModulesSummary.describe("overworld", modules).split("\n");
|
||||
|
||||
assertTrue(lines.length > 2);
|
||||
assertTrue(lines[1].startsWith(" Host: Snow, Snow"));
|
||||
assertTrue(lines[2].startsWith(" Snow"));
|
||||
for (String line : lines) {
|
||||
assertTrue(line.length() <= 100, line);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void singleModuleReadsAsOneModule() {
|
||||
var modules = List.of(pack(new MossModule(), "modules/moss.jar"));
|
||||
|
||||
assertEquals("""
|
||||
Pack overworld uses 1 module: all from the pack
|
||||
Moss (example:moss 1.0.0) from modules/moss.jar""",
|
||||
Pack overworld uses 1 module:
|
||||
Pack: Moss 1.0.0 (modules/moss.jar)""",
|
||||
PackModulesSummary.describe("overworld", modules));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user