@@ -2,20 +2,19 @@ package processing.app.gradle
2
2
3
3
import androidx.compose.runtime.mutableStateListOf
4
4
import 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
9
5
import 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
12
10
import processing.app.Messages
13
11
import processing.app.Mode
14
12
import processing.app.Platform
13
+ import processing.app.Platform.getContentFile
14
+ import processing.app.Platform.getSettingsFolder
15
15
import processing.app.Preferences
16
16
import processing.app.Sketch
17
17
import processing.app.ui.Editor
18
- import java.io.*
19
18
import kotlin.io.path.createTempDirectory
20
19
import kotlin.io.path.deleteIfExists
21
20
import kotlin.io.path.writeText
@@ -48,51 +47,51 @@ class GradleService(
48
47
49
48
val jobs = mutableStateListOf<GradleJob >()
50
49
val workingDir = createTempDirectory()
50
+
51
51
val debugPort = (30_000 .. 60_000 ).random()
52
+ val logPort = debugPort + 1
53
+ val errPort = logPort + 1
52
54
53
55
fun run (){
54
- startAction (" run" )
56
+ startJob (" run" )
55
57
}
56
58
57
59
fun export (){
58
- startAction (" runDistributable" )
60
+ startJob (" runDistributable" )
59
61
}
60
62
61
63
fun stop (){
62
- stopActions ()
64
+ stopJobs ()
63
65
}
64
66
65
- private fun startAction (vararg tasks : String ) {
67
+ private fun startJob (vararg tasks : String ) {
66
68
if (! active.value) return
67
- editor?.let { println (Language . text(" gradle.using_gradle" )) }
69
+ editor?.let { println (text(" gradle.using_gradle" )) }
68
70
69
71
val job = GradleJob ()
70
72
job.service = this
71
73
job.configure = {
72
- setup ()
74
+ setupGradle ()
73
75
forTasks(tasks.joinToString(" " ))
74
76
}
75
77
jobs.add(job)
76
78
job.start()
77
79
}
78
80
79
- private fun stopActions (){
80
- jobs
81
- .forEach(GradleJob ::cancel)
81
+ private fun stopJobs (){
82
+ jobs.forEach(GradleJob ::cancel)
82
83
}
83
84
84
- private fun setupGradle (): MutableList <String > {
85
+ private fun BuildLauncher. setupGradle (extraArguments : List <String > = listOf()) {
85
86
val sketch = sketch ? : throw IllegalStateException (" Sketch is not set" )
86
-
87
87
val copy = sketch.isReadOnly || sketch.isUntitled
88
-
89
88
val sketchFolder = if (copy) workingDir.resolve(" sketch" ).toFile() else sketch.folder
90
89
if (copy){
91
90
// If the sketch is read-only, we copy it to the working directory
92
91
// This allows us to run the sketch without modifying the original files
93
92
sketch.folder.copyRecursively(sketchFolder, overwrite = true )
94
93
}
95
-
94
+ // Save the unsaved code into the working directory for gradle to compile
96
95
val unsaved = sketch.code
97
96
.map { code ->
98
97
val file = workingDir.resolve(" unsaved/${code.fileName} " )
@@ -106,16 +105,18 @@ class GradleService(
106
105
}
107
106
return @map code.fileName
108
107
}
109
-
108
+ // Collect the variables to pass to gradle
110
109
val variables = mapOf (
111
110
" group" to System .getProperty(" processing.group" , " org.processing" ),
112
- " version" to Base . getVersionName(),
111
+ " version" to getVersionName(),
113
112
" sketchFolder" to sketchFolder,
114
- " sketchbook" to Base . getSketchbookFolder(),
113
+ " sketchbook" to getSketchbookFolder(),
115
114
" workingDir" to workingDir.toAbsolutePath().toString(),
116
- " settings" to Platform . getSettingsFolder().absolutePath.toString(),
115
+ " settings" to getSettingsFolder().absolutePath.toString(),
117
116
" unsaved" to unsaved.joinToString(" ," ),
118
117
" debugPort" to debugPort.toString(),
118
+ " logPort" to logPort.toString(),
119
+ " errPort" to errPort.toString(),
119
120
" fullscreen" to System .getProperty(" processing.fullscreen" , " false" ).equals(" true" ),
120
121
" display" to 1 , // TODO: Implement
121
122
" external" to true ,
@@ -126,8 +127,8 @@ class GradleService(
126
127
// "stop.color" to "0xFF000000", // TODO: Implement
127
128
" stop.hide" to false , // TODO: Implement
128
129
)
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
131
132
val initGradle = workingDir.resolve(" init.gradle.kts" ).apply {
132
133
val content = """
133
134
beforeSettings{
@@ -148,8 +149,7 @@ class GradleService(
148
149
149
150
writeText(content)
150
151
}
151
-
152
-
152
+ // Create the build.gradle.kts file in the sketch folder
153
153
val buildGradle = sketchFolder.resolve(" build.gradle.kts" )
154
154
val generate = buildGradle.let {
155
155
if (! it.exists()) return @let true
@@ -158,67 +158,63 @@ class GradleService(
158
158
if (! contents.contains(" @processing-auto-generated" )) return @let false
159
159
160
160
val version = contents.substringAfter(" version=" ).substringBefore(" \n " )
161
- if (version != Base . getVersionName()) return @let true
161
+ if (version != getVersionName()) return @let true
162
162
163
163
val modeTitle = contents.substringAfter(" mode=" ).substringBefore(" " )
164
- if (this . mode.title != modeTitle) return @let true
164
+ if (mode.title != modeTitle) return @let true
165
165
166
- return @let Base . DEBUG
166
+ return @let DEBUG
167
167
}
168
168
if (generate) {
169
169
Messages .log(" build.gradle.kts outdated or not found in ${sketch.folder} , creating one" )
170
170
val header = """
171
- // @processing-auto-generated mode=${mode.title} version=${Base . getVersionName()}
171
+ // @processing-auto-generated mode=${mode.title} version=${getVersionName()}
172
172
//
173
173
""" .trimIndent()
174
174
175
- val instructions = Language . text(" gradle.instructions" )
175
+ val instructions = text(" gradle.instructions" )
176
176
.split(" \n " )
177
177
.joinToString(" \n " ) { " // $it " }
178
178
179
179
val configuration = """
180
180
181
181
plugins{
182
- id("org.processing.java") version "${Base . getVersionName()} "
182
+ id("org.processing.java") version "${getVersionName()} "
183
183
}
184
184
""" .trimIndent()
185
185
val content = " ${header} \n ${instructions} \n ${configuration} "
186
186
buildGradle.writeText(content)
187
187
}
188
+ // Create the settings.gradle.kts file in the sketch folder
188
189
val settingsGradle = sketchFolder.resolve(" settings.gradle.kts" )
189
190
if (! settingsGradle.exists()) {
190
191
settingsGradle.createNewFile()
191
192
}
192
-
193
+ // Collect the arguments to pass to gradle
193
194
val arguments = mutableListOf (" --init-script" , initGradle.toAbsolutePath().toString())
194
- if (! Base . DEBUG ) arguments.add(" --quiet" )
195
+ if (! DEBUG ) arguments.add(" --quiet" )
195
196
if (copy){
196
197
arguments + = listOf (" --project-dir" , sketchFolder.absolutePath)
197
198
}
199
+
198
200
arguments.addAll(variables.entries
199
201
.filter { it.value != null }
200
202
.map { " -Pprocessing.${it.key} =${it.value} " }
201
203
)
204
+ arguments.addAll(extraArguments)
202
205
203
- return arguments
204
- }
205
-
206
+ withArguments(* arguments.toTypedArray())
206
207
207
- private fun BuildLauncher.setup (extraArguments : List <String > = listOf()) {
208
208
// TODO: Instead of shipping Processing with a build-in JDK we should download the JDK through Gradle
209
209
setJavaHome(Platform .getJavaHome())
210
-
211
- val arguments = setupGradle()
212
- arguments.addAll(extraArguments)
213
- withArguments(* arguments.toTypedArray())
214
210
}
215
211
216
212
// Hooks for java to check if the Gradle service is running since mutableStateOf is not accessible in java
217
213
fun getEnabled (): Boolean {
218
214
return active.value
219
215
}
220
216
fun setEnabled (active : Boolean ) {
221
- if (! active) stopActions ()
217
+ if (! active) stopJobs ()
222
218
this .active.value = active
223
219
}
224
220
}
0 commit comments