Skip to content

Commit 2d95be8

Browse files
authored
feat: export folders from index.ts (#455)
1 parent 7fe47b9 commit 2d95be8

File tree

7 files changed

+149
-52
lines changed

7 files changed

+149
-52
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenVisitor.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -411,33 +411,30 @@ public Void serviceShape(ServiceShape shape) {
411411
}
412412

413413
private void generateClient(ServiceShape shape) {
414-
// Generate the modular service client.
414+
// Generate the bare-bones service client.
415415
writers.useShapeWriter(shape, writer -> new ServiceGenerator(
416416
settings, model, symbolProvider, writer, integrations, runtimePlugins, applicationProtocol).run());
417417

418-
// Generate the non-modular service client.
418+
// Generate the aggregated service client.
419419
Symbol serviceSymbol = symbolProvider.toSymbol(shape);
420-
String nonModularName = serviceSymbol.getName().replace("Client", "");
420+
String aggregatedClientName = serviceSymbol.getName().replace("Client", "");
421421
String filename = serviceSymbol.getDefinitionFile().replace("Client", "");
422422
writers.useFileWriter(filename, writer -> new NonModularServiceGenerator(
423-
settings, model, symbolProvider, nonModularName, writer, applicationProtocol).run());
423+
settings, model, symbolProvider, aggregatedClientName, writer, applicationProtocol).run());
424424

425425
// Generate each operation for the service.
426426
TopDownIndex topDownIndex = TopDownIndex.of(model);
427427
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
428-
boolean hasPaginatedOperation = false;
429428

430429
for (OperationShape operation : containedOperations) {
431430
if (operation.hasTrait(PaginatedTrait.ID)) {
432-
hasPaginatedOperation = true;
433431
String outputFilename = PaginationGenerator.getOutputFilelocation(operation);
434432
writers.useFileWriter(outputFilename, paginationWriter ->
435433
new PaginationGenerator(model, service, operation, symbolProvider, paginationWriter,
436-
nonModularName).run());
434+
aggregatedClientName).run());
437435
}
438436
if (operation.hasTrait(WaitableTrait.ID)) {
439437
WaitableTrait waitableTrait = operation.expectTrait(WaitableTrait.class);
440-
441438
waitableTrait.getWaiters().forEach((String waiterName, Waiter waiter) -> {
442439
String outputFilename = WaiterGenerator.getOutputFileLocation(waiterName);
443440
writers.useFileWriter(outputFilename, waiterWriter ->
@@ -447,13 +444,18 @@ private void generateClient(ServiceShape shape) {
447444
}
448445
}
449446

450-
if (hasPaginatedOperation) {
447+
if (containedOperations.stream().anyMatch(operation -> operation.hasTrait(PaginatedTrait.ID))) {
448+
PaginationGenerator.writeIndex(model, service, fileManifest);
451449
writers.useFileWriter(PaginationGenerator.PAGINATION_INTERFACE_FILE, paginationWriter ->
452450
PaginationGenerator.generateServicePaginationInterfaces(
453-
nonModularName,
451+
aggregatedClientName,
454452
serviceSymbol,
455453
paginationWriter));
456454
}
455+
456+
if (containedOperations.stream().anyMatch(operation -> operation.hasTrait(WaitableTrait.ID))) {
457+
WaiterGenerator.writeIndex(model, service, fileManifest);
458+
}
457459
}
458460

459461
private void generateServiceInterface(ServiceShape shape) {
@@ -487,12 +489,14 @@ private void generateCommands(ServiceShape shape) {
487489
for (OperationShape operation : containedOperations) {
488490
// Right now this only generates stubs
489491
if (settings.generateClient()) {
492+
CommandGenerator.writeIndex(model, service, symbolProvider, fileManifest);
490493
writers.useShapeWriter(operation, commandWriter -> new CommandGenerator(
491494
settings, model, operation, symbolProvider, commandWriter,
492495
runtimePlugins, protocolGenerator, applicationProtocol).run());
493496
}
494497

495498
if (settings.generateServerSdk()) {
499+
ServerCommandGenerator.writeIndex(model, service, symbolProvider, fileManifest);
496500
writers.useShapeWriter(operation, symbolProvider, commandWriter -> new ServerCommandGenerator(
497501
settings, model, operation, symbolProvider, commandWriter,
498502
protocolGenerator, applicationProtocol).run());

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.Optional;
24+
import java.util.Set;
25+
import java.util.TreeSet;
2426
import java.util.stream.Collectors;
27+
import software.amazon.smithy.build.FileManifest;
2528
import software.amazon.smithy.codegen.core.Symbol;
2629
import software.amazon.smithy.codegen.core.SymbolProvider;
2730
import software.amazon.smithy.model.Model;
2831
import software.amazon.smithy.model.knowledge.OperationIndex;
32+
import software.amazon.smithy.model.knowledge.TopDownIndex;
2933
import software.amazon.smithy.model.shapes.MemberShape;
3034
import software.amazon.smithy.model.shapes.OperationShape;
3135
import software.amazon.smithy.model.shapes.ServiceShape;
@@ -300,4 +304,21 @@ private void writeSerdeDispatcher(boolean isInput) {
300304
writer.write("return $L($L, context);", serdeFunctionName, isInput ? "input" : "output");
301305
}
302306
}
307+
308+
static void writeIndex(
309+
Model model,
310+
ServiceShape service,
311+
SymbolProvider symbolProvider,
312+
FileManifest fileManifest
313+
) {
314+
TypeScriptWriter writer = new TypeScriptWriter("");
315+
316+
TopDownIndex topDownIndex = TopDownIndex.of(model);
317+
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
318+
for (OperationShape operation : containedOperations) {
319+
writer.write("export * from \"./$L\";", symbolProvider.toSymbol(operation).getName());
320+
}
321+
322+
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/commands/index.ts", writer.toString());
323+
}
303324
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/IndexGenerator.java

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515

1616
package software.amazon.smithy.typescript.codegen;
1717

18+
import java.util.ArrayList;
1819
import java.util.List;
19-
import java.util.Set;
20-
import java.util.TreeSet;
2120
import software.amazon.smithy.build.FileManifest;
2221
import software.amazon.smithy.codegen.core.Symbol;
2322
import software.amazon.smithy.codegen.core.SymbolProvider;
@@ -30,7 +29,6 @@
3029
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
3130
import software.amazon.smithy.utils.SmithyInternalApi;
3231
import software.amazon.smithy.waiters.WaitableTrait;
33-
import software.amazon.smithy.waiters.Waiter;
3432

3533
/**
3634
* Generates an index to export the service client and each command.
@@ -60,7 +58,7 @@ static void writeIndex(
6058
}
6159

6260
// write export statement for models
63-
writer.write("export * from \"./models/index\";");
61+
writer.write("export * from \"./models\";");
6462
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/index.ts", writer.toString());
6563
}
6664

@@ -76,23 +74,16 @@ static void writeServerIndex(
7674
FileManifest fileManifest
7775
) {
7876
TypeScriptWriter writer = new TypeScriptWriter("");
79-
8077
ServiceShape service = settings.getService(model);
8178
Symbol symbol = symbolProvider.toSymbol(service);
8279

83-
TopDownIndex topDownIndex = TopDownIndex.of(model);
84-
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
85-
for (OperationShape operation : containedOperations) {
86-
writer.write("export * from \"./operations/$L\";", symbolProvider.toSymbol(operation).getName());
87-
}
80+
// Write export statement for operations.
81+
writer.write("export * from \"./operations\";");
82+
8883
writer.write("export * from \"./$L\"", symbol.getName());
8984
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/server/index.ts", writer.toString());
9085
}
9186

92-
private static String getModulePath(String fileLocation) {
93-
return fileLocation.replaceFirst(CodegenUtils.SOURCE_FOLDER, "").replace(".ts", "");
94-
}
95-
9687
private static void writeClientExports(
9788
TypeScriptSettings settings,
9889
Model model,
@@ -104,35 +95,28 @@ private static void writeClientExports(
10495
ServiceShape service = settings.getService(model);
10596
Symbol symbol = symbolProvider.toSymbol(service);
10697

107-
// Write export statement for modular client
108-
writer.write("export * from \"./" + symbol.getName() + "\";");
98+
// Write export statement for bare-bones client.
99+
writer.write("export * from \"./$L\";", symbol.getName());
100+
101+
// Write export statement for aggregated client.
102+
String aggregatedClientName = symbol.getName().replace("Client", "");
103+
writer.write("export * from \"./$L\";", aggregatedClientName);
109104

110-
// Get non-modular client and write its export statement
111-
String nonModularName = symbol.getName().replace("Client", "");
112-
writer.write("export * from \"./" + nonModularName + "\";");
105+
// Write export statement for commands.
106+
writer.write("export * from \"./commands\";");
113107

114-
// write export statements for each command in /commands directory
115108
TopDownIndex topDownIndex = TopDownIndex.of(model);
116-
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
117-
boolean hasPaginatedOperation = false;
118-
for (OperationShape operation : containedOperations) {
119-
writer.write("export * from \"./commands/" + symbolProvider.toSymbol(operation).getName() + "\";");
120-
if (operation.hasTrait(PaginatedTrait.ID)) {
121-
hasPaginatedOperation = true;
122-
String modulePath = getModulePath(PaginationGenerator.getOutputFilelocation(operation));
123-
writer.write("export * from \".$L\";", modulePath);
124-
}
125-
if (operation.hasTrait(WaitableTrait.ID)) {
126-
WaitableTrait waitableTrait = operation.expectTrait(WaitableTrait.class);
127-
waitableTrait.getWaiters().forEach((String waiterName, Waiter waiter) -> {
128-
String modulePath = getModulePath(WaiterGenerator.getOutputFileLocation(waiterName));
129-
writer.write("export * from \".$L\";", modulePath);
130-
});
131-
}
109+
List<OperationShape> operations = new ArrayList<OperationShape>();
110+
operations.addAll(topDownIndex.getContainedOperations(service));
111+
112+
// Export pagination, if present.
113+
if (operations.stream().anyMatch(operation -> operation.hasTrait(PaginatedTrait.ID))) {
114+
writer.write("export * from \"./pagination\";");
132115
}
133-
if (hasPaginatedOperation) {
134-
String modulePath = getModulePath(PaginationGenerator.PAGINATION_INTERFACE_FILE);
135-
writer.write("export * from \".$L\";", modulePath);
116+
117+
// Export waiters, if present.
118+
if (operations.stream().anyMatch(operation -> operation.hasTrait(WaitableTrait.ID))) {
119+
writer.write("export * from \"./waiters\";");
136120
}
137121

138122
// Write each custom export.

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/PaginationGenerator.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717
package software.amazon.smithy.typescript.codegen;
1818

1919
import java.util.Optional;
20+
import java.util.Set;
21+
import java.util.TreeSet;
22+
import software.amazon.smithy.build.FileManifest;
2023
import software.amazon.smithy.codegen.core.CodegenException;
2124
import software.amazon.smithy.codegen.core.Symbol;
2225
import software.amazon.smithy.codegen.core.SymbolProvider;
2326
import software.amazon.smithy.model.Model;
2427
import software.amazon.smithy.model.knowledge.PaginatedIndex;
2528
import software.amazon.smithy.model.knowledge.PaginationInfo;
29+
import software.amazon.smithy.model.knowledge.TopDownIndex;
2630
import software.amazon.smithy.model.shapes.OperationShape;
2731
import software.amazon.smithy.model.shapes.ServiceShape;
32+
import software.amazon.smithy.model.traits.PaginatedTrait;
2833
import software.amazon.smithy.utils.SmithyInternalApi;
2934

3035
@SmithyInternalApi
@@ -121,7 +126,34 @@ static void generateServicePaginationInterfaces(
121126
});
122127
}
123128

124-
private String destructurePath(String path) {
129+
private static String getModulePath(String fileLocation) {
130+
return fileLocation.substring(
131+
fileLocation.lastIndexOf("/") + 1,
132+
fileLocation.length()
133+
).replace(".ts", "");
134+
}
135+
136+
static void writeIndex(
137+
Model model,
138+
ServiceShape service,
139+
FileManifest fileManifest
140+
) {
141+
TypeScriptWriter writer = new TypeScriptWriter("");
142+
writer.write("export * from \"./$L\"", getModulePath(PAGINATION_INTERFACE_FILE));
143+
144+
TopDownIndex topDownIndex = TopDownIndex.of(model);
145+
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
146+
for (OperationShape operation : containedOperations) {
147+
if (operation.hasTrait(PaginatedTrait.ID)) {
148+
String outputFilepath = PaginationGenerator.getOutputFilelocation(operation);
149+
writer.write("export * from \"./$L\"", getModulePath(outputFilepath));
150+
}
151+
}
152+
153+
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/pagination/index.ts", writer.toString());
154+
}
155+
156+
private String destructurePath(String path) {
125157
return "." + path.replace(".", "!.");
126158
}
127159

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServerCommandGenerator.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@
2222
import java.util.Iterator;
2323
import java.util.List;
2424
import java.util.Optional;
25+
import java.util.Set;
26+
import java.util.TreeSet;
27+
import software.amazon.smithy.build.FileManifest;
2528
import software.amazon.smithy.codegen.core.Symbol;
2629
import software.amazon.smithy.codegen.core.SymbolProvider;
2730
import software.amazon.smithy.model.Model;
2831
import software.amazon.smithy.model.knowledge.OperationIndex;
32+
import software.amazon.smithy.model.knowledge.TopDownIndex;
2933
import software.amazon.smithy.model.shapes.MemberShape;
3034
import software.amazon.smithy.model.shapes.OperationShape;
35+
import software.amazon.smithy.model.shapes.ServiceShape;
3136
import software.amazon.smithy.model.shapes.StructureShape;
3237
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
3338
import software.amazon.smithy.utils.SmithyInternalApi;
@@ -232,4 +237,21 @@ private void writeErrorHandlerCase(StructureShape error) {
232237
writer.write("return $L(error, ctx);", serializerFunction);
233238
});
234239
}
240+
241+
static void writeIndex(
242+
Model model,
243+
ServiceShape service,
244+
SymbolProvider symbolProvider,
245+
FileManifest fileManifest
246+
) {
247+
TypeScriptWriter writer = new TypeScriptWriter("");
248+
249+
TopDownIndex topDownIndex = TopDownIndex.of(model);
250+
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
251+
for (OperationShape operation : containedOperations) {
252+
writer.write("export * from \"./$L\";", symbolProvider.toSymbol(operation).getName());
253+
}
254+
255+
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/server/operations/index.ts", writer.toString());
256+
}
235257
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/WaiterGenerator.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,23 @@
1515

1616
package software.amazon.smithy.typescript.codegen;
1717

18+
import java.util.Set;
19+
import java.util.TreeSet;
20+
import software.amazon.smithy.build.FileManifest;
1821
import software.amazon.smithy.codegen.core.CodegenException;
1922
import software.amazon.smithy.codegen.core.Symbol;
2023
import software.amazon.smithy.codegen.core.SymbolProvider;
2124
import software.amazon.smithy.jmespath.JmespathExpression;
25+
import software.amazon.smithy.model.Model;
26+
import software.amazon.smithy.model.knowledge.TopDownIndex;
2227
import software.amazon.smithy.model.shapes.OperationShape;
2328
import software.amazon.smithy.model.shapes.ServiceShape;
2429
import software.amazon.smithy.utils.SmithyInternalApi;
2530
import software.amazon.smithy.waiters.Acceptor;
2631
import software.amazon.smithy.waiters.AcceptorState;
2732
import software.amazon.smithy.waiters.Matcher;
2833
import software.amazon.smithy.waiters.PathMatcher;
34+
import software.amazon.smithy.waiters.WaitableTrait;
2935
import software.amazon.smithy.waiters.Waiter;
3036

3137
@SmithyInternalApi
@@ -193,4 +199,32 @@ private String makeWaiterResult(AcceptorState resultantState) {
193199
throw new CodegenException("Hit an invalid acceptor state to codegen " + resultantState.toString());
194200
}
195201

202+
private static String getModulePath(String fileLocation) {
203+
return fileLocation.substring(
204+
fileLocation.lastIndexOf("/") + 1,
205+
fileLocation.length()
206+
).replace(".ts", "");
207+
}
208+
209+
static void writeIndex(
210+
Model model,
211+
ServiceShape service,
212+
FileManifest fileManifest
213+
) {
214+
TypeScriptWriter writer = new TypeScriptWriter("");
215+
216+
TopDownIndex topDownIndex = TopDownIndex.of(model);
217+
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
218+
for (OperationShape operation : containedOperations) {
219+
if (operation.hasTrait(WaitableTrait.ID)) {
220+
WaitableTrait waitableTrait = operation.expectTrait(WaitableTrait.class);
221+
waitableTrait.getWaiters().forEach((String waiterName, Waiter waiter) -> {
222+
String outputFilepath = WaiterGenerator.getOutputFileLocation(waiterName);
223+
writer.write("export * from \"./$L\"", getModulePath(outputFilepath));
224+
});
225+
}
226+
}
227+
228+
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/waiters/index.ts", writer.toString());
229+
}
196230
}

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/IndexGeneratorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public void writeAdditionalExports(
4242
String contents = manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/index.ts").get();
4343
assertThat(contents, containsString("export * from \"./Example\";"));
4444
assertThat(contents, containsString("export * from \"./ExampleClient\";"));
45-
assertThat(contents, containsString("export * from \"./commands/GetFooCommand\";"));
46-
assertThat(contents, containsString("export * from \"./models/index\";"));
45+
assertThat(contents, containsString("export * from \"./commands\";"));
46+
assertThat(contents, containsString("export * from \"./models\";"));
4747
assertThat(contents, containsString("export * from \"./foo\";"));
4848
}
4949
}

0 commit comments

Comments
 (0)