Skip to content

Commit

Permalink
Migrate ApacheNoticeResourceTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Nov 26, 2024
1 parent bf7cdcf commit 3a76edc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 51 deletions.
33 changes: 12 additions & 21 deletions api/shadow.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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 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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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<String>()
private val organizationEntries = mutableMapOf<String, MutableSet<String>>()
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<String> = objectFactory.property("")

@get:Input
public var addHeader: Boolean = true
public val addHeader: Property<Boolean> = 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<String> = 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<String> = objectFactory.property(
"\n// ------------------------------------------------------------------\n",
)

@get:Input
public var preamble3: String = "This product includes software developed at\n"
public val preamble3: Property<String> = objectFactory.property("This product includes software developed at\n")

@get:Input
public var organizationName: String = "The Apache Software Foundation"
public val organizationName: Property<String> = objectFactory.property("The Apache Software Foundation")

@get:Input
public var organizationURL: String = "http://www.apache.org/"
public val organizationURL: Property<String> = objectFactory.property("http://www.apache.org/")

@get:Input
public var inceptionYear: String = "2006"
public val inceptionYear: Property<String> = objectFactory.property("2006")

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

/**
* The file encoding of the `NOTICE` file.
*/
@get:Optional
@get:Input
public var encoding: String? = null
public val encoding: Property<String> = objectFactory.property()

override fun canTransformResource(element: FileTreeElement): Boolean {
val path = element.relativePath.pathString
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ApacheNoticeResourceTransformerParameterTests extends TransformerTestSuppo

@BeforeEach
void setUp() {
transformer = new ApacheNoticeResourceTransformer()
transformer = new ApacheNoticeResourceTransformer(objectFactory)
stats = new ShadowStats()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ApacheNoticeResourceTransformerTest extends TransformerTestSupport<ApacheN

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

@Test
Expand Down

0 comments on commit 3a76edc

Please sign in to comment.