Skip to content
This repository has been archived by the owner on Mar 23, 2022. It is now read-only.

Commit

Permalink
Add autoExcludeWildcards parameter to Aggregation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Hughes committed Jan 21, 2014
1 parent 864c2de commit a7f5448
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 24 deletions.
34 changes: 23 additions & 11 deletions src/main/java/net_alchim31_maven_yuicompressor/Aggregation.java
Expand Up @@ -5,6 +5,7 @@
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.codehaus.plexus.util.DirectoryScanner;
Expand All @@ -18,10 +19,18 @@ public class Aggregation {
public boolean removeIncluded = false;
public boolean insertNewLine = false;
public boolean fixLastSemicolon = false;

public void run() throws Exception {
public boolean autoExcludeWildcards = false;

public List<File> run(Collection<File> previouslyIncludedFiles) throws Exception {
defineInputDir();
List<File> files = getIncludedFiles();

List<File> files;
if (autoExcludeWildcards) {
files = getIncludedFiles(previouslyIncludedFiles);
} else {
files = getIncludedFiles(null);
}

if (files.size() != 0) {
output = output.getCanonicalFile();
output.getParentFile().mkdirs();
Expand All @@ -35,7 +44,7 @@ public void run() throws Exception {
try {
IOUtil.copy(in, out);
if (fixLastSemicolon) {
out.write(';');
out.write(';');
}
if (insertNewLine) {
out.write('\n');
Expand All @@ -53,26 +62,29 @@ public void run() throws Exception {
out = null;
}
}

return files;
}


private void defineInputDir() throws Exception {
if (inputDir == null) {
inputDir = output.getParentFile();
}
inputDir = inputDir.getCanonicalFile();
}
private List<File> getIncludedFiles() throws Exception {
ArrayList<File> back = new ArrayList<File>();

private List<File> getIncludedFiles(Collection<File> previouslyIncludedFiles) throws Exception {
List<File> filesToAggregate = new ArrayList<File>();
if (includes != null) {
for (String include : includes) {
addInto(include, back);
addInto(include, filesToAggregate, previouslyIncludedFiles);
}
}
return back;
return filesToAggregate;
}

private void addInto(String include, List<File> includedFiles) throws Exception {
private void addInto(String include, List<File> includedFiles, Collection<File> previouslyIncludedFiles) throws Exception {
if (include.indexOf('*') > -1) {
DirectoryScanner scanner = newScanner();
scanner.setIncludes(new String[] { include });
Expand All @@ -81,7 +93,7 @@ private void addInto(String include, List<File> includedFiles) throws Exception
Arrays.sort(rpaths);
for (String rpath : rpaths) {
File file = new File(scanner.getBasedir(), rpath);
if (!includedFiles.contains(file)) {
if (!includedFiles.contains(file) && (previouslyIncludedFiles == null || !previouslyIncludedFiles.contains(file))) {
includedFiles.add(file);
}
}
Expand Down
Expand Up @@ -6,6 +6,9 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.zip.GZIPOutputStream;

import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -155,9 +158,12 @@ protected void afterProcess() throws Exception {

private void aggregate() throws Exception {
if (aggregations != null) {
Set<File> previouslyIncludedFiles = new HashSet<File>();
for(Aggregation aggregation : aggregations) {
getLog().info("generate aggregation : " + aggregation.output);
aggregation.run();
Collection<File> aggregatedFiles = aggregation.run(previouslyIncludedFiles);
previouslyIncludedFiles.addAll(aggregatedFiles);

File gzipped = gzipIfRequested(aggregation.output);
if (statistics) {
if (gzipped != null) {
Expand Down
@@ -1,6 +1,8 @@
package net_alchim31_maven_yuicompressor;

import java.io.File;
import java.util.Collection;
import java.util.HashSet;

import junit.framework.TestCase;

Expand All @@ -26,17 +28,17 @@ public void test0to1() throws Exception {
target.output = new File(dir_, "output.js");

assertFalse(target.output.exists());
target.run();
target.run(null);
assertFalse(target.output.exists());

target.includes = new String[]{};
assertFalse(target.output.exists());
target.run();
target.run(null);
assertFalse(target.output.exists());

target.includes = new String[]{"**/*.js"};
assertFalse(target.output.exists());
target.run();
target.run(null);
assertFalse(target.output.exists());
}

Expand All @@ -49,7 +51,7 @@ public void test1to1() throws Exception {
target.includes = new String[]{f1.getName()};

assertFalse(target.output.exists());
target.run();
target.run(null);
assertTrue(target.output.exists());
assertEquals(FileUtils.fileRead(f1), FileUtils.fileRead(target.output));
}
Expand All @@ -66,14 +68,14 @@ public void test2to1() throws Exception {

target.includes = new String[]{f1.getName(), f2.getName()};
assertFalse(target.output.exists());
target.run();
target.run(null);
assertTrue(target.output.exists());
assertEquals(FileUtils.fileRead(f1) + FileUtils.fileRead(f2), FileUtils.fileRead(target.output));

target.output.delete();
target.includes = new String[]{"*.js"};
assertFalse(target.output.exists());
target.run();
target.run(null);
assertTrue(target.output.exists());
assertEquals(FileUtils.fileRead(f1) + FileUtils.fileRead(f2), FileUtils.fileRead(target.output));
}
Expand All @@ -90,14 +92,14 @@ public void testNoDuplicateAggregation() throws Exception {

target.includes = new String[]{f1.getName(), f1.getName(), f2.getName()};
assertFalse(target.output.exists());
target.run();
target.run(null);
assertTrue(target.output.exists());
assertEquals(FileUtils.fileRead(f1) + FileUtils.fileRead(f2), FileUtils.fileRead(target.output));

target.output.delete();
target.includes = new String[]{f1.getName(), "*.js"};
assertFalse(target.output.exists());
target.run();
target.run(null);
assertTrue(target.output.exists());
assertEquals(FileUtils.fileRead(f1) + FileUtils.fileRead(f2), FileUtils.fileRead(target.output));
}
Expand All @@ -114,7 +116,7 @@ public void test2to1Order() throws Exception {

target.includes = new String[]{f2.getName(), f1.getName()};
assertFalse(target.output.exists());
target.run();
target.run(null);
assertTrue(target.output.exists());
assertEquals(FileUtils.fileRead(f2) + FileUtils.fileRead(f1), FileUtils.fileRead(target.output));
}
Expand All @@ -132,7 +134,7 @@ public void test2to1WithNewLine() throws Exception {
target.includes = new String[]{f1.getName(), f2.getName()};

assertFalse(target.output.exists());
target.run();
target.run(null);
assertTrue(target.output.exists());
assertEquals(FileUtils.fileRead(f1) + "\n" + FileUtils.fileRead(f2) + "\n", FileUtils.fileRead(target.output));
}
Expand All @@ -149,7 +151,7 @@ public void testAbsolutePathFromInside() throws Exception {

target.includes = new String[]{f1.getAbsolutePath(), f2.getName()};
assertFalse(target.output.exists());
target.run();
target.run(null);
assertTrue(target.output.exists());
assertEquals(FileUtils.fileRead(f1) + FileUtils.fileRead(f2), FileUtils.fileRead(target.output));
}
Expand All @@ -167,11 +169,39 @@ public void testAbsolutePathFromOutside() throws Exception {

target.includes = new String[]{f1.getAbsolutePath(), f2.getName()};
assertFalse(target.output.exists());
target.run();
target.run(null);
assertTrue(target.output.exists());
assertEquals(FileUtils.fileRead(f1) + FileUtils.fileRead(f2), FileUtils.fileRead(target.output));
} finally {
f1.delete();
}
}

public void testAutoExcludeWildcards() throws Exception {
File f1 = new File(dir_, "01.js");
FileUtils.fileWrite(f1.getAbsolutePath(), "1");

File f2 = new File(dir_, "02.js");
FileUtils.fileWrite(f2.getAbsolutePath(), "22\n22");

Aggregation target = new Aggregation();
target.autoExcludeWildcards = true;
target.output = new File(dir_, "output.js");

Collection<File> previouslyIncluded = new HashSet<File>();
previouslyIncluded.add(f1);

target.includes = new String[]{f1.getName(), f2.getName()};
assertFalse(target.output.exists());
target.run(previouslyIncluded);
assertTrue(target.output.exists());
assertEquals(FileUtils.fileRead(f1) + FileUtils.fileRead(f2), FileUtils.fileRead(target.output));

target.output.delete();
target.includes = new String[]{"*.js"};
assertFalse(target.output.exists());
target.run(previouslyIncluded);
assertTrue(target.output.exists());
assertEquals(FileUtils.fileRead(f2), FileUtils.fileRead(target.output));
}
}

0 comments on commit a7f5448

Please sign in to comment.