Skip to content

Commit

Permalink
Migrate AppendingTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Nov 26, 2024
1 parent d7d5ef6 commit 518ecce
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
15 changes: 12 additions & 3 deletions api/shadow.api
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public class com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicen
public fun <init> ()V
public fun canTransformResource (Lorg/gradle/api/file/FileTreeElement;)Z
public fun getName ()Ljava/lang/String;
public fun getObjectFactory ()Lorg/gradle/api/model/ObjectFactory;
public fun hasTransformedResource ()Z
public fun modifyOutputStream (Lorg/apache/tools/zip/ZipOutputStream;Z)V
public fun transform (Lcom/github/jengelman/gradle/plugins/shadow/transformers/TransformerContext;)V
Expand Down Expand Up @@ -397,12 +398,12 @@ public class com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNotic
}

public class com/github/jengelman/gradle/plugins/shadow/transformers/AppendingTransformer : com/github/jengelman/gradle/plugins/shadow/transformers/Transformer {
public fun <init> ()V
public fun <init> (Lorg/gradle/api/model/ObjectFactory;)V
public fun canTransformResource (Lorg/gradle/api/file/FileTreeElement;)Z
public final fun getResource ()Ljava/lang/String;
public fun getObjectFactory ()Lorg/gradle/api/model/ObjectFactory;
public final fun getResource ()Lorg/gradle/api/provider/Property;
public fun hasTransformedResource ()Z
public fun modifyOutputStream (Lorg/apache/tools/zip/ZipOutputStream;Z)V
public final fun setResource (Ljava/lang/String;)V
public fun transform (Lcom/github/jengelman/gradle/plugins/shadow/transformers/TransformerContext;)V
}

Expand All @@ -426,6 +427,7 @@ public class com/github/jengelman/gradle/plugins/shadow/transformers/DontInclude
public fun <init> ()V
public fun canTransformResource (Lorg/gradle/api/file/FileTreeElement;)Z
public fun getName ()Ljava/lang/String;
public fun getObjectFactory ()Lorg/gradle/api/model/ObjectFactory;
public final fun getResource ()Ljava/lang/String;
public fun hasTransformedResource ()Z
public fun modifyOutputStream (Lorg/apache/tools/zip/ZipOutputStream;Z)V
Expand All @@ -446,6 +448,7 @@ public class com/github/jengelman/gradle/plugins/shadow/transformers/IncludeReso
public fun canTransformResource (Lorg/gradle/api/file/FileTreeElement;)Z
public final fun getFile ()Ljava/io/File;
public fun getName ()Ljava/lang/String;
public fun getObjectFactory ()Lorg/gradle/api/model/ObjectFactory;
public final fun getResource ()Ljava/lang/String;
public fun hasTransformedResource ()Z
public fun modifyOutputStream (Lorg/apache/tools/zip/ZipOutputStream;Z)V
Expand Down Expand Up @@ -545,11 +548,17 @@ public class com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFile
public abstract interface class com/github/jengelman/gradle/plugins/shadow/transformers/Transformer : org/gradle/api/Named {
public abstract fun canTransformResource (Lorg/gradle/api/file/FileTreeElement;)Z
public fun getName ()Ljava/lang/String;
public fun getObjectFactory ()Lorg/gradle/api/model/ObjectFactory;
public abstract fun hasTransformedResource ()Z
public abstract fun modifyOutputStream (Lorg/apache/tools/zip/ZipOutputStream;Z)V
public abstract fun transform (Lcom/github/jengelman/gradle/plugins/shadow/transformers/TransformerContext;)V
}

public final class com/github/jengelman/gradle/plugins/shadow/transformers/Transformer$DefaultImpls {
public static fun getName (Lcom/github/jengelman/gradle/plugins/shadow/transformers/Transformer;)Ljava/lang/String;
public static fun getObjectFactory (Lcom/github/jengelman/gradle/plugins/shadow/transformers/Transformer;)Lorg/gradle/api/model/ObjectFactory;
}

public final class com/github/jengelman/gradle/plugins/shadow/transformers/TransformerContext {
public static final field Companion Lcom/github/jengelman/gradle/plugins/shadow/transformers/TransformerContext$Companion;
public fun <init> (Ljava/lang/String;Ljava/io/InputStream;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public abstract class ShadowJar :
override fun append(resourcePath: String): ShadowJar {
return runCatching {
transform(AppendingTransformer::class.java) {
it.resource = resourcePath
it.resource.set(resourcePath)
}
}.getOrDefault(this)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.github.jengelman.gradle.plugins.shadow.transformers

import java.io.ByteArrayOutputStream
import javax.inject.Inject
import org.apache.tools.zip.ZipEntry
import org.apache.tools.zip.ZipOutputStream
import org.gradle.api.file.FileTreeElement
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional

Expand All @@ -17,7 +20,9 @@ import org.gradle.api.tasks.Optional
*/
@CacheableTransformer
@Suppress("ktlint:standard:backing-property-naming")
public open class AppendingTransformer : Transformer {
public open class AppendingTransformer @Inject constructor(
override val objectFactory: ObjectFactory,
) : Transformer {
/**
* Defer initialization, see [issue 763](https://github.com/GradleUp/shadow/issues/763).
*/
Expand All @@ -26,10 +31,10 @@ public open class AppendingTransformer : Transformer {

@get:Optional
@get:Input
public var resource: String? = null
public val resource: Property<String> = objectFactory.property(String::class.java)

override fun canTransformResource(element: FileTreeElement): Boolean {
return resource.equals(element.relativePath.pathString, ignoreCase = true)
return resource.orNull.equals(element.relativePath.pathString, ignoreCase = true)
}

override fun transform(context: TransformerContext) {
Expand All @@ -44,7 +49,7 @@ public open class AppendingTransformer : Transformer {
}

override fun modifyOutputStream(os: ZipOutputStream, preserveFileTimestamps: Boolean) {
val entry = ZipEntry(requireNotNull(resource))
val entry = ZipEntry(resource.get())
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
os.putNextEntry(entry)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ class AppendingTransformerTest extends TransformerTestSupport<AppendingTransform

@BeforeEach
void setUp() {
transformer = new AppendingTransformer()
transformer = new AppendingTransformer(objectFactory)
}

@Test
void testCanTransformResource() {
transformer.resource = "abcdefghijklmnopqrstuvwxyz"
transformer.resource.set("abcdefghijklmnopqrstuvwxyz")

assertTrue(transformer.canTransformResource(getFileElement("abcdefghijklmnopqrstuvwxyz")))
assertTrue(transformer.canTransformResource(getFileElement("ABCDEFGHIJKLMNOPQRSTUVWXYZ")))
Expand Down

0 comments on commit 518ecce

Please sign in to comment.