|
| 1 | +import java.net.URI |
| 2 | +import java.nio.file.FileSystems |
| 3 | +import java.nio.file.Files |
| 4 | +import java.util.LinkedHashSet |
| 5 | +import java.util.zip.ZipFile |
| 6 | +import org.gradle.api.Action |
| 7 | +import org.gradle.api.Task |
| 8 | +import org.gradle.api.file.FileCollection |
| 9 | +import org.gradle.api.tasks.bundling.AbstractArchiveTask |
| 10 | + |
| 11 | +class MergeSpringMetadataAction( |
| 12 | + private val runtimeClasspath: FileCollection, |
| 13 | + private val springMetadataFiles: List<String>, |
| 14 | +) : Action<Task> { |
| 15 | + |
| 16 | + override fun execute(task: Task) { |
| 17 | + val archiveTask = task as AbstractArchiveTask |
| 18 | + val jar = archiveTask.archiveFile.get().asFile |
| 19 | + val runtimeJars = runtimeClasspath.files.filter { it.name.endsWith(".jar") } |
| 20 | + val uri = URI.create("jar:${jar.toURI()}") |
| 21 | + |
| 22 | + FileSystems.newFileSystem(uri, mapOf("create" to "false")).use { fs -> |
| 23 | + springMetadataFiles.forEach { entryPath -> |
| 24 | + val target = fs.getPath(entryPath) |
| 25 | + val contents = mutableListOf<String>() |
| 26 | + |
| 27 | + if (Files.exists(target)) { |
| 28 | + contents.add(Files.readString(target)) |
| 29 | + } |
| 30 | + |
| 31 | + runtimeJars.forEach { depJar -> |
| 32 | + try { |
| 33 | + ZipFile(depJar).use { zip -> |
| 34 | + val entry = zip.getEntry(entryPath) |
| 35 | + if (entry != null) { |
| 36 | + contents.add(zip.getInputStream(entry).bufferedReader().readText()) |
| 37 | + } |
| 38 | + } |
| 39 | + } catch (_: Exception) { |
| 40 | + // Ignore non-zip files on the runtime classpath. |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + val merged = |
| 45 | + when { |
| 46 | + entryPath == "META-INF/spring.factories" -> mergeListProperties(contents) |
| 47 | + entryPath.endsWith(".imports") -> mergeLineBasedMetadata(contents) |
| 48 | + else -> mergeMapProperties(contents) |
| 49 | + } |
| 50 | + |
| 51 | + if (merged.isNotEmpty()) { |
| 52 | + if (target.parent != null) { |
| 53 | + Files.createDirectories(target.parent) |
| 54 | + } |
| 55 | + Files.write(target, merged.toByteArray()) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + val serviceEntries = linkedSetOf<String>() |
| 60 | + |
| 61 | + runtimeJars.forEach { depJar -> |
| 62 | + try { |
| 63 | + ZipFile(depJar).use { zip -> |
| 64 | + val entries = zip.entries() |
| 65 | + while (entries.hasMoreElements()) { |
| 66 | + val entry = entries.nextElement() |
| 67 | + if (!entry.isDirectory && entry.name.startsWith("META-INF/services/")) { |
| 68 | + serviceEntries.add(entry.name) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } catch (_: Exception) { |
| 73 | + // Ignore non-zip files on the runtime classpath. |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + serviceEntries.forEach { entryPath -> |
| 78 | + val providers = LinkedHashSet<String>() |
| 79 | + val target = fs.getPath(entryPath) |
| 80 | + |
| 81 | + if (Files.exists(target)) { |
| 82 | + Files.newBufferedReader(target).useLines { lines -> |
| 83 | + lines.forEach { line -> |
| 84 | + val provider = line.trim() |
| 85 | + if (provider.isNotEmpty() && !provider.startsWith("#")) { |
| 86 | + providers.add(provider) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + runtimeJars.forEach { depJar -> |
| 93 | + try { |
| 94 | + ZipFile(depJar).use { zip -> |
| 95 | + val entry = zip.getEntry(entryPath) |
| 96 | + if (entry != null) { |
| 97 | + zip.getInputStream(entry).bufferedReader().useLines { lines -> |
| 98 | + lines.forEach { line -> |
| 99 | + val provider = line.trim() |
| 100 | + if (provider.isNotEmpty() && !provider.startsWith("#")) { |
| 101 | + providers.add(provider) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } catch (_: Exception) { |
| 108 | + // Ignore non-zip files on the runtime classpath. |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if (providers.isNotEmpty()) { |
| 113 | + if (target.parent != null) { |
| 114 | + Files.createDirectories(target.parent) |
| 115 | + } |
| 116 | + Files.write(target, providers.joinToString(separator = "\n", postfix = "\n").toByteArray()) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private fun mergeLineBasedMetadata(contents: List<String>): String { |
| 123 | + val lines = LinkedHashSet<String>() |
| 124 | + |
| 125 | + contents.forEach { content -> |
| 126 | + content.lineSequence().forEach { rawLine -> |
| 127 | + val line = rawLine.trim() |
| 128 | + if (line.isNotEmpty() && !line.startsWith("#")) { |
| 129 | + lines.add(line) |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return if (lines.isEmpty()) "" else lines.joinToString(separator = "\n", postfix = "\n") |
| 135 | + } |
| 136 | + |
| 137 | + private fun mergeMapProperties(contents: List<String>): String { |
| 138 | + val merged = linkedMapOf<String, String>() |
| 139 | + |
| 140 | + contents.forEach { content -> |
| 141 | + parseProperties(content).forEach { (key, value) -> |
| 142 | + merged[key] = value |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return if (merged.isEmpty()) { |
| 147 | + "" |
| 148 | + } else { |
| 149 | + merged.entries.joinToString(separator = "\n", postfix = "\n") { (key, value) -> "$key=$value" } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private fun mergeListProperties(contents: List<String>): String { |
| 154 | + val merged = linkedMapOf<String, LinkedHashSet<String>>() |
| 155 | + |
| 156 | + contents.forEach { content -> |
| 157 | + parseProperties(content).forEach { (key, value) -> |
| 158 | + val values = merged.getOrPut(key) { LinkedHashSet() } |
| 159 | + value |
| 160 | + .split(',') |
| 161 | + .map(String::trim) |
| 162 | + .filter(String::isNotEmpty) |
| 163 | + .forEach(values::add) |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return if (merged.isEmpty()) { |
| 168 | + "" |
| 169 | + } else { |
| 170 | + merged.entries.joinToString(separator = "\n", postfix = "\n") { (key, values) -> |
| 171 | + "$key=${values.joinToString(separator = ",")}" |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private fun parseProperties(content: String): List<Pair<String, String>> { |
| 177 | + val logicalLines = mutableListOf<String>() |
| 178 | + val current = StringBuilder() |
| 179 | + |
| 180 | + content.lineSequence().forEach { rawLine -> |
| 181 | + val line = rawLine.trim() |
| 182 | + if (current.isEmpty() && (line.isEmpty() || line.startsWith("#") || line.startsWith("!"))) { |
| 183 | + return@forEach |
| 184 | + } |
| 185 | + |
| 186 | + val normalized = if (current.isEmpty()) line else line.trimStart() |
| 187 | + current.append( |
| 188 | + if (endsWithContinuation(rawLine)) normalized.dropLast(1) else normalized, |
| 189 | + ) |
| 190 | + |
| 191 | + if (!endsWithContinuation(rawLine)) { |
| 192 | + logicalLines.add(current.toString()) |
| 193 | + current.setLength(0) |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + if (current.isNotEmpty()) { |
| 198 | + logicalLines.add(current.toString()) |
| 199 | + } |
| 200 | + |
| 201 | + return logicalLines.mapNotNull { line -> |
| 202 | + val separatorIndex = line.indexOfFirst { it == '=' || it == ':' } |
| 203 | + if (separatorIndex <= 0) { |
| 204 | + null |
| 205 | + } else { |
| 206 | + line.substring(0, separatorIndex).trim() to line.substring(separatorIndex + 1).trim() |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + private fun endsWithContinuation(line: String): Boolean { |
| 212 | + var backslashCount = 0 |
| 213 | + |
| 214 | + for (index in line.length - 1 downTo 0) { |
| 215 | + if (line[index] == '\\') { |
| 216 | + backslashCount++ |
| 217 | + } else { |
| 218 | + break |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + return backslashCount % 2 == 1 |
| 223 | + } |
| 224 | +} |
0 commit comments