|
6 | 6 | package aws.sdk.kotlin.gradle.customsdk |
7 | 7 |
|
8 | 8 | import org.gradle.api.Project |
| 9 | +import org.gradle.api.artifacts.Dependency |
9 | 10 | import org.gradle.api.provider.Property |
| 11 | +import org.gradle.api.provider.Provider |
10 | 12 |
|
11 | 13 | /** |
12 | 14 | * Extension for configuring the AWS Custom SDK Build plugin. |
13 | 15 | * |
14 | 16 | * Provides a type-safe DSL for specifying which AWS services and operations |
15 | 17 | * to include in the custom SDK build. |
16 | 18 | */ |
17 | | -abstract class AwsCustomSdkBuildExtension(private val project: Project) { |
18 | | - |
19 | | - /** |
20 | | - * AWS region for the generated clients (optional, defaults to us-east-1) |
21 | | - */ |
22 | | - abstract val region: Property<String> |
23 | | - |
| 19 | +abstract class AwsCustomSdkBuildExtension(internal val project: Project) { |
24 | 20 | /** |
25 | | - * Output directory for generated clients (optional, defaults to build/generated/aws-custom-sdk) |
| 21 | + * Output directory for generated clients. Defaults to `${project.layout.buildDirectory}/generated-src/aws-sdk`. |
26 | 22 | */ |
27 | 23 | abstract val outputDirectory: Property<String> |
28 | 24 |
|
29 | 25 | /** |
30 | | - * Package name for generated clients (optional, defaults to aws.sdk.kotlin.custom) |
31 | | - */ |
32 | | - abstract val packageName: Property<String> |
33 | | - |
34 | | - /** |
35 | | - * Enable strict validation of operations against generated constants (default: true) |
| 26 | + * Package name for generated clients. Defaults to `aws.sdk.kotlin.services`. |
36 | 27 | */ |
37 | | - abstract val strictValidation: Property<Boolean> |
38 | | - |
| 28 | + abstract val packageNamePrefix: Property<String> |
| 29 | + |
39 | 30 | /** |
40 | 31 | * Set of selected services with their operations |
41 | 32 | */ |
42 | | - private val selectedServices = mutableMapOf<String, MutableSet<String>>() |
43 | | - |
| 33 | + internal val selectedServices = mutableMapOf<String, Set<String>>() |
| 34 | + |
44 | 35 | init { |
45 | 36 | // Set default values |
46 | | - region.convention("us-east-1") |
47 | | - outputDirectory.convention("${project.buildDir}/generated/aws-custom-sdk") |
48 | | - packageName.convention("aws.sdk.kotlin.custom") |
49 | | - strictValidation.convention(true) |
| 37 | + outputDirectory.convention("${project.layout.buildDirectory}/generated-src/aws-sdk") |
| 38 | + packageNamePrefix.convention("aws.sdk.kotlin.services") |
50 | 39 | } |
51 | | - |
52 | | - /** |
53 | | - * Configure a service with selected operations using type-safe constants |
54 | | - * |
55 | | - * Example with type-safe constants: |
56 | | - * ``` |
57 | | - * service("lambda") { |
58 | | - * operations( |
59 | | - * LambdaOperations.CreateFunction, |
60 | | - * LambdaOperations.InvokeFunction, |
61 | | - * LambdaOperations.DeleteFunction |
62 | | - * ) |
63 | | - * } |
64 | | - * ``` |
65 | | - * |
66 | | - * Example with string literals (backward compatibility): |
67 | | - * ``` |
68 | | - * service("s3") { |
69 | | - * operations("GetObject", "PutObject") |
70 | | - * } |
71 | | - * ``` |
72 | | - */ |
73 | | - fun service(serviceName: String, configure: ServiceConfiguration.() -> Unit) { |
74 | | - val serviceConfig = ServiceConfiguration(serviceName, project) |
75 | | - serviceConfig.configure() |
76 | | - |
77 | | - val operations = serviceConfig.getOperations() |
78 | | - |
79 | | - // Validate operations if strict validation is enabled |
80 | | - if (strictValidation.get()) { |
81 | | - val validationResult = ConstantsRegistry.validateOperations(serviceName, operations) |
82 | | - if (!validationResult.isValid) { |
83 | | - project.logger.warn("AWS Custom SDK: ${validationResult.message}") |
84 | | - if (validationResult.invalidOperations.isNotEmpty()) { |
85 | | - project.logger.warn("Available operations for $serviceName: ${ConstantsRegistry.getServiceOperations(serviceName).sorted().joinToString(", ")}") |
86 | | - } |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - selectedServices[serviceName] = operations.toMutableSet() |
91 | | - } |
92 | | - |
93 | | - /** |
94 | | - * Configure multiple services using a DSL block |
95 | | - * |
96 | | - * Example: |
97 | | - * ``` |
98 | | - * services { |
99 | | - * lambda { |
100 | | - * operations(LambdaOperations.CreateFunction, LambdaOperations.InvokeFunction) |
101 | | - * } |
102 | | - * s3 { |
103 | | - * operations(S3Operations.GetObject, S3Operations.PutObject) |
104 | | - * } |
105 | | - * } |
106 | | - * ``` |
107 | | - */ |
108 | | - fun services(configure: ServicesConfiguration.() -> Unit) { |
109 | | - val servicesConfig = ServicesConfiguration(this) |
110 | | - servicesConfig.configure() |
111 | | - } |
112 | | - |
113 | | - /** |
114 | | - * Get all selected services and their operations |
115 | | - */ |
116 | | - fun getSelectedServices(): Map<String, Set<String>> = selectedServices.toMap() |
117 | | - |
118 | | - /** |
119 | | - * Get validation summary for all configured services |
120 | | - */ |
121 | | - fun getValidationSummary(): Map<String, ConstantsRegistry.ValidationResult> { |
122 | | - return selectedServices.mapValues { (serviceName, operations) -> |
123 | | - ConstantsRegistry.validateOperations(serviceName, operations) |
124 | | - } |
125 | | - } |
126 | | - |
127 | | - /** |
128 | | - * Configuration block for multiple services |
129 | | - */ |
130 | | - class ServicesConfiguration(private val extension: AwsCustomSdkBuildExtension) { |
131 | | - |
132 | | - fun lambda(configure: ServiceConfiguration.() -> Unit) { |
133 | | - extension.service("lambda", configure) |
134 | | - } |
135 | | - |
136 | | - fun s3(configure: ServiceConfiguration.() -> Unit) { |
137 | | - extension.service("s3", configure) |
138 | | - } |
139 | | - |
140 | | - fun dynamodb(configure: ServiceConfiguration.() -> Unit) { |
141 | | - extension.service("dynamodb", configure) |
142 | | - } |
143 | | - |
144 | | - fun apigateway(configure: ServiceConfiguration.() -> Unit) { |
145 | | - extension.service("apigateway", configure) |
146 | | - } |
147 | | - |
148 | | - /** |
149 | | - * Generic service configuration for services not explicitly defined |
150 | | - */ |
151 | | - fun service(serviceName: String, configure: ServiceConfiguration.() -> Unit) { |
152 | | - extension.service(serviceName, configure) |
153 | | - } |
154 | | - } |
155 | | - |
156 | | - /** |
157 | | - * Configuration block for a specific service |
158 | | - */ |
159 | | - class ServiceConfiguration(private val serviceName: String, private val project: Project) { |
160 | | - private val operations = mutableSetOf<String>() |
161 | | - |
162 | | - /** |
163 | | - * Add operations using type-safe constants or string literals |
164 | | - * |
165 | | - * Examples: |
166 | | - * ``` |
167 | | - * operations(LambdaOperations.CreateFunction, LambdaOperations.InvokeFunction) |
168 | | - * operations("CreateFunction", "InvokeFunction") |
169 | | - * ``` |
170 | | - */ |
171 | | - fun operations(vararg operationConstants: String) { |
172 | | - operations.addAll(operationConstants) |
173 | | - } |
174 | | - |
175 | | - /** |
176 | | - * Add operations using a collection of constants |
177 | | - */ |
178 | | - fun operations(operationConstants: Collection<String>) { |
179 | | - operations.addAll(operationConstants) |
180 | | - } |
181 | | - |
182 | | - /** |
183 | | - * Add all available operations for this service |
184 | | - */ |
185 | | - fun allOperations() { |
186 | | - val availableOps = ConstantsRegistry.getServiceOperations(serviceName) |
187 | | - if (availableOps.isNotEmpty()) { |
188 | | - operations.addAll(availableOps) |
189 | | - project.logger.info("Added all ${availableOps.size} operations for $serviceName service") |
190 | | - } else { |
191 | | - project.logger.warn("No operation constants available for $serviceName service") |
192 | | - } |
193 | | - } |
194 | | - |
195 | | - /** |
196 | | - * Add operations matching a pattern (regex) |
197 | | - */ |
198 | | - fun operationsMatching(pattern: String) { |
199 | | - val regex = Regex(pattern) |
200 | | - val availableOps = ConstantsRegistry.getServiceOperations(serviceName) |
201 | | - val matchingOps = availableOps.filter { regex.matches(it) } |
202 | | - operations.addAll(matchingOps) |
203 | | - project.logger.info("Added ${matchingOps.size} operations matching '$pattern' for $serviceName service") |
| 40 | + |
| 41 | + internal fun service(serviceName: String, operationNames: Set<String>): Provider<Dependency> { |
| 42 | + check(serviceName !in selectedServices) { |
| 43 | + "Service $serviceName configured multiple times. All service configuration should be in a single DSL block." |
204 | 44 | } |
205 | | - |
206 | | - internal fun getOperations(): Set<String> = operations.toSet() |
| 45 | + |
| 46 | + selectedServices[serviceName] = operationNames |
207 | 47 | } |
208 | 48 | } |
0 commit comments