|
| 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