Skip to content

Commit 49f8c31

Browse files
GooolerCopilot
andauthored
Reuse most logic for writing entries (#1971)
* New File.remapClass * Reuse writeToZip * Update src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/RelocatorRemapper.kt Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Extension for FileCopyDetails * Reuse multiReleaseRegex --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent ed47d0c commit 49f8c31

2 files changed

Lines changed: 19 additions & 40 deletions

File tree

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/RelocatorRemapper.kt

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.github.jengelman.gradle.plugins.shadow.internal
22

33
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
4-
import com.github.jengelman.gradle.plugins.shadow.relocation.relocatePath
5-
import org.apache.tools.zip.UnixStat
6-
import org.apache.tools.zip.ZipOutputStream
74
import org.gradle.api.GradleException
85
import org.gradle.api.file.FileCopyDetails
96
import org.vafer.jdeb.shaded.objectweb.asm.ClassReader
@@ -13,15 +10,10 @@ import org.vafer.jdeb.shaded.objectweb.asm.commons.ClassRemapper
1310
import org.vafer.jdeb.shaded.objectweb.asm.commons.Remapper
1411

1512
/**
16-
* Applies remapping to the given class with the specified relocation path. The remapped class is
17-
* then written to the zip file.
13+
* Applies remapping to the given class file using the provided relocators and returns the
14+
* (possibly) remapped class bytes. If no remapping is required, the original bytes are returned.
1815
*/
19-
internal fun FileCopyDetails.remapClass(
20-
relocators: Set<Relocator>,
21-
zipOutStr: ZipOutputStream,
22-
preserveFileTimestamps: Boolean,
23-
lastModified: Long,
24-
) =
16+
internal fun FileCopyDetails.remapClass(relocators: Set<Relocator>): ByteArray =
2517
file.readBytes().let { bytes ->
2618
var modified = false
2719
val remapper = RelocatorRemapper(relocators) { modified = true }
@@ -42,20 +34,7 @@ internal fun FileCopyDetails.remapClass(
4234
}
4335

4436
// If we didn't need to change anything, keep the original bytes as-is.
45-
val newBytes = if (modified) cw.toByteArray() else bytes
46-
47-
// Temporarily remove the multi-release prefix.
48-
val multiReleasePrefix = "^META-INF/versions/\\d+/".toRegex().find(path)?.value.orEmpty()
49-
val newPath = path.replace(multiReleasePrefix, "")
50-
val relocatedPath = multiReleasePrefix + relocators.relocatePath(newPath)
51-
val entry =
52-
zipEntry(relocatedPath, preserveFileTimestamps, lastModified) {
53-
unixMode = UnixStat.FILE_FLAG or permissions.toUnixNumeric()
54-
}
55-
// Now we put it back on so the class file is written out with the right extension.
56-
zipOutStr.putNextEntry(entry)
57-
zipOutStr.write(newBytes)
58-
zipOutStr.closeEntry()
37+
if (modified) cw.toByteArray() else bytes
5938
}
6039

6140
/**

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,13 @@ constructor(
174174
if (relocators.isEmpty()) {
175175
fileDetails.writeToZip(path)
176176
} else {
177-
fileDetails.remapClass(
178-
relocators = relocators,
179-
zipOutStr = zipOutStr,
180-
preserveFileTimestamps = preserveFileTimestamps,
181-
lastModified = fileDetails.lastModified,
182-
)
177+
with(fileDetails) {
178+
// Temporarily remove the multi-release prefix.
179+
val multiReleasePrefix = multiReleaseRegex.find(path)?.value.orEmpty()
180+
val newPath = path.replace(multiReleasePrefix, "")
181+
val relocatedPath = multiReleasePrefix + relocators.relocatePath(newPath)
182+
writeToZip(entryName = relocatedPath, bytes = remapClass(relocators = relocators))
183+
}
183184
}
184185
}
185186
enableKotlinModuleRemapping && path.endsWith(".kotlin_module") -> {
@@ -246,13 +247,7 @@ constructor(
246247
// matter to the compiler.
247248
else -> path.replace(".kotlin_module", ".shadow.kotlin_module")
248249
}
249-
val entry =
250-
zipEntry(entryName, preserveFileTimestamps, lastModified) {
251-
unixMode = UnixStat.FILE_FLAG or permissions.toUnixNumeric()
252-
}
253-
zipOutStr.putNextEntry(entry)
254-
zipOutStr.write(newBytes)
255-
zipOutStr.closeEntry()
250+
writeToZip(entryName = entryName, bytes = newBytes)
256251
}
257252

258253
private fun transform(fileDetails: FileCopyDetails, path: String): Boolean {
@@ -265,19 +260,24 @@ constructor(
265260
return true
266261
}
267262

268-
private fun FileCopyDetails.writeToZip(entryName: String) {
263+
private fun FileCopyDetails.writeToZip(entryName: String, bytes: ByteArray? = null) {
269264
val entry =
270265
zipEntry(entryName, preserveFileTimestamps, lastModified) {
271266
unixMode = UnixStat.FILE_FLAG or permissions.toUnixNumeric()
272267
}
273268
zipOutStr.putNextEntry(entry)
274-
copyTo(zipOutStr)
269+
if (bytes == null) {
270+
copyTo(zipOutStr)
271+
} else {
272+
zipOutStr.write(bytes)
273+
}
275274
zipOutStr.closeEntry()
276275
}
277276
}
278277

279278
public companion object {
280279
private val logger = Logging.getLogger(ShadowCopyAction::class.java)
280+
private val multiReleaseRegex = "^META-INF/versions/\\d+/".toRegex()
281281

282282
private val ZipOutputStream.entries: List<ZipEntry>
283283
get() =

0 commit comments

Comments
 (0)