@@ -11,6 +11,9 @@ import com.github.ajalt.clikt.parameters.options.flag
11
11
import com.github.ajalt.clikt.parameters.options.help
12
12
import com.github.ajalt.clikt.parameters.options.option
13
13
import processing.app.ui.Start
14
+ import java.io.File
15
+ import java.util.prefs.Preferences
16
+ import kotlin.concurrent.thread
14
17
15
18
class Processing : SuspendingCliktCommand (" processing" ){
16
19
val version by option(" -v" ," --version" )
@@ -29,6 +32,11 @@ class Processing: SuspendingCliktCommand("processing"){
29
32
return
30
33
}
31
34
35
+ thread {
36
+ // Update the install locations in preferences
37
+ updateInstallLocations()
38
+ }
39
+
32
40
val subcommand = currentContext.invokedSubcommand
33
41
if (subcommand == null ) {
34
42
Start .main(sketches.toTypedArray())
@@ -49,10 +57,13 @@ class LSP: SuspendingCliktCommand("lsp"){
49
57
override fun help (context : Context ) = " Start the Processing Language Server"
50
58
override suspend fun run (){
51
59
try {
60
+ // run in headless mode
61
+ System .setProperty(" java.awt.headless" , " true" )
62
+
52
63
// Indirect invocation since app does not depend on java mode
53
64
Class .forName(" processing.mode.java.lsp.PdeLanguageServer" )
54
65
.getMethod(" main" , Array <String >::class .java)
55
- .invoke(null , * arrayOf<Any >(emptyList< String >() ))
66
+ .invoke(null , arrayOf<String >())
56
67
} catch (e: Exception ) {
57
68
throw InternalError (" Failed to invoke main method" , e)
58
69
}
@@ -76,9 +87,8 @@ class LegacyCLI(val args: Array<String>): SuspendingCliktCommand( "cli"){
76
87
override suspend fun run (){
77
88
val cliArgs = args.filter { it != " cli" }
78
89
try {
79
- if (build){
80
- System .setProperty(" java.awt.headless" , " true" )
81
- }
90
+ System .setProperty(" java.awt.headless" , " true" )
91
+
82
92
// Indirect invocation since app does not depend on java mode
83
93
Class .forName(" processing.mode.java.Commander" )
84
94
.getMethod(" main" , Array <String >::class .java)
@@ -87,4 +97,50 @@ class LegacyCLI(val args: Array<String>): SuspendingCliktCommand( "cli"){
87
97
throw InternalError (" Failed to invoke main method" , e)
88
98
}
89
99
}
100
+ }
101
+
102
+ fun updateInstallLocations (){
103
+ val preferences = Preferences .userRoot().node(" org/processing/app" )
104
+ val installLocations = preferences.get(" installLocations" , " " )
105
+ .split(" ," )
106
+ .dropLastWhile { it.isEmpty() }
107
+ .filter { install ->
108
+ try {
109
+ val (path, version) = install.split(" ^" )
110
+ val file = File (path)
111
+ if (! file.exists() || file.isDirectory){
112
+ return @filter false
113
+ }
114
+ // call the path to check if it is a valid install location
115
+ val process = ProcessBuilder (path, " --version" )
116
+ .redirectErrorStream(true )
117
+ .start()
118
+ val exitCode = process.waitFor()
119
+ if (exitCode != 0 ){
120
+ return @filter false
121
+ }
122
+ val output = process.inputStream.bufferedReader().readText()
123
+ return @filter output.contains(version)
124
+ } catch (e: Exception ){
125
+ false
126
+ }
127
+ }
128
+ .toMutableList()
129
+ val command = ProcessHandle .current().info().command()
130
+ if (command.isEmpty) {
131
+ return
132
+ }
133
+ val installLocation = " ${command.get()} ^${Base .getVersionName()} "
134
+
135
+
136
+ // Check if the installLocation is already in the list
137
+ if (installLocations.contains(installLocation)) {
138
+ return
139
+ }
140
+
141
+ // Add the installLocation to the list
142
+ installLocations.add(installLocation)
143
+
144
+ // Save the updated list back to preferences
145
+ preferences.put(" installLocations" , java.lang.String .join(" ," , installLocations))
90
146
}
0 commit comments