Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a whitespace trimming mode to ManifestAppenderTransformer #620

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class ManifestAppenderTransformer implements Transformer {

private byte[] manifestContents = []
private final List<Tuple2<String, ? extends Comparable<?>>> attributes = []
private boolean trimTrailingWhitespace = false

ManifestAppenderTransformer() {}

List<Tuple2<String, ? extends Comparable<?>>> getAttributes() { attributes }

Expand All @@ -49,6 +52,11 @@ class ManifestAppenderTransformer implements Transformer {
this
}

ManifestAppenderTransformer trimTrailingWhitespace() {
trimTrailingWhitespace = true
this
}

@Override
boolean canTransformResource(FileTreeElement element) {
MANIFEST_NAME.equalsIgnoreCase(element.relativePath.pathString)
Expand All @@ -72,7 +80,13 @@ class ManifestAppenderTransformer implements Transformer {
ZipEntry entry = new ZipEntry(MANIFEST_NAME)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
os.putNextEntry(entry)
os.write(manifestContents)

if (trimTrailingWhitespace) {
os.write(trimTrailingWhitespace(manifestContents))
os.write(EOL)
} else {
os.write(manifestContents)
}

if (!attributes.isEmpty()) {
for (attribute in attributes) {
Expand All @@ -85,4 +99,8 @@ class ManifestAppenderTransformer implements Transformer {
attributes.clear()
}
}

static byte[] trimTrailingWhitespace(byte[] contents) {
new String(contents, UTF_8).replaceFirst("\\s++\$", "").getBytes(UTF_8)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,35 @@ class ManifestAppenderTransformerTest extends TransformerTestSupport {
assertEquals(sourceLines, targetLines)
}

@Test
void testTransformationTrimmingTrailingWhitespace() {
def sourceLines = getResourceStream(MANIFEST_NAME).readLines()
def trimmedContent = sourceLines
while (trimmedContent.last().trim().isEmpty()) {
trimmedContent.removeLast()
}

transformer.with {
trimTrailingWhitespace()
append('Name', 'org/foo/bar/')
transform(new TransformerContext(MANIFEST_NAME, getResourceStream(MANIFEST_NAME), Collections.<Relocator>emptyList(), new ShadowStats()))
}

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, true)
} finally {
zipOutputStream.close()
}

def targetLines = readFrom(testableZipFile, MANIFEST_NAME)
assertEquals(trimmedContent + "Name: org/foo/bar/" + "", targetLines)
}

static List<String> readFrom(File jarFile, String resourceName) {
def zip = new ZipFile(jarFile)
try {
Expand Down