Skip to content

Commit

Permalink
Rework jooq codegen for consistent clean builds
Browse files Browse the repository at this point in the history
  • Loading branch information
DrewCarlson committed Oct 19, 2024
1 parent ce0530c commit c927142
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 151 deletions.
3 changes: 3 additions & 0 deletions build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ dependencies {
implementation(libsCommon.spotless.plugin)
implementation(libsCommon.kotlin.gradle.conventions)
implementation(libsCommon.atomicfu.plugin)
implementation(libsServer.flyway.core)
implementation(libsServer.jdbc.sqlite)
implementation(libsServer.jooq.gradle)
}

repositories {
Expand Down
38 changes: 38 additions & 0 deletions build-logic/src/main/kotlin/FlywayMigrateTask.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import org.gradle.api.DefaultTask
import org.gradle.api.file.Directory
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import org.flywaydb.core.Flyway

// For various reasons, don't use the flyway gradle plugin
// https://github.com/flyway/flyway/issues/3550
@CacheableTask
abstract class FlywayMigrateTask : DefaultTask() {

@get:Input
abstract val driver: Property<String>

@get:Input
abstract val url: Property<String>

@get:InputDirectory
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract var migrationsLocation: Directory

// Output is required to make the task cacheable
@OutputFile
val outputFile: RegularFileProperty = project.objects.fileProperty().convention(
project.layout.buildDirectory.file(url.map { it.substringAfterLast(":") })
)

@TaskAction
fun run() {
Flyway.configure()
.driver(driver.get())
.dataSource(url.get(), null, null)
.locations("filesystem:${migrationsLocation.asFile}")
.load()
.migrate()
}
}
95 changes: 95 additions & 0 deletions build-logic/src/main/kotlin/JooqCodegenConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import org.jooq.meta.jaxb.Configuration
import org.jooq.meta.jaxb.ForcedType
import org.jooq.meta.jaxb.Logging


fun Configuration.anystreamConfig(dbUrl: String) {
logging = Logging.DEBUG
jdbc.apply {
driver = "org.sqlite.JDBC"
url = dbUrl
}
generator.apply {
name = "Generator"
strategy.name = "JooqStrategy"
target.packageName = "anystream.db"
generate.apply {
isDaos = false
isRecords = true
isKotlinNotNullRecordAttributes = true
isKotlinNotNullInterfaceAttributes = true
isJavaTimeTypes = false
// Pojos as simple data classes
isSerializablePojos = false
isImmutablePojos = true
isPojosToString = false
isPojosEqualsAndHashCode = false
isPojosAsKotlinDataClasses = true
isKotlinNotNullPojoAttributes = true
}
database.apply {
name = "org.jooq.meta.sqlite.SQLiteDatabase"
excludes = listOf(
// Exclude flyway migration tables
"flyway_.*",
// Exclude search meta tables
"searchable_content_.*",
).joinToString("|")
forcedTypes.addAll(forcedTypes())
}
}
}

fun forcedType(
userType: String,
includeExpression: String,
converter: String? = null,
isEnumConverter: Boolean = false
): ForcedType =
ForcedType().apply {
this.userType = userType
this.includeExpression = includeExpression
this.isEnumConverter = isEnumConverter
this.converter = converter
}

fun forcedTypeEnum(
userType: String,
includeExpression: String,
): ForcedType = forcedType(userType, includeExpression, isEnumConverter = true)

fun forcedTypes(): List<ForcedType> = listOf(
ForcedType().apply {
includeTypes = "DATETIME"
userType = "kotlinx.datetime.Instant"
binding = "anystream.db.converter.JooqInstantBinding"
//generator = "anystream.db.converter.JooqInstantGenerator"
},
forcedType(
userType = "anystream.models.Permission",
includeExpression = "user_permission.value",
converter = "anystream.db.converter.PermissionConverter"
),
forcedType(
userType = "anystream.models.Permission",
includeExpression = "user_permission.value",
converter = "anystream.db.converter.PermissionConverter"
),
forcedType(
userType = "anystream.models.Permission",
includeExpression = "user_permission.value",
converter = "anystream.db.converter.PermissionConverter"
),
forcedType(
userType = "kotlin.Set<anystream.models.Permission>",
includeExpression = "invite_code.permissions",
converter = "anystream.db.converter.PermissionSetConverter"
),
forcedTypeEnum("anystream.models.StreamEncodingType", "stream_encoding.type"),
forcedTypeEnum("anystream.models.MediaType", "media_type"),
forcedTypeEnum("anystream.models.MediaLinkType", "media_link.type"),
forcedTypeEnum("anystream.models.MediaKind", "media_kind"),
forcedTypeEnum("anystream.models.Descriptor", "descriptor"),
forcedType("kotlin.String", "searchable_content.id"),
forcedType("kotlin.String", "searchable_content.content"),
)
4 changes: 3 additions & 1 deletion build-logic/src/main/kotlin/constants.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.gradle.api.JavaVersion
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

val JAVA_TARGET = JavaVersion.VERSION_17
val JAVA_TARGET = JavaVersion.VERSION_17
val JVM_TARGET = JvmTarget.JVM_17
19 changes: 7 additions & 12 deletions build-logic/src/main/kotlin/multiplatform-lib.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import com.android.build.gradle.LibraryExtension
import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMetadataTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask

plugins {
kotlin("multiplatform")
Expand Down Expand Up @@ -57,10 +59,8 @@ kotlin {
jvm()
if (hasAndroidSdk) {
androidTarget {
compilations.all {
kotlinOptions {
jvmTarget = JAVA_TARGET.majorVersion
}
compilerOptions {
jvmTarget.set(JVM_TARGET)
}
}
}
Expand All @@ -72,13 +72,8 @@ kotlin {
iosX64()
}
applyDefaultHierarchyTemplate()

targets.all {
compilations.configureEach {
compilerOptions.configure {
freeCompilerArgs.add("-Xexpect-actual-classes")
}
}
compilerOptions {
freeCompilerArgs.add("-Xexpect-actual-classes")
}

@Suppress("UNUSED_VARIABLE")
Expand Down Expand Up @@ -148,7 +143,7 @@ kotlin {

afterEvaluate {
if (extensions.findByName("ksp") != null) {
tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>>().all {
tasks.withType<KotlinCompilationTask<*>>().all {
if (name != "kspCommonMainKotlinMetadata") {
dependsOn("kspCommonMainKotlinMetadata")
}
Expand Down
8 changes: 2 additions & 6 deletions build-logic/src/main/kotlin/server-lib.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ kotlin {
optIn.add("kotlin.io.path.ExperimentalPathApi")
}
jvmToolchain(JAVA_TARGET.majorVersion.toInt())
target {
compilations.all {
kotlinOptions {
jvmTarget = JAVA_TARGET.majorVersion
}
}
compilerOptions {
jvmTarget.set(JVM_TARGET)
}
}

Expand Down
58 changes: 53 additions & 5 deletions client/data-models/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import nu.studer.gradle.jooq.JooqGenerate

plugins {
id("multiplatform-lib")
alias(libsServer.plugins.jooq)
}

kotlin {
jvm()

targets.all {
compilations.all {
compileTaskProvider.configure {
inputs.dir(layout.buildDirectory.dir("generated-src/jooq/main"))
dependsOn("generateJooq")
}
}
}

sourceSets {
val commonMain by getting {
kotlin.srcDirs(
"src",
buildDir.resolve("generated-src/jooq/main")
layout.buildDirectory.dir("generated-src/jooq/main")
)
dependencies {
implementation(libsCommon.serialization.core)
Expand All @@ -23,7 +33,45 @@ kotlin {
}
}

tasks.withType<KotlinCompile> {
inputs.files(fileTree(layout.buildDirectory.dir("generated-src")).files)
dependsOn(":server:db-models:movePojos")
dependencies {
jooqGenerator(libsServer.jdbc.sqlite)
jooqGenerator(projects.server.dbModels.jooqGenerator)
}

val dbFile = layout.buildDirectory.file("anystream-reference.db").get().asFile
val dbUrl = "jdbc:sqlite:${dbFile.absolutePath}"
val migrationPath = projects.server.dbModels.dependencyProject.file("src/main/resources/db/migration")

val flywayMigrate by tasks.registering(FlywayMigrateTask::class) {
driver.set("org.sqlite.JDBC")
url.set(dbUrl)
migrationsLocation = layout.projectDirectory.dir(migrationPath.absolutePath)
if (dbFile.exists()) {
inputs.file(dbFile)
doFirst { dbFile.delete() }
}
}

jooq {
version.set(libsServer.versions.jooq.get())
configurations {
create("main") {
jooqConfiguration.anystreamConfig(dbUrl)
}
}
}

val dbClassesTree = fileTree(layout.buildDirectory.dir("generated-src/jooq/main")) {
include("anystream/db/**")
}

tasks.getByName<JooqGenerate>("generateJooq") {
inputs.dir(migrationPath)
allInputsDeclared.set(true)
dependsOn("flywayMigrate")
doLast {
delete {
delete(dbClassesTree)
}
}
}
8 changes: 5 additions & 3 deletions gradle/libsServer.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ logback = "1.5.6"
koinKtor = "4.0.0"
icu4j = "74.2"
mockk = "1.13.10"
jooqGradle = "9.0"

[plugins]
shadowjar = { id = "com.github.johnrengelman.shadow", version = "8.1.1" }
jooq = { id = "nu.studer.jooq", version = "9.0" }
flyway = { id = "org.flywaydb.flyway", version = "10.1.0" }
jooq = { id = "nu.studer.jooq", version.ref = "jooqGradle" }
flyway = { id = "org.flywaydb.flyway", version = "10.19.0" }

[libraries]
flyway-core = { module = "org.flywaydb:flyway-core", version = "10.1.0" }
flyway-core = { module = "org.flywaydb:flyway-core", version = "10.19.0" }
fastObjectPool = { module = "cn.danielw:fast-object-pool", version = "2.2.1" }
mockk = { module = "io.mockk:mockk", version.ref = "mockk" }

Expand Down Expand Up @@ -53,6 +54,7 @@ jooq-kotlin = { module = "org.jooq:jooq-kotlin", version.ref = "jooq" }
jooq-coroutines = { module = "org.jooq:jooq-kotlin-coroutines", version.ref = "jooq" }
jooq-test = { module = "org.jooq:jooq-test", version.ref = "jooq" }
jooq-test-kotlin = { module = "org.jooq:jooq-test-kotlin", version.ref = "jooq" }
jooq-gradle = { module = "nu.studer:gradle-jooq-plugin", version.ref = "jooqGradle" }

[bundles]
icu4j = ["icu4j", "icu4j-charset", "icu4j-localespi"]
Expand Down
Loading

0 comments on commit c927142

Please sign in to comment.