diff --git a/api/shadow.api b/api/shadow.api index 411fa3dd2..2df52965b 100644 --- a/api/shadow.api +++ b/api/shadow.api @@ -370,30 +370,21 @@ public class com/github/jengelman/gradle/plugins/shadow/transformers/ApacheLicen } public class com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformer : com/github/jengelman/gradle/plugins/shadow/transformers/Transformer { - public fun ()V + public fun (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 final fun getAddHeader ()Lorg/gradle/api/provider/Property; + public final fun getCopyright ()Lorg/gradle/api/provider/Property; + public final fun getEncoding ()Lorg/gradle/api/provider/Property; + public final fun getInceptionYear ()Lorg/gradle/api/provider/Property; + public fun getObjectFactory ()Lorg/gradle/api/model/ObjectFactory; + public final fun getOrganizationName ()Lorg/gradle/api/provider/Property; + public final fun getOrganizationURL ()Lorg/gradle/api/provider/Property; + public final fun getPreamble1 ()Lorg/gradle/api/provider/Property; + public final fun getPreamble2 ()Lorg/gradle/api/provider/Property; + public final fun getPreamble3 ()Lorg/gradle/api/provider/Property; + public final 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 } diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformer.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformer.kt index e0c8cbfee..fa394e718 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformer.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformer.kt @@ -1,14 +1,18 @@ package com.github.jengelman.gradle.plugins.shadow.transformers +import com.github.jengelman.gradle.plugins.shadow.internal.property import java.io.PrintWriter import java.nio.charset.Charset import java.text.SimpleDateFormat import java.util.Date import java.util.Locale import java.util.TreeSet +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 @@ -19,52 +23,56 @@ import org.gradle.api.tasks.Optional * * @author John Engelman */ -public open class ApacheNoticeResourceTransformer : Transformer { +public open class ApacheNoticeResourceTransformer @Inject constructor( + override val objectFactory: ObjectFactory, +) : Transformer { private val entries = mutableSetOf() private val organizationEntries = mutableMapOf>() - private val charset get() = if (encoding.isNullOrEmpty()) Charsets.UTF_8 else Charset.forName(encoding) + private val charset + get() = if (encoding.orNull.isNullOrEmpty()) Charsets.UTF_8 else Charset.forName(encoding.get()) - /** - * MSHADE-101 :: NullPointerException when projectName is missing - */ @get:Input - public var projectName: String = "" + public val projectName: Property = objectFactory.property("") @get:Input - public var addHeader: Boolean = true + public val addHeader: Property = objectFactory.property(true) @get:Input - public var preamble1: String = """ - // ------------------------------------------------------------------ - // NOTICE file corresponding to the section 4d of The Apache License, - // Version 2.0, in this case for - """.trimIndent() + public val preamble1: Property = objectFactory.property( + """ + // ------------------------------------------------------------------ + // NOTICE file corresponding to the section 4d of The Apache License, + // Version 2.0, in this case for + """.trimIndent(), + ) @get:Input - public var preamble2: String = "\n// ------------------------------------------------------------------\n" + public val preamble2: Property = objectFactory.property( + "\n// ------------------------------------------------------------------\n", + ) @get:Input - public var preamble3: String = "This product includes software developed at\n" + public val preamble3: Property = objectFactory.property("This product includes software developed at\n") @get:Input - public var organizationName: String = "The Apache Software Foundation" + public val organizationName: Property = objectFactory.property("The Apache Software Foundation") @get:Input - public var organizationURL: String = "http://www.apache.org/" + public val organizationURL: Property = objectFactory.property("http://www.apache.org/") @get:Input - public var inceptionYear: String = "2006" + public val inceptionYear: Property = objectFactory.property("2006") @get:Optional @get:Input - public var copyright: String? = null + public val copyright: Property = objectFactory.property() /** * The file encoding of the `NOTICE` file. */ @get:Optional @get:Input - public var encoding: String? = null + public val encoding: Property = objectFactory.property() override fun canTransformResource(element: FileTreeElement): Boolean { val path = element.relativePath.pathString @@ -76,17 +84,18 @@ public open class ApacheNoticeResourceTransformer : Transformer { override fun transform(context: TransformerContext) { if (entries.isEmpty()) { val year = SimpleDateFormat("yyyy", Locale.US).format(Date()).let { - if (inceptionYear != it) "$inceptionYear-$it" else it + val iy = inceptionYear.get() + if (iy != it) "$iy-$it" else it } // add headers - if (addHeader) { - entries.add("$preamble1$projectName$preamble2") + if (addHeader.get()) { + entries.add("${preamble1.get()}${projectName.get()}${preamble2.get()}") } else { entries.add("") } // fake second entry, we'll look for a real one later - entries.add("$projectName\nCopyright $year $organizationName\n") - entries.add("$preamble3$organizationName ($organizationURL).\n") + entries.add("${projectName.get()}\nCopyright $year ${organizationName.get()}\n") + entries.add("${preamble3.get()}${organizationName.get()} (${organizationURL.get()}).\n") } val reader = context.inputStream.bufferedReader(charset) @@ -112,8 +121,8 @@ public open class ApacheNoticeResourceTransformer : Transformer { lineCount++ } else { val entry = sb.toString() - if (entry.startsWith(projectName) && entry.contains("Copyright ")) { - copyright = entry + if (entry.startsWith(projectName.get()) && entry.contains("Copyright ")) { + copyright.set(entry) } if (currentOrg == null) { entries.add(entry) @@ -149,8 +158,8 @@ public open class ApacheNoticeResourceTransformer : Transformer { var count = 0 for (line in entries) { count++ - if (line == copyright && count != 2) continue - if (count == 2 && copyright != null) { + if (line == copyright.orNull && count != 2) continue + if (count == 2 && copyright.orNull != null) { writer.print(copyright) writer.print('\n') } else { diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerParameterTests.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerParameterTests.groovy index e81f6ab8b..d5e0932d0 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerParameterTests.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerParameterTests.groovy @@ -39,7 +39,7 @@ class ApacheNoticeResourceTransformerParameterTests extends TransformerTestSuppo @BeforeEach void setUp() { - transformer = new ApacheNoticeResourceTransformer() + transformer = new ApacheNoticeResourceTransformer(objectFactory) stats = new ShadowStats() } diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.groovy index 0b2646aa8..4cb7ff0f7 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformerTest.groovy @@ -45,7 +45,7 @@ class ApacheNoticeResourceTransformerTest extends TransformerTestSupport