Start zip output
This commit is contained in:
parent
69e93f4def
commit
883610878a
10
pom.xml
10
pom.xml
@ -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>
|
||||
|
||||
@ -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()
|
||||
}
|
||||
|
||||
@ -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)
|
||||
18
src/main/kotlin/starter/ProjectZip.kt
Normal file
18
src/main/kotlin/starter/ProjectZip.kt
Normal 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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
21
src/main/kotlin/starter/Templates.kt
Normal file
21
src/main/kotlin/starter/Templates.kt
Normal 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
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
11
src/main/kotlin/starter/utils/PebbleUtils.kt
Normal file
11
src/main/kotlin/starter/utils/PebbleUtils.kt
Normal 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()
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package starter
|
||||
package starter.utils
|
||||
|
||||
import org.w3c.dom.Document
|
||||
import org.w3c.dom.NodeList
|
||||
27
src/main/kotlin/starter/utils/ZipOutput.kt
Normal file
27
src/main/kotlin/starter/utils/ZipOutput.kt
Normal 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()
|
||||
}
|
||||
}
|
||||
@ -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>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user