Skip to content

Commit 48ace22

Browse files
authored
Kotlin migration part 3 (#1020)
* Move ZipCompressor into `com.github.jengelman.gradle.plugins.shadow.tasks` * Convert ZipCompressor * Convert CleanProperties * Convert DefaultZipCompressor * Rename TransformerContext.is * Convert TransformerContext * Convert ShadowStats * Convert ApacheLicenseResourceTransformer * Convert DontIncludeResourceTransformer * Convert IncludeResourceTransformer
1 parent 1f25269 commit 48ace22

33 files changed

+287
-424
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.github.jengelman.gradle.plugins.shadow
2+
3+
import org.gradle.api.GradleException
4+
5+
open class ShadowStats {
6+
open var totalTime: Long = 0
7+
open var jarStartTime: Long = 0
8+
open var jarEndTime: Long = 0
9+
open var jarCount: Int = 1
10+
open var processingJar: Boolean = false
11+
open val relocations: MutableMap<String, String> = mutableMapOf()
12+
13+
open val relocationString: String
14+
get() {
15+
return relocations.map { (k, v) -> "$k$v" }
16+
.sorted()
17+
.joinToString("\n")
18+
}
19+
20+
open val jarTiming: Long
21+
get() = jarEndTime - jarStartTime
22+
23+
open val totalTimeSecs: Double
24+
get() = totalTime / 1000.0
25+
26+
open val averageTimePerJar: Double
27+
get() = totalTime / jarCount.toDouble()
28+
29+
open val averageTimeSecsPerJar: Double
30+
get() = averageTimePerJar / 1000.0
31+
32+
open val buildScanData: Map<String, String>
33+
get() = mapOf(
34+
"dependencies" to jarCount.toString(),
35+
"relocations" to relocationString,
36+
)
37+
38+
open fun relocate(src: String, dst: String) {
39+
relocations[src] = dst
40+
}
41+
42+
open fun startJar() {
43+
if (processingJar) throw GradleException("Can only time one entry at a time")
44+
processingJar = true
45+
jarStartTime = System.currentTimeMillis()
46+
}
47+
48+
open fun finishJar() {
49+
if (processingJar) {
50+
jarEndTime = System.currentTimeMillis()
51+
jarCount++
52+
totalTime += jarTiming
53+
processingJar = false
54+
}
55+
}
56+
57+
open fun printStats() {
58+
println(this)
59+
}
60+
61+
override fun toString(): String {
62+
return """
63+
*******************
64+
GRADLE SHADOW STATS
65+
66+
Total Jars: $jarCount (includes project)
67+
Total Time: ${totalTimeSecs}s [${totalTime}ms]
68+
Average Time/Jar: ${averageTimeSecsPerJar}s [${averageTimePerJar}ms]
69+
*******************
70+
""".trimIndent()
71+
}
72+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.jengelman.gradle.plugins.shadow.internal
2+
3+
import java.io.BufferedWriter
4+
import java.io.IOException
5+
import java.io.Writer
6+
import java.util.Date
7+
import java.util.Properties
8+
9+
internal class CleanProperties : Properties() {
10+
@Throws(IOException::class)
11+
override fun store(writer: Writer, comments: String) {
12+
super.store(StripCommentsWithTimestampBufferedWriter(writer), comments)
13+
}
14+
15+
private class StripCommentsWithTimestampBufferedWriter(out: Writer) : BufferedWriter(out) {
16+
private val lengthOfExpectedTimestamp = ("#" + Date().toString()).length
17+
18+
@Throws(IOException::class)
19+
override fun write(str: String) {
20+
if (str.couldBeCommentWithTimestamp) return
21+
super.write(str)
22+
}
23+
24+
private val String?.couldBeCommentWithTimestamp: Boolean get() {
25+
return this != null && startsWith("#") && length == lengthOfExpectedTimestamp
26+
}
27+
}
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.github.jengelman.gradle.plugins.shadow.internal
2+
3+
import com.github.jengelman.gradle.plugins.shadow.tasks.ZipCompressor
4+
import java.io.File
5+
import org.apache.tools.zip.Zip64Mode
6+
import org.apache.tools.zip.ZipOutputStream
7+
import org.gradle.api.UncheckedIOException
8+
9+
internal class DefaultZipCompressor(
10+
allowZip64Mode: Boolean,
11+
private val entryCompressionMethod: Int,
12+
) : ZipCompressor {
13+
private val zip64Mode = if (allowZip64Mode) Zip64Mode.AsNeeded else Zip64Mode.Never
14+
15+
override fun createArchiveOutputStream(destination: File): ZipOutputStream {
16+
try {
17+
return ZipOutputStream(destination).apply {
18+
setUseZip64(zip64Mode)
19+
setMethod(entryCompressionMethod)
20+
}
21+
} catch (e: Exception) {
22+
throw UncheckedIOException("Unable to create ZIP output stream for file $destination.", e)
23+
}
24+
}
25+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.github.jengelman.gradle.plugins.shadow.tasks
2+
3+
import org.gradle.api.internal.file.archive.compression.ArchiveOutputStreamFactory
4+
5+
interface ZipCompressor : ArchiveOutputStreamFactory
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.jengelman.gradle.plugins.shadow.transformers
2+
3+
import org.gradle.api.file.FileTreeElement
4+
5+
/**
6+
* Prevents duplicate copies of the license
7+
*
8+
* Modified from `org.apache.maven.plugins.shade.resouce.ApacheLicenseResourceTransformer.java`
9+
*
10+
* @author John Engelman
11+
*/
12+
open class ApacheLicenseResourceTransformer : Transformer by NoOpTransformer {
13+
override fun canTransformResource(element: FileTreeElement): Boolean {
14+
val path = element.relativePath.pathString
15+
return LICENSE_PATH.equals(path, ignoreCase = true) ||
16+
LICENSE_TXT_PATH.regionMatches(0, path, 0, LICENSE_TXT_PATH.length, ignoreCase = true)
17+
}
18+
19+
private companion object {
20+
private const val LICENSE_PATH = "META-INF/LICENSE"
21+
private const val LICENSE_TXT_PATH = "META-INF/LICENSE.txt"
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.jengelman.gradle.plugins.shadow.transformers
2+
3+
import org.gradle.api.file.FileTreeElement
4+
import org.gradle.api.tasks.Input
5+
import org.gradle.api.tasks.Optional
6+
7+
/**
8+
* A resource processor that prevents the inclusion of an arbitrary resource into the shaded JAR.
9+
*
10+
* Modified from `org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer.java`
11+
*
12+
* @author John Engelman
13+
*/
14+
open class DontIncludeResourceTransformer : Transformer by NoOpTransformer {
15+
@get:Optional
16+
@get:Input
17+
var resource: String? = null
18+
19+
override fun canTransformResource(element: FileTreeElement): Boolean {
20+
val path = element.relativePath.pathString
21+
return !resource.isNullOrEmpty() && path.endsWith(resource!!)
22+
}
23+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.jengelman.gradle.plugins.shadow.transformers
2+
3+
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext.Companion.getEntryTimestamp
4+
import java.io.File
5+
import org.apache.tools.zip.ZipEntry
6+
import org.apache.tools.zip.ZipOutputStream
7+
import org.gradle.api.tasks.Input
8+
import org.gradle.api.tasks.InputFile
9+
import org.gradle.api.tasks.PathSensitive
10+
import org.gradle.api.tasks.PathSensitivity
11+
12+
/**
13+
* A resource processor that allows the addition of an arbitrary file
14+
* content into the shaded JAR.
15+
*
16+
* Modified from `org.apache.maven.plugins.shade.resource.IncludeResourceTransformer.java`
17+
*
18+
* @author John Engelman
19+
*/
20+
open class IncludeResourceTransformer : Transformer by NoOpTransformer {
21+
@get:InputFile
22+
@get:PathSensitive(PathSensitivity.NONE)
23+
var file: File? = null
24+
25+
@get:Input
26+
var resource: String? = null
27+
28+
override fun hasTransformedResource(): Boolean = file?.exists() == true
29+
30+
override fun modifyOutputStream(os: ZipOutputStream, preserveFileTimestamps: Boolean) {
31+
val entry = ZipEntry(requireNotNull(resource))
32+
entry.time = getEntryTimestamp(preserveFileTimestamps, entry.time)
33+
os.putNextEntry(entry)
34+
35+
requireNotNull(file).inputStream().use { inputStream ->
36+
inputStream.copyTo(os)
37+
}
38+
}
39+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.github.jengelman.gradle.plugins.shadow.transformers
2+
3+
import com.github.jengelman.gradle.plugins.shadow.ShadowStats
4+
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
5+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction
6+
import java.io.InputStream
7+
import java.util.GregorianCalendar
8+
9+
data class TransformerContext @JvmOverloads constructor(
10+
val path: String,
11+
val inputStream: InputStream,
12+
val relocators: List<Relocator> = emptyList(),
13+
val stats: ShadowStats = ShadowStats(),
14+
) {
15+
class Builder {
16+
private var path = ""
17+
private var inputStream: InputStream? = null
18+
private var relocators = emptyList<Relocator>()
19+
private var stats = ShadowStats()
20+
21+
fun path(path: String): Builder = apply { this.path = path }
22+
fun inputStream(inputStream: InputStream): Builder = apply { this.inputStream = inputStream }
23+
fun relocators(relocators: List<Relocator>): Builder = apply { this.relocators = relocators }
24+
fun stats(stats: ShadowStats): Builder = apply { this.stats = stats }
25+
fun build(): TransformerContext = TransformerContext(
26+
path = path,
27+
inputStream = inputStream ?: error("inputStream is required"),
28+
relocators = relocators,
29+
stats = stats,
30+
)
31+
}
32+
33+
companion object {
34+
/**
35+
* TODO: replace this with [ShadowCopyAction.CONSTANT_TIME_FOR_ZIP_ENTRIES]
36+
*/
37+
private val CONSTANT_TIME_FOR_ZIP_ENTRIES = GregorianCalendar(1980, 1, 1, 0, 0, 0).getTimeInMillis()
38+
39+
@JvmStatic
40+
fun builder(): Builder = Builder()
41+
42+
@JvmStatic
43+
fun getEntryTimestamp(preserveFileTimestamps: Boolean, entryTime: Long): Long {
44+
return if (preserveFileTimestamps) entryTime else CONSTANT_TIME_FOR_ZIP_ENTRIES
45+
}
46+
}
47+
}

src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowStats.groovy

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/main/groovy/com/github/jengelman/gradle/plugins/shadow/internal/CleanProperties.groovy

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)