Expand globs for templates + files & Add default prompt value
This commit is contained in:
parent
7a7ebe724e
commit
6e1e82c2c5
@ -1,5 +1,9 @@
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"name":"com.electronwill.nightconfig.toml.TomlFormat"
|
"name":"com.electronwill.nightconfig.toml.TomlFormat"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name":"java.util.Map$Entry",
|
||||||
|
"allPublicMethods":true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@ -3,17 +3,26 @@ package scaffold
|
|||||||
import com.electronwill.nightconfig.core.Config
|
import com.electronwill.nightconfig.core.Config
|
||||||
import com.electronwill.nightconfig.core.UnmodifiableConfig
|
import com.electronwill.nightconfig.core.UnmodifiableConfig
|
||||||
import com.electronwill.nightconfig.core.file.FileConfig
|
import com.electronwill.nightconfig.core.file.FileConfig
|
||||||
|
import java.nio.file.Files
|
||||||
|
import java.nio.file.Files.isRegularFile
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
|
import kotlin.streams.toList
|
||||||
|
|
||||||
data class GeneratorConfig(
|
data class GeneratorConfig(
|
||||||
val templates: List<String>,
|
val templates: List<Path>,
|
||||||
val files: List<Path>,
|
val files: List<Path>,
|
||||||
val prompts: Map<String, List<PromptValue>>
|
val prompts: Map<String, List<PromptValue>>
|
||||||
)
|
)
|
||||||
|
|
||||||
fun Generator.loadConfig(): GeneratorConfig {
|
fun Generator.loadConfig(): GeneratorConfig {
|
||||||
val config: UnmodifiableConfig = FileConfig.of(rootPath.resolve("config.toml")).apply { load() }
|
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 prompts = mutableMapOf<String, MutableList<PromptValue>>()
|
||||||
val section = config.get<Any?>("prompt") ?: error("Missing prompt section")
|
val section = config.get<Any?>("prompt") ?: error("Missing prompt section")
|
||||||
@ -28,6 +37,7 @@ fun Generator.loadConfig(): GeneratorConfig {
|
|||||||
val promptValue = PromptValue(
|
val promptValue = PromptValue(
|
||||||
name = prompt.get("name") ?: error("Missing name"),
|
name = prompt.get("name") ?: error("Missing name"),
|
||||||
info = prompt.get("info"),
|
info = prompt.get("info"),
|
||||||
|
default = prompt.get("default"),
|
||||||
type = prompt.getEnum("type", PromptType::class.java) ?: PromptType.String
|
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)
|
return GeneratorConfig(templates, files, prompts)
|
||||||
}
|
}
|
||||||
@ -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
11
app/src/Globs.kt
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,5 +5,6 @@ enum class PromptType { String }
|
|||||||
data class PromptValue(
|
data class PromptValue(
|
||||||
val name: String,
|
val name: String,
|
||||||
val info: String?,
|
val info: String?,
|
||||||
|
val default: String?,
|
||||||
val type: PromptType,
|
val type: PromptType,
|
||||||
)
|
)
|
||||||
|
|||||||
@ -19,11 +19,11 @@ fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val scaffoldConfigDirectory = configDirectory.resolve("scaffold")
|
val scaffoldConfigDirectory = configDirectory.resolve("scaffold")
|
||||||
val generatorService = Generators(scaffoldConfigDirectory)
|
val generators = Generators(scaffoldConfigDirectory)
|
||||||
|
|
||||||
Scaffold().subcommands(
|
Scaffold().subcommands(
|
||||||
ListCommand(generatorService),
|
ListCommand(generators),
|
||||||
GenerateCommand(generatorService),
|
GenerateCommand(generators),
|
||||||
NewCommand(generatorService)
|
NewCommand(generators)
|
||||||
).main(args)
|
).main(args)
|
||||||
}
|
}
|
||||||
@ -32,10 +32,13 @@ class GenerateCommand(private val generators: Generators) : CliktCommand("genera
|
|||||||
for ((path, prompts) in config.prompts) {
|
for ((path, prompts) in config.prompts) {
|
||||||
val map = mutableMapOf<String, Any>()
|
val map = mutableMapOf<String, Any>()
|
||||||
templateContext[path] = map
|
templateContext[path] = map
|
||||||
for ((name, info, type) in prompts) {
|
for ((name, info, default, type) in prompts) {
|
||||||
if (type != PromptType.String) TODO()
|
if (type != PromptType.String) TODO()
|
||||||
|
|
||||||
val value = prompt(info ?: name)!!
|
val value = prompt(
|
||||||
|
text = info ?: "$path.$name",
|
||||||
|
default = default
|
||||||
|
)!!
|
||||||
map[name] = value
|
map[name] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -47,9 +50,9 @@ class GenerateCommand(private val generators: Generators) : CliktCommand("genera
|
|||||||
.build()!!
|
.build()!!
|
||||||
|
|
||||||
for (template in config.templates) {
|
for (template in config.templates) {
|
||||||
val renderedTemplate = pebble.getTemplate(template)(templateContext)
|
val templatePath = generator.treeRoot.relativize(template).toString()
|
||||||
val outputPath = output.resolve(template)
|
val renderedTemplate = pebble.getTemplate(templatePath)(templateContext)
|
||||||
|
val outputPath = output.resolve(templatePath)
|
||||||
Files.createDirectories(outputPath.parent)
|
Files.createDirectories(outputPath.parent)
|
||||||
Files.writeString(outputPath, renderedTemplate)
|
Files.writeString(outputPath, renderedTemplate)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,5 +4,15 @@ import com.github.ajalt.clikt.core.CliktCommand
|
|||||||
import scaffold.Generators
|
import scaffold.Generators
|
||||||
|
|
||||||
class ListCommand(private val generators: Generators) : CliktCommand("list") {
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user