Skip to content

feat: export folders from index.ts #455

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 8 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -411,33 +411,30 @@ public Void serviceShape(ServiceShape shape) {
}

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

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

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

for (OperationShape operation : containedOperations) {
if (operation.hasTrait(PaginatedTrait.ID)) {
hasPaginatedOperation = true;
String outputFilename = PaginationGenerator.getOutputFilelocation(operation);
writers.useFileWriter(outputFilename, paginationWriter ->
new PaginationGenerator(model, service, operation, symbolProvider, paginationWriter,
nonModularName).run());
aggregatedClientName).run());
}
if (operation.hasTrait(WaitableTrait.ID)) {
WaitableTrait waitableTrait = operation.expectTrait(WaitableTrait.class);

waitableTrait.getWaiters().forEach((String waiterName, Waiter waiter) -> {
String outputFilename = WaiterGenerator.getOutputFileLocation(waiterName);
writers.useFileWriter(outputFilename, waiterWriter ->
Expand All @@ -447,13 +444,18 @@ private void generateClient(ServiceShape shape) {
}
}

if (hasPaginatedOperation) {
if (containedOperations.stream().anyMatch(operation -> operation.hasTrait(PaginatedTrait.ID))) {
PaginationGenerator.writeIndex(model, service, fileManifest);
writers.useFileWriter(PaginationGenerator.PAGINATION_INTERFACE_FILE, paginationWriter ->
PaginationGenerator.generateServicePaginationInterfaces(
nonModularName,
aggregatedClientName,
serviceSymbol,
paginationWriter));
}

if (containedOperations.stream().anyMatch(operation -> operation.hasTrait(WaitableTrait.ID))) {
WaiterGenerator.writeIndex(model, service, fileManifest);
}
}

private void generateServiceInterface(ServiceShape shape) {
Expand Down Expand Up @@ -487,12 +489,14 @@ private void generateCommands(ServiceShape shape) {
for (OperationShape operation : containedOperations) {
// Right now this only generates stubs
if (settings.generateClient()) {
CommandGenerator.writeIndex(model, service, symbolProvider, fileManifest);
writers.useShapeWriter(operation, commandWriter -> new CommandGenerator(
settings, model, operation, symbolProvider, commandWriter,
runtimePlugins, protocolGenerator, applicationProtocol).run());
}

if (settings.generateServerSdk()) {
ServerCommandGenerator.writeIndex(model, service, symbolProvider, fileManifest);
writers.useShapeWriter(operation, symbolProvider, commandWriter -> new ServerCommandGenerator(
settings, model, operation, symbolProvider, commandWriter,
protocolGenerator, applicationProtocol).run());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.OperationIndex;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
Expand Down Expand Up @@ -300,4 +304,21 @@ private void writeSerdeDispatcher(boolean isInput) {
writer.write("return $L($L, context);", serdeFunctionName, isInput ? "input" : "output");
}
}

static void writeIndex(
Model model,
ServiceShape service,
SymbolProvider symbolProvider,
FileManifest fileManifest
) {
TypeScriptWriter writer = new TypeScriptWriter("");

TopDownIndex topDownIndex = TopDownIndex.of(model);
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
for (OperationShape operation : containedOperations) {
writer.write("export * from \"./$L\";", symbolProvider.toSymbol(operation).getName());
}

fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/commands/index.ts", writer.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@

package software.amazon.smithy.typescript.codegen;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
Expand All @@ -30,7 +29,6 @@
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
import software.amazon.smithy.utils.SmithyInternalApi;
import software.amazon.smithy.waiters.WaitableTrait;
import software.amazon.smithy.waiters.Waiter;

/**
* Generates an index to export the service client and each command.
Expand Down Expand Up @@ -60,7 +58,7 @@ static void writeIndex(
}

// write export statement for models
writer.write("export * from \"./models/index\";");
writer.write("export * from \"./models\";");
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/index.ts", writer.toString());
}

Expand All @@ -76,23 +74,16 @@ static void writeServerIndex(
FileManifest fileManifest
) {
TypeScriptWriter writer = new TypeScriptWriter("");

ServiceShape service = settings.getService(model);
Symbol symbol = symbolProvider.toSymbol(service);

TopDownIndex topDownIndex = TopDownIndex.of(model);
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
for (OperationShape operation : containedOperations) {
writer.write("export * from \"./operations/$L\";", symbolProvider.toSymbol(operation).getName());
}
// Write export statement for operations.
writer.write("export * from \"./operations\";");

writer.write("export * from \"./$L\"", symbol.getName());
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/server/index.ts", writer.toString());
}

private static String getModulePath(String fileLocation) {
return fileLocation.replaceFirst(CodegenUtils.SOURCE_FOLDER, "").replace(".ts", "");
}

private static void writeClientExports(
TypeScriptSettings settings,
Model model,
Expand All @@ -104,35 +95,28 @@ private static void writeClientExports(
ServiceShape service = settings.getService(model);
Symbol symbol = symbolProvider.toSymbol(service);

// Write export statement for modular client
writer.write("export * from \"./" + symbol.getName() + "\";");
// Write export statement for bare-bones client.
writer.write("export * from \"./$L\";", symbol.getName());

// Write export statement for aggregated client.
String aggregatedClientName = symbol.getName().replace("Client", "");
writer.write("export * from \"./$L\";", aggregatedClientName);

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

// write export statements for each command in /commands directory
TopDownIndex topDownIndex = TopDownIndex.of(model);
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
boolean hasPaginatedOperation = false;
for (OperationShape operation : containedOperations) {
writer.write("export * from \"./commands/" + symbolProvider.toSymbol(operation).getName() + "\";");
if (operation.hasTrait(PaginatedTrait.ID)) {
hasPaginatedOperation = true;
String modulePath = getModulePath(PaginationGenerator.getOutputFilelocation(operation));
writer.write("export * from \".$L\";", modulePath);
}
if (operation.hasTrait(WaitableTrait.ID)) {
WaitableTrait waitableTrait = operation.expectTrait(WaitableTrait.class);
waitableTrait.getWaiters().forEach((String waiterName, Waiter waiter) -> {
String modulePath = getModulePath(WaiterGenerator.getOutputFileLocation(waiterName));
writer.write("export * from \".$L\";", modulePath);
});
}
List<OperationShape> operations = new ArrayList<OperationShape>();
operations.addAll(topDownIndex.getContainedOperations(service));

// Export pagination, if present.
if (operations.stream().anyMatch(operation -> operation.hasTrait(PaginatedTrait.ID))) {
writer.write("export * from \"./pagination\";");
}
if (hasPaginatedOperation) {
String modulePath = getModulePath(PaginationGenerator.PAGINATION_INTERFACE_FILE);
writer.write("export * from \".$L\";", modulePath);

// Export waiters, if present.
if (operations.stream().anyMatch(operation -> operation.hasTrait(WaitableTrait.ID))) {
writer.write("export * from \"./waiters\";");
}

// Write each custom export.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
package software.amazon.smithy.typescript.codegen;

import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.PaginatedIndex;
import software.amazon.smithy.model.knowledge.PaginationInfo;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.traits.PaginatedTrait;
import software.amazon.smithy.utils.SmithyInternalApi;

@SmithyInternalApi
Expand Down Expand Up @@ -121,7 +126,34 @@ static void generateServicePaginationInterfaces(
});
}

private String destructurePath(String path) {
private static String getModulePath(String fileLocation) {
return fileLocation.substring(
fileLocation.lastIndexOf("/") + 1,
fileLocation.length()
).replace(".ts", "");
}

static void writeIndex(
Model model,
ServiceShape service,
FileManifest fileManifest
) {
TypeScriptWriter writer = new TypeScriptWriter("");
writer.write("export * from \"./$L\"", getModulePath(PAGINATION_INTERFACE_FILE));

TopDownIndex topDownIndex = TopDownIndex.of(model);
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
for (OperationShape operation : containedOperations) {
if (operation.hasTrait(PaginatedTrait.ID)) {
String outputFilepath = PaginationGenerator.getOutputFilelocation(operation);
writer.write("export * from \"./$L\"", getModulePath(outputFilepath));
}
}

fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/pagination/index.ts", writer.toString());
}

private String destructurePath(String path) {
return "." + path.replace(".", "!.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.OperationIndex;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
import software.amazon.smithy.utils.SmithyInternalApi;
Expand Down Expand Up @@ -232,4 +237,21 @@ private void writeErrorHandlerCase(StructureShape error) {
writer.write("return $L(error, ctx);", serializerFunction);
});
}

static void writeIndex(
Model model,
ServiceShape service,
SymbolProvider symbolProvider,
FileManifest fileManifest
) {
TypeScriptWriter writer = new TypeScriptWriter("");

TopDownIndex topDownIndex = TopDownIndex.of(model);
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
for (OperationShape operation : containedOperations) {
writer.write("export * from \"./$L\";", symbolProvider.toSymbol(operation).getName());
}

fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/server/operations/index.ts", writer.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@

package software.amazon.smithy.typescript.codegen;

import java.util.Set;
import java.util.TreeSet;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.jmespath.JmespathExpression;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.utils.SmithyInternalApi;
import software.amazon.smithy.waiters.Acceptor;
import software.amazon.smithy.waiters.AcceptorState;
import software.amazon.smithy.waiters.Matcher;
import software.amazon.smithy.waiters.PathMatcher;
import software.amazon.smithy.waiters.WaitableTrait;
import software.amazon.smithy.waiters.Waiter;

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

private static String getModulePath(String fileLocation) {
return fileLocation.substring(
fileLocation.lastIndexOf("/") + 1,
fileLocation.length()
).replace(".ts", "");
}

static void writeIndex(
Model model,
ServiceShape service,
FileManifest fileManifest
) {
TypeScriptWriter writer = new TypeScriptWriter("");

TopDownIndex topDownIndex = TopDownIndex.of(model);
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
for (OperationShape operation : containedOperations) {
if (operation.hasTrait(WaitableTrait.ID)) {
WaitableTrait waitableTrait = operation.expectTrait(WaitableTrait.class);
waitableTrait.getWaiters().forEach((String waiterName, Waiter waiter) -> {
String outputFilepath = WaiterGenerator.getOutputFileLocation(waiterName);
writer.write("export * from \"./$L\"", getModulePath(outputFilepath));
});
}
}

fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/waiters/index.ts", writer.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void writeAdditionalExports(
String contents = manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/index.ts").get();
assertThat(contents, containsString("export * from \"./Example\";"));
assertThat(contents, containsString("export * from \"./ExampleClient\";"));
assertThat(contents, containsString("export * from \"./commands/GetFooCommand\";"));
assertThat(contents, containsString("export * from \"./models/index\";"));
assertThat(contents, containsString("export * from \"./commands\";"));
assertThat(contents, containsString("export * from \"./models\";"));
assertThat(contents, containsString("export * from \"./foo\";"));
}
}