Compare commits
4 Commits
69e93f4def
...
94269c7a87
| Author | SHA1 | Date | |
|---|---|---|---|
| 94269c7a87 | |||
| 8ba3dac0b2 | |||
| ddf8558578 | |||
| 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,29 @@
|
||||
package starter
|
||||
|
||||
import org.koin.core.context.startKoin
|
||||
import org.koin.dsl.bind
|
||||
import org.koin.dsl.module
|
||||
import starter.templates.MainTemplate
|
||||
import starter.templates.PomTemplate
|
||||
import starter.templates.Template
|
||||
|
||||
val mainModule = module {
|
||||
single { Config().load() }
|
||||
single { PebbleModule().engine() }
|
||||
single { Server(get(), get(), get()) }
|
||||
single { Views(get()) }
|
||||
single { ProjectZip(getAll()) }
|
||||
}
|
||||
|
||||
val templateModule = module {
|
||||
single { PomTemplate(get()) } bind Template::class
|
||||
single { MainTemplate(get()) } bind Template::class
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val config = Config()
|
||||
val loaded = config.load()
|
||||
val server = Server(Views(PebbleModule().engine()),loaded)
|
||||
val koin = startKoin {
|
||||
modules(mainModule, templateModule)
|
||||
}.koin
|
||||
val server = koin.get<Server>()
|
||||
server.run()
|
||||
}
|
||||
|
||||
@ -18,4 +18,11 @@ 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,
|
||||
val inputs: List<Input>,
|
||||
val dependencies: List<Dependency>,
|
||||
)
|
||||
|
||||
23
src/main/kotlin/starter/ProjectZip.kt
Normal file
23
src/main/kotlin/starter/ProjectZip.kt
Normal file
@ -0,0 +1,23 @@
|
||||
package starter
|
||||
|
||||
import starter.templates.Template
|
||||
import starter.utils.ZipOutput
|
||||
import starter.utils.sanitizeFilename
|
||||
|
||||
class ProjectZip(private val templates: List<Template>) {
|
||||
|
||||
fun createZip(project: Project): String {
|
||||
val name: String
|
||||
|
||||
ZipOutput(sanitizeFilename(project.name)).use {
|
||||
name = it.name
|
||||
|
||||
templates.forEach { template ->
|
||||
it.write(template.path(project), template.render(project))
|
||||
}
|
||||
}
|
||||
|
||||
return name
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,8 +1,14 @@
|
||||
package starter
|
||||
|
||||
import io.javalin.Javalin
|
||||
import starter.utils.sanitizeFilename
|
||||
import java.io.File
|
||||
|
||||
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,9 +31,14 @@ 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))
|
||||
ctx.contentType("text/xml")
|
||||
val projectName = inputs.find { it.name == "name" }!!.value!!
|
||||
val basePackage = inputs.find { it.name == "basePackage" }!!.value!!
|
||||
val project = Project(projectName, basePackage, inputs, deps)
|
||||
|
||||
ctx.contentType("application/zip")
|
||||
ctx.header("Content-Disposition", "attachment; filename=\"${sanitizeFilename(projectName)}.zip\"")
|
||||
val zipFile = projectZip.createZip(project)
|
||||
ctx.result(File(zipFile).readBytes())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
13
src/main/kotlin/starter/templates/MainTemplate.kt
Normal file
13
src/main/kotlin/starter/templates/MainTemplate.kt
Normal file
@ -0,0 +1,13 @@
|
||||
package starter.templates
|
||||
|
||||
import com.mitchellbosecke.pebble.PebbleEngine
|
||||
import starter.Project
|
||||
import starter.utils.render
|
||||
|
||||
class MainTemplate(private val engine: PebbleEngine) : Template {
|
||||
override fun path(project: Project) =
|
||||
project.basePackage.replace('.', '/') + "/" + project.name.toLowerCase().capitalize() + ".kt"
|
||||
|
||||
override fun render(project: Project) =
|
||||
engine.render("starter/main/main", mapOf("basePackage" to project.basePackage))
|
||||
}
|
||||
23
src/main/kotlin/starter/templates/PomTemplate.kt
Normal file
23
src/main/kotlin/starter/templates/PomTemplate.kt
Normal file
@ -0,0 +1,23 @@
|
||||
package starter.templates
|
||||
|
||||
import com.mitchellbosecke.pebble.PebbleEngine
|
||||
import starter.Project
|
||||
import starter.utils.prettyPrintXml
|
||||
import starter.utils.render
|
||||
|
||||
class PomTemplate(private val engine: PebbleEngine) : Template {
|
||||
override fun path(project: Project) = "pom.xml"
|
||||
|
||||
override fun render(project: Project): String {
|
||||
val args: MutableMap<String, Any?> = mutableMapOf(
|
||||
"dependencies" to project.dependencies.sortedBy { it.scope },
|
||||
)
|
||||
|
||||
project.inputs.forEach {
|
||||
args[it.name] = it.value
|
||||
}
|
||||
|
||||
val rendered = engine.render("starter/pom/pom", args)
|
||||
return prettyPrintXml(rendered)
|
||||
}
|
||||
}
|
||||
8
src/main/kotlin/starter/templates/Template.kt
Normal file
8
src/main/kotlin/starter/templates/Template.kt
Normal file
@ -0,0 +1,8 @@
|
||||
package starter.templates
|
||||
|
||||
import starter.Project
|
||||
|
||||
interface Template {
|
||||
fun path(project: Project): String
|
||||
fun render(project: Project): String
|
||||
}
|
||||
3
src/main/kotlin/starter/utils/PathUtils.kt
Normal file
3
src/main/kotlin/starter/utils/PathUtils.kt
Normal file
@ -0,0 +1,3 @@
|
||||
package starter.utils
|
||||
|
||||
fun sanitizeFilename(inputName: String): String = inputName.replace("[^a-zA-Z0-9-_.]".toRegex(), "_")
|
||||
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
|
||||
29
src/main/kotlin/starter/utils/ZipOutput.kt
Normal file
29
src/main/kotlin/starter/utils/ZipOutput.kt
Normal file
@ -0,0 +1,29 @@
|
||||
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())
|
||||
val name: String
|
||||
get() = zipFile.name
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
6
src/main/resources/starter/main/main.twig
Normal file
6
src/main/resources/starter/main/main.twig
Normal file
@ -0,0 +1,6 @@
|
||||
package {{ basePackage }}
|
||||
|
||||
|
||||
fun main() {
|
||||
println("Hello world!")
|
||||
}
|
||||
@ -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>
|
||||
@ -4,11 +4,11 @@
|
||||
|
||||
<plugins>
|
||||
|
||||
{% include "starter/plugins/@default" %}
|
||||
{% include "starter/pom/plugins/@default" %}
|
||||
|
||||
{% include "starter/plugins/@kotlin" %}
|
||||
{% include "starter/pom/plugins/@kotlin" %}
|
||||
|
||||
{% include "starter/plugins/@shade" %}
|
||||
{% include "starter/pom/plugins/@shade" %}
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
@ -14,10 +14,10 @@
|
||||
<main.class>{{ basePackage }}/{{ name | lower | capitalize }}Kt</main.class>
|
||||
</properties>
|
||||
|
||||
{% include "starter/@dependencies" %}
|
||||
{% include "starter/pom/@dependencies" %}
|
||||
|
||||
{% include "starter/@repositories" %}
|
||||
{% include "starter/pom/@repositories" %}
|
||||
|
||||
{% include "starter/@plugins" %}
|
||||
{% include "starter/pom/@plugins" %}
|
||||
|
||||
</project>
|
||||
Loading…
x
Reference in New Issue
Block a user