Skip to content

Commit

Permalink
Override the default formatting of MANIFEST.MF files, so the files ar…
Browse files Browse the repository at this point in the history
…e always valid UTF-8

This was reported in gradle/gradle#5225
  • Loading branch information
floscher committed Feb 6, 2022
1 parent cbc938a commit 881551a
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.TaskProvider
import org.openstreetmap.josm.gradle.plugin.common.SplitStringToMaxByteLengthIterator
import org.openstreetmap.josm.gradle.plugin.config.JosmManifest
import org.openstreetmap.josm.gradle.plugin.i18n.io.LangFileDecoder
import org.openstreetmap.josm.gradle.plugin.i18n.io.MsgId
Expand All @@ -21,7 +22,6 @@ import java.io.File
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.util.jar.Manifest
import javax.inject.Inject

@CacheableTask
Expand Down Expand Up @@ -162,12 +162,23 @@ public open class GenerateJarManifest @Inject constructor(

@TaskAction
public fun action() {
Manifest().let { manifest ->
manifest.mainAttributes.putAll(predefinedAttributes.get())
outputFile.get().asFile.outputStream().use {
manifest.write(it)
outputFile.get().asFile.writeText(
predefinedAttributes.get().entries.joinToString("\n", postfix = "\n") {
mainAttributeLine(it.key, it.value)
}
)
}

private fun mainAttributeLine(key: String, value: String): String {
require(key.toByteArray().size <= 70) {
"Main attributes in a MANIFEST.MF file must not be longer than 70 bytes"
}
require(!key.contains(Regex("[:\n\r\u0000]"))) {
"Main attributes in a MANIFEST.MF file must not contain characters `CR`, `LF`, `NUL` or `:`!"
}
return SplitStringToMaxByteLengthIterator("$key: $value", 72, 71)
.asSequence()
.joinToString("\n ")
}

private sealed class RequiredAttribute {
Expand Down

0 comments on commit 881551a

Please sign in to comment.