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