1
+ package processing.app.api
2
+
3
+ import com.github.ajalt.clikt.command.SuspendingCliktCommand
4
+ import com.github.ajalt.clikt.core.Context
5
+ import com.github.ajalt.clikt.core.subcommands
6
+ import com.github.ajalt.clikt.parameters.arguments.argument
7
+ import com.github.ajalt.clikt.parameters.arguments.help
8
+ import com.github.ajalt.clikt.parameters.options.flag
9
+ import com.github.ajalt.clikt.parameters.options.help
10
+ import com.github.ajalt.clikt.parameters.options.option
11
+ import processing.app.Language
12
+ import processing.app.Platform
13
+ import processing.app.Preferences
14
+ import java.io.File
15
+
16
+ class SketchCommand : SuspendingCliktCommand (" sketch" ){
17
+ override fun help (context : Context ) = " Manage a Processing sketch"
18
+ override suspend fun run () {
19
+
20
+ }
21
+ init {
22
+ subcommands(Format ())
23
+ }
24
+
25
+ class Format : SuspendingCliktCommand (" format" ){
26
+ override fun help (context : Context ) = " Format a Processing sketch"
27
+ val file by argument(" file" )
28
+ .help(" Path to the sketch file to format" )
29
+ val inPlace by option(" -i" ," --inplace" )
30
+ .flag()
31
+ .help(" Format the file in place, otherwise prints to stdout" )
32
+
33
+ override suspend fun run (){
34
+ try {
35
+ Platform .init ()
36
+ Language .init ()
37
+ Preferences .init ()
38
+
39
+ // run in headless mode
40
+ System .setProperty(" java.awt.headless" , " true" )
41
+
42
+ val clazz = Class .forName(" processing.mode.java.AutoFormat" )
43
+ // Indirect invocation since app does not depend on java mode
44
+ val formatter = clazz
45
+ .getDeclaredConstructor()
46
+ .newInstance()
47
+
48
+ val method = clazz.getMethod(" format" , String ::class .java)
49
+ val code = File (file).readText()
50
+
51
+ val formatted = method.invoke(formatter, code) as String
52
+ if (inPlace) {
53
+ File (file).writeText(formatted)
54
+ return
55
+ }
56
+ println (formatted)
57
+ } catch (e: Exception ) {
58
+ throw InternalError (" Failed to invoke main method" , e)
59
+ }
60
+ }
61
+ }
62
+ }
0 commit comments