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

Fix #211: Append System.lineSeparator() to last line #212

Merged
merged 1 commit into from
Oct 1, 2024
Merged
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 @@ -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;
}
}
}
Loading