Skip to content

Commit 98f744d

Browse files
committed
Copy over WriteManifest task (unused for now)
1 parent 863f747 commit 98f744d

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) Forge Development LLC and contributors
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
package net.minecraftforge.gradleutils;
6+
7+
import org.gradle.api.DefaultTask;
8+
import org.gradle.api.file.DuplicatesStrategy;
9+
import org.gradle.api.file.ProjectLayout;
10+
import org.gradle.api.file.RegularFileProperty;
11+
import org.gradle.api.java.archives.internal.ManifestInternal;
12+
import org.gradle.api.plugins.JavaPluginExtension;
13+
import org.gradle.api.provider.Property;
14+
import org.gradle.api.tasks.Input;
15+
import org.gradle.api.tasks.OutputFile;
16+
import org.gradle.api.tasks.SourceSet;
17+
import org.gradle.api.tasks.TaskAction;
18+
import org.gradle.api.tasks.TaskProvider;
19+
import org.gradle.jvm.tasks.Jar;
20+
import org.gradle.language.jvm.tasks.ProcessResources;
21+
import org.jetbrains.annotations.ApiStatus;
22+
23+
import javax.inject.Inject;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
import java.nio.file.Files;
27+
28+
@ApiStatus.Internal
29+
@ApiStatus.Experimental
30+
public abstract class WriteManifest extends DefaultTask implements GradleUtilsTask {
31+
protected abstract @Input Property<byte[]> getInputBytes();
32+
protected abstract @OutputFile RegularFileProperty getOutput();
33+
34+
protected abstract @Inject ProjectLayout getLayout();
35+
36+
@Inject
37+
public WriteManifest(TaskProvider<? extends Jar> jar) {
38+
// The output name is ALWAYS "MANIFEST.MF", and output cannot be changed
39+
this.getOutput().value(getLayout().getBuildDirectory().file(this.getName() + "/MANIFEST.MF")).disallowChanges();
40+
41+
var tasks = getProject().getTasks();
42+
var sourceSet = findSourceSet(jar.getName());
43+
44+
tasks.named(sourceSet.getProcessResourcesTaskName(), ProcessResources.class, processResources -> {
45+
processResources.dependsOn(this);
46+
processResources.from(this, copy -> {
47+
copy.into("META-INF");
48+
copy.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE);
49+
});
50+
});
51+
52+
afterEvaluate(project -> {
53+
try (var os = new ByteArrayOutputStream()) {
54+
// RATIONALE: ManifestInternal has not changed since Gradle 2.14
55+
// Due to the hacky nature of needing the proper manifest in the resources, this is the only good way of doing this
56+
// The DefaultManifest object cannot be serialized into the Gradle cache, and the normal Manifest interface does not have this method
57+
// This should be the only Gradle internals we need to use in all of ForgeDev, thankfully
58+
((ManifestInternal) jar.get().getManifest()).writeTo(os);
59+
this.getInputBytes().value(os.toByteArray()).finalizeValue();
60+
} catch (IOException e) {
61+
throw new RuntimeException(e);
62+
}
63+
});
64+
}
65+
66+
@TaskAction
67+
protected void exec() throws IOException {
68+
Files.write(
69+
this.getOutput().getAsFile().get().toPath(),
70+
this.getInputBytes().get()
71+
);
72+
}
73+
74+
private SourceSet findSourceSet(String jarTaskName) {
75+
var java = getProject().getExtensions().getByType(JavaPluginExtension.class);
76+
var candidates = java.getSourceSets().matching(sourceSet -> sourceSet.getJarTaskName().equals(jarTaskName)).iterator();
77+
return candidates.hasNext() ? candidates.next() : java.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
78+
}
79+
}

0 commit comments

Comments
 (0)