Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ jobs:
run: ./gradlew ktlintCheck

- name: Run tests with coverage
run: ./gradlew core:koverXmlReport
run: ./gradlew core:koverXmlReport plugin:koverXmlReport

- name: Upload coverage to Codecov
if: github.event_name == 'pull_request'
uses: codecov/codecov-action@v5
with:
files: core/build/reports/kover/report.xml
files: core/build/reports/kover/report.xml,plugin/build/reports/kover/report.xml
fail_ci_if_error: false
token: ${{ secrets.CODECOV_TOKEN }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build/
.gradle/
.kotlin/
.idea/
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ object ApiClientBaseGenerator {

private fun buildMapToResult(t: TypeVariableName): FunSpec = FunSpec
.builder(MAP_TO_RESULT)
.addModifiers(KModifier.PRIVATE, KModifier.SUSPEND, KModifier.INLINE)
.addAnnotation(PublishedApi::class)
.addModifiers(KModifier.INTERNAL, KModifier.SUSPEND, KModifier.INLINE)
.addTypeVariable(t)
.receiver(HTTP_RESPONSE)
.contextParameters(listOf(ContextParameter(RAISE.parameterizedBy(HTTP_ERROR))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ object ApiResponseGenerator {
private const val MESSAGE = "message"
private const val TYPE = "type"

fun generate(): List<FileSpec> = listOf(generateHttpError(), generateHttpSuccess())

fun generateHttpError(): FileSpec {
val enumType = TypeSpec
.enumBuilder(HTTP_ERROR_TYPE)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.avsystem.justworks.core.gen

import com.avsystem.justworks.core.model.ApiSpec
import java.io.File

/**
* Facade that orchestrates model and client code generation,
* writing the produced files to the given output directory.
*/
object CodeGenerator {
data class Result(val modelFiles: Int, val clientFiles: Int)

fun generate(
spec: ApiSpec,
modelPackage: String,
apiPackage: String,
outputDir: File
): Result {
val modelFiles = ModelGenerator(modelPackage).generate(spec)
modelFiles.forEach { it.writeTo(outputDir) }

val hasPolymorphicTypes = modelFiles.any { it.name == SerializersModuleGenerator.FILE_NAME }

val clientFiles = ClientGenerator(apiPackage, modelPackage).generate(spec, hasPolymorphicTypes)
clientFiles.forEach { it.writeTo(outputDir) }

return Result(modelFiles.size, clientFiles.size)
}

fun generateSharedTypes(outputDir: File): Int {
val files = ApiResponseGenerator.generate() + ApiClientBaseGenerator.generate()
files.forEach { it.writeTo(outputDir) }
Comment thread
halotukozak marked this conversation as resolved.
return files.size
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import com.squareup.kotlinpoet.PropertySpec
* that registers each sealed interface with its subclass variants.
*/
class SerializersModuleGenerator(private val modelPackage: String) {
companion object {
const val FILE_NAME = "SerializersModule"
}

/**
* Generates a [FileSpec] containing the SerializersModule registration.
* Returns null if the hierarchy has no sealed types to register.
Expand Down Expand Up @@ -48,7 +52,7 @@ class SerializersModuleGenerator(private val modelPackage: String) {
.build()

return FileSpec
.builder(modelPackage, "SerializersModule")
.builder(modelPackage, FILE_NAME)
.addProperty(prop)
.build()
}
Expand Down
69 changes: 69 additions & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
plugins {
kotlin("jvm")
`java-gradle-plugin`
`maven-publish`
id("org.jetbrains.kotlinx.kover") version "0.9.1"
}

kotlin {
jvmToolchain(21)
}

dependencies {
implementation(project(":core"))
testImplementation(kotlin("test"))
}

gradlePlugin {
plugins {
create("justworks") {
id = "com.avsystem.justworks"
implementationClass = "com.avsystem.justworks.gradle.JustworksPlugin"
}
}
}

// Functional test source set
val functionalTest by sourceSets.creating {
compileClasspath += sourceSets["main"].output
runtimeClasspath += sourceSets["main"].output
}

val functionalTestImplementation by configurations.getting {
extendsFrom(configurations["testImplementation"])
}

val functionalTestRuntimeOnly by configurations.getting {
extendsFrom(configurations["testRuntimeOnly"])
}

dependencies {
functionalTestImplementation(gradleTestKit())
}

val functionalTestTask =
tasks.register<Test>("functionalTest") {
testClassesDirs = functionalTest.output.classesDirs
classpath = functionalTest.runtimeClasspath
useJUnitPlatform()
}

tasks.check {
dependsOn(functionalTestTask)
}

kover {
currentProject {
sources {
excludedSourceSets.add("functionalTest")
}
}
}

tasks.named("koverXmlReport") {
dependsOn(functionalTestTask)
}

tasks.test {
useJUnitPlatform()
}
Loading
Loading