View Javadoc

1   /**
2    *
3    */
4   package org_scala_tools_maven;
5   
6   import java.io.File;
7   import java.io.IOException;
8   import java.util.ArrayList;
9   import java.util.Collections;
10  import java.util.HashSet;
11  import java.util.List;
12  import java.util.Set;
13  
14  import org_scala_tools_maven_executions.MainHelper;
15  
16  /**
17   * @author david.bernard
18   */
19  abstract public class ScalaSourceMojoSupport extends ScalaMojoSupport {
20  
21      /**
22       * Enables/Disables sending java source to the scala compiler.
23       *
24       * @parameter default-value="true"
25       */
26      protected boolean sendJavaToScalac = true;
27  
28      /**
29       * A list of inclusion filters for the compiler.
30       * ex :
31       * <pre>
32       *    &lt;includes&gt;
33       *      &lt;include&gt;SomeFile.scala&lt;/include&gt;
34       *    &lt;/includes&gt;
35       * </pre>
36       *
37       * @parameter
38       */
39      protected Set<String> includes = new HashSet<String>();
40  
41      /**
42       * A list of exclusion filters for the compiler.
43       * ex :
44       * <pre>
45       *    &lt;excludes&gt;
46       *      &lt;exclude&gt;SomeBadFile.scala&lt;/exclude&gt;
47       *    &lt;/excludes&gt;
48       * </pre>
49       *
50       * @parameter
51       */
52      protected Set<String> excludes = new HashSet<String>();
53  
54      /**
55       * Retreives the list of *all* root source directories.  We need to pass all .java and .scala files into the scala compiler
56       */
57      abstract protected List<File> getSourceDirectories() throws Exception;
58  
59      private boolean _filterPrinted = false;
60  
61      /**
62       * Finds all source files in a set of directories with a given extension.
63       */
64      protected List<File> findSourceWithFilters() throws Exception {
65          return  findSourceWithFilters(getSourceDirectories());
66      }
67  
68      protected void initFilters() throws Exception {
69        if (includes.isEmpty()) {
70          includes.add("**/*.scala");
71          if (sendJavaToScalac && isJavaSupportedByCompiler()) {
72              includes.add("**/*.java");
73          }
74        }
75        if (!_filterPrinted && getLog().isInfoEnabled()) {
76          StringBuilder builder = new StringBuilder("includes = [");
77          for (String include : includes) {
78              builder.append(include).append(",");
79          }
80          builder.append("]");
81          getLog().info(builder.toString());
82  
83          builder = new StringBuilder("excludes = [");
84          for (String exclude : excludes) {
85              builder.append(exclude).append(",");
86          }
87          builder.append("]");
88          getLog().info(builder.toString());
89          _filterPrinted = true;
90        }
91      }
92      
93      /**
94       * Finds all source files in a set of directories with a given extension.
95       */
96      protected List<File> findSourceWithFilters(List<File> sourceRootDirs) throws Exception {
97          List<File> sourceFiles = new ArrayList<File>();
98          
99          initFilters();
100 
101         // TODO - Since we're making files anyway, perhaps we should just test
102         // for existence here...
103         for (File dir : sourceRootDirs) {
104             String[] tmpFiles = MainHelper.findFiles(dir, includes.toArray(new String[includes.size()]), excludes.toArray(new String[excludes.size()]));
105             for (String tmpLocalFile : tmpFiles) {
106                 File tmpAbsFile = normalize(new File(dir, tmpLocalFile));
107                 sourceFiles.add(tmpAbsFile);
108             }
109         }
110         //scalac is sensible to scala file order, file system can't garanty file order => unreproductible build error across platform
111         // to garanty reproductible command line we order file by path (os dependend).
112         Collections.sort(sourceFiles);
113         return sourceFiles;
114     }
115 
116     protected File normalize(File f) {
117         try {
118             f = f.getCanonicalFile();
119         } catch (IOException exc) {
120             f = f.getAbsoluteFile();
121         }
122         return f;
123     }
124 
125     /**
126      * This limits the source directories to only those that exist for real.
127      */
128     protected List<File> normalize(List<String> compileSourceRootsList) {
129         List<File> newCompileSourceRootsList = new ArrayList<File>();
130         if (compileSourceRootsList != null) {
131             // copy as I may be modifying it
132             for (String srcDir : compileSourceRootsList) {
133                 File srcDirFile = normalize(new File(srcDir));
134                 if (!newCompileSourceRootsList.contains(srcDirFile) && srcDirFile.exists()) {
135                     newCompileSourceRootsList.add(srcDirFile);
136                 }
137             }
138         }
139         return newCompileSourceRootsList;
140     }
141 }