@@ -7,6 +7,7 @@ import org.gradle.work.InputChanges
77import processing.mode.java.preproc.PdePreprocessor
88import java.io.File
99import java.util.concurrent.Callable
10+ import java.util.jar.JarFile
1011import javax.inject.Inject
1112
1213abstract class ProcessingTask : SourceTask () {
@@ -45,22 +46,66 @@ abstract class ProcessingTask : SourceTask() {
4546 }.joinToString(" \n " ){
4647 it.readText()
4748 }
48- File (outputDirectory, " $sketchName .java" )
49- .bufferedWriter()
50- .use { out ->
51- val meta = PdePreprocessor
52- .builderFor(sketchName)
53- .build()
54- .write(out , combined)
49+ val javaFile = File (outputDirectory, " $sketchName .java" ).bufferedWriter()
5550
51+ val meta = PdePreprocessor
52+ .builderFor(sketchName)
53+ .build()
54+ .write(javaFile, combined)
5655
57- val importStatement = meta.importStatements
58- println (sketchBook )
56+ javaFile.flush()
57+ javaFile.close( )
5958
60- // for (import in importStatement) {
61- // project.dependencies.add("implementation", import)
62- // }
59+ // Scan all the libaries in the sketchbook
60+ val libraries = File (sketchBook, " libraries" )
61+ .listFiles { file -> file.isDirectory }
62+ ?.map { folder ->
63+ // Find all the jars in the sketch book
64+ val jars = folder.resolve(" library" )
65+ .listFiles{ file -> file.extension == " jar" }
66+ ?.map{ file ->
67+
68+ // Inside of each jar, look for the defined classes
69+ val jar = JarFile (file)
70+ val classes = jar.entries().asSequence()
71+ .filter { entry -> entry.name.endsWith(" .class" ) }
72+ .map { entry -> entry.name }
73+ .map { it.substringBeforeLast(' /' ).replace(' /' , ' .' ) }
74+ .distinct()
75+ .toList()
76+
77+ // Return a reference to the jar and its classes
78+ return @map object {
79+ val name = file.name
80+ val path = file
81+ val classes = classes
82+ }
83+ }? : emptyList()
84+
85+ // Save the parsed jars and which folder
86+ return @map object {
87+ val name = folder.name
88+ val path = folder
89+ val jars = jars
90+ }
6391 }
92+
93+ // Loop over the import statements and find the library jars that provide those imports
94+ val dependencies = mutableSetOf<File >()
95+ meta.importStatements.map { import ->
96+ libraries?.map { library ->
97+ library.jars.map { jar ->
98+ jar.classes
99+ .filter { className -> className.startsWith(import.packageName) }
100+ .map { _ ->
101+ dependencies.add(jar.path)
102+ }
103+ }
104+ }
105+ }
106+ // Write the dependencies to a file
107+ val deps = File (outputDirectory, " $sketchName .dependencies" )
108+ deps.writeText(dependencies.joinToString(" \n " ) { it.absolutePath })
64109 }
65110
66111 @get:Inject
0 commit comments