Skip to content

Commit

Permalink
Replace publishWithWriter with publishWithStream
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Dec 12, 2024
1 parent 0c9494b commit 49f710a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class AppendableMavenFileModule extends MavenFileModule {
}
String classifier = (String) artifact['classifier'] ?: ''
if (files.containsKey(classifier)) {
publishWithStream(artifactFile) { OutputStream os ->
publish(artifactFile) { OutputStream os ->
IOUtils.copy(files[classifier].newInputStream(), os)
}
} else {
publishWithStream(artifactFile) { OutputStream os ->
publish(artifactFile) { OutputStream os ->
writeJar(os, contents[classifier])
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule
updateRootMavenMetaData(rootMavenMetaData)

if (publishesMetaDataFile) {
publishWithWriter(metaDataFile) { Writer writer ->
publish(metaDataFile) { OutputStream outputStream ->
MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer()
metadataWriter.write(writer, getMetaData([]))
metadataWriter.write(outputStream, getMetaData([]))
}
}

publishWithWriter(pomFile) { Writer writer ->
publish(pomFile) { OutputStream outputStream ->
def pomPackaging = packaging ?: type
// Create a new Maven Model
Model model = new Model()
Expand All @@ -132,7 +132,7 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule

// Write the model to the POM file
MavenXpp3Writer pomWriter = new MavenXpp3Writer()
pomWriter.write(writer, model)
pomWriter.write(outputStream, model)
}
return this
}
Expand All @@ -144,9 +144,9 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule
allVersions = metaData.versioning.versions
}
allVersions << version
publishWithWriter(rootMavenMetaData) { Writer writer ->
publish(rootMavenMetaData) { OutputStream outputStream ->
MetadataXpp3Writer metadataWriter = new MetadataXpp3Writer()
metadataWriter.write(writer, getMetaData(allVersions))
metadataWriter.write(outputStream, getMetaData(allVersions))
}
}

Expand Down Expand Up @@ -187,8 +187,8 @@ abstract class AbstractMavenModule extends AbstractModule implements MavenModule
if (type == 'pom') {
return artifactFile
}
publishWithWriter(artifactFile) { Writer writer ->
writer << "${artifactFile.name} : $artifactContent"
publish(artifactFile) { OutputStream outputStream ->
outputStream.write("${artifactFile.name} : $artifactContent".bytes)
}
return artifactFile
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,16 @@ package com.github.jengelman.gradle.plugins.shadow.util.repo
import com.github.jengelman.gradle.plugins.shadow.util.HashUtil
import java.io.File
import java.io.OutputStream
import java.io.Writer
import java.math.BigInteger

abstract class AbstractModule {

protected abstract fun onPublish(file: File)

protected fun publishWithWriter(file: File, action: (Writer) -> Unit) {
publishCommon(file) { it.writer().use(action) }
}

protected fun publishWithStream(file: File, action: (OutputStream) -> Unit) {
publishCommon(file) { it.outputStream().use(action) }
}

private fun publishCommon(file: File, action: (File) -> Unit) {
protected fun publish(file: File, action: (OutputStream) -> Unit) {
val hashBefore = if (file.exists()) getHash(file, "sha1") else null
val tempFile = file.resolveSibling("${file.name}.tmp")
action(tempFile)
tempFile.outputStream().use(action)

val hashAfter = getHash(tempFile, "sha1")
if (hashAfter == hashBefore) {
Expand Down

0 comments on commit 49f710a

Please sign in to comment.