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