Skip to content

Commit

Permalink
Trim the last EOL in AppendingTransformer processing
Browse files Browse the repository at this point in the history
This is useful for supporting the custom line separator in the future. In that case we can handle `resources/application.yml` files, that are separated by `---`.
I did the same patch for Gradle Shadow plugin in GradleUp/shadow@8300f91.
  • Loading branch information
Goooler committed Jan 19, 2025
1 parent 3a844d0 commit acc8cb2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ public boolean canTransformResource(String r) {
@Override
public void processResource(String resource, InputStream is, List<Relocator> relocators, long time)
throws IOException {
if (data.size() > 0) {
// Append the EOL before the new content to ensure the EOL is not at the end of the file.
data.write('\n');
}
IOUtil.copy(is, data);
data.write('\n');
if (time > this.time) {
this.time = time;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@
*/
package org.apache.maven.plugins.shade.resource;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Locale;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -56,4 +64,37 @@ public void testCanTransformResource() {
assertTrue(transformer.canTransformResource("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
assertFalse(transformer.canTransformResource("META-INF/MANIFEST.MF"));
}

@Test
public void testProcessResource() throws IOException {
transformer.resource = "test-resource";
String firstLine = "first line";
String secondLine = "second line";
InputStream firstIs = new ByteArrayInputStream(firstLine.getBytes());
InputStream secondIs = new ByteArrayInputStream(secondLine.getBytes());

transformer.processResource("", firstIs, Collections.emptyList());
transformer.processResource("", secondIs, Collections.emptyList());

final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (final JarOutputStream jarOutputStream = new JarOutputStream(out)) {
transformer.modifyOutputStream(jarOutputStream);
}

try (final JarInputStream jis = new JarInputStream(new ByteArrayInputStream(out.toByteArray()))) {
assertEquals("test-resource", jis.getNextJarEntry().getName());
String result = read(jis);
assertEquals(firstLine + "\n" + secondLine, result);
}
}

private String read(final JarInputStream jar) throws IOException {
final StringBuilder builder = new StringBuilder();
final byte[] buffer = new byte[512];
int read;
while ((read = jar.read(buffer)) >= 0) {
builder.append(new String(buffer, 0, read));
}
return builder.toString();
}
}

0 comments on commit acc8cb2

Please sign in to comment.