forked from accordproject/models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
477 lines (416 loc) · 19.7 KB
/
build.js
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.
*/
'use strict';
const concertoVersions = require('./concertoVersions');
const DEFAULT_CONCERTO_VERSION = concertoVersions.defaultVersion;
const rimraf = require('rimraf');
const path = require('path');
const nunjucks = require('nunjucks');
const AdmZip = require('adm-zip');
const semver = require('semver');
const plantumlEncoder = require('plantuml-encoder');
const {
promisify
} = require('util');
const {
resolve
} = require('path');
const fs = require('fs-extra')
const readdir = promisify(fs.readdir);
const rename = promisify(fs.rename);
const stat = promisify(fs.stat);
const mkdirp = require('mkdirp');
async function getFiles(dir) {
const subdirs = await readdir(dir);
const files = await Promise.all(subdirs.map(async (subdir) => {
const res = resolve(dir, subdir);
return (await stat(res)).isDirectory() ? getFiles(res) : res;
}));
return files.reduce((a, f) => a.concat(f), []);
}
async function generatePlantUML(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile) {
// generate the PlantUML for the ModelFile
try {
const generatedPumlFile = `${destPath}/${fileNameNoExt}.puml`;
const visitor = new thisConcerto.CodeGen.PlantUMLVisitor();
const fileWriter = new thisConcerto.FileWriter(buildDir);
fileWriter.openFile(generatedPumlFile);
fileWriter.writeLine(0, '@startuml');
const params = {fileWriter : fileWriter};
modelFile.accept(visitor, params);
fileWriter.writeLine(0, '@enduml');
fileWriter.closeFile();
// save the UML
const modelFilePlantUML = fs.readFileSync(generatedPumlFile, 'utf8');
const encoded = plantumlEncoder.encode(modelFilePlantUML)
return `https://www.plantuml.com/plantuml/svg/${encoded}`;
}
catch(err) {
console.log(` Generating PlantUML for ${destPath}/${fileNameNoExt}: ${err.message}`);
}
}
async function generateTypescript(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile) {
try {
// generate the Typescript for the ModelFile
const visitor = new thisConcerto.CodeGen.TypescriptVisitor();
const fileWriter = new thisConcerto.FileWriter(buildDir);
const zip = new AdmZip();
// override closeFile to aggregate all the files into a single zip
fileWriter.closeFile = function() {
if (!this.fileName) {
throw new Error('No file open');
}
// add file to zip
const content = fileWriter.getBuffer();
zip.addFile(this.fileName, Buffer.alloc(content.length, content), `Generated from ${modelFile.getName()}`);
zip.writeZip(`${destPath}/${fileNameNoExt}.ts.zip`);
this.fileName = null;
this.relativeDir = null;
this.clearBuffer();
};
const params = {fileWriter : fileWriter};
modelFile.getModelManager().accept(visitor, params);
}
catch(err) {
console.log(` Generating Typescript for ${destPath}/${fileNameNoExt}: ${err.message}`);
}
}
async function generateCSharp(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile) {
try {
// generate the Typescript for the ModelFile
const visitor = new thisConcerto.CodeGen.CSharpVisitor();
const fileWriter = new thisConcerto.FileWriter(buildDir);
const zip = new AdmZip();
// override closeFile to aggregate all the files into a single zip
fileWriter.closeFile = function() {
if (!this.fileName) {
throw new Error('No file open');
}
// add file to zip
const content = fileWriter.getBuffer();
zip.addFile(this.fileName, Buffer.alloc(content.length, content), `Generated from ${modelFile.getName()}`);
zip.writeZip(`${destPath}/${fileNameNoExt}.cs.zip`);
this.fileName = null;
this.relativeDir = null;
this.clearBuffer();
};
const params = {fileWriter : fileWriter};
modelFile.getModelManager().accept(visitor, params);
}
catch(err) {
console.log(` Generating CSharp for ${destPath}/${fileNameNoExt}: ${err.message}`);
}
}
async function generateOData(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile) {
try {
// generate the OData for the ModelFile
const visitor = new thisConcerto.CodeGen.ODataVisitor();
const fileWriter = new thisConcerto.FileWriter(buildDir);
const zip = new AdmZip();
// override closeFile to aggregate all the files into a single zip
fileWriter.closeFile = function() {
if (!this.fileName) {
throw new Error('No file open');
}
// add file to zip
const content = fileWriter.getBuffer();
zip.addFile(this.fileName, Buffer.alloc(content.length, content), `Generated from ${modelFile.getName()}`);
zip.writeZip(`${destPath}/${fileNameNoExt}.csdl.zip`);
this.fileName = null;
this.relativeDir = null;
this.clearBuffer();
};
const params = {fileWriter : fileWriter};
modelFile.getModelManager().accept(visitor, params);
}
catch(err) {
console.log(` Generating OData for ${destPath}/${fileNameNoExt}: ${err.message}`);
}
}
async function generateXmlSchema(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile) {
try {
// generate the XML Schema for the ModelFile
const visitor = new thisConcerto.CodeGen.XmlSchemaVisitor();
const fileWriter = new thisConcerto.FileWriter(buildDir);
const zip = new AdmZip();
// override closeFile to aggregate all the files into a single zip
fileWriter.closeFile = function() {
if (!this.fileName) {
throw new Error('No file open');
}
// add file to zip
const content = fileWriter.getBuffer();
zip.addFile(this.fileName, Buffer.alloc(content.length, content), `Generated from ${modelFile.getName()}`);
zip.writeZip(`${destPath}/${fileNameNoExt}.xsd.zip`);
this.fileName = null;
this.relativeDir = null;
this.clearBuffer();
};
const params = {fileWriter : fileWriter};
modelFile.getModelManager().accept(visitor, params);
}
catch(err) {
console.log(` Generating XmlSchema for ${destPath}/${fileNameNoExt}: ${err.message}`);
}
}
async function generateJsonSchema(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile) {
try {
// generate the JSON Schema
const visitor = new thisConcerto.CodeGen.JSONSchemaVisitor();
const params = {};
const jsonSchemas = modelFile.getModelManager().accept(visitor, params);
const generatedJsonFile = `${destPath}/${fileNameNoExt}.json`;
// save JSON Schema
fs.writeFile( `${generatedJsonFile}`, JSON.stringify(jsonSchemas), function (err) {
if (err) {
return console.log(err);
}
});
}
catch(err) {
console.log(` Generating JsonSchema for ${destPath}/${fileNameNoExt}: ${err.message}`);
}
}
async function generateGraphQL(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile) {
try {
// generate the GraphQL for the ModelFile
const generatedFile = `${destPath}/${fileNameNoExt}.gql`;
const visitor = new thisConcerto.CodeGen.GraphQLVisitor();
const fileWriter = new thisConcerto.FileWriter(buildDir);
// override closeFile to rename the output file
fileWriter.closeFile = function() {
if (!this.fileName) {
throw new Error('No file open');
}
const content = fileWriter.getBuffer();
fs.writeFileSync(generatedFile, content);
};
const params = {fileWriter : fileWriter};
modelFile.getModelManager().accept(visitor, params);
}
catch(err) {
console.log(` Generating GraphQL for ${destPath}/${fileNameNoExt}: ${err.message}`);
}
}
async function generateJsonAst(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile) {
try {
// generate the Json Abstract Syntax Tree (AST) based on Concerto Metamodel
const generatedJsonFile = `${destPath}/${fileNameNoExt}.ast.json`;
const modelManager = modelFile.getModelManager();
const modelText = modelFile.getDefinitions();
const ast = thisConcerto.MetaModel.ctoToMetaModelAndResolve ? thisConcerto.MetaModel.ctoToMetaModelAndResolve(modelManager, modelText, true) :
thisConcerto.Parser.parse(modelText);
const fileWriter = new thisConcerto.FileWriter(buildDir);
// save JSON AST
fs.writeFile( `${generatedJsonFile}`, JSON.stringify(ast), function (err) {
if (err) {
return console.log(err);
}
});
}
catch(err) {
console.log(` Generating JSON Syntax Tree for ${destPath}/${fileNameNoExt}: ${err.message}`);
}
}
async function generateJava(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile) {
try {
// generate the Java for the ModelFile
const visitor = new thisConcerto.CodeGen.JavaVisitor();
const fileWriter = new thisConcerto.FileWriter(buildDir);
const zip = new AdmZip();
// override closeFile to aggregate all the files into a single zip
fileWriter.closeFile = function() {
if (!this.fileName) {
throw new Error('No file open');
}
// add file to zip
const content = fileWriter.getBuffer();
zip.addFile(this.fileName, Buffer.alloc(content.length, content), `Generated from ${modelFile.getName()}`);
zip.writeZip(`${destPath}/${fileNameNoExt}.jar`);
this.fileName = null;
this.relativeDir = null;
this.clearBuffer();
};
const params = {fileWriter : fileWriter};
modelFile.getModelManager().accept(visitor, params);
}
catch(err) {
console.log(` Generating Java for ${destPath}/${fileNameNoExt}: ${err.message}`);
}
}
async function generateGo(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile) {
try {
// generate the Go Lang for the ModelFile
const visitor = new thisConcerto.CodeGen.GoLangVisitor();
const fileWriter = new thisConcerto.FileWriter(buildDir);
const zip = new AdmZip();
// override closeFile to aggregate all the files into a single zip
fileWriter.closeFile = function() {
if (!this.fileName) {
throw new Error('No file open');
}
// add file to zip
const content = fileWriter.getBuffer();
zip.addFile(this.fileName, Buffer.alloc(content.length, content), `Generated from ${modelFile.getName()}`);
zip.writeZip(`${destPath}/${fileNameNoExt}.go.zip`);
this.fileName = null;
this.relativeDir = null;
this.clearBuffer();
};
const params = {fileWriter : fileWriter};
modelFile.getModelManager().accept(visitor, params);
}
catch(err) {
console.log(` Generating Go for ${destPath}/${fileNameNoExt}: ${err.message}`);
}
}
const rootDir = resolve(__dirname, './src');
const buildDir = resolve(__dirname, './build');
let modelFileIndex = [];
/**
* Returns concerto classes for a version compatible with the model
* based on a comment like: // requires: concerto-core:0.82
* @param {object} concertoVersions - supported Concerto versions
* @param {*} modelText the CTO model text
* @return {object} supported concerto version classes
*/
function findCompatibleVersion(concertoVersions, modelText) {
const defaultConcertoVersion = concertoVersions[DEFAULT_CONCERTO_VERSION];
const commentRegex = /^\/\/.*requires:.*concerto-core:(?<versionRange>.*)$/m;
const declarationRegex = /^concerto version \"(?<versionRange>.*)\"$/m;
const match = modelText.match(declarationRegex) || modelText.match(commentRegex);
const versionRange = match ? match.groups.versionRange.replace(' ', '') : null;
const foundConcertoVersion = Object.entries(concertoVersions).find(([version,concerto]) => {
return versionRange && semver.satisfies( version, versionRange );
});
const result = foundConcertoVersion ? foundConcertoVersion[1] : defaultConcertoVersion;
return result;
}
(async function () {
// delete build directory
rimraf.sync(buildDir);
nunjucks.configure('./views', { autoescape: true });
// copy the logo to build directory
await fs.copy('assets', './build/assets');
await fs.copy('styles.css', './build/styles.css');
await fs.copy('fonts.css', './build/fonts.css');
await fs.copy('_headers', './build/_headers');
await fs.copy('_redirects', './build/_redirects');
// validate and copy all the files
const files = await getFiles(rootDir);
const filter = process.argv[2];
for( const file of files ) {
try {
if (!file.match(filter)){
continue;
}
const modelText = fs.readFileSync(file, 'utf8');
const thisConcerto = findCompatibleVersion(concertoVersions, modelText);
let modelManager = new thisConcerto.ModelManager();
if(semver.satisfies(thisConcerto.concertoVersion, '0.82.x')) {
// load system model if we are using 0.82
const systemModel = fs.readFileSync(rootDir + '/cicero/base.cto', 'utf8');
modelManager.addModelFile(systemModel, 'base.cto', false, true);
}
if(semver.lte(thisConcerto.concertoVersion, '3.0.0')) {
modelManager = new thisConcerto.ModelManager({ strict: true });
}
let modelFile = null;
if(semver.satisfies(thisConcerto.concertoVersion, '0.82.x')) {
modelFile = new thisConcerto.ModelFile(modelManager, modelText, file);
}
else {
const ast = thisConcerto.Parser.parse(modelText, file);
modelFile = new thisConcerto.ModelFile(modelManager, ast, modelText, file);
}
console.log(`🔄 Processing ${modelFile.getNamespace()} using Concerto v${thisConcerto.concertoVersion}`);
let modelFilePlantUML = '';
// passed validation, so copy to build dir
const dest = file.replace('/src/', '/build/');
const destPath = path.dirname(dest);
const relative = destPath.slice(buildDir.length);
const fileName = path.basename(file);
const fileNameNoExt = path.parse(fileName).name;
await fs.ensureDir(destPath);
let umlURL = '';
// Find the model version
const modelVersionStr = relative.match(/v\d+(\.\d+){0,2}/g);
const isLegacyModelVersionScheme = modelVersionStr !== null && modelVersionStr.length === 1;
if (!isLegacyModelVersionScheme) { // Skip indexing models with the old versioning scheme, they have all been migrated now
const semverStr = modelFile.getName().split("@").pop().slice(0,-4);
const isSemverVersionScheme = semver.valid(semverStr);
const modelVersion = isSemverVersionScheme ? `${semverStr}` : '0.1.0';
if(semver.satisfies(thisConcerto.concertoVersion, '0.82.x')) {
modelManager.addModelFile(modelFile, modelFile.getName(), true);
}
else {
modelManager.addModelFile(modelFile, modelText, modelFile.getName(), true);
}
// use the FORCE_PUBLISH flag to disable download of
// external models and model validation
if(!process.env.FORCE_PUBLISH) {
await modelManager.updateExternalModels();
}
umlURL = await generatePlantUML(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile);
await generateTypescript(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile);
await generateXmlSchema(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile);
await generateJsonSchema(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile);
await generateJava(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile);
await generateGo(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile);
if(thisConcerto.CodeGen.GraphQLVisitor) {
await generateGraphQL(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile);
}
if(thisConcerto.MetaModel) {
await generateJsonAst(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile);
}
if(thisConcerto.CodeGen.CSharpVisitor) {
await generateCSharp(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile);
}
if(thisConcerto.CodeGen.ODataVisitor) {
await generateOData(thisConcerto, buildDir, destPath, fileNameNoExt, modelFile);
}
// copy the CTO file to the build dir
await fs.copy(file, dest);
// generate the html page for the model
const generatedHtmlFile = `${relative}/${fileNameNoExt}.html`;
const serverRoot = process.env.SERVER_ROOT;
const templateResult = nunjucks.render('model.njk', { serverRoot: serverRoot, modelFile: modelFile, modelVersion: modelVersion, filePath: `${relative}/${fileNameNoExt}`, umlURL: umlURL, concerto: thisConcerto });
modelFileIndex.push({htmlFile: generatedHtmlFile, modelFile: modelFile, modelVersion: modelVersion});
console.log(`✅ Processed ${modelFile.getNamespace()} version ${modelVersion}`);
fs.writeFile( `./build/${generatedHtmlFile}`, templateResult, function (err) {
if (err) {
return console.log(err);
}
});
} else {
// copy the CTO file to the build dir
await fs.copy(file, dest);
}
} catch (err) {
console.log(`❗ Error handling ${file}`);
console.log(err.message);
console.log(err);
}
}; // for
// generate the index html page
modelFileIndex = modelFileIndex.sort((a, b) => a.modelFile.getNamespace().localeCompare(b.modelFile.getNamespace()));
const serverRoot = process.env.SERVER_ROOT;
const templateResult = nunjucks.render('index.njk', { serverRoot: serverRoot, modelFileIndex: modelFileIndex });
fs.writeFile( './build/index.html', templateResult, function (err) {
if (err) {
return console.log(err);
}
});
}
)();