11package org.processing.java.gradle
22
33import org.gradle.api.DefaultTask
4- import org.gradle.api.file.DirectoryProperty
4+ import org.gradle.api.file.ConfigurableFileCollection
55import org.gradle.api.file.RegularFileProperty
6- import org.gradle.api.tasks.InputDirectory
7- import org.gradle.api.tasks.Optional
6+ import org.gradle.api.tasks.InputFiles
87import org.gradle.api.tasks.OutputFile
98import org.gradle.api.tasks.TaskAction
109import java.io.File
@@ -17,19 +16,17 @@ This task stores the resulting information in a file that can be used later to r
1716 */
1817abstract class LibrariesTask : DefaultTask () {
1918
20- // TODO: Allow multiple directories
21- @InputDirectory
22- @Optional
23- val librariesDirectory: DirectoryProperty = project.objects.directoryProperty()
19+ @InputFiles
20+ val libraryDirectories: ConfigurableFileCollection = project.files()
2421
2522 @OutputFile
2623 val librariesMetaData: RegularFileProperty = project.objects.fileProperty()
2724
2825 init {
29- librariesMetaData.convention( project.layout.buildDirectory.file( " processing/libraries" ))
26+ librariesMetaData.convention { project.gradle.gradleUserHomeDir.resolve( " common/ processing/libraries" ) }
3027 }
3128
32- data class Jar (
29+ data class Jar (
3330 val path : File ,
3431 val classes : List <String >
3532 ) : java.io.Serializable
@@ -40,45 +37,45 @@ abstract class LibrariesTask : DefaultTask() {
4037
4138 @TaskAction
4239 fun execute () {
43- if (! librariesDirectory.isPresent) {
44- logger.error(" Libraries directory is not set. Libraries will not be imported." )
45- val meta = ObjectOutputStream (librariesMetaData.get().asFile.outputStream())
46- meta.writeObject(arrayListOf<Library >())
47- meta.close()
48- return
49- }
50- val libraries = librariesDirectory.get().asFile
51- .listFiles { file -> file.isDirectory }
52- ?.map { folder ->
53- // Find all the jars in the sketchbook
54- val jars = folder.resolve(" library" )
55- .listFiles{ file -> file.extension == " jar" }
56- ?.map{ file ->
40+ val output = libraryDirectories.flatMap { librariesDirectory ->
41+ if (! librariesDirectory.exists()) {
42+ logger.error(" Libraries directory (${librariesDirectory.path} ) does not exist. Libraries will not be imported." )
43+ return @flatMap emptyList()
44+ }
45+ val libraries = librariesDirectory
46+ .listFiles { file -> file.isDirectory }
47+ ?.map { folder ->
48+ // Find all the jars in the sketchbook
49+ val jars = folder.resolve(" library" )
50+ .listFiles{ file -> file.extension == " jar" }
51+ ?.map{ file ->
5752
58- // Inside each jar, look for the defined classes
59- val jar = JarFile (file)
60- val classes = jar.entries().asSequence()
61- .filter { entry -> entry.name.endsWith(" .class" ) }
62- .map { entry -> entry.name }
63- .map { it.substringBeforeLast(' /' ).replace(' /' , ' .' ) }
64- .distinct()
65- .toList()
53+ // Inside each jar, look for the defined classes
54+ val jar = JarFile (file)
55+ val classes = jar.entries().asSequence()
56+ .filter { entry -> entry.name.endsWith(" .class" ) }
57+ .map { entry -> entry.name }
58+ .map { it.substringBeforeLast(' /' ).replace(' /' , ' .' ) }
59+ .distinct()
60+ .toList()
6661
67- // Return a reference to the jar and its classes
68- return @map Jar (
69- path = file,
70- classes = classes
71- )
72- }? : emptyList()
62+ // Return a reference to the jar and its classes
63+ return @map Jar (
64+ path = file,
65+ classes = classes
66+ )
67+ }? : emptyList()
7368
74- // Save the parsed jars and which folder
75- return @map Library (
76- jars = jars
77- )
78- }? : emptyList()
69+ // Save the parsed jars and which folder
70+ return @map Library (
71+ jars = jars
72+ )
73+ }? : emptyList()
7974
75+ return @flatMap libraries
76+ }
8077 val meta = ObjectOutputStream (librariesMetaData.get().asFile.outputStream())
81- meta.writeObject(libraries )
78+ meta.writeObject(output )
8279 meta.close()
8380 }
8481}
0 commit comments