From 492933d7285ccca574d9fb01d2c5893192bac3cd Mon Sep 17 00:00:00 2001 From: Ryan Brink <5607577+rgbrizzlehizzle@users.noreply.github.com> Date: Mon, 12 Apr 2021 12:21:28 -0400 Subject: [PATCH] kinda sorta processor (#3) --- .../build.gradle.kts | 0 .../org/leafygreens/kompendium/Kompendium.kt | 12 ++ .../annotations/KompendiumContact.kt | 9 ++ .../kompendium/annotations/KompendiumInfo.kt | 10 ++ .../annotations/KompendiumLicense.kt | 8 ++ .../annotations/KompendiumModule.kt | 5 + .../annotations/KompendiumServers.kt | 7 ++ .../kompendium/models/OpenApiSpec.kt | 4 +- .../models/OpenApiSpecComponents.kt | 0 .../OpenApiSpecExternalDocumentation.kt | 0 .../kompendium/models/OpenApiSpecInfo.kt | 12 ++ .../models/OpenApiSpecInfoContact.kt | 6 +- .../models/OpenApiSpecInfoLicense.kt | 4 +- .../kompendium/models/OpenApiSpecLink.kt | 0 .../kompendium/models/OpenApiSpecMediaType.kt | 0 .../kompendium/models/OpenApiSpecOAuthFlow.kt | 0 .../models/OpenApiSpecOAuthFlows.kt | 0 .../kompendium/models/OpenApiSpecPathItem.kt | 1 - .../models/OpenApiSpecPathItemOperation.kt | 0 .../models/OpenApiSpecReferencable.kt | 0 .../kompendium/models/OpenApiSpecSchema.kt | 0 .../kompendium/models/OpenApiSpecServer.kt | 0 .../models/OpenApiSpecServerVariable.kt | 0 .../kompendium/models/OpenApiSpecTag.kt | 0 .../leafygreens/kompendium/KompendiumTest.kt | 0 .../kompendium/models/OpenApiSpecTest.kt | 0 .../leafygreens/kompendium/util/TestData.kt | 4 +- .../src/test/resources/petstore.json | 0 .../build.gradle.kts | 4 +- .../leafygreens/kompendium/playground/Main.kt | 49 ++++++++ .../src/main/resources/logback.xml | 0 kompendium-processor/build.gradle.kts | 33 ++++++ .../processor/KompendiumProcessor.kt | 112 ++++++++++++++++++ .../org/leafygreens/kompendium/Kompendium.kt | 7 -- .../kompendium/models/OpenApiSpecInfo.kt | 12 -- .../leafygreens/kompendium/playground/Main.kt | 28 ----- settings.gradle.kts | 7 +- 37 files changed, 273 insertions(+), 61 deletions(-) rename {kompendium => kompendium-core}/build.gradle.kts (100%) create mode 100644 kompendium-core/src/main/kotlin/org/leafygreens/kompendium/Kompendium.kt create mode 100644 kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumContact.kt create mode 100644 kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumInfo.kt create mode 100644 kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumLicense.kt create mode 100644 kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumModule.kt create mode 100644 kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumServers.kt rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpec.kt (80%) rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecComponents.kt (100%) rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecExternalDocumentation.kt (100%) create mode 100644 kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfo.kt rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfoContact.kt (51%) rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfoLicense.kt (70%) rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecLink.kt (100%) rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecMediaType.kt (100%) rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecOAuthFlow.kt (100%) rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecOAuthFlows.kt (100%) rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecPathItem.kt (87%) rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecPathItemOperation.kt (100%) rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecReferencable.kt (100%) rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecSchema.kt (100%) rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecServer.kt (100%) rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecServerVariable.kt (100%) rename {kompendium => kompendium-core}/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecTag.kt (100%) rename {kompendium => kompendium-core}/src/test/kotlin/org/leafygreens/kompendium/KompendiumTest.kt (100%) rename {kompendium => kompendium-core}/src/test/kotlin/org/leafygreens/kompendium/models/OpenApiSpecTest.kt (100%) rename {kompendium => kompendium-core}/src/test/kotlin/org/leafygreens/kompendium/util/TestData.kt (99%) rename {kompendium => kompendium-core}/src/test/resources/petstore.json (100%) rename {playground => kompendium-playground}/build.gradle.kts (85%) create mode 100644 kompendium-playground/src/main/kotlin/org/leafygreens/kompendium/playground/Main.kt rename {playground => kompendium-playground}/src/main/resources/logback.xml (100%) create mode 100644 kompendium-processor/build.gradle.kts create mode 100644 kompendium-processor/src/main/kotlin/org/leafygreens/kompendium/processor/KompendiumProcessor.kt delete mode 100644 kompendium/src/main/kotlin/org/leafygreens/kompendium/Kompendium.kt delete mode 100644 kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfo.kt delete mode 100644 playground/src/main/kotlin/org/leafygreens/kompendium/playground/Main.kt diff --git a/kompendium/build.gradle.kts b/kompendium-core/build.gradle.kts similarity index 100% rename from kompendium/build.gradle.kts rename to kompendium-core/build.gradle.kts diff --git a/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/Kompendium.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/Kompendium.kt new file mode 100644 index 000000000..a5459bd6a --- /dev/null +++ b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/Kompendium.kt @@ -0,0 +1,12 @@ +package org.leafygreens.kompendium + +import org.leafygreens.kompendium.models.OpenApiSpec +import org.leafygreens.kompendium.models.OpenApiSpecInfo + +class Kompendium { + val spec = OpenApiSpec( + info = OpenApiSpecInfo(), + servers = mutableListOf(), + paths = mutableMapOf() + ) +} diff --git a/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumContact.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumContact.kt new file mode 100644 index 000000000..bf075271e --- /dev/null +++ b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumContact.kt @@ -0,0 +1,9 @@ +package org.leafygreens.kompendium.annotations + +@Retention(AnnotationRetention.SOURCE) +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS) +annotation class KompendiumContact( + val name: String, + val url: String = "", + val email: String = "" +) diff --git a/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumInfo.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumInfo.kt new file mode 100644 index 000000000..8ad223688 --- /dev/null +++ b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumInfo.kt @@ -0,0 +1,10 @@ +package org.leafygreens.kompendium.annotations + +@Retention(AnnotationRetention.SOURCE) +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS) +annotation class KompendiumInfo( + val title: String, + val version: String, + val description: String = "", + val termsOfService: String = "" +) diff --git a/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumLicense.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumLicense.kt new file mode 100644 index 000000000..065ac60b6 --- /dev/null +++ b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumLicense.kt @@ -0,0 +1,8 @@ +package org.leafygreens.kompendium.annotations + +@Retention(AnnotationRetention.SOURCE) +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS) +annotation class KompendiumLicense( + val name: String, + val url: String = "" +) diff --git a/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumModule.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumModule.kt new file mode 100644 index 000000000..1d6bec009 --- /dev/null +++ b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumModule.kt @@ -0,0 +1,5 @@ +package org.leafygreens.kompendium.annotations + +@Retention(AnnotationRetention.SOURCE) +@Target(AnnotationTarget.FUNCTION) +annotation class KompendiumModule diff --git a/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumServers.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumServers.kt new file mode 100644 index 000000000..999523a0b --- /dev/null +++ b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/annotations/KompendiumServers.kt @@ -0,0 +1,7 @@ +package org.leafygreens.kompendium.annotations + +@Retention(AnnotationRetention.SOURCE) +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS) +annotation class KompendiumServers( + val urls: Array +) diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpec.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpec.kt similarity index 80% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpec.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpec.kt index b3d170ee0..7872cea12 100644 --- a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpec.kt +++ b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpec.kt @@ -4,8 +4,8 @@ data class OpenApiSpec( val openapi: String = "3.0.3", val info: OpenApiSpecInfo? = null, // TODO Needs to default to server object with url of `/` - val servers: List? = null, - val paths: Map? = null, + val servers: MutableList? = null, + val paths: MutableMap? = null, val components: OpenApiSpecComponents? = null, // todo needs to reference objects in the components -> security scheme 🤔 val security: List>>? = null, diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecComponents.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecComponents.kt similarity index 100% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecComponents.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecComponents.kt diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecExternalDocumentation.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecExternalDocumentation.kt similarity index 100% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecExternalDocumentation.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecExternalDocumentation.kt diff --git a/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfo.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfo.kt new file mode 100644 index 000000000..86cf5dedd --- /dev/null +++ b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfo.kt @@ -0,0 +1,12 @@ +package org.leafygreens.kompendium.models + +import java.net.URI + +data class OpenApiSpecInfo( + var title: String? = null, + var version: String? = null, + var description: String? = null, + var termsOfService: URI? = null, + var contact: OpenApiSpecInfoContact? = null, + var license: OpenApiSpecInfoLicense? = null +) diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfoContact.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfoContact.kt similarity index 51% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfoContact.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfoContact.kt index a478ea1be..1a2042b7f 100644 --- a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfoContact.kt +++ b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfoContact.kt @@ -3,7 +3,7 @@ package org.leafygreens.kompendium.models import java.net.URI data class OpenApiSpecInfoContact( - val name: String, - val url: URI? = null, - val email: String? = null // TODO Enforce email? + var name: String, + var url: URI? = null, + var email: String? = null // TODO Enforce email? ) diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfoLicense.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfoLicense.kt similarity index 70% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfoLicense.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfoLicense.kt index f4ca67142..293a11ab7 100644 --- a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfoLicense.kt +++ b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfoLicense.kt @@ -3,6 +3,6 @@ package org.leafygreens.kompendium.models import java.net.URI data class OpenApiSpecInfoLicense( - val name: String, - val url: URI? + var name: String, + var url: URI? = null ) diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecLink.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecLink.kt similarity index 100% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecLink.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecLink.kt diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecMediaType.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecMediaType.kt similarity index 100% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecMediaType.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecMediaType.kt diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecOAuthFlow.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecOAuthFlow.kt similarity index 100% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecOAuthFlow.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecOAuthFlow.kt diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecOAuthFlows.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecOAuthFlows.kt similarity index 100% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecOAuthFlows.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecOAuthFlows.kt diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecPathItem.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecPathItem.kt similarity index 87% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecPathItem.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecPathItem.kt index 3bef065b2..94f7b85e9 100644 --- a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecPathItem.kt +++ b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecPathItem.kt @@ -1,7 +1,6 @@ package org.leafygreens.kompendium.models data class OpenApiSpecPathItem( - // val `$ref`: String?, // TODO need example of this... or just make whole thing referencable? val summary: String? = null, val description: String? = null, val get: OpenApiSpecPathItemOperation? = null, diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecPathItemOperation.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecPathItemOperation.kt similarity index 100% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecPathItemOperation.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecPathItemOperation.kt diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecReferencable.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecReferencable.kt similarity index 100% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecReferencable.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecReferencable.kt diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecSchema.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecSchema.kt similarity index 100% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecSchema.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecSchema.kt diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecServer.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecServer.kt similarity index 100% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecServer.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecServer.kt diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecServerVariable.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecServerVariable.kt similarity index 100% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecServerVariable.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecServerVariable.kt diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecTag.kt b/kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecTag.kt similarity index 100% rename from kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecTag.kt rename to kompendium-core/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecTag.kt diff --git a/kompendium/src/test/kotlin/org/leafygreens/kompendium/KompendiumTest.kt b/kompendium-core/src/test/kotlin/org/leafygreens/kompendium/KompendiumTest.kt similarity index 100% rename from kompendium/src/test/kotlin/org/leafygreens/kompendium/KompendiumTest.kt rename to kompendium-core/src/test/kotlin/org/leafygreens/kompendium/KompendiumTest.kt diff --git a/kompendium/src/test/kotlin/org/leafygreens/kompendium/models/OpenApiSpecTest.kt b/kompendium-core/src/test/kotlin/org/leafygreens/kompendium/models/OpenApiSpecTest.kt similarity index 100% rename from kompendium/src/test/kotlin/org/leafygreens/kompendium/models/OpenApiSpecTest.kt rename to kompendium-core/src/test/kotlin/org/leafygreens/kompendium/models/OpenApiSpecTest.kt diff --git a/kompendium/src/test/kotlin/org/leafygreens/kompendium/util/TestData.kt b/kompendium-core/src/test/kotlin/org/leafygreens/kompendium/util/TestData.kt similarity index 99% rename from kompendium/src/test/kotlin/org/leafygreens/kompendium/util/TestData.kt rename to kompendium-core/src/test/kotlin/org/leafygreens/kompendium/util/TestData.kt index 7cf5f5aa6..289e424e7 100644 --- a/kompendium/src/test/kotlin/org/leafygreens/kompendium/util/TestData.kt +++ b/kompendium-core/src/test/kotlin/org/leafygreens/kompendium/util/TestData.kt @@ -53,7 +53,7 @@ object TestData { description = "Find out more about Swagger", url = URI("http://swagger.io") ), - servers = listOf( + servers = mutableListOf( OpenApiSpecServer( url = URI("https://petstore.swagger.io/v2") ), @@ -83,7 +83,7 @@ object TestData { ) ) ), - paths = mapOf( + paths = mutableMapOf( "/pet" to OpenApiSpecPathItem( put = OpenApiSpecPathItemOperation( tags = setOf("pet"), diff --git a/kompendium/src/test/resources/petstore.json b/kompendium-core/src/test/resources/petstore.json similarity index 100% rename from kompendium/src/test/resources/petstore.json rename to kompendium-core/src/test/resources/petstore.json diff --git a/playground/build.gradle.kts b/kompendium-playground/build.gradle.kts similarity index 85% rename from playground/build.gradle.kts rename to kompendium-playground/build.gradle.kts index cf01277fd..ca6801f1f 100644 --- a/playground/build.gradle.kts +++ b/kompendium-playground/build.gradle.kts @@ -1,4 +1,5 @@ plugins { + kotlin("kapt") application } @@ -6,7 +7,8 @@ dependencies { implementation(platform("org.jetbrains.kotlin:kotlin-bom")) implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") - implementation(projects.kompendium) + implementation(projects.kompendiumCore) + kapt(projects.kompendiumProcessor) implementation(libs.bundles.ktor) implementation(libs.bundles.logging) diff --git a/kompendium-playground/src/main/kotlin/org/leafygreens/kompendium/playground/Main.kt b/kompendium-playground/src/main/kotlin/org/leafygreens/kompendium/playground/Main.kt new file mode 100644 index 000000000..c0b45afbf --- /dev/null +++ b/kompendium-playground/src/main/kotlin/org/leafygreens/kompendium/playground/Main.kt @@ -0,0 +1,49 @@ +package org.leafygreens.kompendium.playground + +import io.ktor.application.Application +import io.ktor.application.call +import io.ktor.response.respondText +import io.ktor.routing.get +import io.ktor.routing.route +import io.ktor.routing.routing +import io.ktor.server.engine.embeddedServer +import io.ktor.server.netty.Netty +import org.leafygreens.kompendium.annotations.KompendiumContact +import org.leafygreens.kompendium.annotations.KompendiumInfo +import org.leafygreens.kompendium.annotations.KompendiumLicense +import org.leafygreens.kompendium.annotations.KompendiumModule +import org.leafygreens.kompendium.annotations.KompendiumServers + +@KompendiumInfo( + title = "Test API", + version = "0.0.1", + description = "An API for testing" +) +@KompendiumContact( + name = "Homer Simpson", + url = "https://en.wikipedia.org/wiki/The_Simpsons", + email = "chunkylover53@aol.com" +) +@KompendiumLicense( + name = "DOH", + url = "https://opensource.org/licenses/DOH" +) +@KompendiumServers(urls = [ "https://thesimpsonsquoteapi.glitch.me/quotes" ]) +fun main() { + embeddedServer( + Netty, + port = 8080, + module = Application::mainModule + ).start(wait = true) +} + +@KompendiumModule +fun Application.mainModule() { + routing { + route("/") { + get { + call.respondText("hi") + } + } + } +} diff --git a/playground/src/main/resources/logback.xml b/kompendium-playground/src/main/resources/logback.xml similarity index 100% rename from playground/src/main/resources/logback.xml rename to kompendium-playground/src/main/resources/logback.xml diff --git a/kompendium-processor/build.gradle.kts b/kompendium-processor/build.gradle.kts new file mode 100644 index 000000000..bb3cd3747 --- /dev/null +++ b/kompendium-processor/build.gradle.kts @@ -0,0 +1,33 @@ +plugins { + kotlin("kapt") + `java-library` + `maven-publish` +} + +dependencies { + implementation(platform("org.jetbrains.kotlin:kotlin-bom")) + implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") + implementation(projects.kompendiumCore) + implementation("com.google.auto.service:auto-service:1.0") + kapt("com.google.auto.service:auto-service:1.0") + testImplementation("org.jetbrains.kotlin:kotlin-test") + testImplementation("org.jetbrains.kotlin:kotlin-test-junit") +} +// +//publishing { +// repositories { +// maven { +// name = "GithubPackages" +// url = uri("https://maven.pkg.github.com/lg-backbone/kompendium") +// credentials { +// username = System.getenv("GITHUB_ACTOR") +// password = System.getenv("GITHUB_TOKEN") +// } +// } +// } +// publications { +// create("kompendium") { +// from(components["kotlin"]) +// } +// } +//} diff --git a/kompendium-processor/src/main/kotlin/org/leafygreens/kompendium/processor/KompendiumProcessor.kt b/kompendium-processor/src/main/kotlin/org/leafygreens/kompendium/processor/KompendiumProcessor.kt new file mode 100644 index 000000000..48fbc5885 --- /dev/null +++ b/kompendium-processor/src/main/kotlin/org/leafygreens/kompendium/processor/KompendiumProcessor.kt @@ -0,0 +1,112 @@ +package org.leafygreens.kompendium.processor + +import com.google.auto.service.AutoService +import java.net.URI +import javax.annotation.processing.AbstractProcessor +import javax.annotation.processing.Processor +import javax.annotation.processing.RoundEnvironment +import javax.annotation.processing.SupportedSourceVersion +import javax.lang.model.SourceVersion +import javax.lang.model.element.TypeElement +import org.leafygreens.kompendium.Kompendium +import org.leafygreens.kompendium.annotations.KompendiumContact +import org.leafygreens.kompendium.annotations.KompendiumInfo +import org.leafygreens.kompendium.annotations.KompendiumLicense +import org.leafygreens.kompendium.annotations.KompendiumModule +import org.leafygreens.kompendium.annotations.KompendiumServers +import org.leafygreens.kompendium.models.OpenApiSpecInfoContact +import org.leafygreens.kompendium.models.OpenApiSpecInfoLicense +import org.leafygreens.kompendium.models.OpenApiSpecServer + +@AutoService(Processor::class) +@SupportedSourceVersion(SourceVersion.RELEASE_8) +class KompendiumProcessor : AbstractProcessor() { + + private val kompendium = Kompendium() + + companion object { + const val KAPT_KOTLIN_GENERATED_OPTION_NAME = "kapt.kotlin.generated" + } + + override fun getSupportedAnnotationTypes(): MutableSet { + return mutableSetOf( + KompendiumInfo::class.java.canonicalName, + KompendiumContact::class.java.canonicalName, + KompendiumLicense::class.java.canonicalName, + KompendiumServers::class.java.canonicalName, + KompendiumModule::class.java.canonicalName + ) + } + + // TODO Throw error if more than 1 info, contact, etc.? + override fun process(annotations: MutableSet?, roundEnv: RoundEnvironment?): Boolean { + roundEnv?.getElementsAnnotatedWith(KompendiumInfo::class.java)?.forEach { + val info = it.getAnnotation(KompendiumInfo::class.java) + processKompendiumInfo(info) + } + roundEnv?.getElementsAnnotatedWith(KompendiumContact::class.java)?.forEach { + val contact = it.getAnnotation(KompendiumContact::class.java) + processKompendiumContact(contact) + } + roundEnv?.getElementsAnnotatedWith(KompendiumLicense::class.java)?.forEach { + val license = it.getAnnotation(KompendiumLicense::class.java) + processKompendiumLicense(license) + } + roundEnv?.getElementsAnnotatedWith(KompendiumServers::class.java)?.forEach { + val servers = it.getAnnotation(KompendiumServers::class.java) + processKompendiumServers(servers) + } + return true + } + + private fun processKompendiumInfo(info: KompendiumInfo) { + kompendium.spec.info?.apply { + this.title = info.title + this.version = info.version + info.description.blankToNull()?.let { desc -> + this.description = desc + } + info.termsOfService.blankToNull()?.let { tos -> + this.termsOfService = URI(tos) + } + } + } + + private fun processKompendiumContact(contact: KompendiumContact) { + kompendium.spec.info?.apply { + this.contact = OpenApiSpecInfoContact( + name = contact.name + ).apply { + contact.url.blankToNull()?.let { url -> + this.url = URI(url) + } + contact.email.blankToNull()?.let { email -> + this.email = email + } + } + } + } + + private fun processKompendiumLicense(license: KompendiumLicense) { + kompendium.spec.info?.apply { + this.license = OpenApiSpecInfoLicense( + name = license.name + ).apply { + license.url.blankToNull()?.let { url -> + this.url = URI(url) + } + } + } + } + + private fun processKompendiumServers(servers: KompendiumServers) { + servers.urls.forEach { url -> + kompendium.spec.servers?.add(OpenApiSpecServer(URI(url))) + } + } + + private fun String.blankToNull(): String? = ifBlank { + null + } + +} diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/Kompendium.kt b/kompendium/src/main/kotlin/org/leafygreens/kompendium/Kompendium.kt deleted file mode 100644 index 88ddef1ea..000000000 --- a/kompendium/src/main/kotlin/org/leafygreens/kompendium/Kompendium.kt +++ /dev/null @@ -1,7 +0,0 @@ -package org.leafygreens.kompendium - -import org.leafygreens.kompendium.models.OpenApiSpec - -class Kompendium { - val spec = OpenApiSpec() -} diff --git a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfo.kt b/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfo.kt deleted file mode 100644 index 0789e5e42..000000000 --- a/kompendium/src/main/kotlin/org/leafygreens/kompendium/models/OpenApiSpecInfo.kt +++ /dev/null @@ -1,12 +0,0 @@ -package org.leafygreens.kompendium.models - -import java.net.URI - -data class OpenApiSpecInfo( - val title: String, - val version: String, - val description: String?, - val termsOfService: URI? = null, - val contact: OpenApiSpecInfoContact? = null, - val license: OpenApiSpecInfoLicense? = null -) diff --git a/playground/src/main/kotlin/org/leafygreens/kompendium/playground/Main.kt b/playground/src/main/kotlin/org/leafygreens/kompendium/playground/Main.kt deleted file mode 100644 index 733a8cfd7..000000000 --- a/playground/src/main/kotlin/org/leafygreens/kompendium/playground/Main.kt +++ /dev/null @@ -1,28 +0,0 @@ -package org.leafygreens.kompendium.playground - -import io.ktor.application.Application -import io.ktor.application.call -import io.ktor.response.respondText -import io.ktor.routing.get -import io.ktor.routing.route -import io.ktor.routing.routing -import io.ktor.server.engine.embeddedServer -import io.ktor.server.netty.Netty - -fun main() { - embeddedServer( - Netty, - port = 8080, - module = Application::mainModule - ).start(wait = true) -} - -fun Application.mainModule() { - routing { - route("/") { - get { - call.respondText("hi") - } - } - } -} diff --git a/settings.gradle.kts b/settings.gradle.kts index 59b961c85..bbf181450 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,6 +1,7 @@ -rootProject.name = "root" -include("kompendium") -include("playground") +rootProject.name = "kompendium" +include("kompendium-core") +include("kompendium-processor") +include("kompendium-playground") // Feature Previews enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")