1
+ package org.processing.java.gradle
2
+
3
+ import org.gradle.api.DefaultTask
4
+ import org.gradle.api.GradleException
5
+ import org.gradle.api.file.RegularFileProperty
6
+ import org.gradle.api.tasks.InputFile
7
+ import org.gradle.api.tasks.TaskAction
8
+ import java.io.File
9
+ import java.io.ObjectInputStream
10
+
11
+ /*
12
+ * The DependenciesTask resolves the dependencies for the sketch based on the libraries used
13
+ */
14
+ abstract class DependenciesTask : DefaultTask () {
15
+ @InputFile
16
+ val librariesMetaData: RegularFileProperty = project.objects.fileProperty()
17
+
18
+ @InputFile
19
+ val sketchMetaData: RegularFileProperty = project.objects.fileProperty()
20
+
21
+ init {
22
+ librariesMetaData.convention(project.layout.buildDirectory.file(" processing/libraries" ))
23
+ sketchMetaData.convention(project.layout.buildDirectory.file(" processing/sketch" ))
24
+ }
25
+
26
+ @TaskAction
27
+ fun execute () {
28
+ val sketchMetaFile = sketchMetaData.get().asFile
29
+ val librariesMetaFile = librariesMetaData.get().asFile
30
+
31
+ val libraries = librariesMetaFile.inputStream().use { input ->
32
+ ObjectInputStream (input).readObject() as ArrayList <LibrariesTask .Library >
33
+ }
34
+
35
+ val sketch = sketchMetaFile.inputStream().use { input ->
36
+ ObjectInputStream (input).readObject() as PDETask .SketchMeta
37
+ }
38
+
39
+ val dependencies = mutableSetOf<File >()
40
+
41
+ // Loop over the import statements in the sketch and import the relevant jars from the libraries
42
+ sketch.importStatements.forEach import@{ statement ->
43
+ libraries.forEach { library ->
44
+ library.jars.forEach { jar ->
45
+ jar.classes.forEach { className ->
46
+ if (className.startsWith(statement)) {
47
+ dependencies.addAll(library.jars.map { it.path } )
48
+ return @import
49
+ }
50
+ }
51
+ }
52
+ }
53
+ }
54
+ project.dependencies.add(" implementation" , project.files(dependencies) )
55
+
56
+ // TODO: Mutating the dependencies of configuration ':implementation' after it has been resolved or consumed. This
57
+
58
+ // TODO: Add only if user is compiling for P2D or P3D
59
+ // Add JOGL and Gluegen dependencies
60
+ project.dependencies.add(" runtimeOnly" , " org.jogamp.jogl:jogl-all-main:2.5.0" )
61
+ project.dependencies.add(" runtimeOnly" , " org.jogamp.gluegen:gluegen-rt:2.5.0" )
62
+
63
+ val os = System .getProperty(" os.name" ).lowercase()
64
+ val arch = System .getProperty(" os.arch" ).lowercase()
65
+
66
+ val variant = when {
67
+ os.contains(" mac" ) -> " macosx-universal"
68
+ os.contains(" win" ) && arch.contains(" 64" ) -> " windows-amd64"
69
+ os.contains(" linux" ) && arch.contains(" aarch64" ) -> " linux-aarch64"
70
+ os.contains(" linux" ) && arch.contains(" arm" ) -> " linux-arm"
71
+ os.contains(" linux" ) && arch.contains(" amd64" ) -> " linux-amd64"
72
+ else -> throw GradleException (" Unsupported OS/architecture: $os / $arch " )
73
+ }
74
+
75
+ project.dependencies.add(" runtimeOnly" , " org.jogamp.gluegen:gluegen-rt:2.5.0:natives-$variant " )
76
+ project.dependencies.add(" runtimeOnly" , " org.jogamp.jogl:nativewindow:2.5.0:natives-$variant " )
77
+ project.dependencies.add(" runtimeOnly" , " org.jogamp.jogl:newt:2.5.0:natives-$variant " )
78
+ }
79
+ }
0 commit comments