diff --git a/app/resources/META-INF/native-image/reflect-config.json b/app/resources/META-INF/native-image/reflect-config.json index e5d3dbc..4232fc6 100644 --- a/app/resources/META-INF/native-image/reflect-config.json +++ b/app/resources/META-INF/native-image/reflect-config.json @@ -1,5 +1,9 @@ [ { "name":"com.electronwill.nightconfig.toml.TomlFormat" +}, +{ + "name":"java.util.Map$Entry", + "allPublicMethods":true } ] diff --git a/app/src/Config.kt b/app/src/Config.kt index 1945ce6..564618a 100644 --- a/app/src/Config.kt +++ b/app/src/Config.kt @@ -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, + val templates: List, val files: List, val prompts: Map> ) fun Generator.loadConfig(): GeneratorConfig { val config: UnmodifiableConfig = FileConfig.of(rootPath.resolve("config.toml")).apply { load() } - val templates = config.get>("templates") + + val allFiles = Files.walk(treeRoot).filter { isRegularFile(it) }.toList() + + fun List.expandGlobs() = flatMap { Globs.matches(allFiles, treeRoot, it) } + + val files = config.get>("files").expandGlobs() + val templates = config.get>("templates").expandGlobs() val prompts = mutableMapOf>() val section = config.get("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>("files").flatMap { path -> - if ("*" in path) FileGlob.listFiles(treeRoot, path) - else listOf(treeRoot.resolve(path)) - } - return GeneratorConfig(templates, files, prompts) -} \ No newline at end of file +} diff --git a/app/src/FileGlob.kt b/app/src/FileGlob.kt deleted file mode 100644 index a48b67c..0000000 --- a/app/src/FileGlob.kt +++ /dev/null @@ -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 { - val matcher = FileSystems.getDefault().getPathMatcher("glob:$root/$glob") - - val matches = mutableListOf() - - Files.walkFileTree(root, object : SimpleFileVisitor() { - override fun visitFile(file: Path, attrs: BasicFileAttributes?): FileVisitResult { - if (matcher.matches(file)) matches.add(file) - return FileVisitResult.CONTINUE - } - }) - - return matches - } -} \ No newline at end of file diff --git a/app/src/Globs.kt b/app/src/Globs.kt new file mode 100644 index 0000000..a8200d8 --- /dev/null +++ b/app/src/Globs.kt @@ -0,0 +1,11 @@ +package scaffold + +import java.nio.file.FileSystems +import java.nio.file.Path + +object Globs { + fun matches(allFiles: List, root: Path, glob: String): List { + val matcher = FileSystems.getDefault().getPathMatcher("glob:$root/$glob") + return allFiles.filter(matcher::matches) + } +} \ No newline at end of file diff --git a/app/src/PromptValue.kt b/app/src/PromptValue.kt index ff3ed78..d1e01df 100644 --- a/app/src/PromptValue.kt +++ b/app/src/PromptValue.kt @@ -5,5 +5,6 @@ enum class PromptType { String } data class PromptValue( val name: String, val info: String?, + val default: String?, val type: PromptType, ) diff --git a/app/src/Scaffold.kt b/app/src/Scaffold.kt index 6e1a105..a65270c 100644 --- a/app/src/Scaffold.kt +++ b/app/src/Scaffold.kt @@ -19,11 +19,11 @@ fun main(args: Array) { } 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) } \ No newline at end of file diff --git a/app/src/commands/GenerateCommand.kt b/app/src/commands/GenerateCommand.kt index f2e7b1b..49394a6 100644 --- a/app/src/commands/GenerateCommand.kt +++ b/app/src/commands/GenerateCommand.kt @@ -32,10 +32,13 @@ class GenerateCommand(private val generators: Generators) : CliktCommand("genera for ((path, prompts) in config.prompts) { val map = mutableMapOf() 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) } diff --git a/app/src/commands/ListCommand.kt b/app/src/commands/ListCommand.kt index 4d86279..16aba26 100644 --- a/app/src/commands/ListCommand.kt +++ b/app/src/commands/ListCommand.kt @@ -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()) + } + } + } } \ No newline at end of file