Compare commits
3 Commits
00fec096cb
...
aaa7a63bfb
| Author | SHA1 | Date | |
|---|---|---|---|
| aaa7a63bfb | |||
| b0eaa16752 | |||
| 67e7a642db |
18
config.toml
18
config.toml
@ -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]
|
||||||
|
|||||||
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -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)
|
||||||
@ -7,23 +7,26 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.result(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.contentType("text/xml")
|
ctx.contentType("text/xml")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
49
src/main/java/starter/Utils.kt
Normal file
49
src/main/java/starter/Utils.kt
Normal 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()
|
||||||
|
}
|
||||||
@ -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
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -19,6 +19,6 @@
|
|||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
<configuration>
|
<configuration>
|
||||||
<jvmTarget>14</jvmTarget>
|
<jvmTarget>${java.version}</jvmTarget>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
@ -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>
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
<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.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 %}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user