-
-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathSketch.kt
More file actions
50 lines (46 loc) · 1.68 KB
/
Copy pathSketch.kt
File metadata and controls
50 lines (46 loc) · 1.68 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
package processing.app.api
import kotlinx.serialization.Serializable
import java.io.File
class Sketch {
companion object{
@Serializable
data class Sketch(
val type: String = "sketch",
val name: String,
val path: String,
val mode: String = "java",
)
@Serializable
data class Folder(
val type: String = "folder",
val name: String,
val path: String,
val mode: String = "java",
val children: List<Folder> = emptyList(),
val sketches: List<Sketch> = emptyList()
)
fun getSketches(file: File, filter: (File) -> Boolean = { true }): Folder? {
val name = file.name
val (sketchesFolders, childrenFolders) = file.listFiles()?.filter (File::isDirectory)?.partition { isSketchFolder(it) } ?: return Folder(
name = name,
path = file.absolutePath,
sketches = emptyList(),
children = emptyList()
)
val children = childrenFolders.filter(filter).mapNotNull { getSketches(it) }
val sketches = sketchesFolders.map { Sketch(name = it.name, path = it.absolutePath) }
if(sketches.isEmpty() && children.isEmpty()) {
return null
}
return Folder(
name = name,
path = file.absolutePath,
children = children,
sketches = sketches
)
}
fun isSketchFolder(file: File): Boolean {
return file.isDirectory && file.listFiles().any { it.isFile && it.name.endsWith(".pde") }
}
}
}