@@ -2,33 +2,29 @@ package processing.app.gradle
22
33import androidx.compose.runtime.mutableStateListOf
44import androidx.compose.runtime.mutableStateOf
5- import kotlinx.coroutines.CoroutineScope
6- import kotlinx.coroutines.Dispatchers
7- import kotlinx.coroutines.launch
85import org.gradle.tooling.BuildLauncher
9- import org.gradle.tooling.GradleConnector
10- import org.gradle.tooling.ProjectConnection
116import processing.app.Base
127import processing.app.Language
138import processing.app.Messages
9+ import processing.app.Mode
1410import processing.app.Platform
1511import processing.app.Preferences
12+ import processing.app.Sketch
1613import processing.app.gradle.helpers.ActionGradleJob
17- import processing.app.gradle.helpers.BackgroundGradleJob
1814import processing.app.ui.Editor
1915import java.io.*
20- import javax.swing.SwingUtilities
21- import javax.swing.event.DocumentEvent
22- import javax.swing.event.DocumentListener
16+ import kotlin.io.path.createTempDirectory
2317import kotlin.io.path.deleteIfExists
2418import kotlin.io.path.writeText
2519
2620// TODO: Test offline mode, gradle seems to be included as not needed to be downloaded.
2721// TODO: Test running examples
2822// TODO: Report failures to the console
23+ // TODO: Remove dependency on getting mode from Editor
24+ // TODO: Remove dependency on editor (editor is not mockable and not usable in the CLI, or move editor away from JFrame)
25+ // TODO: Highlight errors in the editor
2926
3027// TODO: ---- FUTURE ----
31- // TODO: Remove dependency on editor (editor is not mockable, or move editor away from JFrame)
3228// TODO: Improve progress tracking
3329// TODO: PoC new debugger/tweak mode
3430// TODO: Allow for plugins to skip gradle entirely / new modes
@@ -39,48 +35,46 @@ import kotlin.io.path.writeText
3935// It will create the necessary build files for gradle to run
4036// Then it will kick off a new GradleJob to run the tasks
4137// GradleJob manages the gradle build and connects the debugger
42- class GradleService (val editor : Editor ) {
43- val folder: File get() = editor.sketch.folder
38+ class GradleService (
39+ // TODO: Move to a mode object after decoupling from Editor
40+ val mode : Mode ,
41+ val editor : Editor ? ,
42+ ) {
4443 val active = mutableStateOf(Preferences .getBoolean(" run.use_gradle" ))
4544
46- val jobs = mutableStateListOf<GradleJob >()
47- val workingDir = kotlin.io.path.createTempDirectory()
48- val debugPort = (30_000 .. 60_000 ).random()
45+ var sketch: Sketch ? = null
4946
50- private val scope = CoroutineScope (Dispatchers .IO )
47+ var out : PrintStream = System .out
48+ var err: PrintStream = System .err
5149
52- // Hooks for java to check if the Gradle service is running since mutableStateOf is not accessible in java
53- fun getEnabled (): Boolean {
54- return active.value
55- }
56- fun setEnabled (active : Boolean ) {
57- this .active.value = active
58- }
50+ val jobs = mutableStateListOf<GradleJob >()
51+ val workingDir = createTempDirectory()
52+ val debugPort = (30_000 .. 60_000 ).random()
5953
6054 // TODO: Add support for present
6155 fun run (){
6256 stopActions()
63- editor.console.clear()
6457
6558 val job = ActionGradleJob ()
6659 job.service = this
6760 job.configure = {
6861 setup()
6962 forTasks(" run" )
7063 }
64+ jobs.add(job)
7165 job.start()
7266 }
7367
7468 fun export (){
7569 stopActions()
76- editor.console.clear()
7770
7871 val job = ActionGradleJob ()
7972 job.service = this
8073 job.configure = {
8174 setup()
8275 forTasks(" runDistributable" )
8376 }
77+ jobs.add(job)
8478 job.start()
8579 }
8680
@@ -95,7 +89,9 @@ class GradleService(val editor: Editor) {
9589 }
9690
9791 private fun setupGradle (): MutableList <String > {
98- val unsaved = editor.sketch.code
92+ val sketch = sketch ? : throw IllegalStateException (" Sketch is not set" )
93+
94+ val unsaved = sketch.code
9995 .map { code ->
10096 val file = workingDir.resolve(" unsaved/${code.fileName} " )
10197 file.parent.toFile().mkdirs()
@@ -114,7 +110,7 @@ class GradleService(val editor: Editor) {
114110 val variables = mapOf (
115111 " group" to group,
116112 " version" to Base .getVersionName(),
117- " sketchFolder" to folder.absolutePath,
113+ " sketchFolder" to sketch. folder.absolutePath,
118114 " sketchbook" to Base .getSketchbookFolder(),
119115 " workingDir" to workingDir.toAbsolutePath().toString(),
120116 " settings" to Platform .getSettingsFolder().absolutePath.toString(),
@@ -123,12 +119,12 @@ class GradleService(val editor: Editor) {
123119 " fullscreen" to false , // TODO: Implement
124120 " display" to 1 , // TODO: Implement
125121 " external" to true ,
126- " editor.location" to editor.location.let { " ${it.x} ,${it.y} " },
122+ " editor.location" to editor? .location? .let { " ${it.x} ,${it.y} " },
127123 // "awt.disable" to false,
128124 // "window.color" to "0xFF000000", // TODO: Implement
129125 // "stop.color" to "0xFF000000", // TODO: Implement
130126 " stop.hide" to false , // TODO: Implement
131- " sketch.folder" to folder.absolutePath,
127+ " sketch.folder" to sketch. folder.absolutePath,
132128 )
133129 val repository = Platform .getContentFile(" repository" ).absolutePath.replace(""" \""" , """ \\""" )
134130
@@ -154,7 +150,7 @@ class GradleService(val editor: Editor) {
154150 }
155151
156152
157- val buildGradle = folder.resolve(" build.gradle.kts" )
153+ val buildGradle = sketch. folder.resolve(" build.gradle.kts" )
158154 val generate = buildGradle.let {
159155 if (! it.exists()) return @let true
160156
@@ -164,15 +160,15 @@ class GradleService(val editor: Editor) {
164160 val version = contents.substringAfter(" version=" ).substringBefore(" \n " )
165161 if (version != Base .getVersionName()) return @let true
166162
167- val mode = contents.substringAfter(" mode=" ).substringBefore(" " )
168- if (editor .mode.title != mode ) return @let true
163+ val modeTitle = contents.substringAfter(" mode=" ).substringBefore(" " )
164+ if (this .mode.title != modeTitle ) return @let true
169165
170166 return @let Base .DEBUG
171167 }
172168 if (generate) {
173- Messages .log(" build.gradle.kts outdated or not found in ${folder} , creating one" )
169+ Messages .log(" build.gradle.kts outdated or not found in ${sketch. folder} , creating one" )
174170 val header = """
175- // @processing-auto-generated mode=${editor. mode.title} version=${Base .getVersionName()}
171+ // @processing-auto-generated mode=${mode.title} version=${Base .getVersionName()}
176172 //
177173 """ .trimIndent()
178174
@@ -193,14 +189,17 @@ class GradleService(val editor: Editor) {
193189 val content = " ${header} \n ${instructions} \n ${configuration} "
194190 buildGradle.writeText(content)
195191 }
196- val settingsGradle = folder.resolve(" settings.gradle.kts" )
192+ val settingsGradle = sketch. folder.resolve(" settings.gradle.kts" )
197193 if (! settingsGradle.exists()) {
198194 settingsGradle.createNewFile()
199195 }
200196
201197 val arguments = mutableListOf (" --init-script" , initGradle.toAbsolutePath().toString())
202198 if (! Base .DEBUG ) arguments.add(" --quiet" )
203- arguments.addAll(variables.entries.map { " -Pprocessing.${it.key} =${it.value} " })
199+ arguments.addAll(variables.entries
200+ .filter { it.value != null }
201+ .map { " -Pprocessing.${it.key} =${it.value} " }
202+ )
204203
205204 return arguments
206205 }
@@ -213,4 +212,12 @@ class GradleService(val editor: Editor) {
213212 arguments.addAll(extraArguments)
214213 withArguments(* arguments.toTypedArray())
215214 }
215+
216+ // Hooks for java to check if the Gradle service is running since mutableStateOf is not accessible in java
217+ fun getEnabled (): Boolean {
218+ return active.value
219+ }
220+ fun setEnabled (active : Boolean ) {
221+ this .active.value = active
222+ }
216223}
0 commit comments