1
0

Start zip output

This commit is contained in:
Hubert Van De Walle 2020-09-10 18:48:47 +02:00
parent 69e93f4def
commit 883610878a
11 changed files with 121 additions and 31 deletions

10
pom.xml
View File

@ -46,6 +46,16 @@
<artifactId>javalin</artifactId>
<version>3.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.20</version>
</dependency>
<dependency>
<groupId>org.koin</groupId>
<artifactId>koin-core</artifactId>
<version>2.1.6</version>
</dependency>
</dependencies>
<build>

View File

@ -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()
}

View File

@ -19,3 +19,5 @@ data class Dependency(
)
data class Input(val name: String, val display: String, val value: String? = null)
data class Project(val name: String, val basePackage: String)

View File

@ -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)
}
}
}

View File

@ -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")
}
}

View File

@ -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
)
}
}

View File

@ -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
)
}
}

View File

@ -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()
}

View File

@ -1,4 +1,4 @@
package starter
package starter.utils
import org.w3c.dom.Document
import org.w3c.dom.NodeList

View File

@ -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()
}
}

View File

@ -4,7 +4,7 @@
<groupId>{{ dep.groupId }}</groupId>
<artifactId>{{ dep.artifactId }}</artifactId>
<version>{{ dep.version }}</version>
{% if dep.scope.toString == "Test" %}
{% if dep.scope == "Test" %}
<scope>test</scope>
{% endif %}
</dependency>