Skip to content

Commit 05a47b5

Browse files
authored
Add extensions for Iterable<Relocator> (#1710)
* New `CompositeRelocator` * Remove `RelocatorRemapper.mapPath` * Cleanups * Replace `CompositeRelocator` with `relocatePath` extension * Add `relocateClass` extension * Cleanups * Update changelog
1 parent 575fece commit 05a47b5

File tree

8 files changed

+47
-52
lines changed

8 files changed

+47
-52
lines changed

api/shadow.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ public final class com/github/jengelman/gradle/plugins/shadow/relocation/Relocat
106106

107107
public final class com/github/jengelman/gradle/plugins/shadow/relocation/RelocationContextKt {
108108
public static final fun relocateClass (Lcom/github/jengelman/gradle/plugins/shadow/relocation/Relocator;Ljava/lang/String;)Ljava/lang/String;
109+
public static final fun relocateClass (Ljava/lang/Iterable;Ljava/lang/String;)Ljava/lang/String;
109110
public static final fun relocatePath (Lcom/github/jengelman/gradle/plugins/shadow/relocation/Relocator;Ljava/lang/String;)Ljava/lang/String;
111+
public static final fun relocatePath (Ljava/lang/Iterable;Ljava/lang/String;)Ljava/lang/String;
110112
}
111113

112114
public abstract interface class com/github/jengelman/gradle/plugins/shadow/relocation/Relocator {

docs/changes/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
## [Unreleased](https://github.com/GradleUp/shadow/compare/9.1.0...HEAD) - 2025-xx-xx
55

6+
### Added
7+
8+
- Add extensions for `Iterable<Relocator>`. ([#1710](https://github.com/GradleUp/shadow/pull/1710))
9+
610
### Changed
711

812
- Merge Gradle Module descriptors into the modern `META-INF` path. ([#1706](https://github.com/GradleUp/shadow/pull/1706))

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ internal class RelocatorRemapper(
2828

2929
override fun map(internalName: String): String = mapName(internalName)
3030

31-
fun mapPath(path: String): String {
32-
val dotIndex = path.indexOf('.')
33-
return if (dotIndex == -1) path else map(path.take(dotIndex))
34-
}
35-
3631
private fun mapName(name: String, mapLiterals: Boolean = false): String {
3732
val newName = mapNameImpl(name, mapLiterals)
3833
if (newName != name) {

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/RelocationContext.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,21 @@ public fun Relocator.relocateClass(className: String): String {
1515
public fun Relocator.relocatePath(path: String): String {
1616
return relocatePath(RelocatePathContext(path))
1717
}
18+
19+
public fun Iterable<Relocator>.relocateClass(className: String): String {
20+
forEach { relocator ->
21+
if (relocator.canRelocateClass(className)) {
22+
return relocator.relocateClass(className)
23+
}
24+
}
25+
return className
26+
}
27+
28+
public fun Iterable<Relocator>.relocatePath(path: String): String {
29+
forEach { relocator ->
30+
if (relocator.canRelocatePath(path)) {
31+
return relocator.relocatePath(path)
32+
}
33+
}
34+
return path
35+
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.github.jengelman.gradle.plugins.shadow.internal.RelocatorRemapper
44
import com.github.jengelman.gradle.plugins.shadow.internal.cast
55
import com.github.jengelman.gradle.plugins.shadow.internal.zipEntry
66
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
7+
import com.github.jengelman.gradle.plugins.shadow.relocation.relocatePath
78
import com.github.jengelman.gradle.plugins.shadow.transformers.ResourceTransformer
89
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
910
import java.io.File
@@ -166,9 +167,9 @@ public open class ShadowCopyAction(
166167
}
167168
}
168169
else -> {
169-
val mapped = RelocatorRemapper(relocators).map(path)
170-
if (transform(fileDetails, mapped)) return
171-
fileDetails.writeToZip(mapped)
170+
val relocated = relocators.relocatePath(path)
171+
if (transform(fileDetails, relocated)) return
172+
fileDetails.writeToZip(relocated)
172173
}
173174
}
174175
}
@@ -215,26 +216,26 @@ public open class ShadowCopyAction(
215216
// Temporarily remove the multi-release prefix.
216217
val multiReleasePrefix = "^META-INF/versions/\\d+/".toRegex().find(path)?.value.orEmpty()
217218
val newPath = path.replace(multiReleasePrefix, "")
218-
val mappedName = multiReleasePrefix + remapper.mapPath(newPath)
219+
val relocatedPath = multiReleasePrefix + relocators.relocatePath(newPath)
219220
try {
220-
val entry = zipEntry("$mappedName.class", preserveFileTimestamps, lastModified) {
221+
val entry = zipEntry(relocatedPath, preserveFileTimestamps, lastModified) {
221222
unixMode = UnixStat.FILE_FLAG or permissions.toUnixNumeric()
222223
}
223224
// Now we put it back on so the class file is written out with the right extension.
224225
zipOutStr.putNextEntry(entry)
225226
zipOutStr.write(newBytes)
226227
zipOutStr.closeEntry()
227228
} catch (_: ZipException) {
228-
logger.warn("We have a duplicate $mappedName in source project")
229+
logger.warn("We have a duplicate $relocatedPath in source project")
229230
}
230231
}
231232

232-
private fun transform(fileDetails: FileCopyDetails, mapped: String): Boolean {
233+
private fun transform(fileDetails: FileCopyDetails, path: String): Boolean {
233234
val transformer = transformers.find { it.canTransformResource(fileDetails) } ?: return false
234235
fileDetails.file.inputStream().use { inputStream ->
235236
transformer.transform(
236237
TransformerContext(
237-
path = mapped,
238+
path = path,
238239
inputStream = inputStream,
239240
relocators = relocators,
240241
),

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ComponentsXmlResourceTransformer.kt

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,16 @@ public open class ComponentsXmlResourceTransformer : ResourceTransformer {
6363

6464
val children = newDom.getChild("components").getChildren("component")
6565
for (component in children) {
66-
var role: String? = getValue(component, "role")
67-
role = getRelocatedClass(role, context)
66+
val role = getValue(component, "role").let {
67+
context.relocators.relocateClass(it)
68+
}
6869
setValue(component, "role", role)
6970

7071
val roleHint = getValue(component, "role-hint")
7172

72-
var impl: String? = getValue(component, "implementation")
73-
impl = getRelocatedClass(impl, context)
73+
val impl = getValue(component, "implementation").let {
74+
context.relocators.relocateClass(it)
75+
}
7476
setValue(component, "implementation", impl)
7577

7678
val key = "$role:$roleHint"
@@ -84,8 +86,9 @@ public open class ComponentsXmlResourceTransformer : ResourceTransformer {
8486
if (requirements != null && requirements.childCount > 0) {
8587
for (r in requirements.childCount - 1 downTo 0) {
8688
val requirement = requirements.getChild(r)
87-
var requiredRole: String? = getValue(requirement, "role")
88-
requiredRole = getRelocatedClass(requiredRole, context)
89+
val requiredRole = getValue(requirement, "role").let {
90+
context.relocators.relocateClass(it)
91+
}
8992
setValue(requirement, "role", requiredRole)
9093
}
9194
}
@@ -105,17 +108,6 @@ public open class ComponentsXmlResourceTransformer : ResourceTransformer {
105108
public companion object {
106109
public const val COMPONENTS_XML_PATH: String = "META-INF/plexus/components.xml"
107110

108-
private fun getRelocatedClass(className: String?, context: TransformerContext): String? {
109-
if (!className.isNullOrEmpty()) {
110-
for (relocator in context.relocators) {
111-
if (relocator.canRelocateClass(className)) {
112-
return relocator.relocateClass(className)
113-
}
114-
}
115-
}
116-
return className
117-
}
118-
119111
private fun getValue(dom: Xpp3Dom, element: String): String {
120112
return dom.getChild(element).value.orEmpty()
121113
}

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/Log4j2PluginsCacheFileTransformer.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ public open class Log4j2PluginsCacheFileTransformer : ResourceTransformer {
6767
internal fun relocatePlugins(pluginCache: PluginCache) {
6868
pluginCache.allCategories.values.forEach { currentMap ->
6969
currentMap.values.forEach { currentPluginEntry ->
70-
val className = currentPluginEntry.className
71-
tempRelocators.firstOrNull { it.canRelocateClass(className) }?.let { relocator ->
72-
// Then we perform that relocation and update the plugin entry to reflect the new value.
73-
currentPluginEntry.className = relocator.relocateClass(className)
74-
}
70+
currentPluginEntry.className = tempRelocators.relocateClass(currentPluginEntry.className)
7571
}
7672
}
7773
}

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformer.kt

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,11 @@ public open class ServiceFileTransformer(
4545
}
4646

4747
override fun transform(context: TransformerContext) {
48-
var resource = context.path.substringAfter("$path/")
49-
context.relocators.forEach { relocator ->
50-
if (relocator.canRelocateClass(resource)) {
51-
resource = relocator.relocateClass(resource)
52-
return@forEach
53-
}
54-
}
55-
resource = "$path/$resource"
48+
val resource = path + "/" +
49+
context.relocators.relocateClass(context.path.substringAfter("$path/"))
5650
val out = serviceEntries.getOrPut(resource) { mutableSetOf() }
57-
58-
context.inputStream.bufferedReader().use { it.readLines() }.forEach {
59-
var line = it
60-
context.relocators.forEach { relocator ->
61-
if (relocator.canRelocateClass(line)) {
62-
line = relocator.relocateClass(line)
63-
}
64-
}
65-
out.add(line)
51+
context.inputStream.bufferedReader().use { it.readLines() }.forEach { line ->
52+
out.add(context.relocators.relocateClass(line))
6653
}
6754
}
6855

0 commit comments

Comments
 (0)