Skip to content

Commit ff9c3d7

Browse files
trivikrsrchase
authored andcommitted
chore: use Paths.get() for computing paths (smithy-lang#458)
1 parent 6c5abd7 commit ff9c3d7

14 files changed

+83
-44
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

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

18+
import java.nio.file.Paths;
1819
import java.util.ArrayList;
1920
import java.util.Collection;
2021
import java.util.Comparator;
@@ -373,8 +374,8 @@ public Void serviceShape(ServiceShape shape) {
373374

374375
if (protocolGenerator != null) {
375376
LOGGER.info("Generating serde for protocol " + protocolGenerator.getName() + " on " + shape.getId());
376-
String fileName = CodegenUtils.SOURCE_FOLDER + "/protocols/"
377-
+ ProtocolGenerator.getSanitizedName(protocolGenerator.getName()) + ".ts";
377+
String fileName = Paths.get(CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
378+
ProtocolGenerator.getSanitizedName(protocolGenerator.getName()) + ".ts").toString();
378379
writers.useFileWriter(fileName, writer -> {
379380
ProtocolGenerator.GenerationContext context = new ProtocolGenerator.GenerationContext();
380381
context.setProtocolName(protocolGenerator.getName());

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static software.amazon.smithy.typescript.codegen.CodegenUtils.getBlobStreamingMembers;
1919
import static software.amazon.smithy.typescript.codegen.CodegenUtils.writeStreamingMemberType;
2020

21+
import java.nio.file.Paths;
2122
import java.util.List;
2223
import java.util.Map;
2324
import java.util.Optional;
@@ -45,6 +46,7 @@
4546
@SmithyInternalApi
4647
final class CommandGenerator implements Runnable {
4748

49+
static final String COMMANDS_FOLDER = "commands";
4850
static final String COMMAND_PROPERTIES_SECTION = "command_properties";
4951
static final String COMMAND_BODY_EXTRA_SECTION = "command_body_extra";
5052
static final String COMMAND_CONSTRUCTOR_SECTION = "command_constructor";
@@ -299,8 +301,8 @@ private void writeSerdeDispatcher(boolean isInput) {
299301
? ProtocolGenerator.getSerFunctionName(symbol, protocolGenerator.getName())
300302
: ProtocolGenerator.getDeserFunctionName(symbol, protocolGenerator.getName());
301303
writer.addImport(serdeFunctionName, serdeFunctionName,
302-
"./" + CodegenUtils.SOURCE_FOLDER + "/protocols/"
303-
+ ProtocolGenerator.getSanitizedName(protocolGenerator.getName()));
304+
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
305+
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())).toString());
304306
writer.write("return $L($L, context);", serdeFunctionName, isInput ? "input" : "output");
305307
}
306308
}
@@ -319,6 +321,8 @@ static void writeIndex(
319321
writer.write("export * from \"./$L\";", symbolProvider.toSymbol(operation).getName());
320322
}
321323

322-
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/commands/index.ts", writer.toString());
324+
fileManifest.writeFile(
325+
Paths.get(CodegenUtils.SOURCE_FOLDER, CommandGenerator.COMMANDS_FOLDER, "index.ts").toString(),
326+
writer.toString());
323327
}
324328
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.UnsupportedEncodingException;
2121
import java.net.URLDecoder;
2222
import java.nio.charset.StandardCharsets;
23+
import java.nio.file.Paths;
2324
import java.util.List;
2425
import java.util.Locale;
2526
import java.util.Map;
@@ -407,7 +408,8 @@ private void setupStubService(Symbol operationSymbol,
407408
});
408409

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

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

747749
writer.addImport("serializeFrameworkException", null,
748-
"./" + CodegenUtils.SOURCE_FOLDER + "/protocols/"
749-
+ ProtocolGenerator.getSanitizedName(protocolGenerator.getName()));
750+
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
751+
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())).toString());
750752
writer.addImport("ValidationFailure", "__ValidationFailure", "@aws-smithy/server-common");
751753
writer.write("const handler = new $T(service, testMux, serFn, serializeFrameworkException, "
752754
+ "(ctx: {}, f: __ValidationFailure[]) => { if (f) { throw f; } return undefined;});", handlerSymbol);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

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

18+
import java.nio.file.Paths;
1819
import java.util.ArrayList;
1920
import java.util.List;
2021
import software.amazon.smithy.build.FileManifest;
@@ -56,7 +57,7 @@ static void writeIndex(
5657

5758
// write export statement for models
5859
writer.write("export * from \"./models\";");
59-
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/index.ts", writer.toString());
60+
fileManifest.writeFile(Paths.get(CodegenUtils.SOURCE_FOLDER, "index.ts").toString(), writer.toString());
6061
}
6162

6263
private static void writeProtocolExports(ProtocolGenerator protocolGenerator, TypeScriptWriter writer) {
@@ -78,7 +79,9 @@ static void writeServerIndex(
7879
writer.write("export * from \"./operations\";");
7980

8081
writer.write("export * from \"./$L\"", symbol.getName());
81-
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/server/index.ts", writer.toString());
82+
fileManifest.writeFile(
83+
Paths.get(CodegenUtils.SOURCE_FOLDER, ServerSymbolVisitor.SERVER_FOLDER, "index.ts").toString(),
84+
writer.toString());
8285
}
8386

8487
private static void writeClientExports(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

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

18+
import java.nio.file.Paths;
1819
import software.amazon.smithy.utils.SmithyUnstableApi;
1920

2021
/**
@@ -68,6 +69,6 @@ String getTemplateFileName() {
6869
abstract String getTemplateFileName();
6970

7071
String getTargetFilename() {
71-
return CodegenUtils.SOURCE_FOLDER + "/" + getTemplateFileName().replace(".template", "");
72+
return Paths.get(CodegenUtils.SOURCE_FOLDER, getTemplateFileName().replace(".template", "")).toString();
7273
}
7374
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import java.nio.file.Paths;
1920
import java.util.Optional;
2021
import java.util.Set;
2122
import java.util.TreeSet;
@@ -35,7 +36,9 @@
3536
@SmithyInternalApi
3637
final class PaginationGenerator implements Runnable {
3738

38-
static final String PAGINATION_INTERFACE_FILE = CodegenUtils.SOURCE_FOLDER + "/pagination/Interfaces.ts";
39+
static final String PAGINATION_FOLDER = "pagination";
40+
static final String PAGINATION_INTERFACE_FILE =
41+
Paths.get(CodegenUtils.SOURCE_FOLDER, PAGINATION_FOLDER, "Interfaces.ts").toString();
3942

4043
private final TypeScriptWriter writer;
4144
private final PaginationInfo paginatedInfo;
@@ -99,15 +102,17 @@ public void run() {
99102

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

104108
writeCommandRequest();
105109
writeMethodRequest();
106110
writePager();
107111
}
108112

109113
static String getOutputFilelocation(OperationShape operation) {
110-
return CodegenUtils.SOURCE_FOLDER + "/pagination/" + operation.getId().getName() + "Paginator.ts";
114+
return Paths.get(CodegenUtils.SOURCE_FOLDER, PAGINATION_FOLDER,
115+
operation.getId().getName() + "Paginator.ts").toString();
111116
}
112117

113118
static void generateServicePaginationInterfaces(
@@ -150,7 +155,9 @@ static void writeIndex(
150155
}
151156
}
152157

153-
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/pagination/index.ts", writer.toString());
158+
fileManifest.writeFile(
159+
Paths.get(CodegenUtils.SOURCE_FOLDER, PAGINATION_FOLDER, "index.ts").toString(),
160+
writer.toString());
154161
}
155162

156163
private String destructurePath(String path) {

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static software.amazon.smithy.typescript.codegen.CodegenUtils.getBlobStreamingMembers;
1919
import static software.amazon.smithy.typescript.codegen.CodegenUtils.writeStreamingMemberType;
2020

21+
import java.nio.file.Paths;
2122
import java.util.Collections;
2223
import java.util.Iterator;
2324
import java.util.List;
@@ -43,6 +44,8 @@
4344
@SmithyInternalApi
4445
final class ServerCommandGenerator implements Runnable {
4546

47+
static final String COMMANDS_FOLDER = "operations";
48+
4649
private final TypeScriptSettings settings;
4750
private final Model model;
4851
private final OperationShape operation;
@@ -175,12 +178,14 @@ private void writeOperationSerializer() {
175178
serializerName, serverSymbol, operationSymbol.getName(), errorsType, () -> {
176179
String serializerFunction = ProtocolGenerator.getGenericSerFunctionName(operationSymbol) + "Response";
177180
String deserializerFunction = ProtocolGenerator.getGenericDeserFunctionName(operationSymbol) + "Request";
181+
178182
writer.addImport(serializerFunction, null,
179-
"./" + CodegenUtils.SOURCE_FOLDER + "/protocols/"
180-
+ ProtocolGenerator.getSanitizedName(protocolGenerator.getName()));
183+
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
184+
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())).toString());
181185
writer.addImport(deserializerFunction, null,
182-
"./" + CodegenUtils.SOURCE_FOLDER + "/protocols/"
183-
+ ProtocolGenerator.getSanitizedName(protocolGenerator.getName()));
186+
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
187+
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())).toString());
188+
184189
writer.write("serialize = $L;", serializerFunction);
185190
writer.write("deserialize = $L;", deserializerFunction);
186191
writer.write("");
@@ -231,8 +236,8 @@ private void writeErrorHandlerCase(StructureShape error) {
231236
Symbol errorSymbol = symbolProvider.toSymbol(error);
232237
String serializerFunction = ProtocolGenerator.getGenericSerFunctionName(errorSymbol) + "Error";
233238
writer.addImport(serializerFunction, null,
234-
"./" + CodegenUtils.SOURCE_FOLDER + "/protocols/"
235-
+ ProtocolGenerator.getSanitizedName(protocolGenerator.getName()));
239+
Paths.get(".", CodegenUtils.SOURCE_FOLDER, ProtocolGenerator.PROTOCOLS_FOLDER,
240+
ProtocolGenerator.getSanitizedName(protocolGenerator.getName())).toString());
236241
writer.openBlock("case $S: {", "}", error.getId().getName(), () -> {
237242
writer.write("return $L(error, ctx);", serializerFunction);
238243
});
@@ -252,6 +257,9 @@ static void writeIndex(
252257
writer.write("export * from \"./$L\";", symbolProvider.toSymbol(operation).getName());
253258
}
254259

255-
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/server/operations/index.ts", writer.toString());
260+
fileManifest.writeFile(
261+
Paths.get(CodegenUtils.SOURCE_FOLDER, ServerSymbolVisitor.SERVER_FOLDER, COMMANDS_FOLDER, "index.ts")
262+
.toString(),
263+
writer.toString());
256264
}
257265
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

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

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

4345
private final Model model;
@@ -118,8 +120,8 @@ private String flattenShapeName(ToShapeId id) {
118120
}
119121

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

140142
public String formatModuleName(Shape shape, String name) {
141143
if (shape.getType() == ShapeType.SERVICE) {
142-
return "/server/" + name;
144+
return Paths.get(SERVER_FOLDER, name).toString();
143145
} else if (shape.getType() == ShapeType.OPERATION) {
144-
return "/server/operations/" + name;
146+
return Paths.get(SERVER_FOLDER, ServerCommandGenerator.COMMANDS_FOLDER, name).toString();
145147
}
146148

147149
throw new IllegalArgumentException("Unsupported shape type: " + shape.getType());

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

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

18+
import java.nio.file.Paths;
1819
import java.util.Comparator;
1920
import java.util.List;
2021
import java.util.Map;
@@ -93,7 +94,7 @@ static String getResolvedConfigTypeName(Symbol symbol) {
9394
public void run() {
9495
writer.addImport("Client", "__Client", "@aws-sdk/smithy-client");
9596
writer.addImport("getRuntimeConfig", "__getRuntimeConfig",
96-
"./" + CodegenUtils.SOURCE_FOLDER + "/runtimeConfig");
97+
Paths.get(".", CodegenUtils.SOURCE_FOLDER, "runtimeConfig").toString());
9798

9899
// Normalize the input and output types of the command to account for
99100
// things like an operation adding input where there once wasn't any

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static java.lang.String.format;
1919

20+
import java.nio.file.Paths;
2021
import java.util.Comparator;
2122
import java.util.HashMap;
2223
import java.util.HashSet;
@@ -407,8 +408,8 @@ private Symbol.Builder createSymbolBuilder(Shape shape, String typeName, String
407408
}
408409

409410
private Symbol.Builder createGeneratedSymbolBuilder(Shape shape, String typeName, String namespace) {
410-
String prefixedNamespace = "./" + CodegenUtils.SOURCE_FOLDER
411-
+ (namespace.startsWith(".") ? namespace.substring(1) : namespace);
411+
String prefixedNamespace = Paths.get(".", CodegenUtils.SOURCE_FOLDER,
412+
(namespace.startsWith(".") ? namespace.substring(1) : namespace)).toString();
412413
return createSymbolBuilder(shape, typeName, prefixedNamespace)
413414
.definitionFile(toFilename(prefixedNamespace));
414415
}
@@ -423,7 +424,7 @@ private String toFilename(String namespace) {
423424
*/
424425
static final class ModuleNameDelegator {
425426
static final int DEFAULT_CHUNK_SIZE = 300;
426-
static final String SHAPE_NAMESPACE_PREFIX = "/models/";
427+
static final String SHAPE_NAMESPACE_PREFIX = "models";
427428

428429
private final Map<Shape, String> visitedModels = new HashMap<>();
429430
private int bucketCount = 0;
@@ -437,14 +438,14 @@ static final class ModuleNameDelegator {
437438
public String formatModuleName(Shape shape, String name) {
438439
// All shapes except for the service and operations are stored in models.
439440
if (shape.getType() == ShapeType.SERVICE) {
440-
return "./" + name;
441+
return Paths.get(".", name).toString();
441442
} else if (shape.getType() == ShapeType.OPERATION) {
442-
return "./commands/" + name;
443+
return Paths.get(".", CommandGenerator.COMMANDS_FOLDER, name).toString();
443444
} else if (visitedModels.containsKey(shape)) {
444445
return visitedModels.get(shape);
445446
}
446447
// Add models into buckets no bigger than chunk size.
447-
String path = "." + SHAPE_NAMESPACE_PREFIX + "models_" + bucketCount;
448+
String path = Paths.get(".", SHAPE_NAMESPACE_PREFIX, "models_" + bucketCount).toString();
448449
visitedModels.put(shape, path);
449450
currentBucketSize++;
450451
if (currentBucketSize == chunkSize) {
@@ -456,15 +457,17 @@ public String formatModuleName(Shape shape, String name) {
456457

457458
static void writeModelIndex(Model model, SymbolProvider symbolProvider, FileManifest fileManifest) {
458459
TypeScriptWriter writer = new TypeScriptWriter("");
459-
String modelPrefix = "./" + CodegenUtils.SOURCE_FOLDER + SHAPE_NAMESPACE_PREFIX;
460+
String modelPrefix = Paths.get(".", CodegenUtils.SOURCE_FOLDER, SHAPE_NAMESPACE_PREFIX).toString();
460461
model.shapes()
461462
.map(shape -> symbolProvider.toSymbol(shape).getNamespace())
462463
.filter(namespace -> namespace.startsWith(modelPrefix))
463464
.distinct()
464465
.sorted(Comparator.naturalOrder())
465466
.forEach(namespace -> writer.write(
466-
"export * from $S;", namespace.replaceFirst(modelPrefix, "./")));
467-
fileManifest.writeFile(CodegenUtils.SOURCE_FOLDER + "/models/index.ts", writer.toString());
467+
"export * from $S;", namespace.replaceFirst(modelPrefix, ".")));
468+
fileManifest.writeFile(
469+
Paths.get(CodegenUtils.SOURCE_FOLDER, SHAPE_NAMESPACE_PREFIX, "index.ts").toString(),
470+
writer.toString());
468471
}
469472
}
470473
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ void useShapeWriter(Shape shape, SymbolProvider provider, Consumer<TypeScriptWri
9090
// Checkout/create the appropriate writer for the shape.
9191
Symbol symbol = provider.toSymbol(shape);
9292
String fileName = symbol.getDefinitionFile();
93-
if (!fileName.startsWith("./" + CodegenUtils.SOURCE_FOLDER)) {
94-
fileName = "./" + CodegenUtils.SOURCE_FOLDER + "/" + fileName;
93+
if (!fileName.startsWith(Paths.get(".", CodegenUtils.SOURCE_FOLDER).toString())) {
94+
fileName = Paths.get(".", CodegenUtils.SOURCE_FOLDER, fileName).toString();
9595
}
9696
TypeScriptWriter writer = checkoutWriter(fileName);
9797

0 commit comments

Comments
 (0)