Unverified Commit 213b00f4 authored by Russell Cohen's avatar Russell Cohen Committed by GitHub
Browse files

Add Gradle Task to generate SDK examples (#203)

* Add Gradle Task to generate SDK examples

* Rename base task
parent 36f67c5f
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -11,3 +11,6 @@ Generate an SDK:

Generate, compile, and test an SDK:
`./gradlew :aws:sdk:build`

Run an SDK example:
`./gradlew :aws:sdk:runExample --example dynamo-helloworld`
+60 −6
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ plugins {
val smithyVersion: String by project

val sdkOutputDir = buildDir.resolve("aws-sdk")
val awsServices = discoverServices()
// TODO: smithy-http should be removed
val runtimeModules = listOf("smithy-types", "smithy-http")
val awsModules = listOf("aws-auth", "aws-endpoint", "aws-types")
@@ -40,6 +39,13 @@ dependencies {

data class AwsService(val service: String, val module: String, val modelFile: File, val extraConfig: String? = null)

val awsServices: Provider<List<AwsService>> = project.providers.provider { discoverServices() }

/**
 * Discovers services from the `models` directory
 *
 * Do not invoke this function directly. Use the `awsServices` provider.
 */
fun discoverServices(): List<AwsService> {
    val models = project.file("models")
    return fileTree(models).map { file ->
@@ -88,14 +94,14 @@ fun generateSmithyBuild(tests: List<AwsService>): String {
task("generateSmithyBuild") {
    description = "generate smithy-build.json"
    doFirst {
        projectDir.resolve("smithy-build.json").writeText(generateSmithyBuild(awsServices))
        projectDir.resolve("smithy-build.json").writeText(generateSmithyBuild(awsServices.get()))
    }
}

task("relocateServices") {
    description = "relocate AWS services to their final destination"
    doLast {
        awsServices.forEach {
        awsServices.get().forEach {
            copy {
                from("$buildDir/smithyprojections/sdk/${it.module}/rust-codegen")
                into(sdkOutputDir.resolve(it.module))
@@ -104,6 +110,19 @@ task("relocateServices") {
    }
}

task("relocateExamples") {
    description = "relocate the examples folder & rewrite path dependencies"
    doLast {
        copy {
            from(projectDir)
            include("examples/**")
            into(sdkOutputDir)
            exclude("**/target")
            filter { line -> line.replace("build/aws-sdk/", "") }
        }
    }
}

tasks.register<Copy>("relocateAwsRuntime") {
    from("$rootDir/aws/rust-runtime")
    awsModules.forEach {
@@ -136,7 +155,11 @@ tasks.register<Copy>("relocateRuntime") {
}

fun generateCargoWorkspace(services: List<AwsService>): String {
    val modules = services.map(AwsService::module) + runtimeModules + awsModules
    val examples = projectDir.resolve("examples").listFiles { file -> !file.name.startsWith(".") }?.toList()
        ?.map { "examples/${it.name}" }.orEmpty()

    val modules = services.map(AwsService::module) + runtimeModules + awsModules + examples
        ?.toList()
    return """
    [workspace]
    members = [
@@ -147,12 +170,18 @@ fun generateCargoWorkspace(services: List<AwsService>): String {
task("generateCargoWorkspace") {
    description = "generate Cargo.toml workspace file"
    doFirst {
        sdkOutputDir.resolve("Cargo.toml").writeText(generateCargoWorkspace(awsServices))
        sdkOutputDir.resolve("Cargo.toml").writeText(generateCargoWorkspace(awsServices.get()))
    }
}

task("finalizeSdk") {
    finalizedBy("relocateServices", "relocateRuntime", "relocateAwsRuntime", "generateCargoWorkspace")
    finalizedBy(
        "relocateServices",
        "relocateRuntime",
        "relocateAwsRuntime",
        "relocateExamples",
        "generateCargoWorkspace"
    )
}

tasks["smithyBuildJar"].dependsOn("generateSmithyBuild")
@@ -192,6 +221,31 @@ tasks.register<Exec>("cargoClippy") {
    dependsOn("assemble")
}

tasks.register<RunExampleTask>("runExample") {
    dependsOn("assemble")
    outputDir = sdkOutputDir
}

open class RunExampleTask @javax.inject.Inject constructor() : Exec() {
    @Option(option = "example", description = "Example to run")
    var example: String? = null
    set(value) {
        workingDir = workingDir.resolve(value!!)
        if (!workingDir.exists()) {
            throw kotlin.Exception("Example directory ${workingDir} does not exist")
        }
        field = value
    }

    @Input
    var outputDir: File? = null
        set(value) {
            workingDir = value!!.resolve("examples")
            commandLine = listOf("cargo", "run")
            field = value
        }
}

tasks["test"].finalizedBy("cargoCheck", "cargoClippy", "cargoTest", "cargoDocs")

tasks["clean"].doFirst {
+2 −0
Original line number Diff line number Diff line
target/
Cargo.lock
+16 −0
Original line number Diff line number Diff line
[package]
name = "dynamo-helloworld"
version = "0.1.0"
authors = ["Russell Cohen <rcoh@amazon.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
dynamodb = { version = "0.0.1", path = "../../build/aws-sdk/dynamodb" }
aws-auth = { path = "../../build/aws-sdk/aws-auth" }
aws-types = { path = "../../build/aws-sdk/aws-types" }
tokio = { version = "1", features = ["full"] }
smithy-http = { path = "../../build/aws-sdk/smithy-http" }
http = "0.2.3"
env_logger = "0.8.2"
+11 −0
Original line number Diff line number Diff line
# DynamoDB Hello World Example
This repo has a simple hello-world example for DynamoDB that will create a table if it doesn't exist & list tables present in the database.

By default, the code is written to target DynamoDB local—A docker compose file is provided for convenience. Usage:

```
docker-compose up -d
cargo run
```

The example can also be updated to run against production DynamoDB if credentials are provided.
Loading