Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.smithy.typescript.codegen;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
Expand Down Expand Up @@ -373,8 +374,8 @@ public Void serviceShape(ServiceShape shape) {

if (protocolGenerator != null) {
LOGGER.info("Generating serde for protocol " + protocolGenerator.getName() + " on " + shape.getId());
String fileName = CodegenUtils.SOURCE_FOLDER + "/protocols/"
+ ProtocolGenerator.getSanitizedName(protocolGenerator.getName()) + ".ts";
String fileName = Paths.get(CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
ProtocolGenerator.getSanitizedName(protocolGenerator.getName()) + ".ts").toString();
writers.useFileWriter(fileName, writer -> {
ProtocolGenerator.GenerationContext context = new ProtocolGenerator.GenerationContext();
context.setProtocolName(protocolGenerator.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static software.amazon.smithy.typescript.codegen.CodegenUtils.getBlobStreamingMembers;
import static software.amazon.smithy.typescript.codegen.CodegenUtils.writeStreamingMemberType;

import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -45,6 +46,7 @@
@SmithyInternalApi
final class CommandGenerator implements Runnable {

static final String COMMANDS_FOLDER = "commands";
static final String COMMAND_PROPERTIES_SECTION = "command_properties";
static final String COMMAND_BODY_EXTRA_SECTION = "command_body_extra";
static final String COMMAND_CONSTRUCTOR_SECTION = "command_constructor";
Expand Down Expand Up @@ -299,8 +301,8 @@ private void writeSerdeDispatcher(boolean isInput) {
? ProtocolGenerator.getSerFunctionName(symbol, protocolGenerator.getName())
: ProtocolGenerator.getDeserFunctionName(symbol, protocolGenerator.getName());
writer.addImport(serdeFunctionName, serdeFunctionName,
"./" + CodegenUtils.SOURCE_FOLDER + "/protocols/"
+ ProtocolGenerator.getSanitizedName(protocolGenerator.getName()));
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())).toString());
writer.write("return $L($L, context);", serdeFunctionName, isInput ? "input" : "output");
}
}
Expand All @@ -319,6 +321,8 @@ static void writeIndex(
writer.write("export * from \"./$L\";", symbolProvider.toSymbol(operation).getName());
}

fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/commands/index.ts", writer.toString());
fileManifest.writeFile(
Paths.get(CodegenUtils.SOURCE_FOLDER, CommandGenerator.COMMANDS_FOLDER, "index.ts").toString(),
writer.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -407,7 +408,8 @@ private void setupStubService(Symbol operationSymbol,
});

String getHandlerName = "get" + handlerSymbol.getName();
writer.addImport(getHandlerName, null, "./" + CodegenUtils.SOURCE_FOLDER + "/server/");
writer.addImport(getHandlerName, null,
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ServerSymbolVisitor.SERVER_FOLDER).toString());

if (!usesDefaultValidation) {
writer.addImport("ValidationFailure", "__ValidationFailure", "@aws-smithy/server-common");
Expand Down Expand Up @@ -745,8 +747,8 @@ private void writeServerResponseTest(OperationShape operation, HttpResponseTestC
+ " { return new TestSerializer(); };", serviceOperationsSymbol, serviceSymbol);

writer.addImport("serializeFrameworkException", null,
"./" + CodegenUtils.SOURCE_FOLDER + "/protocols/"
+ ProtocolGenerator.getSanitizedName(protocolGenerator.getName()));
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())).toString());
writer.addImport("ValidationFailure", "__ValidationFailure", "@aws-smithy/server-common");
writer.write("const handler = new $T(service, testMux, serFn, serializeFrameworkException, "
+ "(ctx: {}, f: __ValidationFailure[]) => { if (f) { throw f; } return undefined;});", handlerSymbol);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.smithy.typescript.codegen;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import software.amazon.smithy.build.FileManifest;
Expand Down Expand Up @@ -59,7 +60,7 @@ static void writeIndex(

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

private static void writeProtocolExports(ProtocolGenerator protocolGenerator, TypeScriptWriter writer) {
Expand All @@ -81,7 +82,9 @@ static void writeServerIndex(
writer.write("export * from \"./operations\";");

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

private static void writeClientExports(
Expand Down Expand Up @@ -123,6 +126,6 @@ private static void writeClientExports(
for (TypeScriptIntegration integration : integrations) {
integration.writeAdditionalExports(settings, model, symbolProvider, writer);
}
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/index.ts", writer.toString());
fileManifest.writeFile(Paths.get(CodegenUtils.SOURCE_FOLDER, "index.ts").toString(), writer.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.smithy.typescript.codegen;

import java.nio.file.Paths;
import software.amazon.smithy.utils.SmithyUnstableApi;

/**
Expand Down Expand Up @@ -68,6 +69,6 @@ String getTemplateFileName() {
abstract String getTemplateFileName();

String getTargetFilename() {
return CodegenUtils.SOURCE_FOLDER + "/" + getTemplateFileName().replace(".template", "");
return Paths.get(CodegenUtils.SOURCE_FOLDER, getTemplateFileName().replace(".template", "")).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package software.amazon.smithy.typescript.codegen;

import java.nio.file.Paths;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
Expand All @@ -35,7 +36,9 @@
@SmithyInternalApi
final class PaginationGenerator implements Runnable {

static final String PAGINATION_INTERFACE_FILE = CodegenUtils.SOURCE_FOLDER + "/pagination/Interfaces.ts";
static final String PAGINATION_FOLDER = "pagination";
static final String PAGINATION_INTERFACE_FILE =
Paths.get(CodegenUtils.SOURCE_FOLDER, PAGINATION_FOLDER, "Interfaces.ts").toString();

private final TypeScriptWriter writer;
private final PaginationInfo paginatedInfo;
Expand Down Expand Up @@ -99,15 +102,17 @@ public void run() {

// Import Pagination types
writer.addImport("Paginator", "Paginator", "@aws-sdk/types");
writer.addImport(paginationType, paginationType, "./" + PAGINATION_INTERFACE_FILE.replace(".ts", ""));
writer.addImport(paginationType, paginationType,
Paths.get(".", PAGINATION_INTERFACE_FILE.replace(".ts", "")).toString());

writeCommandRequest();
writeMethodRequest();
writePager();
}

static String getOutputFilelocation(OperationShape operation) {
return CodegenUtils.SOURCE_FOLDER + "/pagination/" + operation.getId().getName() + "Paginator.ts";
return Paths.get(CodegenUtils.SOURCE_FOLDER, PAGINATION_FOLDER,
operation.getId().getName() + "Paginator.ts").toString();
}

static void generateServicePaginationInterfaces(
Expand Down Expand Up @@ -150,7 +155,9 @@ static void writeIndex(
}
}

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

private String destructurePath(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static software.amazon.smithy.typescript.codegen.CodegenUtils.getBlobStreamingMembers;
import static software.amazon.smithy.typescript.codegen.CodegenUtils.writeStreamingMemberType;

import java.nio.file.Paths;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand All @@ -43,6 +44,8 @@
@SmithyInternalApi
final class ServerCommandGenerator implements Runnable {

static final String COMMANDS_FOLDER = "operations";

private final TypeScriptSettings settings;
private final Model model;
private final OperationShape operation;
Expand Down Expand Up @@ -175,12 +178,14 @@ private void writeOperationSerializer() {
serializerName, serverSymbol, operationSymbol.getName(), errorsType, () -> {
String serializerFunction = ProtocolGenerator.getGenericSerFunctionName(operationSymbol) + "Response";
String deserializerFunction = ProtocolGenerator.getGenericDeserFunctionName(operationSymbol) + "Request";

writer.addImport(serializerFunction, null,
"./" + CodegenUtils.SOURCE_FOLDER + "/protocols/"
+ ProtocolGenerator.getSanitizedName(protocolGenerator.getName()));
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())).toString());
writer.addImport(deserializerFunction, null,
"./" + CodegenUtils.SOURCE_FOLDER + "/protocols/"
+ ProtocolGenerator.getSanitizedName(protocolGenerator.getName()));
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())).toString());

writer.write("serialize = $L;", serializerFunction);
writer.write("deserialize = $L;", deserializerFunction);
writer.write("");
Expand Down Expand Up @@ -231,8 +236,8 @@ private void writeErrorHandlerCase(StructureShape error) {
Symbol errorSymbol = symbolProvider.toSymbol(error);
String serializerFunction = ProtocolGenerator.getGenericSerFunctionName(errorSymbol) + "Error";
writer.addImport(serializerFunction, null,
"./" + CodegenUtils.SOURCE_FOLDER + "/protocols/"
+ ProtocolGenerator.getSanitizedName(protocolGenerator.getName()));
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())).toString());
writer.openBlock("case $S: {", "}", error.getId().getName(), () -> {
writer.write("return $L(error, ctx);", serializerFunction);
});
Expand All @@ -252,6 +257,9 @@ static void writeIndex(
writer.write("export * from \"./$L\";", symbolProvider.toSymbol(operation).getName());
}

fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/server/operations/index.ts", writer.toString());
fileManifest.writeFile(
Paths.get(CodegenUtils.SOURCE_FOLDER, ServerSymbolVisitor.SERVER_FOLDER, COMMANDS_FOLDER, "index.ts")
.toString(),
writer.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static software.amazon.smithy.typescript.codegen.TypeScriptDependency.SERVER_COMMON;

import java.nio.file.Paths;
import java.util.logging.Logger;
import software.amazon.smithy.codegen.core.ReservedWordSymbolProvider;
import software.amazon.smithy.codegen.core.ReservedWords;
Expand All @@ -38,6 +39,7 @@
*/
@SmithyInternalApi
final class ServerSymbolVisitor extends ShapeVisitor.Default<Symbol> implements SymbolProvider {
static final String SERVER_FOLDER = "server";
private static final Logger LOGGER = Logger.getLogger(ServerSymbolVisitor.class.getName());

private final Model model;
Expand Down Expand Up @@ -118,8 +120,8 @@ private String flattenShapeName(ToShapeId id) {
}

private Symbol.Builder createGeneratedSymbolBuilder(Shape shape, String typeName, String namespace) {
String prefixedNamespace = "./" + CodegenUtils.SOURCE_FOLDER
+ (namespace.startsWith(".") ? namespace.substring(1) : namespace);
String prefixedNamespace = Paths.get(".", CodegenUtils.SOURCE_FOLDER,
(namespace.startsWith(".") ? namespace.substring(1) : namespace)).toString();
return createSymbolBuilder(shape, typeName, prefixedNamespace)
.definitionFile(toFilename(prefixedNamespace));
}
Expand All @@ -139,9 +141,9 @@ static final class ModuleNameDelegator {

public String formatModuleName(Shape shape, String name) {
if (shape.getType() == ShapeType.SERVICE) {
return "/server/" + name;
return Paths.get(SERVER_FOLDER, name).toString();
} else if (shape.getType() == ShapeType.OPERATION) {
return "/server/operations/" + name;
return Paths.get(SERVER_FOLDER, ServerCommandGenerator.COMMANDS_FOLDER, name).toString();
}

throw new IllegalArgumentException("Unsupported shape type: " + shape.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.smithy.typescript.codegen;

import java.nio.file.Paths;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -93,7 +94,7 @@ static String getResolvedConfigTypeName(Symbol symbol) {
public void run() {
writer.addImport("Client", "__Client", "@aws-sdk/smithy-client");
writer.addImport("getRuntimeConfig", "__getRuntimeConfig",
"./" + CodegenUtils.SOURCE_FOLDER + "/runtimeConfig");
Paths.get(".", CodegenUtils.SOURCE_FOLDER, "runtimeConfig").toString());

// Normalize the input and output types of the command to account for
// things like an operation adding input where there once wasn't any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static java.lang.String.format;

import java.nio.file.Paths;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -407,8 +408,8 @@ private Symbol.Builder createSymbolBuilder(Shape shape, String typeName, String
}

private Symbol.Builder createGeneratedSymbolBuilder(Shape shape, String typeName, String namespace) {
String prefixedNamespace = "./" + CodegenUtils.SOURCE_FOLDER
+ (namespace.startsWith(".") ? namespace.substring(1) : namespace);
String prefixedNamespace = Paths.get(".", CodegenUtils.SOURCE_FOLDER,
(namespace.startsWith(".") ? namespace.substring(1) : namespace)).toString();
return createSymbolBuilder(shape, typeName, prefixedNamespace)
.definitionFile(toFilename(prefixedNamespace));
}
Expand All @@ -423,7 +424,7 @@ private String toFilename(String namespace) {
*/
static final class ModuleNameDelegator {
static final int DEFAULT_CHUNK_SIZE = 300;
static final String SHAPE_NAMESPACE_PREFIX = "/models/";
static final String SHAPE_NAMESPACE_PREFIX = "models";

private final Map<Shape, String> visitedModels = new HashMap<>();
private int bucketCount = 0;
Expand All @@ -437,14 +438,14 @@ static final class ModuleNameDelegator {
public String formatModuleName(Shape shape, String name) {
// All shapes except for the service and operations are stored in models.
if (shape.getType() == ShapeType.SERVICE) {
return "./" + name;
return Paths.get(".", name).toString();
} else if (shape.getType() == ShapeType.OPERATION) {
return "./commands/" + name;
return Paths.get(".", CommandGenerator.COMMANDS_FOLDER, name).toString();
} else if (visitedModels.containsKey(shape)) {
return visitedModels.get(shape);
}
// Add models into buckets no bigger than chunk size.
String path = "." + SHAPE_NAMESPACE_PREFIX + "models_" + bucketCount;
String path = Paths.get(".", SHAPE_NAMESPACE_PREFIX, "models_" + bucketCount).toString();
visitedModels.put(shape, path);
currentBucketSize++;
if (currentBucketSize == chunkSize) {
Expand All @@ -456,15 +457,17 @@ public String formatModuleName(Shape shape, String name) {

static void writeModelIndex(Model model, SymbolProvider symbolProvider, FileManifest fileManifest) {
TypeScriptWriter writer = new TypeScriptWriter("");
String modelPrefix = "./" + CodegenUtils.SOURCE_FOLDER + SHAPE_NAMESPACE_PREFIX;
String modelPrefix = Paths.get(".", CodegenUtils.SOURCE_FOLDER, SHAPE_NAMESPACE_PREFIX).toString();
model.shapes()
.map(shape -> symbolProvider.toSymbol(shape).getNamespace())
.filter(namespace -> namespace.startsWith(modelPrefix))
.distinct()
.sorted(Comparator.naturalOrder())
.forEach(namespace -> writer.write(
"export * from $S;", namespace.replaceFirst(modelPrefix, "./")));
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/models/index.ts", writer.toString());
"export * from $S;", namespace.replaceFirst(modelPrefix, ".")));
fileManifest.writeFile(
Paths.get(CodegenUtils.SOURCE_FOLDER, SHAPE_NAMESPACE_PREFIX, "index.ts").toString(),
writer.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ void useShapeWriter(Shape shape, SymbolProvider provider, Consumer<TypeScriptWri
// Checkout/create the appropriate writer for the shape.
Symbol symbol = provider.toSymbol(shape);
String fileName = symbol.getDefinitionFile();
if (!fileName.startsWith("./" + CodegenUtils.SOURCE_FOLDER)) {
fileName = "./" + CodegenUtils.SOURCE_FOLDER + "/" + fileName;
if (!fileName.startsWith(Paths.get(".", CodegenUtils.SOURCE_FOLDER).toString())) {
fileName = Paths.get(".", CodegenUtils.SOURCE_FOLDER, fileName).toString();
}
TypeScriptWriter writer = checkoutWriter(fileName);

Expand Down
Loading