|
| 1 | +/* |
| 2 | + * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.smithy.typescript.codegen; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | +import software.amazon.smithy.build.FileManifest; |
| 21 | +import software.amazon.smithy.codegen.core.CodegenContext; |
| 22 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 23 | +import software.amazon.smithy.model.Model; |
| 24 | +import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator; |
| 25 | +import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin; |
| 26 | +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; |
| 27 | +import software.amazon.smithy.utils.SmithyBuilder; |
| 28 | +import software.amazon.smithy.utils.SmithyUnstableApi; |
| 29 | + |
| 30 | +/** |
| 31 | + * Holds context related to code generation. |
| 32 | + */ |
| 33 | +@SmithyUnstableApi |
| 34 | +public final class TypeScriptCodegenContext |
| 35 | + implements CodegenContext<TypeScriptSettings, TypeScriptWriter, TypeScriptIntegration> { |
| 36 | + |
| 37 | + private final Model model; |
| 38 | + private final TypeScriptSettings settings; |
| 39 | + private final SymbolProvider symbolProvider; |
| 40 | + private final FileManifest fileManifest; |
| 41 | + private final TypeScriptDelegator writerDelegator; |
| 42 | + private final List<TypeScriptIntegration> integrations; |
| 43 | + private final List<RuntimeClientPlugin> runtimePlugins; |
| 44 | + private final ProtocolGenerator protocolGenerator; |
| 45 | + private final ApplicationProtocol applicationProtocol; |
| 46 | + |
| 47 | + private TypeScriptCodegenContext(Builder builder) { |
| 48 | + model = SmithyBuilder.requiredState("model", builder.model); |
| 49 | + settings = SmithyBuilder.requiredState("settings", builder.settings); |
| 50 | + symbolProvider = SmithyBuilder.requiredState("symbolProvider", builder.symbolProvider); |
| 51 | + fileManifest = SmithyBuilder.requiredState("fileManifest", builder.fileManifest); |
| 52 | + writerDelegator = SmithyBuilder.requiredState("writerDelegator", builder.writerDelegator); |
| 53 | + integrations = SmithyBuilder.requiredState("integrations", builder.integrations); |
| 54 | + runtimePlugins = SmithyBuilder.requiredState("runtimePlugins", builder.runtimePlugins); |
| 55 | + protocolGenerator = builder.protocolGenerator; |
| 56 | + applicationProtocol = SmithyBuilder.requiredState("applicationProtocol", builder.applicationProtocol); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Model model() { |
| 61 | + return model; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public TypeScriptSettings settings() { |
| 66 | + return settings; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public SymbolProvider symbolProvider() { |
| 71 | + return symbolProvider; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public FileManifest fileManifest() { |
| 76 | + return fileManifest; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public TypeScriptDelegator writerDelegator() { |
| 81 | + return writerDelegator; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public List<TypeScriptIntegration> integrations() { |
| 86 | + return integrations; |
| 87 | + } |
| 88 | + |
| 89 | + public List<RuntimeClientPlugin> runtimePlugins() { |
| 90 | + return runtimePlugins; |
| 91 | + } |
| 92 | + |
| 93 | + public ProtocolGenerator protocolGenerator() { |
| 94 | + return protocolGenerator; |
| 95 | + } |
| 96 | + |
| 97 | + public ApplicationProtocol applicationProtocol() { |
| 98 | + return applicationProtocol; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @return Returns a builder. |
| 103 | + */ |
| 104 | + public static Builder builder() { |
| 105 | + return new Builder(); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Builds {@link TypeScriptCodegenContext}s. |
| 110 | + */ |
| 111 | + public static final class Builder implements SmithyBuilder<TypeScriptCodegenContext> { |
| 112 | + private Model model; |
| 113 | + private TypeScriptSettings settings; |
| 114 | + private SymbolProvider symbolProvider; |
| 115 | + private FileManifest fileManifest; |
| 116 | + private TypeScriptDelegator writerDelegator; |
| 117 | + private List<TypeScriptIntegration> integrations = new ArrayList<>(); |
| 118 | + private List<RuntimeClientPlugin> runtimePlugins = new ArrayList<>(); |
| 119 | + private ProtocolGenerator protocolGenerator; |
| 120 | + private ApplicationProtocol applicationProtocol; |
| 121 | + |
| 122 | + @Override |
| 123 | + public TypeScriptCodegenContext build() { |
| 124 | + return new TypeScriptCodegenContext(this); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @param model The model being generated. |
| 129 | + * @return Returns the builder. |
| 130 | + */ |
| 131 | + public Builder model(Model model) { |
| 132 | + this.model = model; |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @param settings The resolved settings for the generator. |
| 138 | + * @return Returns the builder. |
| 139 | + */ |
| 140 | + public Builder settings(TypeScriptSettings settings) { |
| 141 | + this.settings = settings; |
| 142 | + return this; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @param symbolProvider The finalized symbol provider for the generator. |
| 147 | + * @return Returns the builder. |
| 148 | + */ |
| 149 | + public Builder symbolProvider(SymbolProvider symbolProvider) { |
| 150 | + this.symbolProvider = symbolProvider; |
| 151 | + return this; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @param fileManifest The file manifest being used in the generator. |
| 156 | + * @return Returns the builder. |
| 157 | + */ |
| 158 | + public Builder fileManifest(FileManifest fileManifest) { |
| 159 | + this.fileManifest = fileManifest; |
| 160 | + return this; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * @param writerDelegator The writer delegator to use in the generator. |
| 165 | + * @return Returns the builder. |
| 166 | + */ |
| 167 | + public Builder writerDelegator(TypeScriptDelegator writerDelegator) { |
| 168 | + this.writerDelegator = writerDelegator; |
| 169 | + return this; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * @param integrations The integrations to use in the generator. |
| 174 | + * @return Returns the builder. |
| 175 | + */ |
| 176 | + public Builder integrations(List<TypeScriptIntegration> integrations) { |
| 177 | + this.integrations.clear(); |
| 178 | + this.integrations.addAll(integrations); |
| 179 | + return this; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * @param runtimePlugins The runtime plugins to use in the generator. |
| 184 | + * @return Returns the builder. |
| 185 | + */ |
| 186 | + public Builder runtimePlugins(List<RuntimeClientPlugin> runtimePlugins) { |
| 187 | + this.runtimePlugins.clear(); |
| 188 | + this.runtimePlugins.addAll(runtimePlugins); |
| 189 | + return this; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * @param protocolGenerator The protocol generator to use in the generator. |
| 194 | + * @return Returns the builder. |
| 195 | + */ |
| 196 | + public Builder protocolGenerator(ProtocolGenerator protocolGenerator) { |
| 197 | + this.protocolGenerator = protocolGenerator; |
| 198 | + return this; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * @param applicationProtocol The application protocol to use in the generator. |
| 203 | + * @return Returns the builder. |
| 204 | + */ |
| 205 | + public Builder applicationProtocol(ApplicationProtocol applicationProtocol) { |
| 206 | + this.applicationProtocol = applicationProtocol; |
| 207 | + return this; |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments