Compare commits
No commits in common. "aaa7a63bfb070245557576259db4768afcebe022" and "00fec096cb155b5094e3ff602ce86cb2e2c4a299" have entirely different histories.
aaa7a63bfb
...
00fec096cb
18
config.toml
18
config.toml
@ -1,21 +1,3 @@
|
|||||||
[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]
|
||||||
|
|||||||
@ -3,25 +3,18 @@ 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>, val inputs: List<Input>)
|
data class StarterConfig(val dependencies: List<Dependency>)
|
||||||
|
|
||||||
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 dependenciesMap = dependenciesNode.valueMap() as Map<String, NightConfig>
|
@Suppress("UNCHECKED_CAST") val valueMap = dependenciesNode.valueMap() as Map<String, NightConfig>
|
||||||
val dependencies = dependenciesMap.map { (name, values) ->
|
val dependencies = valueMap.map { (name, values) ->
|
||||||
Dependency(name, values["groupId"], values["artifactId"], values["version"], values.getOrElse("default", false))
|
Dependency(name, values.get("groupId"), values.get("artifactId"), values.get("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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -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 display: String, val value: String? = null)
|
data class Input(val name: String, val value: String? = null)
|
||||||
@ -7,26 +7,23 @@ 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 ->
|
||||||
ctx.result(views.index(conf.dependencies, conf.inputs))
|
val inputs = listOf(
|
||||||
|
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 inputKeys = conf.inputs.map { it.name }
|
ctx.result(views.pom(deps, name, basePackage))
|
||||||
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.contentType("text/xml")
|
ctx.contentType("text/xml")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,49 +0,0 @@
|
|||||||
package starter
|
|
||||||
|
|
||||||
import org.w3c.dom.Document
|
|
||||||
import org.w3c.dom.NodeList
|
|
||||||
import org.xml.sax.InputSource
|
|
||||||
import java.io.ByteArrayInputStream
|
|
||||||
import java.io.StringWriter
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory
|
|
||||||
import javax.xml.transform.OutputKeys
|
|
||||||
import javax.xml.transform.TransformerFactory
|
|
||||||
import javax.xml.transform.dom.DOMSource
|
|
||||||
import javax.xml.transform.stream.StreamResult
|
|
||||||
import javax.xml.xpath.XPathConstants
|
|
||||||
import javax.xml.xpath.XPathFactory
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
@see https://stackoverflow.com/a/33541820
|
|
||||||
*/
|
|
||||||
fun prettyPrintXml(xml: String, indent: Int = 4): String {
|
|
||||||
// Turn xml string into a document
|
|
||||||
val document: Document = DocumentBuilderFactory.newInstance()
|
|
||||||
.newDocumentBuilder()
|
|
||||||
.parse(InputSource(ByteArrayInputStream(xml.encodeToByteArray())))
|
|
||||||
|
|
||||||
// Remove whitespaces outside tags
|
|
||||||
document.normalize()
|
|
||||||
val xPath = XPathFactory.newInstance().newXPath()
|
|
||||||
val nodeList = xPath.evaluate("//text()[normalize-space()='']",
|
|
||||||
document,
|
|
||||||
XPathConstants.NODESET) as NodeList
|
|
||||||
for (i in 0 until nodeList.length) {
|
|
||||||
val node = nodeList.item(i)
|
|
||||||
node.parentNode.removeChild(node)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup pretty print options
|
|
||||||
val transformerFactory = TransformerFactory.newInstance()
|
|
||||||
transformerFactory.setAttribute("indent-number", indent)
|
|
||||||
val transformer = transformerFactory.newTransformer()
|
|
||||||
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8")
|
|
||||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes")
|
|
||||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes")
|
|
||||||
|
|
||||||
// Return pretty print xml string
|
|
||||||
val stringWriter = StringWriter()
|
|
||||||
transformer.transform(DOMSource(document), StreamResult(stringWriter))
|
|
||||||
return stringWriter.toString()
|
|
||||||
}
|
|
||||||
@ -1,9 +1,7 @@
|
|||||||
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)
|
||||||
@ -13,25 +11,15 @@ 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>, inputs: List<Input>): String {
|
fun pom(dependencies: List<Dependency>, name: String, basePackage: String) = engine.render("starter/pom",
|
||||||
val args: MutableMap<String, Any?> = mutableMapOf(
|
mapOf(
|
||||||
"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
|
|
||||||
)
|
)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -19,6 +19,6 @@
|
|||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
<configuration>
|
<configuration>
|
||||||
<jvmTarget>${java.version}</jvmTarget>
|
<jvmTarget>14</jvmTarget>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
@ -6,10 +6,8 @@
|
|||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>{{ javaVersion }}</java.version>
|
<maven.compiler.target>14</maven.compiler.target>
|
||||||
<kotlin.version>{{ kotlinVersion }}</kotlin.version>
|
<maven.compiler.source>14</maven.compiler.source>
|
||||||
<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>
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<title>Kotlin Starter</title>
|
<title>Kotlin Starter</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
{% macro input(input) %}
|
{% macro input(input) %}
|
||||||
<label>
|
<label>
|
||||||
<span>{{ input.display }}</span>
|
<span>{{ input.name }}</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 %}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user