1 /*
<lambda>null2  * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 import software.amazon.smithy.model.Model
17 import software.amazon.smithy.model.node.Node
18 import software.amazon.smithy.model.shapes.ServiceShape
19 import software.amazon.smithy.gradle.tasks.SmithyBuild
20 import software.amazon.smithy.aws.traits.ServiceTrait
21 import kotlin.streams.toList
22 
23 buildscript {
24     dependencies {
25         "classpath"("software.amazon.smithy:smithy-aws-traits:[1.5.1,2.0.0[")
26     }
27 }
28 
<lambda>null29 plugins {
30     id("software.amazon.smithy") version "0.5.3"
31 }
32 
<lambda>null33 dependencies {
34     implementation(project(":smithy-aws-go-codegen"))
35 }
36 
37 // This project doesn't produce a JAR.
38 tasks["jar"].enabled = false
39 
40 // Run the SmithyBuild task manually since this project needs the built JAR
41 // from smithy-aws-typescript-codegen.
42 tasks["smithyBuildJar"].enabled = false
43 
<lambda>null44 tasks.create<SmithyBuild>("buildSdk") {
45     addRuntimeClasspath = true
46 }
47 
48 // Generates a smithy-build.json file by creating a new projection for every
49 // JSON file found in aws-models/. The generated smithy-build.json file is
50 // not committed to git since it's rebuilt each time codegen is performed.
<lambda>null51 tasks.register("generate-smithy-build") {
52     doLast {
53         val projectionsBuilder = Node.objectNodeBuilder()
54         val modelsDirProp: String by project
55         val models = project.file(modelsDirProp);
56 
57         fileTree(models).filter { it.isFile }.files.forEach eachFile@{ file ->
58             val model = Model.assembler()
59                     .addImport(file.absolutePath)
60                     // Grab the result directly rather than worrying about checking for errors via unwrap.
61                     // All we care about here is the service shape, any unchecked errors will be exposed
62                     // as part of the actual build task done by the smithy gradle plugin.
63                     .assemble().result.get();
64             val services = model.shapes(ServiceShape::class.javaObjectType).sorted().toList();
65             if (services.size != 1) {
66                 throw Exception("There must be exactly one service in each aws model file, but found " +
67                         "${services.size} in ${file.name}: ${services.map { it.id }}");
68             }
69             val service = services[0]
70 
71             var filteredServices: String = System.getenv("SMITHY_GO_BUILD_API")?: ""
72             if (filteredServices.isNotEmpty()) {
73                 for (filteredService in filteredServices.split(",")) {
74                     if (!service.id.toString().startsWith(filteredService)) {
75                         return@eachFile
76                     }
77                 }
78             }
79 
80             val serviceTrait = service.getTrait(ServiceTrait::class.javaObjectType).get();
81 
82             val sdkId = serviceTrait.sdkId
83                     .replace("-", "")
84                     .replace(" ", "")
85                     .toLowerCase();
86             val projectionContents = Node.objectNodeBuilder()
87                     .withMember("imports", Node.fromStrings("${models.absolutePath}${File.separator}${file.name}"))
88                     .withMember("plugins", Node.objectNode()
89                             .withMember("go-codegen", Node.objectNodeBuilder()
90                                     .withMember("service", service.id.toString())
91                                     .withMember("module", "github.com/aws/aws-sdk-go-v2/service/$sdkId")
92                                     .build()))
93                     .build()
94             projectionsBuilder.withMember(sdkId + "." + service.version.toLowerCase(), projectionContents)
95         }
96 
97         file("smithy-build.json").writeText(Node.prettyPrintJson(Node.objectNodeBuilder()
98                 .withMember("version", "1.0")
99                 .withMember("projections", projectionsBuilder.build())
100                 .build()))
101     }
102 }
103 
104 // Run the `buildSdk` automatically.
105 tasks["build"]
106         .dependsOn(tasks["generate-smithy-build"])
107         .finalizedBy(tasks["buildSdk"])
108 
109 // ensure built artifacts are put into the SDK's folders
<lambda>null110 tasks.create<Exec>("copyGoCodegen") {
111     dependsOn ("buildSdk")
112     commandLine ("$rootDir/copy_go_codegen.sh", "$rootDir/..", (tasks["buildSdk"] as SmithyBuild).outputDirectory.absolutePath)
113 }
114 tasks["buildSdk"].finalizedBy(tasks["copyGoCodegen"])
115