Skip to content

Commit 7379166

Browse files
committed
Processing Plugin tests & Refactor
1 parent b64505d commit 7379166

File tree

6 files changed

+227
-56
lines changed

6 files changed

+227
-56
lines changed

app/src/processing/app/gradle/GradleJob.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ import java.nio.file.Path
3333
import kotlin.io.path.deleteIfExists
3434
import kotlin.io.path.writeText
3535

36-
// Starts a gradle job to run the sketch
36+
/*
37+
* The gradle job runs the gradle tasks and manages the gradle connection
38+
*/
3739
class GradleJob(
3840
vararg val tasks: String,
3941
val workingDir: Path,
@@ -231,16 +233,18 @@ class GradleJob(
231233
}
232234
}
233235

234-
fun launchJob(block: suspend CoroutineScope.() -> Unit){
235-
val job = scope.launch { block() }
236-
jobs.add(job)
237-
}
236+
238237

239238
fun cancel(){
240239
cancel.cancel()
241240
jobs.forEach(Job::cancel)
242241
}
243242

243+
private fun launchJob(block: suspend CoroutineScope.() -> Unit){
244+
val job = scope.launch { block() }
245+
jobs.add(job)
246+
}
247+
244248
// Handle exception thrown by Gradle
245249
private fun handleExceptions(action: () -> Unit){
246250
try{
@@ -278,6 +282,7 @@ class GradleJob(
278282
}
279283
}
280284

285+
// TODO: Move to separate file
281286
private fun BuildLauncher.addStateListener(){
282287
addProgressListener(ProgressListener { event ->
283288
if(event is TaskStartEvent) {

java/gradle/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins{
77

88
repositories {
99
mavenCentral()
10+
maven("https://jogamp.org/deployment/maven")
1011
}
1112

1213
dependencies{
@@ -16,9 +17,11 @@ dependencies{
1617
implementation(libs.kotlinGradlePlugin)
1718
implementation(libs.kotlinComposePlugin)
1819

20+
testImplementation(project(":core"))
1921
testImplementation(libs.junit)
2022
}
2123

24+
// TODO: CI/CD for publishing the plugin to the Gradle Plugin Portal
2225
gradlePlugin{
2326
plugins{
2427
create("processing"){

java/gradle/src/main/kotlin/LibrariesTask.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This task stores the resulting information in a file that can be used later to r
1717
*/
1818
abstract class LibrariesTask : DefaultTask() {
1919

20+
// TODO: Allow multiple directories
2021
@InputDirectory
2122
@Optional
2223
val librariesDirectory: DirectoryProperty = project.objects.directoryProperty()
@@ -41,6 +42,9 @@ abstract class LibrariesTask : DefaultTask() {
4142
fun execute() {
4243
if (!librariesDirectory.isPresent) {
4344
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()
4448
return
4549
}
4650
val libraries = librariesDirectory.get().asFile

java/gradle/src/main/kotlin/PDETask.kt

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import org.gradle.work.InputChanges
77
import processing.mode.java.preproc.PdePreprocessor
88
import java.io.File
99
import java.io.ObjectOutputStream
10+
import java.io.Serializable
1011
import java.util.concurrent.Callable
1112
import java.util.jar.JarFile
1213
import javax.inject.Inject
1314

1415

1516
// TODO: Generate sourcemaps
1617
/*
17-
* The PDETask is the main task that processes the .pde files and generates the Java source code
18+
* The PDETask is the main task that processes the .pde files and generates the Java source code through the PdePreprocessor.
1819
*/
1920
abstract class PDETask : SourceTask() {
2021
@get:InputFiles
@@ -24,21 +25,13 @@ abstract class PDETask : SourceTask() {
2425
open val stableSources: FileCollection = project.files(Callable<Any> { this.source })
2526

2627
@OutputDirectory
27-
val outputDirectory = project.objects.directoryProperty()
28-
29-
@get:Input
30-
@get:Optional
31-
var workingDir: String? = null
28+
val outputDirectory: DirectoryProperty = project.objects.directoryProperty()
3229

3330
@get:Input
3431
var sketchName: String = "processing"
3532

36-
@get:Input
37-
@get:Optional
38-
var sketchBook: String? = null
39-
4033
@OutputFile
41-
val sketchMetaData = project.objects.fileProperty()
34+
val sketchMetaData: RegularFileProperty = project.objects.fileProperty()
4235

4336
init{
4437
outputDirectory.convention(project.layout.buildDirectory.dir("generated/pde"))
@@ -49,18 +42,16 @@ abstract class PDETask : SourceTask() {
4942
val sketchName: String,
5043
val sketchRenderer: String?,
5144
val importStatements: List<String>
52-
) : java.io.Serializable
45+
) : Serializable
5346

5447
@TaskAction
5548
fun execute() {
56-
// TODO: Allow pre-processor to run on individual files (future)
57-
// TODO: Only compare file names from both defined roots (e.g. sketch.pde and folder/sketch.pde should both be included)
58-
5949
// Using stableSources since we can only run the pre-processor on the full set of sources
6050
val combined = stableSources
6151
.files
6252
.groupBy { it.name }
6353
.map { entry ->
54+
// TODO: Select by which one is in the unsaved folder
6455
entry.value.maxByOrNull { it.lastModified() }!!
6556
}
6657
.joinToString("\n"){
@@ -74,6 +65,8 @@ abstract class PDETask : SourceTask() {
7465
.build()
7566
.write(javaFile, combined)
7667

68+
// TODO: Save the edits to meta files
69+
7770
javaFile.flush()
7871
javaFile.close()
7972

@@ -87,10 +80,4 @@ abstract class PDETask : SourceTask() {
8780
metaFile.writeObject(sketchMeta)
8881
metaFile.close()
8982
}
90-
91-
@get:Inject
92-
open val deleter: Deleter
93-
get() {
94-
throw UnsupportedOperationException("Decorator takes care of injection")
95-
}
9683
}

java/gradle/src/main/kotlin/ProcessingPlugin.kt

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import java.net.Socket
1717
import java.util.*
1818
import javax.inject.Inject
1919

20-
// TODO: CI/CD for publishing the plugin
2120
class ProcessingPlugin @Inject constructor(private val objectFactory: ObjectFactory) : Plugin<Project> {
2221
override fun apply(project: Project) {
2322
val sketchName = project.layout.projectDirectory.asFile.name.replace(Regex("[^a-zA-Z0-9_]"), "_")
@@ -32,6 +31,7 @@ class ProcessingPlugin @Inject constructor(private val objectFactory: ObjectFact
3231

3332
// TODO: Setup sketchbook when using as a standalone plugin, use the Java Preferences
3433
val sketchbook = project.findProperty("processing.sketchbook") as String?
34+
val settings = project.findProperty("processing.settings") as String?
3535

3636
// Apply the Java plugin to the Project
3737
project.plugins.apply(JavaPlugin::class.java)
@@ -46,13 +46,12 @@ class ProcessingPlugin @Inject constructor(private val objectFactory: ObjectFact
4646
project.tasks.findByName("wrapper")?.enabled = false
4747
}
4848

49-
// Add the compose plugin to wrap the sketch in an executable
50-
project.plugins.apply("org.jetbrains.compose")
51-
5249
// Add kotlin support
5350
project.plugins.apply("org.jetbrains.kotlin.jvm")
5451
// Add jetpack compose support
5552
project.plugins.apply("org.jetbrains.kotlin.plugin.compose")
53+
// Add the compose plugin to wrap the sketch in an executable
54+
project.plugins.apply("org.jetbrains.compose")
5655

5756
// Add the Processing core library (within Processing from the internal maven repo and outside from the internet)
5857
project.dependencies.add("implementation", "$processingGroup:core:${processingVersion}")
@@ -129,8 +128,7 @@ class ProcessingPlugin @Inject constructor(private val objectFactory: ObjectFact
129128

130129
}
131130

132-
project.extensions.getByType(JavaPluginExtension::class.java).sourceSets.all { sourceSet ->
133-
// For each java source set (mostly main) add a new source set for the PDE files
131+
project.extensions.getByType(JavaPluginExtension::class.java).sourceSets.first().let{ sourceSet ->
134132
val pdeSourceSet = objectFactory.newInstance(
135133
DefaultPDESourceDirectorySet::class.java,
136134
objectFactory.sourceDirectorySet("${sourceSet.name}.pde", "${sourceSet.name} Processing Source")
@@ -142,43 +140,37 @@ class ProcessingPlugin @Inject constructor(private val objectFactory: ObjectFact
142140
srcDir("$workingDir/unsaved")
143141
}
144142
sourceSet.allSource.source(pdeSourceSet)
143+
sourceSet.java.srcDir(project.layout.projectDirectory).apply {
144+
include("**/*.java")
145+
exclude("${project.layout.buildDirectory.asFile.get()}/**/*")
146+
}
145147

146148
val librariesTaskName = sourceSet.getTaskName("scanLibraries", "PDE")
147149
val librariesScan = project.tasks.register(librariesTaskName, LibrariesTask::class.java) { task ->
148150
task.description = "Scans the libraries in the sketchbook"
149151
task.librariesDirectory.set(sketchbook?.let { File(it, "libraries") })
152+
// TODO: Save the libraries metadata to settings folder to share between sketches
150153
}
151154

152155
val pdeTaskName = sourceSet.getTaskName("preprocess", "PDE")
153156
val pdeTask = project.tasks.register(pdeTaskName, PDETask::class.java) { task ->
154157
task.description = "Processes the ${sourceSet.name} PDE"
155158
task.source = pdeSourceSet
156159
task.sketchName = sketchName
157-
task.workingDir = workingDir
158-
task.sketchBook = sketchbook
159160

160161
// Set the output of the pre-processor as the input for the java compiler
161162
sourceSet.java.srcDir(task.outputDirectory)
162-
163-
task.doLast {
164-
// Copy java files from the root to the generated directory
165-
project.copy { copyTask ->
166-
copyTask.from(project.layout.projectDirectory){ from ->
167-
from.include("*.java")
168-
}
169-
copyTask.into(task.outputDirectory)
170-
}
171-
}
172163
}
173164

174165
val depsTaskName = sourceSet.getTaskName("addLegacyDependencies", "PDE")
175166
project.tasks.register(depsTaskName, DependenciesTask::class.java){ task ->
167+
task.librariesMetaData
176168
task.dependsOn(pdeTask, librariesScan)
169+
// TODO: Save the libraries metadata to settings folder to share between sketches
177170
}
178171

179-
project.tasks.named(
180-
sourceSet.compileJavaTaskName
181-
) { task ->
172+
// Make sure that the PDE task runs before the java compilation task
173+
project.tasks.named(sourceSet.compileJavaTaskName) { task ->
182174
task.dependsOn(pdeTaskName, depsTaskName)
183175
}
184176
}

0 commit comments

Comments
 (0)