|
| 1 | +package aws.sdk.kotlin.codegen |
| 2 | + |
| 3 | +import software.amazon.smithy.kotlin.codegen.KotlinSettings |
| 4 | +import software.amazon.smithy.kotlin.codegen.core.CodegenContext |
| 5 | +import software.amazon.smithy.kotlin.codegen.core.KotlinDelegator |
| 6 | +import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration |
| 7 | +import software.amazon.smithy.kotlin.codegen.model.expectShape |
| 8 | +import software.amazon.smithy.kotlin.codegen.model.getTrait |
| 9 | +import software.amazon.smithy.model.Model |
| 10 | +import software.amazon.smithy.model.shapes.ServiceShape |
| 11 | +import software.amazon.smithy.model.traits.TitleTrait |
| 12 | + |
| 13 | +/** |
| 14 | + * Maps a services SKD ID to its code examples |
| 15 | + */ |
| 16 | +private val currentCodeExamplesServices = mapOf( |
| 17 | + "API Gateway" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_api-gateway_code_examples.html", |
| 18 | + "Auto Scaling" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_auto-scaling_code_examples.html", |
| 19 | + "Bedrock" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_bedrock_code_examples.html", |
| 20 | + "CloudWatch" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_cloudwatch_code_examples.html", |
| 21 | + "Comprehend" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_comprehend_code_examples.html", |
| 22 | + "DynamoDB" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_dynamodb_code_examples.html", |
| 23 | + "EC2" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_ec2_code_examples.html", |
| 24 | + "ECR" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_ecr_code_examples.html", |
| 25 | + "OpenSearch" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_opensearch_code_examples.html", |
| 26 | + "EventBridge" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_eventbridge_code_examples.html", |
| 27 | + "Glue" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_glue_code_examples.html", |
| 28 | + "IAM" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_iam_code_examples.html", |
| 29 | + "IoT" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_iot_code_examples.html ", |
| 30 | + "Keyspaces" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_keyspaces_code_examples.html", |
| 31 | + "KMS" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_kms_code_examples.html", |
| 32 | + "Lambda" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_lambda_code_examples.html", |
| 33 | + "MediaConvert" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_mediaconvert_code_examples.html", |
| 34 | + "Pinpoint" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_pinpoint_code_examples.html", |
| 35 | + "RDS" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_rds_code_examples.html", |
| 36 | + "Redshift" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_redshift_code_examples.html", |
| 37 | + "Rekognition" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_rekognition_code_examples.html", |
| 38 | +) |
| 39 | + |
| 40 | +/** |
| 41 | + * Maps a services SKD ID to its handwritten module documentation file in the `resources` dir. |
| 42 | + * The module documentation files MUST be markdown files. |
| 43 | + */ |
| 44 | +private val currentHandWrittenServices = mapOf( |
| 45 | + "S3" to "S3.md", |
| 46 | +) |
| 47 | + |
| 48 | +/** |
| 49 | + * Generates an `API.md` file that will be used as module documentation in our API ref docs. |
| 50 | + * Some services have code examples we need to link to. Others have handwritten documentation. |
| 51 | + * The integration renders both into the `API.md` file. |
| 52 | + * |
| 53 | + * See: https://kotlinlang.org/docs/dokka-module-and-package-docs.html |
| 54 | + * |
| 55 | + * See: https://github.com/awslabs/aws-sdk-kotlin/blob/0581f5c5eeaa14dcd8af4ea0dfc088b1057f5ba5/build.gradle.kts#L68-L75 |
| 56 | + */ |
| 57 | +class ModuleDocumentationIntegration( |
| 58 | + private val codeExamples: Map<String, String> = currentCodeExamplesServices, |
| 59 | + private val handWritten: Map<String, String> = currentHandWrittenServices, |
| 60 | +) : KotlinIntegration { |
| 61 | + override fun enabledForService(model: Model, settings: KotlinSettings): Boolean = |
| 62 | + model.expectShape<ServiceShape>(settings.service).sdkId.let { |
| 63 | + codeExamples.keys.contains(it) || handWritten.keys.contains(it) |
| 64 | + } |
| 65 | + |
| 66 | + override fun writeAdditionalFiles(ctx: CodegenContext, delegator: KotlinDelegator) { |
| 67 | + delegator.fileManifest.writeFile( |
| 68 | + "API.md", |
| 69 | + generateModuleDocumentation(ctx, ctx.settings.sdkId), |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + internal fun generateModuleDocumentation( |
| 74 | + ctx: CodegenContext, |
| 75 | + sdkId: String, |
| 76 | + ) = buildString { |
| 77 | + append( |
| 78 | + generateBoilerPlate(ctx), |
| 79 | + ) |
| 80 | + if (codeExamples.keys.contains(sdkId)) { |
| 81 | + append( |
| 82 | + generateCodeExamplesDocs(sdkId), |
| 83 | + ) |
| 84 | + appendLine() |
| 85 | + } |
| 86 | + if (handWritten.keys.contains(sdkId)) { |
| 87 | + append( |
| 88 | + generateHandWrittenDocs(sdkId), |
| 89 | + ) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private fun generateBoilerPlate(ctx: CodegenContext) = buildString { |
| 94 | + // Title must me "Module" followed by the exact module name or dokka won't render it |
| 95 | + appendLine("# Module ${ctx.settings.pkg.name.split(".").last()}") |
| 96 | + appendLine() |
| 97 | + ctx |
| 98 | + .model |
| 99 | + .expectShape<ServiceShape>(ctx.settings.service) |
| 100 | + .getTrait<TitleTrait>() |
| 101 | + ?.value |
| 102 | + ?.let { |
| 103 | + appendLine(it) |
| 104 | + appendLine() |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private fun generateCodeExamplesDocs(sdkId: String) = buildString { |
| 109 | + appendLine("## Code Examples") |
| 110 | + appendLine("To see full code examples, see the $sdkId examples in the AWS Code Library. See ${codeExamples[sdkId]}") |
| 111 | + appendLine() |
| 112 | + } |
| 113 | + |
| 114 | + private fun generateHandWrittenDocs(sdkId: String): String = object {} |
| 115 | + .javaClass |
| 116 | + .classLoader |
| 117 | + .getResourceAsStream("aws/sdk/kotlin/codegen/moduledocumentation/${handWritten[sdkId]}") |
| 118 | + ?.bufferedReader() |
| 119 | + ?.readText() |
| 120 | + ?: throw Exception("Unable to read from file ${handWritten[sdkId]}") |
| 121 | +} |
0 commit comments