Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for setting target source set #214

Merged
merged 1 commit into from
Jul 31, 2024
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
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
Loading