Expand globs for templates + files & Add default prompt value

This commit is contained in:
Hubert Van De Walle 2021-04-02 12:17:59 +02:00
parent 7a7ebe724e
commit 6e1e82c2c5
8 changed files with 52 additions and 40 deletions

View File

@ -1,5 +1,9 @@
[
{
"name":"com.electronwill.nightconfig.toml.TomlFormat"
},
{
"name":"java.util.Map$Entry",
"allPublicMethods":true
}
]

View File

@ -3,17 +3,26 @@ package scaffold
import com.electronwill.nightconfig.core.Config
import com.electronwill.nightconfig.core.UnmodifiableConfig
import com.electronwill.nightconfig.core.file.FileConfig
import java.nio.file.Files
import java.nio.file.Files.isRegularFile
import java.nio.file.Path
import kotlin.streams.toList
data class GeneratorConfig(
val templates: List<String>,
val templates: List<Path>,
val files: List<Path>,
val prompts: Map<String, List<PromptValue>>
)
fun Generator.loadConfig(): GeneratorConfig {
val config: UnmodifiableConfig = FileConfig.of(rootPath.resolve("config.toml")).apply { load() }
val templates = config.get<List<String>>("templates")
val allFiles = Files.walk(treeRoot).filter { isRegularFile(it) }.toList()
fun List<String>.expandGlobs() = flatMap { Globs.matches(allFiles, treeRoot, it) }
val files = config.get<List<String>>("files").expandGlobs()
val templates = config.get<List<String>>("templates").expandGlobs()
val prompts = mutableMapOf<String, MutableList<PromptValue>>()
val section = config.get<Any?>("prompt") ?: error("Missing prompt section")
@ -28,6 +37,7 @@ fun Generator.loadConfig(): GeneratorConfig {
val promptValue = PromptValue(
name = prompt.get("name") ?: error("Missing name"),
info = prompt.get("info"),
default = prompt.get("default"),
type = prompt.getEnum("type", PromptType::class.java) ?: PromptType.String
)
@ -37,10 +47,5 @@ fun Generator.loadConfig(): GeneratorConfig {
}
val files = config.get<List<String>>("files").flatMap { path ->
if ("*" in path) FileGlob.listFiles(treeRoot, path)
else listOf(treeRoot.resolve(path))
}
return GeneratorConfig(templates, files, prompts)
}
}

View File

@ -1,22 +0,0 @@
package scaffold
import java.nio.file.*
import java.nio.file.attribute.BasicFileAttributes
object FileGlob {
fun listFiles(root: Path, glob: String): MutableList<Path> {
val matcher = FileSystems.getDefault().getPathMatcher("glob:$root/$glob")
val matches = mutableListOf<Path>()
Files.walkFileTree(root, object : SimpleFileVisitor<Path>() {
override fun visitFile(file: Path, attrs: BasicFileAttributes?): FileVisitResult {
if (matcher.matches(file)) matches.add(file)
return FileVisitResult.CONTINUE
}
})
return matches
}
}

11
app/src/Globs.kt Normal file
View File

@ -0,0 +1,11 @@
package scaffold
import java.nio.file.FileSystems
import java.nio.file.Path
object Globs {
fun matches(allFiles: List<Path>, root: Path, glob: String): List<Path> {
val matcher = FileSystems.getDefault().getPathMatcher("glob:$root/$glob")
return allFiles.filter(matcher::matches)
}
}

View File

@ -5,5 +5,6 @@ enum class PromptType { String }
data class PromptValue(
val name: String,
val info: String?,
val default: String?,
val type: PromptType,
)

View File

@ -19,11 +19,11 @@ fun main(args: Array<String>) {
}
val scaffoldConfigDirectory = configDirectory.resolve("scaffold")
val generatorService = Generators(scaffoldConfigDirectory)
val generators = Generators(scaffoldConfigDirectory)
Scaffold().subcommands(
ListCommand(generatorService),
GenerateCommand(generatorService),
NewCommand(generatorService)
ListCommand(generators),
GenerateCommand(generators),
NewCommand(generators)
).main(args)
}

View File

@ -32,10 +32,13 @@ class GenerateCommand(private val generators: Generators) : CliktCommand("genera
for ((path, prompts) in config.prompts) {
val map = mutableMapOf<String, Any>()
templateContext[path] = map
for ((name, info, type) in prompts) {
for ((name, info, default, type) in prompts) {
if (type != PromptType.String) TODO()
val value = prompt(info ?: name)!!
val value = prompt(
text = info ?: "$path.$name",
default = default
)!!
map[name] = value
}
}
@ -47,9 +50,9 @@ class GenerateCommand(private val generators: Generators) : CliktCommand("genera
.build()!!
for (template in config.templates) {
val renderedTemplate = pebble.getTemplate(template)(templateContext)
val outputPath = output.resolve(template)
val templatePath = generator.treeRoot.relativize(template).toString()
val renderedTemplate = pebble.getTemplate(templatePath)(templateContext)
val outputPath = output.resolve(templatePath)
Files.createDirectories(outputPath.parent)
Files.writeString(outputPath, renderedTemplate)
}

View File

@ -4,5 +4,15 @@ import com.github.ajalt.clikt.core.CliktCommand
import scaffold.Generators
class ListCommand(private val generators: Generators) : CliktCommand("list") {
override fun run() = echo(generators.findAll().joinToString(" ") { it.last().toString() })
override fun run() {
val generators = generators.findAll()
if (generators.isEmpty()) {
echo("No generators yet, you can create one with `scaffold new`")
} else {
generators.forEach {
echo(it.last())
}
}
}
}