View Javadoc
1   /*
2    * Copyright 2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * You may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.codehaus.gmavenplus.mojo;
18  
19  import org.apache.maven.shared.model.fileset.FileSet;
20  import org.apache.maven.shared.model.fileset.util.FileSetManager;
21  
22  import java.io.File;
23  import java.util.*;
24  
25  import static java.util.Collections.singletonList;
26  
27  
28  /**
29   * This mojo provides access to the Groovy sources.
30   *
31   * @author Keegan Witt
32   * @since 1.0-beta-2
33   */
34  public abstract class AbstractGroovySourcesMojo extends AbstractGroovyMojo {
35  
36      /**
37       * Main source directory name.
38       */
39      protected static final String MAIN = "main";
40  
41      /**
42       * Test source directory name.
43       */
44      protected static final String TEST = "test";
45  
46      /**
47       * Gets the set of included files from the specified source files or source directory (if sources are null).
48       *
49       * @param fromSources        The sources to get the included files from
50       * @param includeJavaSources Whether to include Java sources
51       * @return The included files from the specified sources
52       */
53      protected SortedSet<File> getFiles(final FileSet[] fromSources, final boolean includeJavaSources) {
54          SortedSet<File> files = new TreeSet<>();
55          FileSetManager fileSetManager = new FileSetManager();
56  
57          for (FileSet fileSet : getFilesets(fromSources, includeJavaSources)) {
58              for (String include : fileSetManager.getIncludedFiles(fileSet)) {
59                  files.add(new File(fileSet.getDirectory(), include));
60              }
61          }
62  
63          return files;
64      }
65  
66      /**
67       * Gets the set of included files from the specified source files or source directory (if sources are null).
68       *
69       * @param fromSources        The sources to get the included files from
70       * @param includeJavaSources Whether to include Java sources
71       * @return The included files from the specified sources
72       */
73      protected SortedSet<File> getTestFiles(final FileSet[] fromSources, final boolean includeJavaSources) {
74          SortedSet<File> files = new TreeSet<>();
75          FileSetManager fileSetManager = new FileSetManager();
76  
77          for (FileSet fileSet : getTestFilesets(fromSources, includeJavaSources)) {
78              for (String include : fileSetManager.getIncludedFiles(fileSet)) {
79                  files.add(new File(fileSet.getDirectory(), include));
80              }
81          }
82  
83          return files;
84      }
85  
86      /**
87       * Gets the set of included filesets from the specified source files or source directory (if sources are null).
88       *
89       * @param fromSources        The sources to get the included files from
90       * @param includeJavaSources Whether to include Java sources
91       * @return The included filesets from the specified sources
92       */
93      protected FileSet[] getFilesets(final FileSet[] fromSources, final boolean includeJavaSources) {
94          FileSet[] result;
95          FileSet[] groovyFileSets;
96  
97          if (fromSources != null) {
98              groovyFileSets = fromSources;
99          } else {
100             FileSet groovyFileSet = new FileSet();
101             String groovyDirectory = "src" + File.separator + MAIN + File.separator + "groovy";
102             groovyFileSet.setDirectory(project.getBasedir() + File.separator + groovyDirectory);
103             groovyFileSet.setIncludes(singletonList(GROOVY_SOURCES_PATTERN));
104             groovyFileSets = new FileSet[]{groovyFileSet};
105         }
106 
107         if (includeJavaSources) {
108             List<FileSet> javaFileSets = new ArrayList<>();
109             for (String sourceRoot : project.getCompileSourceRoots()) {
110                 FileSet javaFileSet = new FileSet();
111                 javaFileSet.setDirectory(sourceRoot);
112                 javaFileSet.setIncludes(singletonList(JAVA_SOURCES_PATTERN));
113                 javaFileSets.add(javaFileSet);
114             }
115             FileSet[] javaFileSetsArr = javaFileSets.toArray(new FileSet[0]);
116             result = Arrays.copyOf(groovyFileSets, groovyFileSets.length + javaFileSetsArr.length);
117             System.arraycopy(javaFileSetsArr, 0, result, groovyFileSets.length, javaFileSetsArr.length);
118         } else {
119             result = groovyFileSets;
120         }
121 
122         return result;
123     }
124 
125     /**
126      * Gets the set of included filesets from the specified source files or source directory (if sources are null).
127      *
128      * @param fromSources        The sources to get the included files from
129      * @param includeJavaSources Whether to include Java sources
130      * @return The included filesets from the specified sources
131      */
132     protected FileSet[] getTestFilesets(final FileSet[] fromSources, final boolean includeJavaSources) {
133         FileSet[] result;
134         FileSet[] groovyFileSets;
135 
136         if (fromSources != null) {
137             groovyFileSets = fromSources;
138         } else {
139             FileSet groovyFileSet = new FileSet();
140             String groovyDirectory = "src" + File.separator + TEST + File.separator + "groovy";
141             groovyFileSet.setDirectory(project.getBasedir() + File.separator + groovyDirectory);
142             groovyFileSet.setIncludes(singletonList(GROOVY_SOURCES_PATTERN));
143             groovyFileSets = new FileSet[]{groovyFileSet};
144         }
145 
146         if (includeJavaSources) {
147             List<FileSet> javaFileSets = new ArrayList<>();
148             for (String sourceRoot : project.getTestCompileSourceRoots()) {
149                 FileSet javaFileSet = new FileSet();
150                 javaFileSet.setDirectory(sourceRoot);
151                 javaFileSet.setIncludes(singletonList(JAVA_SOURCES_PATTERN));
152                 javaFileSets.add(javaFileSet);
153             }
154             FileSet[] javaFileSetsArr = javaFileSets.toArray(new FileSet[0]);
155             result = Arrays.copyOf(groovyFileSets, groovyFileSets.length + javaFileSetsArr.length);
156             System.arraycopy(javaFileSetsArr, 0, result, groovyFileSets.length, javaFileSetsArr.length);
157         } else {
158             result = groovyFileSets;
159         }
160 
161         return result;
162     }
163 }