@@ -2,20 +2,19 @@ package processing.app.gradle
22
33import androidx.compose.runtime.mutableStateListOf
44import androidx.compose.runtime.mutableStateOf
5- import androidx.compose.runtime.snapshotFlow
6- import kotlinx.coroutines.CoroutineScope
7- import kotlinx.coroutines.Dispatchers
8- import kotlinx.coroutines.launch
95import org.gradle.tooling.BuildLauncher
10- import processing.app.Base
11- import processing.app.Language
6+ import processing.app.Base.DEBUG
7+ import processing.app.Base.getSketchbookFolder
8+ import processing.app.Base.getVersionName
9+ import processing.app.Language.text
1210import processing.app.Messages
1311import processing.app.Mode
1412import processing.app.Platform
13+ import processing.app.Platform.getContentFile
14+ import processing.app.Platform.getSettingsFolder
1515import processing.app.Preferences
1616import processing.app.Sketch
1717import processing.app.ui.Editor
18- import java.io.*
1918import kotlin.io.path.createTempDirectory
2019import kotlin.io.path.deleteIfExists
2120import kotlin.io.path.writeText
@@ -48,51 +47,51 @@ class GradleService(
4847
4948 val jobs = mutableStateListOf<GradleJob >()
5049 val workingDir = createTempDirectory()
50+
5151 val debugPort = (30_000 .. 60_000 ).random()
52+ val logPort = debugPort + 1
53+ val errPort = logPort + 1
5254
5355 fun run (){
54- startAction (" run" )
56+ startJob (" run" )
5557 }
5658
5759 fun export (){
58- startAction (" runDistributable" )
60+ startJob (" runDistributable" )
5961 }
6062
6163 fun stop (){
62- stopActions ()
64+ stopJobs ()
6365 }
6466
65- private fun startAction (vararg tasks : String ) {
67+ private fun startJob (vararg tasks : String ) {
6668 if (! active.value) return
67- editor?.let { println (Language . text(" gradle.using_gradle" )) }
69+ editor?.let { println (text(" gradle.using_gradle" )) }
6870
6971 val job = GradleJob ()
7072 job.service = this
7173 job.configure = {
72- setup ()
74+ setupGradle ()
7375 forTasks(tasks.joinToString(" " ))
7476 }
7577 jobs.add(job)
7678 job.start()
7779 }
7880
79- private fun stopActions (){
80- jobs
81- .forEach(GradleJob ::cancel)
81+ private fun stopJobs (){
82+ jobs.forEach(GradleJob ::cancel)
8283 }
8384
84- private fun setupGradle (): MutableList <String > {
85+ private fun BuildLauncher. setupGradle (extraArguments : List <String > = listOf()) {
8586 val sketch = sketch ? : throw IllegalStateException (" Sketch is not set" )
86-
8787 val copy = sketch.isReadOnly || sketch.isUntitled
88-
8988 val sketchFolder = if (copy) workingDir.resolve(" sketch" ).toFile() else sketch.folder
9089 if (copy){
9190 // If the sketch is read-only, we copy it to the working directory
9291 // This allows us to run the sketch without modifying the original files
9392 sketch.folder.copyRecursively(sketchFolder, overwrite = true )
9493 }
95-
94+ // Save the unsaved code into the working directory for gradle to compile
9695 val unsaved = sketch.code
9796 .map { code ->
9897 val file = workingDir.resolve(" unsaved/${code.fileName} " )
@@ -106,16 +105,18 @@ class GradleService(
106105 }
107106 return @map code.fileName
108107 }
109-
108+ // Collect the variables to pass to gradle
110109 val variables = mapOf (
111110 " group" to System .getProperty(" processing.group" , " org.processing" ),
112- " version" to Base . getVersionName(),
111+ " version" to getVersionName(),
113112 " sketchFolder" to sketchFolder,
114- " sketchbook" to Base . getSketchbookFolder(),
113+ " sketchbook" to getSketchbookFolder(),
115114 " workingDir" to workingDir.toAbsolutePath().toString(),
116- " settings" to Platform . getSettingsFolder().absolutePath.toString(),
115+ " settings" to getSettingsFolder().absolutePath.toString(),
117116 " unsaved" to unsaved.joinToString(" ," ),
118117 " debugPort" to debugPort.toString(),
118+ " logPort" to logPort.toString(),
119+ " errPort" to errPort.toString(),
119120 " fullscreen" to System .getProperty(" processing.fullscreen" , " false" ).equals(" true" ),
120121 " display" to 1 , // TODO: Implement
121122 " external" to true ,
@@ -126,8 +127,8 @@ class GradleService(
126127 // "stop.color" to "0xFF000000", // TODO: Implement
127128 " stop.hide" to false , // TODO: Implement
128129 )
129- val repository = Platform . getContentFile(" repository" ).absolutePath.replace(""" \""" , """ \\""" )
130-
130+ val repository = getContentFile(" repository" ).absolutePath.replace(""" \""" , """ \\""" )
131+ // Create the init.gradle.kts file in the working directory
131132 val initGradle = workingDir.resolve(" init.gradle.kts" ).apply {
132133 val content = """
133134 beforeSettings{
@@ -148,8 +149,7 @@ class GradleService(
148149
149150 writeText(content)
150151 }
151-
152-
152+ // Create the build.gradle.kts file in the sketch folder
153153 val buildGradle = sketchFolder.resolve(" build.gradle.kts" )
154154 val generate = buildGradle.let {
155155 if (! it.exists()) return @let true
@@ -158,67 +158,63 @@ class GradleService(
158158 if (! contents.contains(" @processing-auto-generated" )) return @let false
159159
160160 val version = contents.substringAfter(" version=" ).substringBefore(" \n " )
161- if (version != Base . getVersionName()) return @let true
161+ if (version != getVersionName()) return @let true
162162
163163 val modeTitle = contents.substringAfter(" mode=" ).substringBefore(" " )
164- if (this . mode.title != modeTitle) return @let true
164+ if (mode.title != modeTitle) return @let true
165165
166- return @let Base . DEBUG
166+ return @let DEBUG
167167 }
168168 if (generate) {
169169 Messages .log(" build.gradle.kts outdated or not found in ${sketch.folder} , creating one" )
170170 val header = """
171- // @processing-auto-generated mode=${mode.title} version=${Base . getVersionName()}
171+ // @processing-auto-generated mode=${mode.title} version=${getVersionName()}
172172 //
173173 """ .trimIndent()
174174
175- val instructions = Language . text(" gradle.instructions" )
175+ val instructions = text(" gradle.instructions" )
176176 .split(" \n " )
177177 .joinToString(" \n " ) { " // $it " }
178178
179179 val configuration = """
180180
181181 plugins{
182- id("org.processing.java") version "${Base . getVersionName()} "
182+ id("org.processing.java") version "${getVersionName()} "
183183 }
184184 """ .trimIndent()
185185 val content = " ${header} \n ${instructions} \n ${configuration} "
186186 buildGradle.writeText(content)
187187 }
188+ // Create the settings.gradle.kts file in the sketch folder
188189 val settingsGradle = sketchFolder.resolve(" settings.gradle.kts" )
189190 if (! settingsGradle.exists()) {
190191 settingsGradle.createNewFile()
191192 }
192-
193+ // Collect the arguments to pass to gradle
193194 val arguments = mutableListOf (" --init-script" , initGradle.toAbsolutePath().toString())
194- if (! Base . DEBUG ) arguments.add(" --quiet" )
195+ if (! DEBUG ) arguments.add(" --quiet" )
195196 if (copy){
196197 arguments + = listOf (" --project-dir" , sketchFolder.absolutePath)
197198 }
199+
198200 arguments.addAll(variables.entries
199201 .filter { it.value != null }
200202 .map { " -Pprocessing.${it.key} =${it.value} " }
201203 )
204+ arguments.addAll(extraArguments)
202205
203- return arguments
204- }
205-
206+ withArguments(* arguments.toTypedArray())
206207
207- private fun BuildLauncher.setup (extraArguments : List <String > = listOf()) {
208208 // TODO: Instead of shipping Processing with a build-in JDK we should download the JDK through Gradle
209209 setJavaHome(Platform .getJavaHome())
210-
211- val arguments = setupGradle()
212- arguments.addAll(extraArguments)
213- withArguments(* arguments.toTypedArray())
214210 }
215211
216212 // Hooks for java to check if the Gradle service is running since mutableStateOf is not accessible in java
217213 fun getEnabled (): Boolean {
218214 return active.value
219215 }
220216 fun setEnabled (active : Boolean ) {
221- if (! active) stopActions ()
217+ if (! active) stopJobs ()
222218 this .active.value = active
223219 }
224220}
0 commit comments