|
| 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.DefaultTask |
| 8 | +import org.gradle.api.file.DirectoryProperty |
| 9 | +import org.gradle.api.provider.MapProperty |
| 10 | +import org.gradle.api.provider.Property |
| 11 | +import org.gradle.api.tasks.* |
| 12 | +import org.gradle.work.DisableCachingByDefault |
| 13 | +import java.io.File |
| 14 | + |
| 15 | +/** |
| 16 | + * Task that generates custom SDK code using Smithy projections and transforms. |
| 17 | + * |
| 18 | + * This task takes the user's operation selections and generates a filtered SDK |
| 19 | + * containing only the selected operations and their dependencies. |
| 20 | + */ |
| 21 | +@DisableCachingByDefault(because = "Custom SDK generation is not cacheable yet") |
| 22 | +abstract class GenerateCustomSdkTask : DefaultTask() { |
| 23 | + |
| 24 | + /** |
| 25 | + * Map of service names to selected operation shape IDs. |
| 26 | + * This comes from the user's DSL configuration. |
| 27 | + */ |
| 28 | + @get:Input |
| 29 | + abstract val selectedOperations: MapProperty<String, List<String>> |
| 30 | + |
| 31 | + /** |
| 32 | + * Package name for the generated SDK. |
| 33 | + */ |
| 34 | + @get:Input |
| 35 | + abstract val packageName: Property<String> |
| 36 | + |
| 37 | + /** |
| 38 | + * Package version for the generated SDK. |
| 39 | + */ |
| 40 | + @get:Input |
| 41 | + abstract val packageVersion: Property<String> |
| 42 | + |
| 43 | + /** |
| 44 | + * Output directory where the generated SDK code will be written. |
| 45 | + */ |
| 46 | + @get:OutputDirectory |
| 47 | + abstract val outputDirectory: DirectoryProperty |
| 48 | + |
| 49 | + /** |
| 50 | + * Directory containing AWS service model files. |
| 51 | + */ |
| 52 | + @get:InputDirectory |
| 53 | + @get:PathSensitive(PathSensitivity.RELATIVE) |
| 54 | + abstract val modelsDirectory: DirectoryProperty |
| 55 | + |
| 56 | + init { |
| 57 | + description = "Generates custom AWS SDK code with only selected operations" |
| 58 | + group = "aws-sdk-kotlin" |
| 59 | + |
| 60 | + // Set default values |
| 61 | + packageName.convention("aws.sdk.kotlin.services.custom") |
| 62 | + packageVersion.convention(project.version.toString()) |
| 63 | + outputDirectory.convention(project.layout.buildDirectory.dir("generated/sources/customSdk")) |
| 64 | + } |
| 65 | + |
| 66 | + @TaskAction |
| 67 | + fun generate() { |
| 68 | + val operations = selectedOperations.get() |
| 69 | + val allOperationShapeIds = operations.values.flatten() |
| 70 | + |
| 71 | + logger.info("Generating custom SDK with ${allOperationShapeIds.size} operations across ${operations.size} services") |
| 72 | + |
| 73 | + if (allOperationShapeIds.isEmpty()) { |
| 74 | + throw IllegalStateException("No operations selected for custom SDK generation") |
| 75 | + } |
| 76 | + |
| 77 | + // Clean output directory |
| 78 | + val outputDir = outputDirectory.get().asFile |
| 79 | + if (outputDir.exists()) { |
| 80 | + outputDir.deleteRecursively() |
| 81 | + } |
| 82 | + outputDir.mkdirs() |
| 83 | + |
| 84 | + // Create Smithy projection configuration |
| 85 | + val projectionConfig = createSmithyProjection(allOperationShapeIds) |
| 86 | + |
| 87 | + // Write projection configuration to file |
| 88 | + val projectionFile = File(outputDir, "smithy-build.json") |
| 89 | + projectionFile.writeText(projectionConfig) |
| 90 | + |
| 91 | + logger.info("Created Smithy projection configuration at: ${projectionFile.absolutePath}") |
| 92 | + |
| 93 | + // Execute smithy-build to generate the custom SDK |
| 94 | + executeSmithyBuild(projectionFile, outputDir) |
| 95 | + |
| 96 | + logger.info("Custom SDK generation completed successfully") |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Create a Smithy projection configuration with the IncludeOperations transform. |
| 101 | + */ |
| 102 | + private fun createSmithyProjection(operationShapeIds: List<String>): String { |
| 103 | + val packageNameValue = packageName.get() |
| 104 | + val packageVersionValue = packageVersion.get() |
| 105 | + |
| 106 | + return """ |
| 107 | + { |
| 108 | + "version": "1.0", |
| 109 | + "imports": [ |
| 110 | + "${getModelFilesPattern()}" |
| 111 | + ], |
| 112 | + "projections": { |
| 113 | + "custom-sdk": { |
| 114 | + "transforms": [ |
| 115 | + ${createIncludeOperationsTransform(operationShapeIds)} |
| 116 | + ], |
| 117 | + "plugins": { |
| 118 | + "kotlin-codegen": { |
| 119 | + "package": { |
| 120 | + "name": "$packageNameValue", |
| 121 | + "version": "$packageVersionValue" |
| 122 | + }, |
| 123 | + "build": { |
| 124 | + "generateFullProject": true, |
| 125 | + "generateDefaultBuildFiles": true |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + """.trimIndent() |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Create the IncludeOperations transform configuration. |
| 137 | + */ |
| 138 | + private fun createIncludeOperationsTransform(operationShapeIds: List<String>): String { |
| 139 | + val operationsJson = operationShapeIds.joinToString( |
| 140 | + prefix = "[\"", |
| 141 | + postfix = "\"]", |
| 142 | + separator = "\", \"" |
| 143 | + ) |
| 144 | + |
| 145 | + return """ |
| 146 | + { |
| 147 | + "name": "awsSmithyKotlinIncludeOperations", |
| 148 | + "args": { |
| 149 | + "operations": $operationsJson |
| 150 | + } |
| 151 | + } |
| 152 | + """.trimIndent() |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Get the pattern for AWS model files. |
| 157 | + */ |
| 158 | + private fun getModelFilesPattern(): String { |
| 159 | + val modelsDir = modelsDirectory.get().asFile |
| 160 | + return "${modelsDir.absolutePath}/*.json" |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Execute smithy-build to generate the custom SDK. |
| 165 | + */ |
| 166 | + private fun executeSmithyBuild(projectionFile: File, outputDir: File) { |
| 167 | + try { |
| 168 | + // For now, we'll create a placeholder implementation |
| 169 | + // In a real implementation, this would execute the Smithy build process |
| 170 | + logger.info("Executing smithy-build with projection: ${projectionFile.absolutePath}") |
| 171 | + |
| 172 | + // Create placeholder generated files to demonstrate the structure |
| 173 | + createPlaceholderGeneratedFiles(outputDir) |
| 174 | + |
| 175 | + } catch (e: Exception) { |
| 176 | + logger.error("Failed to execute smithy-build", e) |
| 177 | + throw TaskExecutionException(this, e) |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Create placeholder generated files to demonstrate the expected output structure. |
| 183 | + * In a real implementation, this would be replaced by actual Smithy code generation. |
| 184 | + */ |
| 185 | + private fun createPlaceholderGeneratedFiles(outputDir: File) { |
| 186 | + val srcDir = File(outputDir, "src/main/kotlin") |
| 187 | + srcDir.mkdirs() |
| 188 | + |
| 189 | + val packageDir = File(srcDir, packageName.get().replace('.', '/')) |
| 190 | + packageDir.mkdirs() |
| 191 | + |
| 192 | + // Create a placeholder client file |
| 193 | + val clientFile = File(packageDir, "CustomSdkClient.kt") |
| 194 | + clientFile.writeText(""" |
| 195 | + /* |
| 196 | + * Generated by AWS SDK for Kotlin Custom SDK Build Plugin |
| 197 | + */ |
| 198 | + package ${packageName.get()} |
| 199 | + |
| 200 | + /** |
| 201 | + * Custom AWS SDK client containing only selected operations. |
| 202 | + * Generated from ${selectedOperations.get().size} services with ${selectedOperations.get().values.flatten().size} total operations. |
| 203 | + */ |
| 204 | + class CustomSdkClient { |
| 205 | + // Generated client implementation would be here |
| 206 | + } |
| 207 | + """.trimIndent()) |
| 208 | + |
| 209 | + logger.info("Created placeholder client at: ${clientFile.absolutePath}") |
| 210 | + } |
| 211 | +} |
0 commit comments