forked from MinecraftForge/MinecraftMavenizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepo.java
More file actions
307 lines (260 loc) · 12.5 KB
/
Repo.java
File metadata and controls
307 lines (260 loc) · 12.5 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.mcmaven.impl.repo;
import net.minecraftforge.mcmaven.impl.Mavenizer;
import net.minecraftforge.mcmaven.impl.cache.Cache;
import net.minecraftforge.mcmaven.impl.data.GradleModule;
import net.minecraftforge.mcmaven.impl.mappings.Mappings;
import net.minecraftforge.mcmaven.impl.repo.mcpconfig.MCPConfigRepo;
import net.minecraftforge.mcmaven.impl.repo.mcpconfig.MCPSide;
import net.minecraftforge.mcmaven.impl.util.Artifact;
import net.minecraftforge.mcmaven.impl.util.GradleAttributes;
import net.minecraftforge.mcmaven.impl.util.POMBuilder;
import net.minecraftforge.mcmaven.impl.util.Task;
import net.minecraftforge.mcmaven.impl.util.Util;
import net.minecraftforge.util.data.json.JsonData;
import net.minecraftforge.util.file.FileUtils;
import static net.minecraftforge.mcmaven.impl.Mavenizer.LOGGER;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.jetbrains.annotations.Nullable;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
public abstract class Repo {
protected final Cache cache;
protected Repo(Cache cache) {
this.cache = cache;
}
public final Cache getCache() {
return cache;
}
public abstract List<PendingArtifact> process(Artifact artifact, Mappings mappings, Map<String, Supplier<String>> outputJson);
protected static PendingArtifact pending(String message, Task task, Artifact artifact, boolean auxiliary) {
return pending(message, task, artifact, auxiliary, (Task) null);
}
protected static PendingArtifact pending(String message, Task task, Artifact artifact, boolean auxiliary, Supplier<GradleModule.Variant[]> variants) {
return pending(message, task, artifact, auxiliary, variantTask(task, variants));
}
protected static PendingArtifact pending(String message, Task task, Artifact artifact, boolean auxiliary, @Nullable Task variants) {
return new PendingArtifact(message, task, artifact, auxiliary, variants);
}
// Sources has no dependencies, so just need to specify the attributes
protected Supplier<GradleModule.Variant[]> sourceVariant(Mappings mappings) {
return () -> new GradleModule.Variant[] {
GradleModule.Variant.of("sources")
.attribute("org.gradle.status", "release")
.attribute("org.gradle.usage", "java-runtime")
.attribute("org.gradle.category", "documentation")
.attribute("org.gradle.dependency.bundling", "external")
.attribute("org.gradle.docstype", "sources")
.attribute("org.gradle.libraryelements", "jar")
.attribute(Mappings.CHANNEL_ATTR, mappings.channel())
.attribute(Mappings.VERSION_ATTR, mappings.version())
};
}
protected Supplier<GradleModule.Variant[]> metadataVariant() {
return () -> new GradleModule.Variant[] {
GradleModule.Variant.of("metadata")
.attribute("org.gradle.status", "release")
.attribute("org.gradle.usage", "metadata")
};
}
protected static Task variantTask(Task parent, Supplier<GradleModule.Variant[]> supplier) {
return Task.named(parent.name() + "[variants]", Task.deps(parent), () -> {
var variants = supplier.get();
var variantFile = new File(parent.execute().getAbsolutePath() + ".variants");
var json = JsonData.toJson(variants);
var cache = Util.cache(variantFile)
.add("data", json);
if (Mavenizer.checkCache(variantFile, cache))
return variantFile;
try {
FileUtils.ensureParent(variantFile);
Files.writeString(variantFile.toPath(), json, StandardCharsets.UTF_8);
cache.save();
} catch (Throwable t) {
throw new RuntimeException("Failed to write artifact variants: %s".formatted(variantFile), t);
}
return variantFile;
});
}
protected Supplier<GradleModule.Variant[]> simpleVariant(String name, Mappings mappings) {
return () -> new GradleModule.Variant[] {
GradleModule.Variant
.of(name)
.attribute("org.gradle.status", "release")
.attribute("org.gradle.category", "library")
.attribute("org.gradle.libraryelements", "jar")
.attribute(Mappings.CHANNEL_ATTR, mappings.channel())
.attribute(Mappings.VERSION_ATTR, mappings.version())
};
}
protected GradleModule.Variant[] classVariants(Mappings mappings, MCPSide side) {
return classVariants(mappings, side, List.of(), List.of(), List.of());
}
// Classes needs a variant for each OS type so that we can have different natives
protected GradleModule.Variant[] classVariants(
Mappings mappings,
MCPSide side,
Collection<Artifact> extraDeps,
Collection<Artifact> extraCompileDeps,
Collection<Artifact> extraRuntimeDeps
) {
var deps = new ArrayList<Artifact>();
deps.addAll(side.getMCLibraries());
deps.addAll(side.getMCPConfigLibraries());
deps.addAll(extraDeps);
// ExtraRuntimeDeps is never used.... Not sure where it was meant to go
return classVariants(
mappings,
side.getMCP().getMinecraftTasks().versionJson,
deps,
extraCompileDeps
);
}
protected GradleModule.Variant[] classVariants(
Mappings mappings,
Task jsonTask,
Collection<Artifact> deps,
Collection<Artifact> extraCompileDeps
) {
var all = new ArrayList<Artifact>();
var natives = new HashMap<GradleAttributes.OperatingSystemFamily, List<Artifact>>();
for (var artifact : deps) {
if (artifact == null)
continue;
var osVariants = EnumSet.noneOf(GradleAttributes.OperatingSystemFamily.class);
for (var os : artifact.getOs()) {
var variant = GradleAttributes.OperatingSystemFamily.from(os);
if (variant != null)
osVariants.add(variant);
}
if (osVariants.isEmpty()) {
all.add(artifact);
} else {
for (var variant : osVariants) {
natives.computeIfAbsent(variant, _ -> new ArrayList<>()).add(artifact);
}
}
}
var json = JsonData.minecraftVersion(jsonTask.execute());
var java = json.javaVersion != null ? json.javaVersion.majorVersion : null;
Consumer<GradleModule.Variant> common = v -> {
v.attribute("org.gradle.status", "release")
.attribute("org.gradle.usage", "java-runtime")
.attribute("org.gradle.category", "library")
.attribute("org.gradle.dependency.bundling", "external")
.attribute("org.gradle.libraryelements", "jar")
.attribute("org.gradle.jvm.environment", "standard-jvm")
.attribute(Mappings.CHANNEL_ATTR, mappings.channel())
.attribute(Mappings.VERSION_ATTR, mappings.version())
;
if (java != null)
v.attribute("org.gradle.jvm.version", java);
v.deps(all);
};
var variants = new ArrayList<GradleModule.Variant>();
// TODO [MCMavenizer][Gradle Modules] Cannot have a common variant because it has incomplete dependencies (missing natives)
// Launching the game wouldn't work because of that. If we need a common variant, it would need to include everything
// But since FG7 will never not have the OS attribute, it wouldn't be used anyways.
//variants.add(GradleModule.Variant.of("classes", common));
for (var e : natives.entrySet()) {
var variant = GradleModule.Variant.of("classes-" + e.getKey().getValue(), common);
variant.attribute(e.getKey());
variant.deps(e.getValue());
variants.add(variant);
}
var apiVariant = GradleModule.Variant.of("api-classes", common);
apiVariant.attribute("org.gradle.usage", "java-api");
apiVariant.deps(extraCompileDeps);
variants.add(apiVariant);
return variants.toArray(new GradleModule.Variant[0]);
}
protected static Task simplePom(File build, Artifact artifact) {
return Task.named("pom[" + artifact.getName() + ']', () -> {
var output = new File(build, artifact.getName() + '-' + artifact.getVersion() + ".pom");
var cache = Util.cache(output);
if (Mavenizer.checkCache(output, cache))
return output;
var builder = new POMBuilder(artifact.getGroup(), artifact.getName(), artifact.getVersion());
FileUtils.ensureParent(output);
try (var os = new FileOutputStream(output)) {
os.write(builder.build().getBytes(StandardCharsets.UTF_8));
} catch (IOException | ParserConfigurationException | TransformerException e) {
Util.sneak(e);
}
cache.save();
return output;
});
}
public record PendingArtifact(
String message,
Task task,
Artifact artifact,
boolean auxiliary,
@Nullable Task variants
) implements Supplier<File> {
@Override
public File get() {
if (this.task.resolved())
return this.task.execute();
try {
LOGGER.info(this.message);
LOGGER.push();
return this.task.execute();
} finally {
if (this.variants != null)
this.variants.execute();
LOGGER.pop();
}
}
public Task getAsTask() {
return task;
}
public PendingArtifact withVariants(Supplier<GradleModule.Variant[]> variants) {
return new PendingArtifact(message, task, artifact, auxiliary, variantTask(task, variants));
}
}
/*
* Artifacts for the "mappings" object:
* channel-version.zip: Zip file containing csv's for classes,fields,methods akin to old MCP bot data
* channel-verson-map2obf.tsrg.gz: gzip compressed tsrg file for mapped names to obf (notch) names
* channel-verson-map2srg.tsrg.gz: gzip compressed tsrg file for mapped names to srg (intermediate) names
*/
protected List<PendingArtifact> mappingArtifacts(File cache, Mappings mappings, MCPSide side, Map<String, Supplier<String>> outputJson) {
if (outputJson != null) {
outputJson.put("mappings.channel", mappings::channel);
outputJson.put("mappings.version", mappings::version);
}
var coords = mappings.getArtifact(side);
var csvs = pending("Mappings Zip", mappings.getCsvZip(side), coords, false);
var pom = pending("Mappings POM", simplePom(cache, coords), coords.withExtension("pom"), false);
// Just create the zip and pom for unobfuscated versions.
if (!MCPConfigRepo.isObfuscated(side.getMCP().getMinecraftTasks().getVersion()))
return List.of(csvs, pom);
var m2o = pending("Mappings map2obf", mappings.getMapped2Obf(side), coords.withClassifier("map2obf").withExtension("tsrg.gz"), false);
var m2s = pending("Mappings map2srg", mappings.getMapped2Srg(side), coords.withClassifier("map2srg").withExtension("tsrg.gz"), false);
if (outputJson != null) {
outputJson.put("mappings.csv.artifact", csvs.artifact()::toString);
outputJson.put("mappings.csv.file", csvs.task().filePathSupplier());
outputJson.put("mappings.obf.artifact", m2o.artifact()::toString);
outputJson.put("mappings.obf.file", m2o.task().filePathSupplier());
outputJson.put("mappings.srg.artifact", m2s.artifact()::toString);
outputJson.put("mappings.srg.file", m2s.task().filePathSupplier());
}
return List.of(csvs, pom, m2o, m2s);
}
}