forked from asyncapi/modelina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractGenerator.ts
209 lines (195 loc) · 6.54 KB
/
AbstractGenerator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import {
InputMetaModel,
OutputModel,
Preset,
Presets,
RenderOutput,
ProcessorOptions,
MetaModel,
ConstrainedMetaModel
} from '../models';
import { InputProcessor } from '../processors';
import { IndentationTypes } from '../helpers';
import { DeepPartial, isPresetWithOptions } from '../utils';
import { AbstractDependencyManager } from './AbstractDependencyManager';
export interface CommonGeneratorOptions<
P extends Preset = Preset,
DependencyManager extends AbstractDependencyManager = AbstractDependencyManager
> {
indentation?: {
type: IndentationTypes;
size: number;
};
defaultPreset?: P;
presets?: Presets<P>;
processorOptions?: ProcessorOptions;
/**
* This dependency manager type serves two functions.
* 1. It can be used to provide a factory for generate functions
* 2. It can be used to provide a single instance of a dependency manager, to add all dependencies together
*
* This depends on context and where it's used.
*/
dependencyManager?: (() => DependencyManager) | DependencyManager;
}
export const defaultGeneratorOptions: CommonGeneratorOptions = {
indentation: {
type: IndentationTypes.SPACES,
size: 2
}
};
/**
* Abstract generator which must be implemented by each language
*/
export abstract class AbstractGenerator<
Options extends CommonGeneratorOptions,
RenderCompleteModelOptions
> {
constructor(
public readonly languageName: string,
public readonly options: Options
) {}
public abstract render(
model: MetaModel,
inputModel: InputMetaModel,
options?: DeepPartial<Options>
): Promise<RenderOutput>;
public abstract renderCompleteModel(
model: MetaModel,
inputModel: InputMetaModel,
completeOptions: Partial<RenderCompleteModelOptions>,
options?: DeepPartial<Options>
): Promise<RenderOutput>;
public abstract constrainToMetaModel(
model: MetaModel,
options: DeepPartial<Options>
): ConstrainedMetaModel;
public abstract getDependencyManager(
options: Options
): AbstractDependencyManager;
public abstract splitMetaModel(model: MetaModel): MetaModel[];
public process(input: Record<string, unknown>): Promise<InputMetaModel> {
return InputProcessor.processor.process(
input,
this.options.processorOptions
);
}
/**
* This function returns an instance of the dependency manager which is either a factory or an instance.
*/
protected getDependencyManagerInstance(
options: Options
): AbstractDependencyManager {
if (options.dependencyManager === undefined) {
throw new Error(
'Internal error, could not find dependency manager instance'
);
}
if (typeof options.dependencyManager === 'function') {
return options.dependencyManager();
}
return options.dependencyManager;
}
/**
* Generates the full output of a model, instead of a scattered model.
*
* OutputModels result is no longer the model itself, but including package, package dependencies and model dependencies.
*
*/
public async generateCompleteModels(
input: any | InputMetaModel,
completeOptions: Partial<RenderCompleteModelOptions>
): Promise<OutputModel[]> {
const inputModel = await this.processInput(input);
const renders = Object.values(inputModel.models).map(async (model) => {
const dependencyManager = this.getDependencyManager(this.options);
const constrainedModel = this.constrainToMetaModel(model, {
dependencyManager
} as DeepPartial<Options>);
const renderedOutput = await this.renderCompleteModel(
constrainedModel,
inputModel,
completeOptions,
{ dependencyManager } as DeepPartial<Options>
);
return OutputModel.toOutputModel({
result: renderedOutput.result,
modelName: renderedOutput.renderedName,
dependencies: renderedOutput.dependencies,
model: constrainedModel,
inputModel
});
});
return Promise.all(renders);
}
/**
* Generates a scattered model where dependencies and rendered results are separated.
*/
public async generate(input: any | InputMetaModel): Promise<OutputModel[]> {
const inputModel = await this.processInput(input);
const renders = Object.values(inputModel.models).map(async (model) => {
const dependencyManager = this.getDependencyManager(this.options);
const constrainedModel = this.constrainToMetaModel(model, {
dependencyManager
} as DeepPartial<Options>);
const renderedOutput = await this.render(constrainedModel, inputModel, {
dependencyManager
} as DeepPartial<Options>);
return OutputModel.toOutputModel({
result: renderedOutput.result,
modelName: renderedOutput.renderedName,
dependencies: renderedOutput.dependencies,
model: constrainedModel,
inputModel
});
});
return Promise.all(renders);
}
/**
* Process any of the input formats to the appropriate InputMetaModel type and split out the meta models
* based on the requirements of the generators
*
* @param input
*/
protected async processInput(
input: any | InputMetaModel
): Promise<InputMetaModel> {
const rawInputModel =
input instanceof InputMetaModel ? input : await this.process(input);
//Split out the models based on the language specific requirements of which models is rendered separately
const splitOutModels: { [key: string]: MetaModel } = {};
for (const model of Object.values(rawInputModel.models)) {
const splitModels = this.splitMetaModel(model);
for (const splitModel of splitModels) {
splitOutModels[splitModel.name] = splitModel;
}
}
rawInputModel.models = splitOutModels;
return rawInputModel;
}
/**
* Get all presets (default and custom ones from options) for a given preset type (class, enum, etc).
*/
protected getPresets(presetType: string): Array<[Preset, unknown]> {
const filteredPresets: Array<[Preset, unknown]> = [];
const defaultPreset = this.options.defaultPreset;
if (defaultPreset !== undefined) {
filteredPresets.push([defaultPreset[String(presetType)], this.options]);
}
const presets = this.options.presets || [];
for (const p of presets) {
if (isPresetWithOptions(p)) {
const preset = p.preset[String(presetType)];
if (preset) {
filteredPresets.push([preset, p.options]);
}
} else {
const preset = p[String(presetType)];
if (preset) {
filteredPresets.push([preset, undefined]);
}
}
}
return filteredPresets;
}
}