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