Skip to content

Commit

Permalink
Fix #211: Append System.lineSeparator() to last line (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored Oct 1, 2024
1 parent 92b5223 commit 6cffca4
Showing 1 changed file with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,27 @@

package org.primefaces.extensions.optimizerplugin.optimizer;

import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Objects;

import com.google.common.io.CharSink;
import com.google.common.io.FileWriteMode;
import com.google.common.io.Files;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import org.primefaces.extensions.optimizerplugin.util.ResourcesSetAdapter;

import com.google.common.io.FileWriteMode;
import com.google.common.io.Files;

/**
* Basis abstract class for Google Closure Compiler / YUI Compressor Optimizers.
*
Expand Down Expand Up @@ -91,21 +98,23 @@ protected File aggregateFiles(ResourcesSetAdapter rsa, Charset cset, boolean del
StringWriter writer = new StringWriter();
IOUtil.copy(in, writer);

CharSink charSink = Files.asCharSink(outputFile, cset, FileWriteMode.APPEND);
charSink.write(System.lineSeparator());
if (delimeters && outputFile.length() > 0) {
// append semicolon to the new file in order to avoid invalid JS code
Files.asCharSink(outputFile, cset, FileWriteMode.APPEND).write(";");
charSink.write(";");
}

// write / append content into / to the new file
Files.asCharSink(outputFile, cset, FileWriteMode.APPEND).write(writer.toString());
charSink.write(writer.toString());
}
}

return outputFile;
}

protected void deleteFilesIfNecessary(ResourcesSetAdapter rsa) {
if (rsa.getAggregation().isRemoveIncluded() && rsa.getFiles().size() > 0) {
if (rsa.getAggregation().isRemoveIncluded() && !rsa.getFiles().isEmpty()) {
for (File file : rsa.getFiles()) {
if (file.exists() && !file.delete()) {
log.warn("File " + file.getName() + " could not be deleted after aggregation.");
Expand All @@ -115,7 +124,7 @@ protected void deleteFilesIfNecessary(ResourcesSetAdapter rsa) {
}

protected void deleteDirectoryIfNecessary(ResourcesSetAdapter rsa) {
if (rsa.getAggregation().isRemoveEmptyDirectories() && rsa.getFiles().size() > 0) {
if (rsa.getAggregation().isRemoveEmptyDirectories() && !rsa.getFiles().isEmpty()) {
for (File file : rsa.getFiles()) {
File directory = file.isDirectory() ? file : file.getParentFile();
while (directory != null && directory.isDirectory() &&
Expand All @@ -142,7 +151,7 @@ protected void prependFile(File prependedFile, File outputFile, Charset cset,
StringWriter writer = new StringWriter();
IOUtil.copy(in, writer);

writer.write(System.getProperty("line.separator"));
writer.write(System.lineSeparator());

// write / append compiled content into / to the new file
Files.asCharSink(outputFile, cset, FileWriteMode.APPEND).write(writer.toString());
Expand Down Expand Up @@ -185,4 +194,4 @@ protected void addToOptimizedSize(File file) {
protected void addToOptimizedSize(long size) {
sizeTotalOptimized = sizeTotalOptimized + size;
}
}
}

0 comments on commit 6cffca4

Please sign in to comment.