Skip to content

Commit 5b27fc6

Browse files
committed
Create Examples JSON
1 parent bd9e475 commit 5b27fc6

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ dependencies {
124124
testImplementation(libs.junitJupiterParams)
125125

126126
implementation(libs.clikt)
127+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
127128
}
128129

129130
tasks.test {

app/src/processing/app/Processing.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.github.ajalt.clikt.parameters.arguments.multiple
1010
import com.github.ajalt.clikt.parameters.options.flag
1111
import com.github.ajalt.clikt.parameters.options.help
1212
import com.github.ajalt.clikt.parameters.options.option
13+
import processing.app.contrib.Contributions
1314
import processing.app.ui.Start
1415
import java.io.File
1516
import java.util.prefs.Preferences
@@ -48,7 +49,8 @@ suspend fun main(args: Array<String>){
4849
Processing()
4950
.subcommands(
5051
LSP(),
51-
LegacyCLI(args)
52+
LegacyCLI(args),
53+
Contributions()
5254
)
5355
.main(args)
5456
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package processing.app.contrib
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 kotlinx.serialization.Serializable
7+
import kotlinx.serialization.encodeToString
8+
import processing.app.Base
9+
import java.io.File
10+
import kotlinx.serialization.json.*
11+
12+
13+
class Contributions: SuspendingCliktCommand(){
14+
override fun help(context: Context) = "Manage Processing contributions"
15+
override suspend fun run() {
16+
System.setProperty("java.awt.headless", "true")
17+
}
18+
init {
19+
subcommands(Examples())
20+
}
21+
22+
class Examples: SuspendingCliktCommand("examples") {
23+
override fun help(context: Context) = "Manage Processing examples"
24+
override suspend fun run() {
25+
}
26+
init {
27+
subcommands(ExamplesList())
28+
}
29+
}
30+
31+
class ExamplesList: SuspendingCliktCommand("list") {
32+
@Serializable
33+
data class Sketch(
34+
val type: String = "sketch",
35+
val name: String,
36+
val path: String,
37+
val mode: String = "java",
38+
)
39+
40+
@Serializable
41+
data class Folder(
42+
val type: String = "folder",
43+
val name: String,
44+
val path: String,
45+
val mode: String = "java",
46+
val children: List<Folder> = emptyList(),
47+
val sketches: List<Sketch> = emptyList()
48+
)
49+
50+
val serializer = Json{
51+
prettyPrint = true
52+
}
53+
54+
override fun help(context: Context) = "List all examples"
55+
override suspend fun run() {
56+
57+
val examplesFolder = Base.getSketchbookExamplesFolder()
58+
59+
// TODO: Decouple modes listing from `Base` class, defaulting to Java mode for now
60+
val resourcesDir = System.getProperty("compose.application.resources.dir")
61+
val javaMode = "$resourcesDir/modes/java"
62+
val javaModeExamples = "$javaMode/examples"
63+
64+
val javaExamples = getExamples(File(javaModeExamples))
65+
66+
val json = serializer.encodeToString(listOf(javaExamples))
67+
println(json)
68+
69+
// Build-in examples for each mode
70+
// Get examples for core libraries
71+
72+
// Examples downloaded in the sketchbook
73+
// Library contributions
74+
// Mode examples
75+
}
76+
77+
suspend fun getExamples(file: File): Folder {
78+
val name = file.name
79+
val (sketchesFolders, childrenFolders) = file.listFiles().partition { isExampleFolder(it) }
80+
81+
val children = childrenFolders.map { getExamples(it) }
82+
val sketches = sketchesFolders.map { Sketch(name = it.name, path = it.absolutePath) }
83+
return Folder(
84+
name = name,
85+
path = file.absolutePath,
86+
children = children,
87+
sketches = sketches
88+
)
89+
}
90+
fun isExampleFolder(file: File): Boolean {
91+
return file.isDirectory && file.listFiles().any { it.isFile && it.name.endsWith(".pde") }
92+
}
93+
}
94+
}
95+

0 commit comments

Comments
 (0)