From aa585358bea691fb02fcdfcf23d2a5894ee03b47 Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Thu, 8 Oct 2020 14:48:11 +0200 Subject: [PATCH] Add ktlint plugin to maven --- pom.xml | 46 +++++++++++++++++++ src/main/kotlin/starter/modules/MainModule.kt | 1 - .../kotlin/starter/modules/PebbleModule.kt | 20 +++++--- .../kotlin/starter/modules/RoutesModule.kt | 3 +- .../kotlin/starter/routes/ZipRouteSupplier.kt | 4 +- .../kotlin/starter/templates/JunitTemplate.kt | 1 - .../kotlin/starter/templates/MainTemplate.kt | 2 +- .../kotlin/starter/templates/PomTemplate.kt | 1 - src/main/kotlin/starter/utils/PebbleUtils.kt | 24 ++++++---- .../starter/templates/PomTemplateTest.kt | 2 - 10 files changed, 79 insertions(+), 25 deletions(-) diff --git a/pom.xml b/pom.xml index bfcaaa1..21bf4dd 100644 --- a/pom.xml +++ b/pom.xml @@ -150,6 +150,52 @@ maven-compiler-plugin 3.8.1 + + + org.apache.maven.plugins + maven-antrun-plugin + 1.7 + + + ktlint + verify + + + + + + + + + run + + + + ktlint-format + + + + + + + + + + run + + + + + + com.pinterest + ktlint + 0.39.0 + + + + diff --git a/src/main/kotlin/starter/modules/MainModule.kt b/src/main/kotlin/starter/modules/MainModule.kt index 9b448dd..afa037e 100644 --- a/src/main/kotlin/starter/modules/MainModule.kt +++ b/src/main/kotlin/starter/modules/MainModule.kt @@ -8,7 +8,6 @@ import org.koin.dsl.onClose import org.slf4j.Logger import org.slf4j.LoggerFactory import starter.ProjectZip -import starter.config.Config import starter.views.Views val mainModule = module { diff --git a/src/main/kotlin/starter/modules/PebbleModule.kt b/src/main/kotlin/starter/modules/PebbleModule.kt index aa2950d..09cce3c 100644 --- a/src/main/kotlin/starter/modules/PebbleModule.kt +++ b/src/main/kotlin/starter/modules/PebbleModule.kt @@ -14,24 +14,32 @@ import starter.utils.PebbleFunction class DepAsXmlPebbleFunction : PebbleFunction { override val name = "depAsXml" override fun getArgumentNames() = listOf("dependency") - override fun execute(args: Map, self: PebbleTemplate, context: EvaluationContext, lineNumber: Int): Any { + + override fun execute( + args: Map, + self: PebbleTemplate, + context: EvaluationContext, + lineNumber: Int, + ): Any { val dep = args["dependency"] as Dependency + fun tagName(name: String) = """$name""" + fun startTag(name: String): String { @Language("html") @Suppress("UnnecessaryVariable") - val result = """<$name>""" + val result = """<""" + + """${tagName(name)}>""" return result } fun endTag(name: String): String { @Language("html") @Suppress("UnnecessaryVariable") - val result = """</$name>""" + val result = """</""" + + """${tagName(name)}>""" return result } - fun tag(name: String, content: String): String { - return """${startTag(name)}$content${endTag(name)}""" - } + fun tag(name: String, content: String) = """${startTag(name)}$content${endTag(name)}""" val result = """ |${startTag("dependency")} diff --git a/src/main/kotlin/starter/modules/RoutesModule.kt b/src/main/kotlin/starter/modules/RoutesModule.kt index 2d5eeeb..e6ebd1d 100644 --- a/src/main/kotlin/starter/modules/RoutesModule.kt +++ b/src/main/kotlin/starter/modules/RoutesModule.kt @@ -20,6 +20,7 @@ val routesModule = module { routes( static(ResourceLoader.Classpath("/assets")), getAll().toRouter() - )) + ) + ) } } diff --git a/src/main/kotlin/starter/routes/ZipRouteSupplier.kt b/src/main/kotlin/starter/routes/ZipRouteSupplier.kt index 5978b54..177986f 100644 --- a/src/main/kotlin/starter/routes/ZipRouteSupplier.kt +++ b/src/main/kotlin/starter/routes/ZipRouteSupplier.kt @@ -15,8 +15,8 @@ import starter.extensions.ok import java.io.ByteArrayInputStream class ZipRouteSupplier( - private val conf: StarterConfig, - private val projectZip: ProjectZip, + private val conf: StarterConfig, + private val projectZip: ProjectZip, ) : RouteSupplier { override fun get() = "/" bind Method.POST to { req -> diff --git a/src/main/kotlin/starter/templates/JunitTemplate.kt b/src/main/kotlin/starter/templates/JunitTemplate.kt index 4f5039f..5a431e1 100644 --- a/src/main/kotlin/starter/templates/JunitTemplate.kt +++ b/src/main/kotlin/starter/templates/JunitTemplate.kt @@ -2,7 +2,6 @@ package starter.templates import com.mitchellbosecke.pebble.PebbleEngine import starter.Project -import starter.utils.prettyPrintXml import starter.utils.render class JunitTemplate(private val engine: PebbleEngine) : Template { diff --git a/src/main/kotlin/starter/templates/MainTemplate.kt b/src/main/kotlin/starter/templates/MainTemplate.kt index 99d29ea..9132b21 100644 --- a/src/main/kotlin/starter/templates/MainTemplate.kt +++ b/src/main/kotlin/starter/templates/MainTemplate.kt @@ -6,7 +6,7 @@ import starter.utils.render class MainTemplate(private val engine: PebbleEngine) : Template { override fun path(project: Project) = - "src/main/kotlin/" + project.basePackage.replace('.', '/') + "/" + project.name.toLowerCase().capitalize() + ".kt" + "src/main/kotlin/" + project.name.toLowerCase().capitalize() + ".kt" override fun render(project: Project) = engine.render("starter/main/index", mapOf("basePackage" to project.basePackage)) diff --git a/src/main/kotlin/starter/templates/PomTemplate.kt b/src/main/kotlin/starter/templates/PomTemplate.kt index b33a444..d1310fb 100644 --- a/src/main/kotlin/starter/templates/PomTemplate.kt +++ b/src/main/kotlin/starter/templates/PomTemplate.kt @@ -2,7 +2,6 @@ package starter.templates import com.mitchellbosecke.pebble.PebbleEngine import starter.Project -import starter.Version import starter.utils.prettyPrintXml import starter.utils.render diff --git a/src/main/kotlin/starter/utils/PebbleUtils.kt b/src/main/kotlin/starter/utils/PebbleUtils.kt index 1b68e50..47c2f39 100644 --- a/src/main/kotlin/starter/utils/PebbleUtils.kt +++ b/src/main/kotlin/starter/utils/PebbleUtils.kt @@ -64,16 +64,20 @@ class PebbleEngineBuilder { PebbleEngine.Builder() .loader(builder.loader) .cacheActive(builder.cache != CacheType.None) - .templateCache(when (builder.cache) { - CacheType.None -> NoOpTemplateCache() - CacheType.Caffeine -> CaffeineTemplateCache() - CacheType.ConcurrentMap -> ConcurrentMapTemplateCache() - }) - .tagCache(when (builder.cache) { - CacheType.None -> NoOpTagCache() - CacheType.Caffeine -> CaffeineTagCache() - CacheType.ConcurrentMap -> ConcurrentMapTagCache() - }) + .templateCache( + when (builder.cache) { + CacheType.None -> NoOpTemplateCache() + CacheType.Caffeine -> CaffeineTemplateCache() + CacheType.ConcurrentMap -> ConcurrentMapTemplateCache() + } + ) + .tagCache( + when (builder.cache) { + CacheType.None -> NoOpTagCache() + CacheType.Caffeine -> CaffeineTagCache() + CacheType.ConcurrentMap -> ConcurrentMapTagCache() + } + ) .extension(object : AbstractExtension() { override fun getFunctions(): Map? = builder.functions.associateBy { it.name }.ifEmpty { null } diff --git a/src/test/kotlin/starter/templates/PomTemplateTest.kt b/src/test/kotlin/starter/templates/PomTemplateTest.kt index a05eef0..4eb6b81 100644 --- a/src/test/kotlin/starter/templates/PomTemplateTest.kt +++ b/src/test/kotlin/starter/templates/PomTemplateTest.kt @@ -72,7 +72,6 @@ internal class PomTemplateTest { .associate { it.nodeName to it.firstChild.nodeValue } Triple(map["groupId"]!!, map["artifactId"]!!, map["version"] ?: "") - }.filterNot { it.second == "kotlin-stdlib-jdk8" } println(deps.joinToString("\n")) @@ -116,6 +115,5 @@ internal class PomTemplateTest { val kotlinxPluginDep = "$kotlinMavenPlugin/dependencies/dependency/artifactId" assertThat(xml.extract(kotlinxPluginDep)) .isEqualTo("kotlin-maven-serialization") - } }