|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package aws.sdk.kotlin.gradle.customsdk |
| 6 | + |
| 7 | +import org.gradle.api.Project |
| 8 | +import org.gradle.api.tasks.TaskProvider |
| 9 | +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension |
| 10 | +import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension |
| 11 | +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile |
| 12 | + |
| 13 | +/** |
| 14 | + * Handles source set integration for generated custom SDK code. |
| 15 | + * Configures both JVM and multiplatform Kotlin projects to include generated sources. |
| 16 | + */ |
| 17 | +object SourceSetIntegration { |
| 18 | + |
| 19 | + /** |
| 20 | + * Configure source sets to include generated SDK code. |
| 21 | + * Supports both Kotlin JVM and Kotlin Multiplatform projects. |
| 22 | + */ |
| 23 | + fun configureSourceSets(project: Project, generateTask: TaskProvider<GenerateCustomSdkTask>) { |
| 24 | + project.logger.info("Configuring source sets for custom SDK generation") |
| 25 | + |
| 26 | + // Configure for Kotlin Multiplatform projects |
| 27 | + project.plugins.withId("org.jetbrains.kotlin.multiplatform") { |
| 28 | + configureMultiplatformSourceSets(project, generateTask) |
| 29 | + } |
| 30 | + |
| 31 | + // Configure for Kotlin JVM projects |
| 32 | + project.plugins.withId("org.jetbrains.kotlin.jvm") { |
| 33 | + configureJvmSourceSets(project, generateTask) |
| 34 | + } |
| 35 | + |
| 36 | + // Configure task dependencies for all Kotlin compilation tasks |
| 37 | + configureTaskDependencies(project, generateTask) |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Configure source sets for Kotlin Multiplatform projects. |
| 42 | + */ |
| 43 | + private fun configureMultiplatformSourceSets(project: Project, generateTask: TaskProvider<GenerateCustomSdkTask>) { |
| 44 | + val kotlin = project.extensions.getByType(KotlinMultiplatformExtension::class.java) |
| 45 | + |
| 46 | + // Add generated sources to commonMain source set |
| 47 | + val commonMain = kotlin.sourceSets.getByName("commonMain") |
| 48 | + commonMain.kotlin.srcDir(generateTask.map { it.outputDirectory.dir("src/main/kotlin") }) |
| 49 | + project.logger.info("Added custom SDK generated sources to commonMain source set") |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Configure source sets for Kotlin JVM projects. |
| 54 | + */ |
| 55 | + private fun configureJvmSourceSets(project: Project, generateTask: TaskProvider<GenerateCustomSdkTask>) { |
| 56 | + val kotlin = project.extensions.getByType(KotlinJvmProjectExtension::class.java) |
| 57 | + |
| 58 | + // Add generated sources to main source set |
| 59 | + val main = kotlin.sourceSets.getByName("main") |
| 60 | + main.kotlin.srcDir(generateTask.map { it.outputDirectory.dir("src/main/kotlin") }) |
| 61 | + project.logger.info("Added custom SDK generated sources to main source set") |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Configure task dependencies to ensure generation runs before Kotlin compilation. |
| 66 | + */ |
| 67 | + private fun configureTaskDependencies(project: Project, generateTask: TaskProvider<GenerateCustomSdkTask>) { |
| 68 | + // Configure task dependencies after project evaluation |
| 69 | + project.afterEvaluate { |
| 70 | + // Make all Kotlin compilation tasks depend on the generation task |
| 71 | + project.tasks.withType(KotlinCompile::class.java).forEach { compileTask -> |
| 72 | + compileTask.dependsOn(generateTask) |
| 73 | + project.logger.debug("Configured ${compileTask.name} to depend on custom SDK generation") |
| 74 | + } |
| 75 | + |
| 76 | + // Also configure dependencies for common Gradle tasks by name |
| 77 | + project.tasks.findByName("compileKotlin")?.dependsOn(generateTask) |
| 78 | + project.tasks.findByName("compileTestKotlin")?.dependsOn(generateTask) |
| 79 | + |
| 80 | + // For multiplatform projects, configure additional compilation tasks |
| 81 | + project.tasks.names.filter { it.contains("compileKotlin") }.forEach { taskName -> |
| 82 | + project.tasks.findByName(taskName)?.dependsOn(generateTask) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Configure IDE integration to ensure generated sources are visible in IDEs. |
| 89 | + */ |
| 90 | + fun configureIdeIntegration(project: Project, generateTask: TaskProvider<GenerateCustomSdkTask>) { |
| 91 | + // Configure IDEA plugin if present |
| 92 | + project.plugins.withId("idea") { |
| 93 | + project.logger.info("Configuring IDEA integration for custom SDK sources") |
| 94 | + |
| 95 | + // The generated sources will be automatically picked up by IDEA |
| 96 | + // since they're added to the Kotlin source sets |
| 97 | + } |
| 98 | + |
| 99 | + // Configure Eclipse plugin if present |
| 100 | + project.plugins.withId("eclipse") { |
| 101 | + project.logger.info("Configuring Eclipse integration for custom SDK sources") |
| 102 | + |
| 103 | + // The generated sources will be automatically picked up by Eclipse |
| 104 | + // since they're added to the Kotlin source sets |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Configure incremental build support. |
| 110 | + */ |
| 111 | + fun configureIncrementalBuild(project: Project, generateTask: TaskProvider<GenerateCustomSdkTask>) { |
| 112 | + // The GenerateCustomSdkTask already has proper input/output annotations |
| 113 | + // This ensures incremental builds work correctly |
| 114 | + |
| 115 | + project.afterEvaluate { |
| 116 | + generateTask.get().outputs.upToDateWhen { |
| 117 | + // Task is up-to-date if inputs haven't changed |
| 118 | + // This is handled automatically by Gradle's input/output tracking |
| 119 | + true |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + project.logger.info("Configured incremental build support for custom SDK generation") |
| 124 | + } |
| 125 | +} |
0 commit comments