11package com .igormaznitsa .jcp .gradle ;
22
3- import static java .util .Collections .emptyList ;
43import static java .util .Collections .emptyMap ;
5- import static org .gradle .api .tasks .SourceSet .MAIN_SOURCE_SET_NAME ;
64
75
86import com .igormaznitsa .jcp .JcpPreprocessor ;
1715import java .util .Arrays ;
1816import java .util .Collections ;
1917import java .util .List ;
20- import java .util .Optional ;
21- import java .util .concurrent .atomic .AtomicBoolean ;
2218import java .util .stream .Collectors ;
2319import javax .annotation .Nullable ;
2420import javax .inject .Inject ;
2521import org .apache .commons .io .FilenameUtils ;
2622import org .gradle .api .DefaultTask ;
2723import org .gradle .api .logging .Logger ;
2824import org .gradle .api .model .ObjectFactory ;
29- import org .gradle .api .plugins .JavaPluginConvention ;
3025import org .gradle .api .provider .ListProperty ;
3126import org .gradle .api .provider .MapProperty ;
3227import org .gradle .api .provider .Property ;
3328import org .gradle .api .provider .ProviderFactory ;
3429import org .gradle .api .tasks .Input ;
3530import org .gradle .api .tasks .InputFiles ;
3631import org .gradle .api .tasks .OutputDirectory ;
37- import org .gradle .api .tasks .SourceSetContainer ;
3832import org .gradle .api .tasks .TaskAction ;
3933import org .gradle .api .tasks .TaskExecutionException ;
4034import org .gradle .execution .commandline .TaskConfigurationException ;
4135
42- public class JcpPreprocessTask extends DefaultTask {
36+ public class JcpTask extends DefaultTask {
4337
4438 public static final String ID = "preprocess" ;
4539
@@ -149,13 +143,8 @@ public class JcpPreprocessTask extends DefaultTask {
149143 */
150144 private final Property <Boolean > dontOverwriteSameContent ;
151145
152- /**
153- * Auto replace java source folders by the target folder.
154- */
155- private final Property <Boolean > autoReplaceSources ;
156-
157146 @ Inject
158- public JcpPreprocessTask (ProviderFactory providerFactory ) {
147+ public JcpTask (ProviderFactory providerFactory ) {
159148 super ();
160149 final ObjectFactory factory = this .getProject ().getObjects ();
161150
@@ -178,58 +167,17 @@ public JcpPreprocessTask(ProviderFactory providerFactory) {
178167
179168 this .vars = factory .mapProperty (String .class , String .class );
180169
181- this .sources = factory .listProperty (File .class ). convention ( providerFactory . provider (() -> findJavaSourceFolders (). orElse ( emptyList ()))) ;
170+ this .sources = factory .listProperty (File .class );
182171
183172 this .configFiles = factory .listProperty (String .class );
184173 this .excludeExtensions = factory .listProperty (String .class ).convention (Collections .singletonList ("xml" ));
185174 this .excludeFolders = factory .listProperty (String .class );
186175 this .fileExtensions = factory .listProperty (String .class ).convention (new ArrayList <>(Arrays .asList ("java" , "txt" , "htm" , "html" )));
187176
188177 this .baseDir = factory .property (File .class ).convention (this .getProject ().getProjectDir ());
189- this .autoReplaceSources = factory .property (Boolean .class ).convention (false );
190178 this .target = factory .property (File .class ).convention (new File (this .getProject ().getBuildDir (), "java-comment-preprocessor" + File .separatorChar + this .getTaskIdentity ().name ));
191179 }
192180
193- private Optional <List <File >> findJavaSourceFolders () {
194- final JavaPluginConvention javaPluginConvention = this .getProject ().getConvention ().findPlugin (JavaPluginConvention .class );
195-
196- if (javaPluginConvention == null ) {
197- this .getLogger ().debug ("Can't find Java plugin" );
198- return Optional .empty ();
199- }
200-
201- final SourceSetContainer srcSetContainer = javaPluginConvention .getSourceSets ();
202- return Optional .of (srcSetContainer
203- .stream ()
204- .filter (x -> MAIN_SOURCE_SET_NAME .equals (x .getName ()))
205- .flatMap (x -> x .getJava ().getSrcDirs ().stream ())
206- .collect (Collectors .toList ()));
207- }
208-
209- private boolean replaceJavaSourceFolders (final File targetFolder ) {
210- final JavaPluginConvention javaPluginConvention = this .getProject ().getConvention ().findPlugin (JavaPluginConvention .class );
211- final AtomicBoolean replaced = new AtomicBoolean ();
212- if (javaPluginConvention == null ) {
213- this .getLogger ().debug ("Can't find Java plugin" );
214- } else {
215- this .getLogger ().debug ("Detected Java plugin, trying to replace its main source set" );
216- final SourceSetContainer srcSetContainer = javaPluginConvention .getSourceSets ();
217- srcSetContainer
218- .stream ()
219- .filter (x -> MAIN_SOURCE_SET_NAME .equals (x .getName ()))
220- .forEach (x -> {
221- x .getJava ().setSrcDirs (Collections .singletonList (targetFolder ));
222- replaced .set (true );
223- });
224- }
225- return replaced .get ();
226- }
227-
228- @ Input
229- public Property <Boolean > getAutoReplaceSources () {
230- return this .autoReplaceSources ;
231- }
232-
233181 @ InputFiles
234182 public ListProperty <File > getSources () {
235183 return this .sources ;
@@ -398,7 +346,7 @@ public void warning(@Nullable final String message) {
398346
399347 final List <File > sourcesList = this .sources .get ();
400348 if (sourcesList .isEmpty ()) {
401- throw new TaskConfigurationException (JcpPreprocessTask .ID , "Source folder list must be defined as 'sources'" , null );
349+ throw new TaskConfigurationException (JcpTask .ID , "Source folder list must be defined as 'sources'" , null );
402350 }
403351
404352 List <File > preparedSourcesList = new ArrayList <>();
@@ -412,7 +360,7 @@ public void warning(@Nullable final String message) {
412360 }
413361 }
414362
415- logger .info ("Sources : " + preparedSourcesList );
363+ logger .info ("Source folders in use : " + preparedSourcesList );
416364
417365 preprocessorContext .setSources (preparedSourcesList .stream ().map (File ::getAbsolutePath ).collect (Collectors .toList ()));
418366 preprocessorContext .setEol (this .eol .get ());
@@ -439,15 +387,8 @@ public void warning(@Nullable final String message) {
439387 });
440388
441389 final JcpPreprocessor preprocessor = new JcpPreprocessor (preprocessorContext );
442- logger .debug ("Start preprocessing..." );
443390
391+ logger .debug ("Preprocessing starting" );
444392 preprocessor .execute ();
445-
446- if (this .autoReplaceSources .get ()) {
447- logger .info ("Trying auto-replace source folders by the preprocessed one: " + targetFolder );
448- if (!this .replaceJavaSourceFolders (targetFolder )) {
449- throw new TaskExecutionException (this , new IllegalStateException ("Can't replace sources automatically, may be some unsupported type of project, use manual replacement!" ));
450- }
451- }
452393 }
453394}
0 commit comments