Skip to content

Commit

Permalink
Migrate transformers to using lazy properties (#1036)
Browse files Browse the repository at this point in the history
* Inject ObjectFactory for Transformer

* Migrate AppendingTransformer

* Add ObjectFactory.property extension

* Migrate ApacheNoticeResourceTransformer

* Migrate DontIncludeResourceTransformer

* Migrate IncludeResourceTransformer

* Migrate ManifestAppenderTransformer

* Migrate ManifestResourceTransformer

* Migrate XmlAppendingTransformer

* Migrate PropertiesFileTransformer

* Replace === with ==

* Mark all public properties open

* Migrate MergeStrategy to enum

* Migrate charset properties to Charsets

* Reformat

* Ensure charset must be serializable

* Note this change

* Mark objectFactory properties final

* Tweak objectFactory

* Tweak kdoc for PropertiesFileTransformer

* Inline getters

* Replace values with entries

* IncludeResourceTransformer.file is not optional

* Rename encoding to charsetName

* Migrate PropertiesFileTransformer.mappings to using Properties

* Tweak comment

* Revert "Migrate PropertiesFileTransformer.mappings to using Properties"

This reverts commit 32da3ee.

* Revert annoying changes to reduce diffs

* Tweak style
  • Loading branch information
Goooler authored Nov 27, 2024
1 parent 932f8f9 commit 7bd3c3f
Show file tree
Hide file tree
Showing 23 changed files with 305 additions and 182 deletions.
116 changes: 59 additions & 57 deletions api/shadow.api
Original file line number Diff line number Diff line change
Expand Up @@ -363,46 +363,38 @@ 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
}

public class com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformer : 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 getAddHeader ()Z
public final fun getCopyright ()Ljava/lang/String;
public final fun getEncoding ()Ljava/lang/String;
public final fun getInceptionYear ()Ljava/lang/String;
public final fun getOrganizationName ()Ljava/lang/String;
public final fun getOrganizationURL ()Ljava/lang/String;
public final fun getPreamble1 ()Ljava/lang/String;
public final fun getPreamble2 ()Ljava/lang/String;
public final fun getPreamble3 ()Ljava/lang/String;
public final fun getProjectName ()Ljava/lang/String;
public fun getAddHeader ()Lorg/gradle/api/provider/Property;
public fun getCharsetName ()Lorg/gradle/api/provider/Property;
public fun getCopyright ()Lorg/gradle/api/provider/Property;
public fun getInceptionYear ()Lorg/gradle/api/provider/Property;
public final fun getObjectFactory ()Lorg/gradle/api/model/ObjectFactory;
public fun getOrganizationName ()Lorg/gradle/api/provider/Property;
public fun getOrganizationURL ()Lorg/gradle/api/provider/Property;
public fun getPreamble1 ()Lorg/gradle/api/provider/Property;
public fun getPreamble2 ()Lorg/gradle/api/provider/Property;
public fun getPreamble3 ()Lorg/gradle/api/provider/Property;
public fun getProjectName ()Lorg/gradle/api/provider/Property;
public fun hasTransformedResource ()Z
public fun modifyOutputStream (Lorg/apache/tools/zip/ZipOutputStream;Z)V
public final fun setAddHeader (Z)V
public final fun setCopyright (Ljava/lang/String;)V
public final fun setEncoding (Ljava/lang/String;)V
public final fun setInceptionYear (Ljava/lang/String;)V
public final fun setOrganizationName (Ljava/lang/String;)V
public final fun setOrganizationURL (Ljava/lang/String;)V
public final fun setPreamble1 (Ljava/lang/String;)V
public final fun setPreamble2 (Ljava/lang/String;)V
public final fun setPreamble3 (Ljava/lang/String;)V
public final fun setProjectName (Ljava/lang/String;)V
public fun transform (Lcom/github/jengelman/gradle/plugins/shadow/transformers/TransformerContext;)V
}

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 final fun getObjectFactory ()Lorg/gradle/api/model/ObjectFactory;
public 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 @@ -423,13 +415,13 @@ public final class com/github/jengelman/gradle/plugins/shadow/transformers/Compo
}

public class com/github/jengelman/gradle/plugins/shadow/transformers/DontIncludeResourceTransformer : 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 fun getName ()Ljava/lang/String;
public final fun getResource ()Ljava/lang/String;
public final fun getObjectFactory ()Lorg/gradle/api/model/ObjectFactory;
public 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 @@ -442,15 +434,14 @@ public class com/github/jengelman/gradle/plugins/shadow/transformers/GroovyExten
}

public class com/github/jengelman/gradle/plugins/shadow/transformers/IncludeResourceTransformer : 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 getFile ()Ljava/io/File;
public fun getFile ()Lorg/gradle/api/file/RegularFileProperty;
public fun getName ()Ljava/lang/String;
public final fun getResource ()Ljava/lang/String;
public final fun getObjectFactory ()Lorg/gradle/api/model/ObjectFactory;
public fun getResource ()Lorg/gradle/api/provider/Property;
public fun hasTransformedResource ()Z
public fun modifyOutputStream (Lorg/apache/tools/zip/ZipOutputStream;Z)V
public final fun setFile (Ljava/io/File;)V
public final fun setResource (Ljava/lang/String;)V
public fun transform (Lcom/github/jengelman/gradle/plugins/shadow/transformers/TransformerContext;)V
}

Expand All @@ -463,25 +454,25 @@ public class com/github/jengelman/gradle/plugins/shadow/transformers/Log4j2Plugi
}

public class com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformer : com/github/jengelman/gradle/plugins/shadow/transformers/Transformer {
public fun <init> ()V
public fun <init> (Lorg/gradle/api/model/ObjectFactory;)V
public fun append (Ljava/lang/String;Ljava/lang/Comparable;)Lcom/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformer;
public fun canTransformResource (Lorg/gradle/api/file/FileTreeElement;)Z
public fun getAttributes ()Ljava/util/List;
public fun getAttributes ()Lorg/gradle/api/provider/ListProperty;
public final 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
}

public class com/github/jengelman/gradle/plugins/shadow/transformers/ManifestResourceTransformer : com/github/jengelman/gradle/plugins/shadow/transformers/Transformer {
public fun <init> ()V
public fun <init> (Lorg/gradle/api/model/ObjectFactory;)V
public fun attributes (Ljava/util/Map;)Lcom/github/jengelman/gradle/plugins/shadow/transformers/ManifestResourceTransformer;
public fun canTransformResource (Lorg/gradle/api/file/FileTreeElement;)Z
public final fun getMainClass ()Ljava/lang/String;
public final fun getManifestEntries ()Ljava/util/Map;
public fun getMainClass ()Lorg/gradle/api/provider/Property;
public fun getManifestEntries ()Lorg/gradle/api/provider/MapProperty;
public final fun getObjectFactory ()Lorg/gradle/api/model/ObjectFactory;
public fun hasTransformedResource ()Z
public fun modifyOutputStream (Lorg/apache/tools/zip/ZipOutputStream;Z)V
public final fun setMainClass (Ljava/lang/String;)V
public final fun setManifestEntries (Ljava/util/Map;)V
public fun transform (Lcom/github/jengelman/gradle/plugins/shadow/transformers/TransformerContext;)V
}

Expand All @@ -494,25 +485,35 @@ public final class com/github/jengelman/gradle/plugins/shadow/transformers/NoOpT
}

public class com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer : 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 getCharset ()Ljava/lang/String;
public final fun getKeyTransformer ()Ljava/util/function/Function;
public final fun getMappings ()Ljava/util/Map;
public final fun getMergeSeparator ()Ljava/lang/String;
public final fun getMergeStrategy ()Ljava/lang/String;
public final fun getPaths ()Ljava/util/List;
public fun getCharsetName ()Lorg/gradle/api/provider/Property;
public fun getKeyTransformer ()Lorg/gradle/api/provider/Property;
public fun getMappings ()Lorg/gradle/api/provider/MapProperty;
public fun getMergeSeparator ()Lorg/gradle/api/provider/Property;
public fun getMergeStrategy ()Lorg/gradle/api/provider/Property;
public final fun getObjectFactory ()Lorg/gradle/api/model/ObjectFactory;
public fun getPaths ()Lorg/gradle/api/provider/ListProperty;
public fun hasTransformedResource ()Z
public fun modifyOutputStream (Lorg/apache/tools/zip/ZipOutputStream;Z)V
public final fun setCharset (Ljava/lang/String;)V
public final fun setKeyTransformer (Ljava/util/function/Function;)V
public final fun setMappings (Ljava/util/Map;)V
public final fun setMergeSeparator (Ljava/lang/String;)V
public final fun setMergeStrategy (Ljava/lang/String;)V
public final fun setPaths (Ljava/util/List;)V
public fun transform (Lcom/github/jengelman/gradle/plugins/shadow/transformers/TransformerContext;)V
}

public final class com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy : java/lang/Enum {
public static final field Append Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy;
public static final field Companion Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy$Companion;
public static final field First Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy;
public static final field Latest Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy;
public static final fun from (Ljava/lang/String;)Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public static fun valueOf (Ljava/lang/String;)Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy;
public static fun values ()[Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy;
}

public final class com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy$Companion {
public final fun from (Ljava/lang/String;)Lcom/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer$MergeStrategy;
}

public class com/github/jengelman/gradle/plugins/shadow/transformers/ServiceFileTransformer : com/github/jengelman/gradle/plugins/shadow/transformers/Transformer, org/gradle/api/tasks/util/PatternFilterable {
public fun <init> ()V
public fun <init> (Lorg/gradle/api/tasks/util/PatternSet;)V
Expand Down Expand Up @@ -545,13 +546,15 @@ 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 {
Expand Down Expand Up @@ -594,14 +597,13 @@ public final class com/github/jengelman/gradle/plugins/shadow/transformers/Trans
public class com/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformer : com/github/jengelman/gradle/plugins/shadow/transformers/Transformer {
public static final field Companion Lcom/github/jengelman/gradle/plugins/shadow/transformers/XmlAppendingTransformer$Companion;
public static final field XSI_NS Ljava/lang/String;
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 getIgnoreDtd ()Z
public final fun getResource ()Ljava/lang/String;
public fun getIgnoreDtd ()Lorg/gradle/api/provider/Property;
public final fun getObjectFactory ()Lorg/gradle/api/model/ObjectFactory;
public fun getResource ()Lorg/gradle/api/provider/Property;
public fun hasTransformedResource ()Z
public fun modifyOutputStream (Lorg/apache/tools/zip/ZipOutputStream;Z)V
public final fun setIgnoreDtd (Z)V
public final fun setResource (Ljava/lang/String;)V
public fun transform (Lcom/github/jengelman/gradle/plugins/shadow/transformers/TransformerContext;)V
}

Expand Down
1 change: 1 addition & 0 deletions src/docs/changes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
**Changed**

- **BREAKING CHANGE:** Rewrite this plugin in Kotlin. ([#1012](https://github.com/GradleUp/shadow/pull/1012))
- **BREAKING CHANGE:** Migrate `Transformer`s to using lazy properties. ([#1036](https://github.com/GradleUp/shadow/pull/1036))
- **BREAKING CHANGE:** Migrate `ShadowJar` to using lazy properties. ([#1044](https://github.com/GradleUp/shadow/pull/1044))
`isEnableRelocation` is deprecated, use `enableRelocation` instead.
- **BREAKING CHANGE:** Resolve `Configuration` directly in `DependencyFilter`. ([#1045](https://github.com/GradleUp/shadow/pull/1045))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.github.jengelman.gradle.plugins.shadow.internal
import java.io.InputStream
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.model.ObjectFactory
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.provider.Property

/**
* Return `runtimeClasspath` or `runtime` configuration.
Expand All @@ -13,6 +15,12 @@ internal inline val Project.runtimeConfiguration: Configuration get() {
?: configurations.getByName("runtime")
}

internal inline fun <reified T : Any> ObjectFactory.property(defaultValue: T? = null): Property<T> {
return property(T::class.java).apply {
if (defaultValue != null) convention(defaultValue)
}
}

@Suppress("NOTHING_TO_INLINE")
internal inline fun <T> unsafeLazy(noinline initializer: () -> T): Lazy<T> =
lazy(LazyThreadSafetyMode.NONE, initializer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.gradle.api.internal.DocumentationRegistry
import org.gradle.api.internal.file.FileResolver
import org.gradle.api.internal.file.copy.CopyAction
import org.gradle.api.internal.file.copy.DefaultCopySpec
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.CacheableTask
Expand Down Expand Up @@ -153,7 +154,15 @@ public abstract class ShadowJar :
}

override fun <T : Transformer> transform(clazz: Class<T>, action: Action<T>?): ShadowJar = apply {
val transformer = clazz.getDeclaredConstructor().newInstance()
// If the constructor takes a single ObjectFactory, inject it in.
val constructor = clazz.constructors.find {
it.parameterTypes.singleOrNull() == ObjectFactory::class.java
}
val transformer = if (constructor != null) {
objectFactory.newInstance(clazz)
} else {
clazz.getDeclaredConstructor().newInstance()
}
addTransform(transformer, action)
}

Expand Down Expand Up @@ -190,7 +199,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
Loading

0 comments on commit 7bd3c3f

Please sign in to comment.