forked from MinecraftForge/MinecraftMavenizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinecraftMaven.java
More file actions
618 lines (532 loc) · 26.1 KB
/
MinecraftMaven.java
File metadata and controls
618 lines (532 loc) · 26.1 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.mcmaven.impl;
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.Repo;
import net.minecraftforge.mcmaven.impl.repo.Repo.PendingArtifact;
import net.minecraftforge.mcmaven.impl.repo.forge.FGVersion;
import net.minecraftforge.mcmaven.impl.repo.forge.ForgeRepo;
import net.minecraftforge.mcmaven.impl.repo.mcpconfig.MCP;
import net.minecraftforge.mcmaven.impl.repo.mcpconfig.MCPConfigRepo;
import net.minecraftforge.mcmaven.impl.repo.mcpconfig.MinecraftTasks;
import net.minecraftforge.mcmaven.impl.util.Artifact;
import net.minecraftforge.mcmaven.impl.util.ComparableVersion;
import net.minecraftforge.mcmaven.impl.util.Constants;
import net.minecraftforge.mcmaven.impl.util.POMBuilder;
import net.minecraftforge.mcmaven.impl.util.ProcessUtils;
import net.minecraftforge.mcmaven.impl.util.Task;
import net.minecraftforge.mcmaven.impl.util.Util;
import net.minecraftforge.srgutils.MinecraftVersion;
import net.minecraftforge.util.data.json.JsonData;
import net.minecraftforge.util.file.FileUtils;
import net.minecraftforge.util.hash.HashUtils;
import static net.minecraftforge.mcmaven.impl.Mavenizer.LOGGER;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Supplier;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.jetbrains.annotations.Nullable;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
// TODO [MCMavenizer][Deobf] ADD DEOBF
// use single detached configuration to resolve individual configurations
// pass in downloaded files to mcmaven (absolute path)
public record MinecraftMaven(
File output,
boolean dependenciesOnly,
Cache cache,
Mappings mappings,
Map<String, String> foreignRepositories,
boolean globalAuxiliaryVariants,
boolean disableGradle,
boolean stubJars,
Set<String> mcpConfigVersions,
List<File> accessTransformer,
List<File> facadeConfigs,
@Nullable File outputJsonFile
) {
// Only 1.14.4+ has official mappings, we can support more when we add more mappings
private static final MinecraftVersion MIN_OFFICIAL_MAPPINGS = MinecraftVersion.from("1.14.4");
private static final ComparableVersion MIN_SUPPORTED_FORGE = new ComparableVersion("1.14.4");
public MinecraftMaven {
LOGGER.info(" Output: " + output.getAbsolutePath());
if (outputJsonFile != null)
LOGGER.info(" Output JSON: " + outputJsonFile.getAbsolutePath());
LOGGER.info(" Dependencies Only: " + dependenciesOnly);
LOGGER.info(" Cache: " + cache.root().getAbsolutePath());
LOGGER.info(" JDK Cache: " + cache.jdks().root().getAbsolutePath());
LOGGER.info(" Offline: " + Mavenizer.isOffline());
LOGGER.info(" Cache Only: " + Mavenizer.isCacheOnly());
LOGGER.info(" Ignore Cache: " + Mavenizer.ignoreCache());
LOGGER.info(" Mappings: " + mappings);
if (!foreignRepositories.isEmpty())
LOGGER.info(" Foreign Repos: [" + String.join(", ", foreignRepositories.values()) + ']');
LOGGER.info(" GradleVariantHack: " + globalAuxiliaryVariants);
LOGGER.info(" Disable Gradle: " + disableGradle);
LOGGER.info(" Stub Jars: " + stubJars);
if (!accessTransformer.isEmpty())
filter(" Access Transformer: ", accessTransformer);
if (!facadeConfigs.isEmpty())
filter(" Facade Config: ", facadeConfigs);
LOGGER.info();
}
private static void filter(String header, List<File> files) {
var prefix = header;
var itor = files.iterator();
while (itor.hasNext()) {
var file = itor.next();
LOGGER.getInfo().print(prefix);
if (prefix == header)
prefix = " ".repeat(header.length());
LOGGER.getInfo().print(file.getAbsolutePath());
if (!file.exists()) {
LOGGER.getInfo().print(" SKIPPING DOESN'T EXIST");
itor.remove();
}
LOGGER.getInfo().println();
}
LOGGER.getInfo().println();
}
public void run(Artifact artifact) {
var module = artifact.getGroup() + ':' + artifact.getName();
var version = artifact.getVersion();
LOGGER.info("Processing Minecraft dependency: %s:%s".formatted(module, version));
Map<String, Supplier<String>> outputJson = null;
if (outputJsonFile != null) {
outputJson = new HashMap<>();
outputJson.put("spec", () -> "1");
}
var mcprepo = new MCPConfigRepo(this.cache, dependenciesOnly);
if (Constants.FORGE_GROUP.equals(artifact.getGroup()) && Constants.FORGE_NAME.equals(artifact.getName())) {
var repo = new ForgeRepo(this.cache, mcprepo);
createForge(artifact, mcprepo, repo, outputJson);
} else if (Constants.MC_GROUP.equals(artifact.getGroup())) {
createMinecraft(artifact, mcprepo, outputJson);
} else {
throw new IllegalArgumentException("Artifact '%s' is currently Unsupported. Will add later".formatted(module));
}
if (outputJson != null) {
var finalized = new TreeMap<String, String>();
for (var entry : outputJson.entrySet())
finalized.put(entry.getKey(), entry.getValue().get());
var parent = outputJsonFile.getParentFile();
if (parent != null && !parent.exists())
parent.mkdirs();
try (var writer = new FileWriter(outputJsonFile)) {
Util.GSON.toJson(finalized, writer);
} catch (IOException e) {
LOGGER.error("Failed to write output json file: " + outputJsonFile.getAbsolutePath(), e);
Util.sneak(e);
}
}
}
protected void createForge(Artifact artifact, MCPConfigRepo mcprepo, ForgeRepo repo, Map<String, Supplier<String>> outputJson) {
if (dependenciesOnly)
throw new IllegalArgumentException("ForgeRepo doesn't currently support dependenciesOnly");
var version = artifact.getVersion();
if (version == null)
throw new IllegalArgumentException("No version specified for Forge");
if ("all".equals(version)) {
if (outputJson != null)
throw new IllegalArgumentException("Output Json does not support bulk operations");
var versions = this.cache.maven().getVersions(artifact);
var mappingCache = new HashMap<String, Mappings>();
for (var ver : versions.reversed()) {
var cver = new ComparableVersion(ver);
if (cver.compareTo(MIN_SUPPORTED_FORGE) < 0)
continue;
var fg = FGVersion.fromForge(ver);
if (fg == null || fg.ordinal() < FGVersion.v3.ordinal()) // Unsupported
continue;
var mappings = mappingCache.computeIfAbsent(forgeToMcVersion(ver), this.mappings::withMCVersion);
var art = artifact.withVersion(ver);
var artifacts = repo.process(art, mappings, outputJson);
finalize(art, mappings, artifacts);
}
} else {
var mappings = this.mappings.withMCVersion(forgeToMcVersion(version));
var artifacts = repo.process(artifact, mappings, outputJson);
finalize(artifact, mappings, artifacts);
}
}
protected void createMinecraft(Artifact artifact, MCPConfigRepo mcprepo, Map<String, Supplier<String>> outputJson) {
if (artifact.getVersion() == null)
throw new IllegalArgumentException("No version specified for MCPConfig");
var version = artifact.getVersion();
if (version == null)
throw new IllegalArgumentException("No version specified for Forge");
if ("all".equals(version)) {
if (outputJson != null)
throw new IllegalArgumentException("Output Json does not support bulk operations");
var manifestFile = mcprepo.getLauncherManifestTask().execute();
var manifest = JsonData.launcherManifest(manifestFile);
for (var ver : manifest.versions) {
try {
var cver = MinecraftVersion.from(ver.id);
if (cver.compareTo(MIN_OFFICIAL_MAPPINGS) < 0)
continue;
} catch (IllegalArgumentException e) {
// Invalid/unknown version, so skip.
continue;
}
var versioned = artifact.withVersion(ver.id);
// If there is no MCPConfig, then we just produce a official named jar
List<PendingArtifact> artifacts = null;
if (hasMcp(mcprepo, ver.id))
artifacts = mcprepo.process(versioned, mappings.withMCVersion(ver.id), outputJson);
else if (mappings.channel().equals("official") && (hasOfficialMappings(mcprepo, ver.id) || !MCPConfigRepo.isObfuscated(ver.id)))
artifacts = mcprepo.processWithoutMcp(versioned, mappings.withMCVersion(ver.id), outputJson);
else {
LOGGER.info("Skipping " + versioned + " no mcp config");
continue;
}
finalize(versioned, mappings, artifacts);
}
} else {
var mcVersion = mcpToMcVersion(version);
var mappings = this.mappings.withMCVersion(mcVersion);
List<PendingArtifact> artifacts = null;
if (hasMcp(mcprepo, version))
artifacts = mcprepo.process(artifact, mappings, outputJson);
else if (mappings.channel().equals("official") && (hasOfficialMappings(mcprepo, mcVersion) || !MCPConfigRepo.isObfuscated(mcVersion)))
artifacts = mcprepo.processWithoutMcp(artifact, mappings, outputJson);
else
throw new IllegalStateException("Can not process " + artifact + " as it does not have a MCPConfig ror official mappings");
finalize(artifact, mappings, artifacts);
}
}
// This is ugly, but there really isn't a good way to check if a file exists. DownlodUtils could probably use a 'exists' that HEAD's and returns false non 404
private boolean hasMcp(MCPConfigRepo repo, String version) {
if (this.mcpConfigVersions.isEmpty()) {
var versions = repo.getCache().maven().getVersions(MCP.artifact("1.21.11"));
this.mcpConfigVersions.addAll(versions);
}
return this.mcpConfigVersions.contains(version);
}
// No official mappings, we can't do anything
private static boolean hasOfficialMappings(MCPConfigRepo repo, String version) {
var tasks = repo.getMCTasks(version);
var versionF = tasks.versionJson.execute();
var json = JsonData.minecraftVersion(versionF);
return json.getDownload(MinecraftTasks.MCFile.CLIENT_MAPPINGS.key) != null ||
json.getDownload(MinecraftTasks.MCFile.SERVER_MAPPINGS.key) != null;
}
private static String forgeToMcVersion(String version) {
// Save for a few april-fools versions, Minecraft doesn't use _ in their version names.
// So when Forge needs to reference a version of Minecraft that uses - in the name, it replaces
// it with _
// This could cause issues if we ever support a version with _ in it, but fuck it I don't care right now.
int idx = version.indexOf('-');
if (idx == -1)
throw new IllegalArgumentException("Invalid Forge version: " + version);
return version.substring(0, idx).replace('_', '-');
}
public static String mcpToMcVersion(String version) {
// MCP names can either be {MCVersion} or {MCVersion}-{Timestamp}, EXA: 1.21.1-20240808.132146
// So lets see if the thing following the last - matches a timestamp
int idx = version.lastIndexOf('-');
if (idx < 0)
return version;
if (!version.substring(idx + 1).matches("\\d{8}\\.\\d{6}"))
return version;
return version.substring(0, idx);
}
protected void finalize(Artifact module, Mappings mappings, List<Repo.PendingArtifact> artifacts) {
var variants = new HashSet<Artifact>();
for (var pending : artifacts) {
if (pending == null)
continue;
// Basically, I want to support multiple variants of a Forge dep.
// Simplest case would be different mapping channels.
// I haven't added an opt-in for making artifacts that use mappings, so just assume any artifact with variants
var artifact = pending.artifact();
var isJar = "jar".equals(artifact.getExtension());
var isSources = "sources".equals(artifact.getClassifier());
if (isJar) {
// No sources allowed in stub repos
if (stubJars && isSources)
continue;
// Only transform main artifacts - This should be the main artifact but that may be a breaking change
if (!accessTransformer.isEmpty() && artifact.getClassifier() == null)
pending = pending.withTask(accessTransform(pending));
// The main artifact
if (artifact.equals(module)) {
if (!facadeConfigs.isEmpty())
pending = pending.withTask(facade(pending));
}
if (stubJars)
pending = pending.withTask(stub(pending));
}
String suffix = null;
if (!disableGradle && pending.variants() != null && !mappings.isPrimary()) {
// If we are not the primary mapping, but we haven't generated the primary mapping yet, do so.
// This will duplicate the files. But the other option is to require not writing the variants until the primary mapping is requested.
var primaryTarget = new File(this.output, artifact.getLocalPath());
if (!primaryTarget.exists())
updateFile(primaryTarget, pending.get(), pending.artifact());
suffix = mappings.channel() + '-' + mappings.version();
if (artifact.getClassifier() == null)
artifact = artifact.withClassifier(suffix);
else
artifact = artifact.withClassifier(artifact.getClassifier() + '-' + suffix);
}
var target = new File(this.output, artifact.getLocalPath());
updateFile(target, pending.get(), pending.artifact());
var varTarget = new File(this.output, artifact.getLocalPath() + ".variants");
if (!disableGradle && pending.variants() != null) {
var source = pending.variants().execute();
var cache = Util.cache(varTarget)
.add("source", source);
if (!Mavenizer.checkCache(varTarget, cache)) {
variants.add(Artifact.from(artifact.getGroup(), artifact.getName(), artifact.getVersion()));
try {
GradleModule.Variant[] data = JsonData.fromJson(source, GradleModule.Variant[].class);
if (!dependenciesOnly) {
var file = new GradleModule.Variant.File(target);
for (var variant : data) {
variant.file(file);
if (suffix != null && !(pending.auxiliary() && this.globalAuxiliaryVariants))
variant.name = variant.name + '-' + suffix;
}
}
// Sort them to make it predictable/easy to diff
Arrays.sort(data, (a, b) -> a.name.compareTo(b.name));
JsonData.toJson(data, varTarget);
cache.save();
} catch (Throwable t) {
throw new RuntimeException("Failed to write artifact variants: %s".formatted(artifact), t);
}
}
}
}
for (var artifact : variants) {
updateVariants(artifact);
}
}
private void updateFile(File target, File source, Artifact artifact) {
var cache = Util.cache(target)
.add("source", source);
var isPom = "pom".equals(artifact.getExtension());
boolean write;
if (isPom) {
cache.addKnown("disableGradle", Boolean.toString(disableGradle));
// Write the pom for non-primary mappings if we haven't generated primary mappings yet
write = !target.exists() || ((disableGradle || mappings.isPrimary()) && !cache.isSame());
} else {
write = !target.exists() || !cache.isSame();
}
if (Mavenizer.ignoreCache())
write = true;
if (write) {
try {
if (disableGradle && isPom) {
makeNonGradlePom(source, target);
} else {
org.apache.commons.io.FileUtils.copyFile(source, target);
}
HashUtils.updateHash(target);
cache.save();
} catch (Throwable t) {
throw new RuntimeException("Failed to generate artifact: %s".formatted(artifact), t);
}
}
}
private Task stub(PendingArtifact pending) {
var input = pending.task();
return Task.named("stub", Task.deps(input), () -> stub(input, pending.artifact()));
}
private File stub(Task inputTask, Artifact artifact) {
var tool = this.cache.maven().download(Constants.STUBIFY);
var target = new File(this.cache.localCache(), artifact.appendClassifier("stub").getLocalPath());
var input = inputTask.execute();
var cache = Util.cache(target)
.add("tool", tool)
.add("input", input);
if (Mavenizer.checkCache(target, cache))
return target;
var args = List.of("--input", input.getAbsolutePath(), "--output", target.getAbsolutePath());
execute("stubify", Constants.STUBIFY_JAVA_VERSION, tool, args, target);
cache.save();
return target;
}
private Task accessTransform(PendingArtifact pending) {
var input = pending.task();
return Task.named("access-transform", Task.deps(input), () -> accessTransform(input, pending.artifact()));
}
private File accessTransform(Task inputTask, Artifact artifact) {
var tool = this.cache.maven().download(Constants.ACCESS_TRANSFORMER);
var target = new File(this.cache.localCache(), artifact.appendClassifier("ated").getLocalPath());
var input = inputTask.execute();
var cache = Util.cache(target)
.add("tool", tool)
.add(accessTransformer)
.add("input", input);
if (Mavenizer.checkCache(target, cache))
return target;
var args = new ArrayList<>(List.of(
"--inJar", input.getAbsolutePath(),
"--outJar", target.getAbsolutePath()
));
for (var file : accessTransformer) {
if (!file.exists())
throw new IllegalStateException("Access Transformer config does not exist: " + file.getAbsolutePath());
args.add("--atfile");
args.add(file.getAbsolutePath());
}
execute("Access Transform", Constants.ACCESS_TRANSFORMER_JAVA_VERSION, tool, args, target);
cache.save();
return target;
}
private Task facade(PendingArtifact pending) {
var input = pending.task();
return Task.named("facade", Task.deps(input), () -> facade(input, pending.artifact()));
}
private File facade(Task inputTask, Artifact artifact) {
var tool = this.cache.maven().download(Constants.FACADE);
var target = new File(this.cache.localCache(), artifact.appendClassifier("facade").getLocalPath());
var input = inputTask.execute();
var cache = Util.cache(target)
.add("tool", tool)
.add(facadeConfigs)
.add("input", input);
if (Mavenizer.checkCache(target, cache))
return target;
var args = new ArrayList<>(List.of(
"--input", input.getAbsolutePath(),
"--output", target.getAbsolutePath()
));
for (var file : facadeConfigs) {
if (!file.exists())
throw new IllegalStateException("Facade config does not exist: " + file.getAbsolutePath());
args.add("--config");
args.add(file.getAbsolutePath());
}
execute("facade ", Constants.FACAD_JAVA_VERSION, tool, args, target);
cache.save();
return target;
}
private void execute(String name, int javaVersion, File tool, List<String> args, File target) {
File jdk;
try {
jdk = this.cache.jdks().get(javaVersion);
} catch (Exception e) {
throw new IllegalStateException("Failed to find JDK for version " + javaVersion, e);
}
// Older versions of AT have a bug where it wont create the directories as needed.
var parent = target.getParentFile();
if (parent != null && !parent.exists())
parent.mkdirs();
var log = new File(target.getAbsolutePath() + ".log");
var ret = ProcessUtils.runJar(jdk, parent, log, tool, Collections.emptyList(), args);
if (ret.exitCode != 0)
throw new IllegalStateException("Failed to " + name + " file (exit code " + ret.exitCode + "), See log: " + log.getAbsolutePath());
}
private void updateVariants(Artifact artifact) {
var root = new File(this.output, artifact.getFolder());
var inputs = new ArrayList<File>();
for (var file : root.listFiles()) {
if (file.isFile() && file.getName().endsWith(".variants")) {
inputs.add(file);
}
}
var target = new File(this.output, artifact.withExtension("module").getLocalPath());
var cache = Util.cache(target);
for (var input : inputs) {
cache.add(input);
}
if (Mavenizer.checkCache(target, cache))
return;
var module = GradleModule.of(artifact);
for (var input : inputs) {
try {
var data = JsonData.fromJson(input, GradleModule.Variant[].class);
for (var variant : data)
module.variant(variant);
} catch (Throwable t) {
throw new RuntimeException("Failed to read artifact variants: %s".formatted(input), t);
}
}
try {
JsonData.toJson(module, target);
HashUtils.updateHash(target);
cache.save();
} catch (Throwable t) {
throw new RuntimeException("Failed to write artifact module: %s".formatted(artifact), t);
}
}
private void makeNonGradlePom(File source, File target) throws IOException, SAXException, ParserConfigurationException, TransformerException {
var docFactory = DocumentBuilderFactory.newInstance();
var docBuilder = docFactory.newDocumentBuilder();
var doc = docBuilder.parse(source);
var modified = false;
var projects = doc.getElementsByTagName("project");
for (var x = 0; x < projects.getLength(); x++) {
var project = projects.item(x);
var children = project.getChildNodes();
for (int y = 0; y < children.getLength(); y++) {
var child = children.item(y);
if (child.getNodeType() == Node.COMMENT_NODE) {
var content = child.getTextContent();
if (content.equals(POMBuilder.GRADLE_MAGIC_COMMENT)) {
project.removeChild(child);
y--;
modified = true;
}
}
}
}
if (!modified) {
org.apache.commons.io.FileUtils.copyFile(source, target);
return;
}
var transformerFactory = TransformerFactory.newInstance();
var transformer = transformerFactory.newTransformer(new StreamSource(new StringReader(
// This needs to strip spacing so our output doesn't have extra whitespace
"""
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
"""
)));
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //Make it pretty
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
var output = new ByteArrayOutputStream();
transformer.transform(new DOMSource(doc), new StreamResult(output));
var data = output.toString().getBytes(StandardCharsets.UTF_8);
FileUtils.ensureParent(target);
try (var os = new FileOutputStream(target)) {
os.write(data);
}
}
}