-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
159 lines (137 loc) · 5.25 KB
/
Main.java
File metadata and controls
159 lines (137 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.cope.addonparser.cli;
import com.cope.addonparser.model.JarScanResult;
import com.cope.addonparser.model.ScanSummary;
import com.cope.addonparser.profile.MappingProfile;
import com.cope.addonparser.scanner.AddonScanner;
import com.cope.addonparser.scanner.IsolatedScanner;
import com.cope.addonparser.scanner.RuntimeMode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public final class Main {
private Main() {}
public static void main(String[] args) throws Exception {
Args parsed = Args.parse(args);
if (parsed == null) {
printUsage();
System.exit(2);
return;
}
// Make profile visible to in-process consumers (e.g. ValueNormalizer) and forwarded
// subprocesses.
System.setProperty(MappingProfile.SYSTEM_PROPERTY, parsed.profile.cliValue());
List<Path> jars = collectJars(parsed.input);
if (jars.isEmpty()) {
System.err.println("No jar files found in: " + parsed.input);
System.exit(1);
return;
}
Files.createDirectories(parsed.outputDir);
ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
ScanSummary summary = new ScanSummary();
summary.jarCount = jars.size();
System.out.println("Runtime mode: " + parsed.mode);
System.out.println("Mapping profile: " + parsed.profile.cliValue());
try (AutoCloseable scanner =
parsed.mode == RuntimeMode.ISOLATED
? new IsolatedScanner(parsed.profile)
: new AddonScanner(parsed.profile)) {
for (Path jar : jars) {
JarScanResult result =
parsed.mode == RuntimeMode.ISOLATED
? ((IsolatedScanner) scanner).scan(jar)
: ((AddonScanner) scanner).scan(jar);
String base = jar.getFileName().toString();
Path outFile = parsed.outputDir.resolve(base + ".json");
mapper.writeValue(outFile.toFile(), result);
summary.outputFiles.add(outFile.getFileName().toString());
if (result.success) {
summary.successCount++;
System.out.println("[OK] " + base + " modules=" + result.modules.size());
} else {
summary.failureCount++;
summary.failedJars.add(base);
System.out.println(
"[FAIL] "
+ base
+ " errors="
+ result.errors.size()
+ " modules="
+ result.modules.size());
}
}
}
Path summaryFile =
parsed.summaryFile != null ? parsed.summaryFile : parsed.outputDir.resolve("summary.json");
Path summaryParent = summaryFile.getParent();
if (summaryParent != null) {
Files.createDirectories(summaryParent);
}
mapper.writeValue(summaryFile.toFile(), summary);
System.out.println(
"Completed. jars="
+ summary.jarCount
+ " ok="
+ summary.successCount
+ " failed="
+ summary.failureCount);
System.out.println("Summary: " + summaryFile.toAbsolutePath());
if (summary.failureCount > 0) {
System.exit(1);
}
}
private static List<Path> collectJars(Path input) throws Exception {
List<Path> jars = new ArrayList<>();
if (Files.isRegularFile(input) && input.toString().toLowerCase().endsWith(".jar")) {
jars.add(input);
return jars;
}
if (Files.isDirectory(input)) {
try (var stream = Files.list(input)) {
stream
.filter(
path -> Files.isRegularFile(path) && path.toString().toLowerCase().endsWith(".jar"))
.sorted(Comparator.comparing(path -> path.getFileName().toString().toLowerCase()))
.forEach(jars::add);
}
}
return jars;
}
private static void printUsage() {
System.out.println(
"Usage: java -jar addon-parser.jar --input <jar-or-dir> [--output <dir>] [--summary <file>] [--mode isolated|legacy] [--profile legacy|26x]");
}
private record Args(
Path input, Path outputDir, Path summaryFile, RuntimeMode mode, MappingProfile profile) {
static Args parse(String[] args) {
Path input = null;
Path output = Paths.get("output");
Path summary = null;
RuntimeMode mode = RuntimeMode.LEGACY;
MappingProfile profile = MappingProfile.fromSystemProperty();
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if ("--input".equals(arg) && i + 1 < args.length) {
input = Paths.get(args[++i]);
} else if ("--output".equals(arg) && i + 1 < args.length) {
output = Paths.get(args[++i]);
} else if ("--summary".equals(arg) && i + 1 < args.length) {
summary = Paths.get(args[++i]);
} else if ("--mode".equals(arg) && i + 1 < args.length) {
mode = RuntimeMode.fromString(args[++i]);
} else if ("--profile".equals(arg) && i + 1 < args.length) {
profile = MappingProfile.fromString(args[++i]);
} else {
return null;
}
}
if (input == null) return null;
return new Args(input, output, summary, mode, profile);
}
}
}