diff --git a/src/main/java/net_alchim31_maven_yuicompressor/Aggregation.java b/src/main/java/net_alchim31_maven_yuicompressor/Aggregation.java index 3a9b966..86dc8df 100644 --- a/src/main/java/net_alchim31_maven_yuicompressor/Aggregation.java +++ b/src/main/java/net_alchim31_maven_yuicompressor/Aggregation.java @@ -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; @@ -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 run(Collection previouslyIncludedFiles) throws Exception { defineInputDir(); - List files = getIncludedFiles(); + + List files; + if (autoExcludeWildcards) { + files = getIncludedFiles(previouslyIncludedFiles); + } else { + files = getIncludedFiles(null); + } + if (files.size() != 0) { output = output.getCanonicalFile(); output.getParentFile().mkdirs(); @@ -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'); @@ -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 getIncludedFiles() throws Exception { - ArrayList back = new ArrayList(); + + private List getIncludedFiles(Collection previouslyIncludedFiles) throws Exception { + List filesToAggregate = new ArrayList(); if (includes != null) { for (String include : includes) { - addInto(include, back); + addInto(include, filesToAggregate, previouslyIncludedFiles); } } - return back; + return filesToAggregate; } - private void addInto(String include, List includedFiles) throws Exception { + private void addInto(String include, List includedFiles, Collection previouslyIncludedFiles) throws Exception { if (include.indexOf('*') > -1) { DirectoryScanner scanner = newScanner(); scanner.setIncludes(new String[] { include }); @@ -81,7 +93,7 @@ private void addInto(String include, List 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); } } diff --git a/src/main/java/net_alchim31_maven_yuicompressor/YuiCompressorMojo.java b/src/main/java/net_alchim31_maven_yuicompressor/YuiCompressorMojo.java index eaacf24..5052007 100644 --- a/src/main/java/net_alchim31_maven_yuicompressor/YuiCompressorMojo.java +++ b/src/main/java/net_alchim31_maven_yuicompressor/YuiCompressorMojo.java @@ -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; @@ -155,9 +158,12 @@ protected void afterProcess() throws Exception { private void aggregate() throws Exception { if (aggregations != null) { + Set previouslyIncludedFiles = new HashSet(); for(Aggregation aggregation : aggregations) { getLog().info("generate aggregation : " + aggregation.output); - aggregation.run(); + Collection aggregatedFiles = aggregation.run(previouslyIncludedFiles); + previouslyIncludedFiles.addAll(aggregatedFiles); + File gzipped = gzipIfRequested(aggregation.output); if (statistics) { if (gzipped != null) { diff --git a/src/test/java/net_alchim31_maven_yuicompressor/AggregationTestCase.java b/src/test/java/net_alchim31_maven_yuicompressor/AggregationTestCase.java index e5a6b20..8e682ee 100644 --- a/src/test/java/net_alchim31_maven_yuicompressor/AggregationTestCase.java +++ b/src/test/java/net_alchim31_maven_yuicompressor/AggregationTestCase.java @@ -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; @@ -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()); } @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); } @@ -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 previouslyIncluded = new HashSet(); + 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)); + } }