Skip to content

Commit

Permalink
feat: add support for setting target source set (#214)
Browse files Browse the repository at this point in the history
Introduces support for setting target source set for each
`GenerateJooqClassesTask` instance.

Source sets are configured right after the plugin is applied, but before
the build script (project) is evaluated, so source set configuration
happens before any values are set in the buildscript. This means a
project extension can't be used to configure the source set name for the
plugin to generate sources for (the source set will be configured before
`targetSourceSet` value is set). Therefore I took a different approach
here: we configure all source sets to include `GenerateJooqClassesTask`
tasks output where `targetSourceSet` value of the task matches the
source set name.

This way it is possible to avoid using something like
`project.afterEvaluate` to configure source sets and configuration still
happens lazily.
  • Loading branch information
monosoul authored Jul 31, 2024
1 parent 58f4d40 commit 4a1ae8f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import org.gradle.testkit.runner.TaskOutcome.SUCCESS
import org.junit.jupiter.api.Test
import strikt.api.expect
import strikt.assertions.isEqualTo
import strikt.assertions.isNull
import strikt.java.exists
import strikt.java.notExists

class ConfigurabilityJooqDockerPluginFunctionalTest : dev.monosoul.jooq.functional.JooqDockerPluginFunctionalTestBase() {
@Test
Expand Down Expand Up @@ -218,4 +220,68 @@ class ConfigurabilityJooqDockerPluginFunctionalTest : dev.monosoul.jooq.function
).exists()
}
}

@Test
fun `should respect targetSourceSet task property`() {
// given
prepareBuildGradleFile {
"""
import dev.monosoul.jooq.RecommendedVersions
plugins {
kotlin("jvm") version "1.9.20"
id("dev.monosoul.jooq-docker")
}
repositories {
mavenCentral()
}
sourceSets {
create("custom") {
compileClasspath = files(main.map { it.compileClasspath })
runtimeClasspath = files(main.map { it.runtimeClasspath })
}
}
tasks.generateJooqClasses {
targetSourceSet.set("custom")
}
dependencies {
jooqCodegen("org.postgresql:postgresql:42.3.6")
implementation("org.postgresql:postgresql:42.5.4")
implementation("org.jooq:jooq:${'$'}{RecommendedVersions.JOOQ_VERSION}")
implementation("org.flywaydb:flyway-core:${'$'}{RecommendedVersions.FLYWAY_VERSION}")
}
""".trimIndent()
}
copyResource(
from = "/V01__init_multiple_schemas.sql",
to = "src/main/resources/db/migration/V01__init_multiple_schemas.sql",
)

// when
val mainSourceSetCompilationResult = runGradleWithArguments("classes")

// then
expect {
that(mainSourceSetCompilationResult).getTask("generateJooqClasses").isNull()
that(
projectFile("build/generated-jooq/org/jooq/generated/tables/Foo.java"),
).notExists()
}

// and when
val testSourceSetCompilationResult = runGradleWithArguments("customClasses")

// then
expect {
that(testSourceSetCompilationResult).generateJooqClassesTask.outcome isEqualTo SUCCESS
that(
projectFile("build/generated-jooq/org/jooq/generated/tables/Foo.java"),
).exists()
}
}
}
7 changes: 7 additions & 0 deletions src/main/kotlin/dev/monosoul/jooq/GenerateJooqClassesTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.gradle.api.tasks.Classpath
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.SourceSet.MAIN_SOURCE_SET_NAME
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.listProperty
Expand All @@ -54,6 +55,12 @@ open class GenerateJooqClassesTask
private val projectLayout: ProjectLayout,
) : DefaultTask(),
SettingsAware {
/**
* Source set name to include generated sources into.
*/
@Input
val targetSourceSet = objectFactory.property<String>().convention(MAIN_SOURCE_SET_NAME)

/**
* List of schemas to take into account when running migrations and generating code.
*/
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/dev/monosoul/jooq/JooqDockerPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.gradle.api.attributes.Bundling
import org.gradle.api.attributes.Bundling.BUNDLING_ATTRIBUTE
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.SourceSet.MAIN_SOURCE_SET_NAME
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.named
Expand Down Expand Up @@ -42,9 +41,13 @@ open class JooqDockerPlugin
tasks.register<GenerateJooqClassesTask>("generateJooqClasses")
pluginManager.withPlugin("org.gradle.java") {
extensions.findByType<JavaPluginExtension>()?.run {
sourceSets.named(MAIN_SOURCE_SET_NAME) {
sourceSets.configureEach {
java {
srcDirs(tasks.withType<GenerateJooqClassesTask>())
srcDirs(
tasks
.withType<GenerateJooqClassesTask>()
.matching { it.targetSourceSet.get() == this@configureEach.name },
)
}
}
}
Expand Down

0 comments on commit 4a1ae8f

Please sign in to comment.