Skip to content

Commit

Permalink
update generator
Browse files Browse the repository at this point in the history
  • Loading branch information
vendelieu committed Sep 20, 2024
1 parent aca4800 commit 905d7c0
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
8 changes: 5 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io.gitlab.arturbosch.detekt.Detekt
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jlleitschuh.gradle.ktlint.KtlintExtension

Expand All @@ -24,14 +25,15 @@ gradlePlugin {
id = "eu.vendeli.jooq.extension"
displayName = "Jooq Extension Gradle Plugin"
description = "Gradle plugin that extends functionality of the JOOQ."
@Suppress("UnstableApiUsage")
tags.set(listOf("kotlin", "spring-boot", "jooq"))
implementationClass = "eu.vendeli.jooq.JooqExtensionPlugin"
}
}
}

configurations.compileClasspath {
// Allow Java 11 dependencies on compile classpath
// Allow Java 11 dependencies on compiler classpath
attributes.attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 11)
}

Expand Down Expand Up @@ -72,9 +74,9 @@ tasks {
}

withType<KotlinCompile> {
kotlinOptions {
compilerOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = javaVersion.majorVersion
jvmTarget.set(JvmTarget.fromTarget(javaVersion.majorVersion))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,27 @@ import java.io.IOException

class ExtendedJavaJooqGenerator : JavaGenerator() {
private val logger: JooqLogger = JooqLogger.getLogger(ExtendedJavaJooqGenerator::class.java)
private val classLoader: ClassLoader = javaClass.classLoader

override fun generateDao(table: TableDefinition) {
super.generateDao(table)

File("$targetDirectory/DAOExtendedImpl.kt").takeIf {
!it.exists()
}?.also { targetFile ->
targetFile.createNewFile()
javaClass.classLoader.getResource("DAOExtendedImpl.kt")!!.openStream().use {
targetFile.writeBytes(it.readBytes())
}
}

val file = getFile(table, GeneratorStrategy.Mode.DAO)
if (file.exists()) {
try {
var fileContent = String(file.readBytes())
var fileContent = file.readBytes().toString(Charsets.UTF_8)

fileContent = fileContent.replace(
"import org.jooq.impl.DAOImpl;\n",
"import eu.vendeli.jooq.impl.DAOExtendedImpl;\nimport org.jooq.DSLContext;\n",
"import org.springframework.beans.factory.annotation.Autowired;\n",
)
fileContent = fileContent.replace(
" extends " + DAOImpl::class.java.getSimpleName(),
Expand All @@ -39,10 +47,6 @@ class ExtendedJavaJooqGenerator : JavaGenerator() {
} catch (e: IOException) {
logger.error("generateDao error: {}", file.absolutePath, e)
}

val daoExtdImplFile = File("${file.parentFile.parentFile.parentFile.path}/DAOExtendedImpl.kt")
daoExtdImplFile.createNewFile()
classLoader.getResource("DAOExtendedImpl.kt")?.openStream()?.copyTo(daoExtdImplFile.outputStream())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package eu.vendeli.jooq.generator

import org.jooq.codegen.GeneratorStrategy
import org.jooq.codegen.KotlinGenerator
import org.jooq.impl.DAOImpl
import org.jooq.meta.TableDefinition
import org.jooq.tools.JooqLogger
import java.io.File
import java.io.IOException

class ExtendedKotlinJooqGenerator : KotlinGenerator() {
private val logger: JooqLogger = JooqLogger.getLogger(ExtendedKotlinJooqGenerator::class.java)

override fun generateDao(table: TableDefinition) {
super.generateDao(table)

File("$targetDirectory/DAOExtendedImpl.kt").takeIf {
!it.exists()
}?.also { targetFile ->
targetFile.createNewFile()
javaClass.classLoader.getResource("DAOExtendedImpl.kt")!!.openStream().use {
targetFile.writeBytes(it.readBytes())
}
}

val file = getFile(table, GeneratorStrategy.Mode.DAO)
if (file.exists()) {
try {
var fileContent = file.readBytes().toString(Charsets.UTF_8)

fileContent = fileContent.replace(
"import org.jooq.impl.DAOImpl\n",
"import org.springframework.beans.factory.annotation.Autowired\n",
)
fileContent = fileContent.replace(
" : " + DAOImpl::class.java.getSimpleName(),
" : eu.vendeli.jooq.impl.DAOExtendedImpl",
)

if (generateSpringAnnotations()) {
val dslContextSetting = ") {\n\t@Autowired\n\tfun setCfg(configuration: Configuration) {\n\t\t" +
"super.setConfiguration(configuration)\n\t}"
fileContent = fileContent.replace(") {", dslContextSetting)
}

file.writeBytes(fileContent.toByteArray())
} catch (e: IOException) {
logger.error("generateDao error: {}", file.absolutePath, e)
}
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/DAOExtendedImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ abstract class DAOExtendedImpl<Record : UpdatableRecord<Record>, Pojo, Id> prote
table: Table<Record>,
type: Class<Pojo>
) : DAOImpl<Record, Pojo, Id>(table, type) {
protected constructor(table: Table<Record>, type: Class<Pojo>, configuration: Configuration) : this(table, type) {
protected constructor(table: Table<Record>, type: Class<Pojo>, configuration: Configuration?) : this(table, type) {
super.setConfiguration(configuration)
}

Expand Down

0 comments on commit 905d7c0

Please sign in to comment.