|
| 1 | +/* Copyright 2016 Google Inc |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +package com.google.api.codegen.gapic; |
| 16 | + |
| 17 | +import com.google.api.codegen.ApiConfig; |
| 18 | +import com.google.api.codegen.rendering.CommonSnippetSetRunner; |
| 19 | +import com.google.api.codegen.transformer.ModelToViewTransformer; |
| 20 | +import com.google.api.codegen.viewmodel.ViewModel; |
| 21 | +import com.google.api.tools.framework.model.Interface; |
| 22 | +import com.google.api.tools.framework.model.Model; |
| 23 | +import com.google.api.tools.framework.model.stages.Merged; |
| 24 | +import com.google.api.tools.framework.snippet.Doc; |
| 25 | + |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.TreeMap; |
| 29 | + |
| 30 | +public class ViewModelGapicProvider implements GapicProvider<Interface> { |
| 31 | + private final Model model; |
| 32 | + private final ApiConfig apiConfig; |
| 33 | + private final CommonSnippetSetRunner snippetSetRunner; |
| 34 | + private final ModelToViewTransformer modelToViewTransformer; |
| 35 | + |
| 36 | + private ViewModelGapicProvider( |
| 37 | + Model model, |
| 38 | + ApiConfig apiConfig, |
| 39 | + CommonSnippetSetRunner snippetSetRunner, |
| 40 | + ModelToViewTransformer modelToViewTransformer) { |
| 41 | + this.model = model; |
| 42 | + this.apiConfig = apiConfig; |
| 43 | + this.snippetSetRunner = snippetSetRunner; |
| 44 | + this.modelToViewTransformer = modelToViewTransformer; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public List<String> getSnippetFileNames() { |
| 49 | + return modelToViewTransformer.getTemplateFileNames(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Map<String, Doc> generate() { |
| 54 | + return generate(null); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Map<String, Doc> generate(String snippetFileName) { |
| 59 | + // Establish required stage for generation. |
| 60 | + model.establishStage(Merged.KEY); |
| 61 | + if (model.getDiagCollector().getErrorCount() > 0) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + List<ViewModel> surfaceDocs = modelToViewTransformer.transform(model, apiConfig); |
| 66 | + if (model.getDiagCollector().getErrorCount() > 0) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + Map<String, Doc> docs = new TreeMap<>(); |
| 71 | + for (ViewModel surfaceDoc : surfaceDocs) { |
| 72 | + if (snippetFileName != null && !surfaceDoc.templateFileName().equals(snippetFileName)) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + Doc doc = snippetSetRunner.generate(surfaceDoc); |
| 76 | + if (doc == null) { |
| 77 | + // generation failed; failures are captured in the model. |
| 78 | + continue; |
| 79 | + } |
| 80 | + docs.put(surfaceDoc.outputPath(), doc); |
| 81 | + } |
| 82 | + |
| 83 | + return docs; |
| 84 | + } |
| 85 | + |
| 86 | + public static Builder newBuilder() { |
| 87 | + return new Builder(); |
| 88 | + } |
| 89 | + |
| 90 | + public static class Builder { |
| 91 | + private Model model; |
| 92 | + private ApiConfig apiConfig; |
| 93 | + private CommonSnippetSetRunner snippetSetRunner; |
| 94 | + private ModelToViewTransformer modelToViewTransformer; |
| 95 | + |
| 96 | + private Builder() {} |
| 97 | + |
| 98 | + public Builder setModel(Model model) { |
| 99 | + this.model = model; |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + public Builder setApiConfig(ApiConfig apiConfig) { |
| 104 | + this.apiConfig = apiConfig; |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + public Builder setSnippetSetRunner(CommonSnippetSetRunner snippetSetRunner) { |
| 109 | + this.snippetSetRunner = snippetSetRunner; |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + public Builder setModelToViewTransformer(ModelToViewTransformer modelToViewTransformer) { |
| 114 | + this.modelToViewTransformer = modelToViewTransformer; |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + public ViewModelGapicProvider build() { |
| 119 | + return new ViewModelGapicProvider(model, apiConfig, snippetSetRunner, modelToViewTransformer); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments