-
Notifications
You must be signed in to change notification settings - Fork 45
/
service-generator.ts
141 lines (128 loc) · 4.63 KB
/
service-generator.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
import { GeneratorOptions } from './generator-options';
import { npmCompliantName } from './generator-utils';
import { GlobalNameFormatter } from './global-name-formatter';
import { inputPaths, ServiceDefinitionPaths } from './input-path-provider';
import {
getServicePath,
readServiceMapping,
VdmMapping
} from './service-mapping';
import { ServiceNameFormatter } from './service-name-formatter';
import { readEdmxFile, ServiceMetadata } from './edmx-parser/edmx-file-reader';
import { readSwaggerFile } from './swagger-parser/swagger-parser';
import { apiBusinessHubMetadata } from './swagger-parser/swagger-util';
import { VdmServiceMetadata, VdmServicePackageMetaData } from './vdm-types';
import { isV2Metadata } from './edmx-to-vdm/edmx-to-vdm-util';
import { getServiceEntitiesV2 } from './edmx-to-vdm/v2';
import { getServiceEntitiesV4 } from './edmx-to-vdm/v4';
class ServiceGenerator {
private globalNameFormatter: GlobalNameFormatter;
private serviceMapping: VdmMapping;
constructor(readonly options: GeneratorOptions) {
this.serviceMapping = readServiceMapping(options);
this.globalNameFormatter = new GlobalNameFormatter(this.serviceMapping);
}
public withServiceMapping(serviceMapping: VdmMapping) {
this.serviceMapping = serviceMapping;
return this;
}
public withGlobalNameFormatter(globalNameFormatter: GlobalNameFormatter) {
this.globalNameFormatter = globalNameFormatter;
return this;
}
public generateAllServices(): VdmServiceMetadata[] {
return inputPaths(this.options.inputDir, this.options.useSwagger).map(p =>
this.generateService(p)
);
}
public generateService(
serviceDefinitionPaths: ServiceDefinitionPaths
): VdmServiceMetadata {
const serviceMetadata = this.readEdmxAndSwaggerFile(serviceDefinitionPaths);
const vdmServicePackageMetaData = this.getServicePackageMetaData(
serviceMetadata,
serviceDefinitionPaths
);
const vdmServiceEntities = isV2Metadata(serviceMetadata.edmx)
? getServiceEntitiesV2(serviceMetadata)
: getServiceEntitiesV4(serviceMetadata);
return {
...vdmServicePackageMetaData,
...vdmServiceEntities
};
}
private getServicePackageMetaData(
serviceMetadata: ServiceMetadata,
serviceDefinitionPaths: ServiceDefinitionPaths
): VdmServicePackageMetaData {
const directoryName = this.globalNameFormatter.uniqueDirectoryName(
ServiceNameFormatter.originalToServiceName(
serviceMetadata.edmx.namespace
),
serviceMetadata.edmx.fileName
);
const npmPackageName = this.globalNameFormatter.uniqueNpmPackageName(
npmCompliantName(directoryName),
serviceMetadata.edmx.fileName
);
const speakingModuleName = ServiceNameFormatter.directoryToSpeakingModuleName(
directoryName
);
const className = `${speakingModuleName.replace(/ /g, '')}`;
return {
oDataVersion: serviceMetadata.edmx.oDataVersion,
namespace: serviceMetadata.edmx.namespace,
originalFileName: serviceMetadata.edmx.fileName,
directoryName,
npmPackageName,
speakingModuleName,
servicePath: getServicePath(
serviceMetadata,
this.serviceMapping[serviceMetadata.edmx.fileName]
),
edmxPath: serviceDefinitionPaths.edmxPath,
apiBusinessHubMetadata: apiBusinessHubMetadata(serviceMetadata.swagger),
className
};
}
private readEdmxAndSwaggerFile(
serviceDefinitionPaths: ServiceDefinitionPaths
): ServiceMetadata {
const serviceMetadata: ServiceMetadata = {
edmx: readEdmxFile(serviceDefinitionPaths.edmxPath)
};
if (serviceDefinitionPaths.swaggerPath) {
serviceMetadata.swagger = readSwaggerFile(
serviceDefinitionPaths.swaggerPath
);
}
return serviceMetadata;
}
}
/**
* @param options Generator options
* @returns the parsed services
*/
export function parseAllServices(
options: GeneratorOptions
): VdmServiceMetadata[] {
return new ServiceGenerator(options).generateAllServices();
}
/**
* @param serviceDefinitionPaths Path to the service definition
* @param options Generator options
* @param mappings mappings for VDM service names to desired name
* @param globalNameFormatter Instance of global name formatter to be used for the parsing process
* @returns the parsed service
*/
export function parseService(
serviceDefinitionPaths: ServiceDefinitionPaths,
options: GeneratorOptions,
mappings: VdmMapping,
globalNameFormatter: GlobalNameFormatter
): VdmServiceMetadata {
return new ServiceGenerator(options)
.withServiceMapping(mappings)
.withGlobalNameFormatter(globalNameFormatter)
.generateService(serviceDefinitionPaths);
}