1
0

Clean things

This commit is contained in:
2020-09-10 14:42:39 +02:00
parent aaa7a63bfb
commit 7fc979053e
9 changed files with 7 additions and 4 deletions
+27
View File
@@ -0,0 +1,27 @@
package starter
import com.electronwill.nightconfig.core.file.FileConfig
import com.electronwill.nightconfig.core.Config as NightConfig
data class StarterConfig(val dependencies: List<Dependency>, val inputs: List<Input>)
class Config {
fun load(): StarterConfig {
val cfg = FileConfig.of("config.toml")
cfg.load()
val dependenciesNode: NightConfig = cfg["dependencies"]
@Suppress("UNCHECKED_CAST") val dependenciesMap = dependenciesNode.valueMap() as Map<String, NightConfig>
val dependencies = dependenciesMap.map { (name, values) ->
Dependency(name, values["groupId"], values["artifactId"], values["version"], values.getOrElse("default", false))
}
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)
}
}
+8
View File
@@ -0,0 +1,8 @@
package starter
fun main() {
val config = Config()
val loaded = config.load()
val server = Server(Views(PebbleModule().engine()),loaded)
server.run()
}
+4
View File
@@ -0,0 +1,4 @@
package starter
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)
+15
View File
@@ -0,0 +1,15 @@
package starter
import com.mitchellbosecke.pebble.PebbleEngine
import com.mitchellbosecke.pebble.loader.ClasspathLoader
class PebbleModule {
fun engine(): PebbleEngine {
val loader = ClasspathLoader()
loader.suffix = ".twig"
return PebbleEngine.Builder()
.loader(loader)
.cacheActive(false)
.build()
}
}
+31
View File
@@ -0,0 +1,31 @@
package starter
import io.javalin.Javalin
class Server(private val views: Views, private val conf: StarterConfig) {
fun run() {
val app = Javalin.create().start(7000)
app.get("/") { ctx ->
ctx.result(views.index(conf.dependencies, conf.inputs))
ctx.contentType("text/html")
}
app.post("/") { ctx ->
val deps = conf.dependencies.filter {
ctx.formParam(it.name) != null
}
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())
}
val generatedPom = views.pom(deps, inputs)
ctx.result(prettyPrintXml(generatedPom))
ctx.contentType("text/xml")
}
}
}
+49
View File
@@ -0,0 +1,49 @@
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()
}
+36
View File
@@ -0,0 +1,36 @@
package starter
import com.mitchellbosecke.pebble.PebbleEngine
import org.slf4j.LoggerFactory
import java.io.StringWriter
private fun PebbleEngine.render(name: String, args: Map<String, Any?> = mapOf()): String {
val template = getTemplate(name)
val writer = StringWriter()
template.evaluate(writer, args)
return writer.toString()
}
class Views(private val engine: PebbleEngine) {
private val logger = LoggerFactory.getLogger(javaClass)
fun index(dependencies: List<Dependency>, inputs: List<Input>) = engine.render("views/index",
mapOf("dependencies" to dependencies, "inputs" to inputs)
)
fun pom(dependencies: List<Dependency>, inputs: List<Input>): String {
val args: MutableMap<String, Any?> = mutableMapOf(
"dependencies" to dependencies,
)
inputs.forEach {
args[it.name] = it.value
}
logger.debug(args.toString())
return engine.render("starter/pom",
args
)
}
}