-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathreadmeGenerator.kt
36 lines (30 loc) · 1.29 KB
/
readmeGenerator.kt
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
import groovy.json.JsonSlurper
import java.io.File
class ReadmeGenerator(
private val librariesDir: File
) {
fun generate(stub: File, destination: File) {
var result = stub.readText()
for ((stubName, processor) in processors) {
result = result.replace("[[$stubName]]", processor())
}
destination.writeText(result)
}
private val processors: Map<String, () -> String> = mapOf(
"supported_libraries" to ::processSupportedLibraries
)
private fun processSupportedLibraries(): String {
val libraryFiles =
librariesDir.listFiles { file -> file.isFile && file.name.endsWith(".json") } ?: emptyArray()
val sortedMap = sortedMapOf<String, String>()
return libraryFiles.toList().map { file ->
val libraryName = file.nameWithoutExtension
val json = JsonSlurper().parse(file) as Map<*, *>
val link = json["link"] as? String
val description = json["description"] as? String
val namePart = if (link == null) libraryName else "[$libraryName]($link)"
val descriptionPart = if (description == null) "" else " - $description"
libraryName to " - $namePart$descriptionPart"
}.toMap(sortedMap).values.joinToString("\n")
}
}