Skip to content

Use DirectedCodegen #585

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 1 commit into from
Sep 16, 2022
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

This file was deleted.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,6 @@ public HttpProtocolTestGenerator(
this.context = context;
}

public HttpProtocolTestGenerator(
GenerationContext context,
ProtocolGenerator protocolGenerator,
TestFilter testFilter
) {
this(context, protocolGenerator, testFilter, (service, operation, testCase, typeScriptSettings) -> false);
}

public HttpProtocolTestGenerator(
GenerationContext context,
ProtocolGenerator protocolGenerator
) {
this(context, protocolGenerator, (service, operation, testCase, typeScriptSettings) -> false);
}

@Override
public void run() {
OperationIndex operationIndex = OperationIndex.of(model);
Expand Down Expand Up @@ -243,7 +228,7 @@ private void onlyIfProtocolMatches(HttpMalformedRequestTestCase testCase, Runnab

private void initializeWriterIfNeeded() {
if (writer == null) {
writer = context.getWriter();
context.getWriterDelegator().useFileWriter(createTestCaseFilename(), writer -> this.writer = writer);
writer.addDependency(TypeScriptDependency.AWS_SDK_TYPES);
writer.addDependency(TypeScriptDependency.AWS_SDK_PROTOCOL_HTTP);
// Add the template to each generated test.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@
import java.util.Set;
import java.util.TreeMap;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.ImportContainer;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.utils.Pair;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
* Internal class used for aggregating imports of a file.
*/
@SmithyInternalApi
final class ImportDeclarations {
final class ImportDeclarations implements ImportContainer {

private final String moduleNameString;
private final Path relativize;
private final Map<String, Pair<String, Ignore>> defaultImports = new TreeMap<>();
private final Map<String, Map<String, String>> namedImports = new TreeMap<>();
Expand All @@ -38,6 +41,7 @@ final class ImportDeclarations {
if (!relativize.startsWith("./")) {
relativize = "./" + relativize;
}
this.moduleNameString = relativize;

// Strip off the filename of what's being relativized since it isn't needed.
this.relativize = Paths.get(relativize).getParent();
Expand Down Expand Up @@ -75,6 +79,13 @@ ImportDeclarations addImport(String name, String alias, String module) {
return this;
}

@Override
public void importSymbol(Symbol symbol, String alias) {
if (!symbol.getNamespace().isEmpty() && !symbol.getNamespace().equals(moduleNameString)) {
addImport(symbol.getName(), alias, symbol.getNamespace());
}
}

@Override
public String toString() {
StringBuilder result = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.typescript.codegen;

import java.util.ArrayList;
import java.util.List;
import software.amazon.smithy.build.FileManifest;
import software.amazon.smithy.codegen.core.CodegenContext;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.SmithyUnstableApi;

/**
* Holds context related to code generation.
*/
@SmithyUnstableApi
public final class TypeScriptCodegenContext
implements CodegenContext<TypeScriptSettings, TypeScriptWriter, TypeScriptIntegration> {

private final Model model;
private final TypeScriptSettings settings;
private final SymbolProvider symbolProvider;
private final FileManifest fileManifest;
private final TypeScriptDelegator writerDelegator;
private final List<TypeScriptIntegration> integrations;
private final List<RuntimeClientPlugin> runtimePlugins;
private final ProtocolGenerator protocolGenerator;
private final ApplicationProtocol applicationProtocol;

private TypeScriptCodegenContext(Builder builder) {
model = SmithyBuilder.requiredState("model", builder.model);
settings = SmithyBuilder.requiredState("settings", builder.settings);
symbolProvider = SmithyBuilder.requiredState("symbolProvider", builder.symbolProvider);
fileManifest = SmithyBuilder.requiredState("fileManifest", builder.fileManifest);
writerDelegator = SmithyBuilder.requiredState("writerDelegator", builder.writerDelegator);
integrations = SmithyBuilder.requiredState("integrations", builder.integrations);
runtimePlugins = SmithyBuilder.requiredState("runtimePlugins", builder.runtimePlugins);
protocolGenerator = builder.protocolGenerator;
applicationProtocol = SmithyBuilder.requiredState("applicationProtocol", builder.applicationProtocol);
}

@Override
public Model model() {
return model;
}

@Override
public TypeScriptSettings settings() {
return settings;
}

@Override
public SymbolProvider symbolProvider() {
return symbolProvider;
}

@Override
public FileManifest fileManifest() {
return fileManifest;
}

@Override
public TypeScriptDelegator writerDelegator() {
return writerDelegator;
}

@Override
public List<TypeScriptIntegration> integrations() {
return integrations;
}

public List<RuntimeClientPlugin> runtimePlugins() {
return runtimePlugins;
}

public ProtocolGenerator protocolGenerator() {
return protocolGenerator;
}

public ApplicationProtocol applicationProtocol() {
return applicationProtocol;
}

/**
* @return Returns a builder.
*/
public static Builder builder() {
return new Builder();
}

/**
* Builds {@link TypeScriptCodegenContext}s.
*/
public static final class Builder implements SmithyBuilder<TypeScriptCodegenContext> {
private Model model;
private TypeScriptSettings settings;
private SymbolProvider symbolProvider;
private FileManifest fileManifest;
private TypeScriptDelegator writerDelegator;
private List<TypeScriptIntegration> integrations = new ArrayList<>();
private List<RuntimeClientPlugin> runtimePlugins = new ArrayList<>();
private ProtocolGenerator protocolGenerator;
private ApplicationProtocol applicationProtocol;

@Override
public TypeScriptCodegenContext build() {
return new TypeScriptCodegenContext(this);
}

/**
* @param model The model being generated.
* @return Returns the builder.
*/
public Builder model(Model model) {
this.model = model;
return this;
}

/**
* @param settings The resolved settings for the generator.
* @return Returns the builder.
*/
public Builder settings(TypeScriptSettings settings) {
this.settings = settings;
return this;
}

/**
* @param symbolProvider The finalized symbol provider for the generator.
* @return Returns the builder.
*/
public Builder symbolProvider(SymbolProvider symbolProvider) {
this.symbolProvider = symbolProvider;
return this;
}

/**
* @param fileManifest The file manifest being used in the generator.
* @return Returns the builder.
*/
public Builder fileManifest(FileManifest fileManifest) {
this.fileManifest = fileManifest;
return this;
}

/**
* @param writerDelegator The writer delegator to use in the generator.
* @return Returns the builder.
*/
public Builder writerDelegator(TypeScriptDelegator writerDelegator) {
this.writerDelegator = writerDelegator;
return this;
}

/**
* @param integrations The integrations to use in the generator.
* @return Returns the builder.
*/
public Builder integrations(List<TypeScriptIntegration> integrations) {
this.integrations.clear();
this.integrations.addAll(integrations);
return this;
}

/**
* @param runtimePlugins The runtime plugins to use in the generator.
* @return Returns the builder.
*/
public Builder runtimePlugins(List<RuntimeClientPlugin> runtimePlugins) {
this.runtimePlugins.clear();
this.runtimePlugins.addAll(runtimePlugins);
return this;
}

/**
* @param protocolGenerator The protocol generator to use in the generator.
* @return Returns the builder.
*/
public Builder protocolGenerator(ProtocolGenerator protocolGenerator) {
this.protocolGenerator = protocolGenerator;
return this;
}

/**
* @param applicationProtocol The application protocol to use in the generator.
* @return Returns the builder.
*/
public Builder applicationProtocol(ApplicationProtocol applicationProtocol) {
this.applicationProtocol = applicationProtocol;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@

import software.amazon.smithy.build.PluginContext;
import software.amazon.smithy.build.SmithyBuildPlugin;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings.ArtifactType;
import software.amazon.smithy.codegen.core.directed.CodegenDirector;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
Expand All @@ -35,11 +34,33 @@ public String getName() {

@Override
public void execute(PluginContext context) {
new CodegenVisitor(context, ArtifactType.CLIENT).execute();
}
CodegenDirector<TypeScriptWriter, TypeScriptIntegration, TypeScriptCodegenContext, TypeScriptSettings> runner
= new CodegenDirector<>();

runner.directedCodegen(new DirectedTypeScriptCodegen());

// Set the SmithyIntegration class to look for and apply using SPI.
runner.integrationClass(TypeScriptIntegration.class);

// Set the FileManifest and Model from the plugin.
runner.fileManifest(context.getFileManifest());
runner.model(context.getModel());

// Create the TypeScriptSettings object from the plugin settings.
TypeScriptSettings settings = TypeScriptSettings.from(context.getModel(), context.getSettings(),
TypeScriptSettings.ArtifactType.CLIENT);
runner.settings(settings);

runner.service(settings.getService());

// Configure the director to perform some common model transforms.
runner.performDefaultCodegenTransforms();

// TODO: Not using below because it would break existing AWS SDKs. Maybe it should be configurable
// so generic SDKs call this by default, but AWS SDKs can opt-out of it via a setting.
// runner.createDedicatedInputsAndOutputs();

@Deprecated
public static SymbolProvider createSymbolProvider(Model model, TypeScriptSettings settings) {
return new SymbolVisitor(model, settings);
// Run it!
runner.run();
}
}
Loading