1
0

Add more options

This commit is contained in:
Hubert Van De Walle 2020-09-10 14:34:00 +02:00
parent 67e7a642db
commit b0eaa16752
8 changed files with 67 additions and 26 deletions

View File

@ -1,3 +1,21 @@
[inputs]
[inputs.name]
default = "example"
display = "Project Name"
[inputs.basePackage]
default = "org.example"
display = "Base package"
[inputs.javaVersion]
default = "14"
display = "Java Version"
[inputs.kotlinVersion]
default = "1.4.10"
display = "Kotlin Version"
[dependencies] [dependencies]
[dependencies.http4k] [dependencies.http4k]

View File

@ -3,18 +3,25 @@ package starter
import com.electronwill.nightconfig.core.Config as NightConfig import com.electronwill.nightconfig.core.Config as NightConfig
import com.electronwill.nightconfig.core.file.FileConfig import com.electronwill.nightconfig.core.file.FileConfig
data class StarterConfig(val dependencies: List<Dependency>) data class StarterConfig(val dependencies: List<Dependency>, val inputs: List<Input>)
class Config { class Config {
fun load(): StarterConfig { fun load(): StarterConfig {
val cfg = FileConfig.of("config.toml") val cfg = FileConfig.of("config.toml")
cfg.load() cfg.load()
val dependenciesNode: NightConfig = cfg["dependencies"] val dependenciesNode: NightConfig = cfg["dependencies"]
@Suppress("UNCHECKED_CAST") val valueMap = dependenciesNode.valueMap() as Map<String, NightConfig> @Suppress("UNCHECKED_CAST") val dependenciesMap = dependenciesNode.valueMap() as Map<String, NightConfig>
val dependencies = valueMap.map { (name, values) -> val dependencies = dependenciesMap.map { (name, values) ->
Dependency(name, values.get("groupId"), values.get("artifactId"), values.get("version"), values.getOrElse("default", false)) Dependency(name, values["groupId"], values["artifactId"], values["version"], values.getOrElse("default", false))
} }
return StarterConfig(dependencies)
val inputsNode: NightConfig = cfg["inputs"]
@Suppress("UNCHECKED_CAST") val inputMap = inputsNode.valueMap() as Map<String, NightConfig>
val inputs = inputMap.map { (name, values) ->
Input(name, values["display"], values["default"])
}
return StarterConfig(dependencies, inputs)
} }
} }

View File

@ -1,4 +1,4 @@
package starter package starter
data class Dependency(val name: String, val groupId: String, val artifactId: String, val version: String, val default: Boolean) data class Dependency(val name: String, val groupId: String, val artifactId: String, val version: String, val default: Boolean)
data class Input(val name: String, val value: String? = null) data class Input(val name: String, val display: String, val value: String? = null)

View File

@ -7,23 +7,25 @@ class Server(private val views: Views, private val conf: StarterConfig) {
val app = Javalin.create().start(7000) val app = Javalin.create().start(7000)
app.get("/") { ctx -> app.get("/") { ctx ->
val inputs = listOf( ctx.result(views.index(conf.dependencies, conf.inputs))
Input("name", "example"),
Input("basePackage", "org.example")
)
ctx.result(views.index(conf.dependencies, inputs))
ctx.contentType("text/html") ctx.contentType("text/html")
} }
app.post("/") { ctx -> app.post("/") { ctx ->
val name = ctx.formParam("name")!!
val basePackage = ctx.formParam("basePackage")!!
val deps = conf.dependencies.filter { val deps = conf.dependencies.filter {
ctx.formParam(it.name) != null ctx.formParam(it.name) != null
} }
val generatedPom = views.pom(deps, name, basePackage) val inputKeys = conf.inputs.map { it.name }
val inputs = ctx.formParamMap()
.filter { it.key in inputKeys }
.map { (name, value) ->
conf.inputs.find { it.name == name }!!.copy(value = value.first())
}
println()
val generatedPom = views.pom(deps, inputs)
ctx.result(prettyPrintXml(generatedPom)) ctx.result(prettyPrintXml(generatedPom))
ctx.contentType("text/xml") ctx.contentType("text/xml")
} }

View File

@ -1,7 +1,9 @@
package starter package starter
import com.mitchellbosecke.pebble.PebbleEngine import com.mitchellbosecke.pebble.PebbleEngine
import org.slf4j.LoggerFactory
import java.io.StringWriter import java.io.StringWriter
import java.util.logging.LogManager
private fun PebbleEngine.render(name: String, args: Map<String, Any?> = mapOf()): String { private fun PebbleEngine.render(name: String, args: Map<String, Any?> = mapOf()): String {
val template = getTemplate(name) val template = getTemplate(name)
@ -11,15 +13,25 @@ private fun PebbleEngine.render(name: String, args: Map<String, Any?> = mapOf())
} }
class Views(private val engine: PebbleEngine) { class Views(private val engine: PebbleEngine) {
private val logger = LoggerFactory.getLogger(javaClass)
fun index(dependencies: List<Dependency>, inputs: List<Input>) = engine.render("views/index", fun index(dependencies: List<Dependency>, inputs: List<Input>) = engine.render("views/index",
mapOf("dependencies" to dependencies, "inputs" to inputs) mapOf("dependencies" to dependencies, "inputs" to inputs)
) )
fun pom(dependencies: List<Dependency>, name: String, basePackage: String) = engine.render("starter/pom", fun pom(dependencies: List<Dependency>, inputs: List<Input>): String {
mapOf( val args: MutableMap<String, Any?> = mutableMapOf(
"dependencies" to dependencies, "dependencies" to dependencies,
"name" to name, )
"basePackage" to basePackage
) inputs.forEach {
) args[it.name] = it.value
}
logger.debug(args.toString())
return engine.render("starter/pom",
args
)
}
} }

View File

@ -19,6 +19,6 @@
</execution> </execution>
</executions> </executions>
<configuration> <configuration>
<jvmTarget>14</jvmTarget> <jvmTarget>${java.version}</jvmTarget>
</configuration> </configuration>
</plugin> </plugin>

View File

@ -6,8 +6,10 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<properties> <properties>
<maven.compiler.target>14</maven.compiler.target> <java.version>{{ javaVersion }}</java.version>
<maven.compiler.source>14</maven.compiler.source> <kotlin.version>{{ kotlinVersion }}</kotlin.version>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<main.class>{{ basePackage }}/{{ name | lower | capitalize }}Kt</main.class> <main.class>{{ basePackage }}/{{ name | lower | capitalize }}Kt</main.class>
</properties> </properties>

View File

@ -9,7 +9,7 @@
{% macro input(input) %} {% macro input(input) %}
<label> <label>
<span>{{ input.name }}</span> <span>{{ input.display }}</span>
<input name="{{ input.name }}" type="text"{% if input.value %} value="{{ input.value }}"{% endif %}> <input name="{{ input.name }}" type="text"{% if input.value %} value="{{ input.value }}"{% endif %}>
</label> </label>
{% endmacro %} {% endmacro %}