Skip to content

Commit

Permalink
Extract task to explicitly merge manifests for -mod
Browse files Browse the repository at this point in the history
  • Loading branch information
octylFractal authored and me4502 committed Jan 24, 2024
1 parent 75ab2d6 commit d533be4
Showing 1 changed file with 82 additions and 11 deletions.
93 changes: 82 additions & 11 deletions worldedit-mod/build.gradle.kts
@@ -1,19 +1,95 @@
import net.fabricmc.loom.task.RemapJarTask
import java.util.jar.Attributes
import java.util.jar.Manifest

plugins {
base
}

applyCommonConfiguration()

tasks.register<Jar>("jar") {
val remapFabric = project(":worldedit-fabric").tasks.named<RemapJarTask>("remapShadowJar")
open class MergeManifests : DefaultTask() {
@InputFiles
val inputManifests: ConfigurableFileCollection = project.objects.fileCollection()

@OutputFile
val outputManifest: RegularFileProperty = project.objects.fileProperty()

companion object {
private fun assertEqual(old: Any?, new: Any?, key: Attributes.Name): Any? {
assert(old == new) { "$key mismatch: $old != $new" }
return old
}

private fun throwException(old: Any?, new: Any?, key: Attributes.Name) {
throw IllegalStateException("Duplicate $key: $new")
}

private val MERGE_LOGIC = mapOf(
Attributes.Name.MANIFEST_VERSION to ::assertEqual,
Attributes.Name.IMPLEMENTATION_VERSION to ::assertEqual,
Attributes.Name.MAIN_CLASS to ::assertEqual,
Attributes.Name("WorldEdit-Version") to ::assertEqual,
Attributes.Name("WorldEdit-Kind") to ::assertEqual,
)
}

private fun mergeAttributes(aggregate: Attributes, input: Attributes) {
input.forEach { (key, value) ->
aggregate.merge(key, value) { old, new ->
val mergeLogic = MERGE_LOGIC[key] ?: ::throwException
mergeLogic(old, new, key as Attributes.Name)
}
}
}

@TaskAction
fun merge() {
val manifest = Manifest()
inputManifests.forEach { manifestFile ->
val inputManifest = manifestFile.inputStream().use { Manifest(it) }
mergeAttributes(manifest.mainAttributes, inputManifest.mainAttributes)
inputManifest.entries.forEach { (key, value) ->
val aggregate = manifest.entries.computeIfAbsent(key) { Attributes() }
mergeAttributes(aggregate, value)
}
}
outputManifest.asFile.get().outputStream().use {
manifest.write(it)
}
}
}

val fabricZipTree = zipTree(
project(":worldedit-fabric").tasks.named<RemapJarTask>("remapShadowJar").flatMap { it.archiveFile }
)
val forgeZipTree = zipTree(
project(":worldedit-forge").tasks.named("shadowJar").map { it.outputs.files.singleFile }
)

val mergeManifests = tasks.register<MergeManifests>("mergeManifests") {
dependsOn(
remapFabric,
project(":worldedit-fabric").tasks.named<RemapJarTask>("remapShadowJar"),
project(":worldedit-forge").tasks.named("reobfShadowJar")
)
from(zipTree({remapFabric.get().archiveFile}))
from(zipTree({project(":worldedit-forge").tasks.getByName("shadowJar").outputs.files.singleFile})) {
inputManifests.from(
fabricZipTree.matching { include("META-INF/MANIFEST.MF") },
forgeZipTree.matching { include("META-INF/MANIFEST.MF") }
)
outputManifest.set(project.layout.buildDirectory.file("mergeManifests/MANIFEST.MF"))
}

tasks.register<Jar>("jar") {
dependsOn(
project(":worldedit-fabric").tasks.named<RemapJarTask>("remapShadowJar"),
project(":worldedit-forge").tasks.named("reobfShadowJar"),
mergeManifests
)
from(fabricZipTree) {
exclude("META-INF/MANIFEST.MF")
}
from(forgeZipTree) {
exclude("META-INF/MANIFEST.MF")
// Duplicated first-party files
exclude("META-INF/services/org.enginehub.piston.CommandManagerService")
exclude("lang/")
Expand All @@ -35,12 +111,7 @@ tasks.register<Jar>("jar") {
exclude("pack.mcmeta")
}
manifest {
from(
{ zipTree({ remapFabric.get().archiveFile }).single { it.name == "MANIFEST.MF" } }
)
from(
{ zipTree({ project(":worldedit-forge").tasks.getByName("shadowJar").outputs.files.singleFile }).single { it.name == "MANIFEST.MF" } }
)
from(mergeManifests.flatMap { it.outputManifest })
}

duplicatesStrategy = DuplicatesStrategy.FAIL
Expand Down

0 comments on commit d533be4

Please sign in to comment.