Separate templates
This commit is contained in:
parent
ddf8558578
commit
8ba3dac0b2
@ -1,20 +1,28 @@
|
||||
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 module = module {
|
||||
val mainModule = module {
|
||||
single { Config().load() }
|
||||
single { PebbleModule().engine() }
|
||||
single { Server(get(), get(), get()) }
|
||||
single { Templates(get()) }
|
||||
single { Views(get()) }
|
||||
single { ProjectZip(get()) }
|
||||
single { ProjectZip(getAll()) }
|
||||
}
|
||||
|
||||
val templateModule = module {
|
||||
single { PomTemplate(get()) } bind Template::class
|
||||
single { MainTemplate(get()) } bind Template::class
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val koin = startKoin {
|
||||
modules(module)
|
||||
modules(mainModule, templateModule)
|
||||
}.koin
|
||||
val server = koin.get<Server>()
|
||||
server.run()
|
||||
|
||||
@ -20,4 +20,9 @@ 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)
|
||||
data class Project(
|
||||
val name: String,
|
||||
val basePackage: String,
|
||||
val inputs: List<Input>,
|
||||
val dependencies: List<Dependency>,
|
||||
)
|
||||
|
||||
@ -1,23 +1,23 @@
|
||||
package starter
|
||||
|
||||
import starter.templates.Template
|
||||
import starter.utils.ZipOutput
|
||||
import starter.utils.prettyPrintXml
|
||||
import starter.utils.sanitizeFilename
|
||||
|
||||
class ProjectZip(private val templates: Templates) {
|
||||
|
||||
fun createZip(project: Project, inputs: List<Input>, dependencies: List<Dependency>): String {
|
||||
val pom = templates.pom(dependencies, inputs)
|
||||
val prettyPom = prettyPrintXml(pom)
|
||||
class ProjectZip(private val templates: List<Template>) {
|
||||
|
||||
fun createZip(project: Project): String {
|
||||
val name: String
|
||||
|
||||
ZipOutput(sanitizeFilename(project.name)).use {
|
||||
name = it.name
|
||||
it.write("pom.xml", prettyPom)
|
||||
|
||||
templates.forEach { template ->
|
||||
it.write(template.path(project), template.render(project))
|
||||
}
|
||||
}
|
||||
|
||||
return name
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,14 +33,12 @@ class Server(
|
||||
|
||||
val projectName = inputs.find { it.name == "name" }!!.value!!
|
||||
val basePackage = inputs.find { it.name == "basePackage" }!!.value!!
|
||||
val project = Project(projectName, basePackage)
|
||||
val project = Project(projectName, basePackage, inputs, deps)
|
||||
|
||||
|
||||
ctx.contentType("text/xml")
|
||||
ctx.contentType("application/zip")
|
||||
ctx.header("Content-Disposition", "attachment; filename=\"${sanitizeFilename(projectName)}.zip\"")
|
||||
val zipFile = projectZip.createZip(project, inputs, deps)
|
||||
val zipFile = projectZip.createZip(project)
|
||||
ctx.result(File(zipFile).readBytes())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,21 +0,0 @@
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
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,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