Start zip output
This commit is contained in:
@@ -1,8 +1,21 @@
|
||||
package starter
|
||||
|
||||
import org.koin.core.context.startKoin
|
||||
import org.koin.dsl.module
|
||||
|
||||
val module = module {
|
||||
single { Config().load() }
|
||||
single { PebbleModule().engine() }
|
||||
single { Server(get(), get(), get()) }
|
||||
single { Templates(get()) }
|
||||
single { Views(get()) }
|
||||
single { ProjectZip(get()) }
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val config = Config()
|
||||
val loaded = config.load()
|
||||
val server = Server(Views(PebbleModule().engine()),loaded)
|
||||
val koin = startKoin {
|
||||
modules(module)
|
||||
}.koin
|
||||
val server = koin.get<Server>()
|
||||
server.run()
|
||||
}
|
||||
|
||||
@@ -18,4 +18,6 @@ data class Dependency(
|
||||
val scope: Scope
|
||||
)
|
||||
|
||||
data class Input(val name: String, val display: String, val value: String? = null)
|
||||
data class Input(val name: String, val display: String, val value: String? = null)
|
||||
|
||||
data class Project(val name: String, val basePackage: String)
|
||||
@@ -0,0 +1,18 @@
|
||||
package starter
|
||||
|
||||
import starter.utils.ZipOutput
|
||||
import starter.utils.prettyPrintXml
|
||||
|
||||
class ProjectZip(private val templates: Templates) {
|
||||
|
||||
fun createZip(project: Project, inputs: List<Input>, dependencies: List<Dependency>) {
|
||||
val pom = templates.pom(dependencies, inputs)
|
||||
val prettyPom = prettyPrintXml(pom)
|
||||
|
||||
ZipOutput(project.name).use {
|
||||
it.write("pom.xml", prettyPom)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,7 +2,11 @@ package starter
|
||||
|
||||
import io.javalin.Javalin
|
||||
|
||||
class Server(private val views: Views, private val conf: StarterConfig) {
|
||||
class Server(
|
||||
private val views: Views,
|
||||
private val conf: StarterConfig,
|
||||
private val projectZip: ProjectZip,
|
||||
) {
|
||||
fun run() {
|
||||
val app = Javalin.create {
|
||||
it.addStaticFiles("/assets")
|
||||
@@ -25,8 +29,13 @@ class Server(private val views: Views, private val conf: StarterConfig) {
|
||||
conf.inputs.find { it.name == name }!!.copy(value = value.first())
|
||||
}
|
||||
|
||||
val generatedPom = views.pom(deps, inputs)
|
||||
ctx.result(prettyPrintXml(generatedPom))
|
||||
val projectName = inputs.find { it.name == "name" }!!.value!!
|
||||
val basePackage = inputs.find { it.name == "basePackage" }!!.value!!
|
||||
val project = Project(projectName, basePackage)
|
||||
|
||||
projectZip.createZip(project, inputs, deps)
|
||||
TODO()
|
||||
ctx.result("prettyPrintXml(generatedPom)")
|
||||
ctx.contentType("text/xml")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package starter
|
||||
|
||||
import com.mitchellbosecke.pebble.PebbleEngine
|
||||
import starter.utils.render
|
||||
|
||||
class Templates(private val engine: PebbleEngine) {
|
||||
fun pom(dependencies: List<Dependency>, inputs: List<Input>): String {
|
||||
|
||||
val args: MutableMap<String, Any?> = mutableMapOf(
|
||||
"dependencies" to dependencies.sortedBy { it.scope },
|
||||
)
|
||||
|
||||
inputs.forEach {
|
||||
args[it.name] = it.value
|
||||
}
|
||||
|
||||
return engine.render("starter/pom",
|
||||
args
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,7 @@ 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()
|
||||
}
|
||||
import starter.utils.render
|
||||
|
||||
class Views(private val engine: PebbleEngine) {
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
@@ -21,18 +14,4 @@ class Views(private val engine: PebbleEngine) {
|
||||
)
|
||||
}
|
||||
|
||||
fun pom(dependencies: List<Dependency>, inputs: List<Input>): String {
|
||||
|
||||
val args: MutableMap<String, Any?> = mutableMapOf(
|
||||
"dependencies" to dependencies.sortedBy { it.scope },
|
||||
)
|
||||
|
||||
inputs.forEach {
|
||||
args[it.name] = it.value
|
||||
}
|
||||
|
||||
return engine.render("starter/pom",
|
||||
args
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package starter.utils
|
||||
|
||||
import com.mitchellbosecke.pebble.PebbleEngine
|
||||
import java.io.StringWriter
|
||||
|
||||
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()
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package starter
|
||||
package starter.utils
|
||||
|
||||
import org.w3c.dom.Document
|
||||
import org.w3c.dom.NodeList
|
||||
@@ -0,0 +1,27 @@
|
||||
package starter.utils
|
||||
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
|
||||
import java.io.File
|
||||
import java.nio.file.Paths
|
||||
|
||||
class ZipOutput(name: String) : AutoCloseable {
|
||||
private val baseDir = File(".").canonicalFile
|
||||
private val zipPath = Paths.get(baseDir.path, "$name.zip")
|
||||
private val zipFile = zipPath.toAbsolutePath().normalize().toFile().apply {
|
||||
createNewFile()
|
||||
}
|
||||
private val outputStream = ZipArchiveOutputStream(zipFile.outputStream())
|
||||
|
||||
fun write(path: String, content: String) {
|
||||
val entry = ZipArchiveEntry(path)
|
||||
outputStream.putArchiveEntry(entry)
|
||||
outputStream.write(content.toByteArray())
|
||||
outputStream.closeArchiveEntry()
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
outputStream.finish()
|
||||
outputStream.close()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user