Skip to content

Commit

Permalink
Add inputStream extension for Properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Dec 4, 2024
1 parent 3e2d2ff commit 3c68867
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.github.jengelman.gradle.plugins.shadow.internal

import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.InputStream
import java.nio.charset.Charset
import java.util.Properties
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.file.RelativePath
Expand Down Expand Up @@ -45,6 +49,17 @@ internal inline fun <T> unsafeLazy(noinline initializer: () -> T): Lazy<T> {
return lazy(LazyThreadSafetyMode.NONE, initializer)
}

internal fun Properties.inputStream(
charset: Charset = Charsets.UTF_8,
comments: String = ""
): ByteArrayInputStream {
val os = ByteArrayOutputStream()
os.writer(charset).use { writer ->
store(writer, comments)
}
return os.toByteArray().inputStream()
}

internal fun Class<*>.requireResourceAsText(name: String): String {
return requireResourceAsStream(name).bufferedReader().readText()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.github.jengelman.gradle.plugins.shadow.transformers

import com.github.jengelman.gradle.plugins.shadow.internal.CleanProperties
import com.github.jengelman.gradle.plugins.shadow.internal.inputStream
import com.github.jengelman.gradle.plugins.shadow.internal.property
import com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer.MergeStrategy
import groovy.lang.Closure
import groovy.lang.Closure.IDENTITY
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.io.InputStreamReader
import java.nio.charset.Charset
import java.util.Properties
import javax.inject.Inject
Expand Down Expand Up @@ -226,22 +225,14 @@ public open class PropertiesFileTransformer @Inject constructor(
val entry = ZipEntry(path)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
os.putNextEntry(entry)
props.toReader().use {
props.inputStream(charset).reader(charset).use {
it.copyTo(zipWriter)
}
zipWriter.flush()
os.closeEntry()
}
}

private fun Properties.toReader(): InputStreamReader {
val os = ByteArrayOutputStream()
os.writer(charset).use { writer ->
store(writer, "")
}
return os.toByteArray().inputStream().reader(charset)
}

public enum class MergeStrategy {
First,
Latest,
Expand Down

0 comments on commit 3c68867

Please sign in to comment.