-
Notifications
You must be signed in to change notification settings - Fork 55
feat: link api ref docs to examples #1550
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
.../aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/ModuleDocumentationIntegration.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package aws.sdk.kotlin.codegen | ||
|
|
||
| import software.amazon.smithy.kotlin.codegen.KotlinSettings | ||
| import software.amazon.smithy.kotlin.codegen.core.CodegenContext | ||
| import software.amazon.smithy.kotlin.codegen.core.KotlinDelegator | ||
| import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration | ||
| import software.amazon.smithy.kotlin.codegen.model.expectShape | ||
| import software.amazon.smithy.kotlin.codegen.model.getTrait | ||
| import software.amazon.smithy.model.Model | ||
| import software.amazon.smithy.model.shapes.ServiceShape | ||
| import software.amazon.smithy.model.traits.TitleTrait | ||
| import java.io.File | ||
|
|
||
| /** | ||
| * Maps a service's SDK ID to its code examples | ||
| */ | ||
| private val CODE_EXAMPLES_SERVICES_MAP = mapOf( | ||
| "API Gateway" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_api-gateway_code_examples.html", | ||
| "Auto Scaling" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_auto-scaling_code_examples.html", | ||
| "Bedrock" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_bedrock_code_examples.html", | ||
| "CloudWatch" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_cloudwatch_code_examples.html", | ||
| "Comprehend" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_comprehend_code_examples.html", | ||
| "DynamoDB" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_dynamodb_code_examples.html", | ||
| "EC2" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_ec2_code_examples.html", | ||
| "ECR" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_ecr_code_examples.html", | ||
| "OpenSearch" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_opensearch_code_examples.html", | ||
| "EventBridge" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_eventbridge_code_examples.html", | ||
| "Glue" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_glue_code_examples.html", | ||
| "IAM" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_iam_code_examples.html", | ||
| "IoT" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_iot_code_examples.html ", | ||
| "Keyspaces" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_keyspaces_code_examples.html", | ||
| "KMS" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_kms_code_examples.html", | ||
| "Lambda" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_lambda_code_examples.html", | ||
| "MediaConvert" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_mediaconvert_code_examples.html", | ||
| "Pinpoint" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_pinpoint_code_examples.html", | ||
| "RDS" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_rds_code_examples.html", | ||
| "Redshift" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_redshift_code_examples.html", | ||
| "Rekognition" to "https://docs.aws.amazon.com/code-library/latest/ug/kotlin_1_rekognition_code_examples.html", | ||
| ) | ||
|
|
||
| /** | ||
| * Generates an `API.md` file that will be used as module documentation in our API ref docs. | ||
| * Some services have code example documentation we need to generate. Others have handwritten documentation. | ||
| * The integration renders both into the `API.md` file. | ||
| * | ||
| * See: https://kotlinlang.org/docs/dokka-module-and-package-docs.html | ||
| * | ||
| * See: https://github.com/awslabs/aws-sdk-kotlin/blob/0581f5c5eeaa14dcd8af4ea0dfc088b1057f5ba5/build.gradle.kts#L68-L75 | ||
| */ | ||
| class ModuleDocumentationIntegration( | ||
| private val codeExamples: Map<String, String> = CODE_EXAMPLES_SERVICES_MAP, | ||
| ) : KotlinIntegration { | ||
| override fun enabledForService(model: Model, settings: KotlinSettings): Boolean = | ||
| codeExamples.keys.contains( | ||
| model | ||
| .expectShape<ServiceShape>(settings.service) | ||
| .sdkId, | ||
| ) || | ||
| handWrittenDocsFile(settings).exists() | ||
|
|
||
| override fun writeAdditionalFiles(ctx: CodegenContext, delegator: KotlinDelegator) { | ||
| delegator.fileManifest.writeFile( | ||
| "API.md", | ||
| generateModuleDocumentation(ctx), | ||
| ) | ||
| } | ||
|
|
||
| internal fun generateModuleDocumentation( | ||
| ctx: CodegenContext, | ||
| ) = buildString { | ||
| val handWrittenDocsFile = handWrittenDocsFile(ctx.settings) | ||
| if (handWrittenDocsFile.exists()) { | ||
| append( | ||
| handWrittenDocsFile.readText(), | ||
| ) | ||
| appendLine() | ||
| } | ||
| if (codeExamples.keys.contains(ctx.settings.sdkId)) { | ||
| if (!handWrittenDocsFile.exists()) { | ||
| append( | ||
| boilerPlate(ctx), | ||
| ) | ||
| } | ||
| append( | ||
| codeExamplesDocs(ctx), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private fun boilerPlate(ctx: CodegenContext) = buildString { | ||
| // Title must be "Module" followed by the exact module name or dokka won't render it | ||
| appendLine("# Module ${ctx.settings.pkg.name.split(".").last()}") | ||
| appendLine() | ||
| ctx | ||
| .model | ||
| .expectShape<ServiceShape>(ctx.settings.service) | ||
| .getTrait<TitleTrait>() | ||
| ?.value | ||
| ?.let { | ||
| appendLine(it) | ||
| appendLine() | ||
| } | ||
| } | ||
|
|
||
| private fun codeExamplesDocs(ctx: CodegenContext) = buildString { | ||
| val sdkId = ctx.settings.sdkId | ||
| val codeExampleLink = codeExamples[sdkId] | ||
| val title = ctx | ||
| .model | ||
| .expectShape<ServiceShape>(ctx.settings.service) | ||
| .getTrait<TitleTrait>() | ||
| ?.value | ||
|
|
||
| appendLine("## Code Examples") | ||
| append("To see full code examples, see the ${title ?: sdkId} examples in the AWS code example library. ") | ||
| appendLine("See $codeExampleLink") | ||
| appendLine() | ||
| } | ||
| } | ||
|
|
||
| private fun handWrittenDocsFile(settings: KotlinSettings): File { | ||
| val sdkRootDir = System.getProperty("user.dir") | ||
| val serviceDir = "$sdkRootDir/services/${settings.pkg.name.split(".").last()}" | ||
|
|
||
| return File("$serviceDir/DOCS.md") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...-sdk-codegen/src/test/kotlin/aws/sdk/kotlin/codegen/ModuleDocumentationIntegrationTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package aws.sdk.kotlin.codegen | ||
|
|
||
| import software.amazon.smithy.kotlin.codegen.test.newTestContext | ||
| import software.amazon.smithy.kotlin.codegen.test.shouldContainOnlyOnceWithDiff | ||
| import software.amazon.smithy.kotlin.codegen.test.toGenerationContext | ||
| import software.amazon.smithy.kotlin.codegen.test.toSmithyModel | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertTrue | ||
|
|
||
| private val model = """ | ||
| ${"$"}version: "2" | ||
|
|
||
| namespace com.test | ||
|
|
||
| use aws.api#service | ||
|
|
||
| @service(sdkId: "Test") | ||
| @title("Test Service") | ||
| service Test { | ||
| version: "1.0.0", | ||
| operations: [] | ||
| } | ||
| """.toSmithyModel() | ||
|
|
||
| val ctx = model.newTestContext("Test") | ||
|
|
||
| class ModuleDocumentationIntegrationTest { | ||
| @Test | ||
| fun integrationIsAppliedCorrectly() { | ||
| assertFalse( | ||
| ModuleDocumentationIntegration().enabledForService(model, ctx.generationCtx.settings), | ||
| ) | ||
| assertTrue( | ||
| ModuleDocumentationIntegration( | ||
| codeExamples = mapOf("Test" to "https://example.com"), | ||
| ).enabledForService(model, ctx.generationCtx.settings), | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun rendersBoilerplate() = | ||
| ModuleDocumentationIntegration( | ||
| codeExamples = mapOf("Test" to "https://example.com"), | ||
| ) | ||
| .generateModuleDocumentation(ctx.toGenerationContext()) | ||
| .shouldContainOnlyOnceWithDiff( | ||
| """ | ||
| # Module test | ||
|
|
||
| Test Service | ||
| """.trimIndent(), | ||
| ) | ||
|
|
||
| @Test | ||
| fun rendersCodeExampleDocs() = | ||
| ModuleDocumentationIntegration( | ||
| codeExamples = mapOf("Test" to "https://example.com"), | ||
| ) | ||
| .generateModuleDocumentation(ctx.toGenerationContext()) | ||
| .shouldContainOnlyOnceWithDiff( | ||
| """ | ||
| ## Code Examples | ||
| To see full code examples, see the Test Service examples in the AWS code example library. See https://example.com | ||
| """.trimIndent(), | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
naming: the function and the value have the same name,
handWrittenDocsFile, which can be hard to read. Consider renaming function togetHandWrittenDocsFileAlso nit/style: you can check if the file exists inside
getHandWrittenDocsFileand returnnullif it doesn't.That would simplify this code section to just:
getHandWrittenDocsFile(ctx)?.let { append(it.readText()) }