Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
Fix for PropertiesFileTransformer breaks Reproducible builds in 8.1.1
Browse files Browse the repository at this point in the history
Co-authored-by: Zongle Wang <wangzongler@gmail.com>
  • Loading branch information
agascon and Goooler authored Jun 7, 2024
1 parent af47fc9 commit 071a11c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
/*
* Source https://stackoverflow.com/a/39043903/519333
*/
package com.github.jengelman.gradle.plugins.shadow.internal

class CleanProperties extends Properties {
private static class StripFirstLineStream extends FilterOutputStream {

private boolean firstLineSeen = false
private static class StripCommentsWithTimestampBufferedWriter extends BufferedWriter {

StripFirstLineStream(final OutputStream out) {
private final int lengthOfExpectedTimestamp

StripCommentsWithTimestampBufferedWriter(final Writer out) {
super(out)

lengthOfExpectedTimestamp = ("#" + new Date().toString()).length()
}

@Override
void write(final int b) throws IOException {
if (firstLineSeen) {
super.write(b)
} else if (b == '\n') {
super.write(b)

firstLineSeen = true
void write(final String str) throws IOException {
if (couldBeCommentWithTimestamp(str)) {
return
}
super.write(str)
}

private boolean couldBeCommentWithTimestamp(final String str) {
return str != null &&
str.startsWith("#") &&
str.length() == lengthOfExpectedTimestamp
}
}

@Override
void store(final OutputStream out, final String comments) throws IOException {
super.store(new StripFirstLineStream(out), null)
void store(final Writer writer, final String comments) throws IOException {
super.store(new StripCommentsWithTimestampBufferedWriter(writer), comments)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,42 @@ final class PropertiesFileTransformerTest extends TransformerTestSupport {
void testTransformation() {
transformer.transform(new TransformerContext(MANIFEST_NAME, getResourceStream(MANIFEST_NAME), Collections.<Relocator>emptyList(), new ShadowStats()))

def testableZipFile = doTransformAndGetTransformedFile(transformer, false)
def targetLines = readFrom(testableZipFile, MANIFEST_NAME)

assertFalse(targetLines.isEmpty())

assertTrue(targetLines.contains("Manifest-Version=1.0"))
}

@Test
void testTransformationPropertiesAreReproducible() {
transformer.transform(new TransformerContext(MANIFEST_NAME, getResourceStream(MANIFEST_NAME), Collections.<Relocator>emptyList(), new ShadowStats()))

def firstRunTransformedFile = doTransformAndGetTransformedFile(transformer, true)
def firstRunTargetLines = readFrom(firstRunTransformedFile, MANIFEST_NAME)

Thread.sleep(1000) // wait for 1sec to ensure timestamps in properties would change

def secondRunTransformedFile = doTransformAndGetTransformedFile(transformer, true)
def secondRunTargetLines = readFrom(secondRunTransformedFile, MANIFEST_NAME)

assertEquals(firstRunTargetLines, secondRunTargetLines)
}

static File doTransformAndGetTransformedFile(final PropertiesFileTransformer transformer, final boolean preserveFileTimestamps) {
def testableZipFile = File.createTempFile("testable-zip-file-", ".jar")
def fileOutputStream = new FileOutputStream(testableZipFile)
def bufferedOutputStream = new BufferedOutputStream(fileOutputStream)
def zipOutputStream = new ZipOutputStream(bufferedOutputStream)

try {
transformer.modifyOutputStream(zipOutputStream, false)
transformer.modifyOutputStream(zipOutputStream, preserveFileTimestamps)
} finally {
zipOutputStream.close()
}
def targetLines = readFrom(testableZipFile, MANIFEST_NAME)

assertFalse(targetLines.isEmpty())

assertTrue(targetLines.contains("Manifest-Version=1.0"))
return testableZipFile
}

static List<String> readFrom(File jarFile, String resourceName) {
Expand Down

0 comments on commit 071a11c

Please sign in to comment.