1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | 'use strict';
|
16 |
|
17 | const fs = require('fs');
|
18 |
|
19 | const ModelManager = require('composer-concerto').ModelManager;
|
20 | const CodeGen = require('./codegen/codegen');
|
21 |
|
22 | const FileWriter = CodeGen.FileWriter;
|
23 | const GoLangVisitor = CodeGen.GoLangVisitor;
|
24 | const JavaVisitor = CodeGen.JavaVisitor;
|
25 | const CordaVisitor = CodeGen.CordaVisitor;
|
26 | const JSONSchemaVisitor = CodeGen.JSONSchemaVisitor;
|
27 | const PlantUMLVisitor = CodeGen.PlantUMLVisitor;
|
28 | const TypescriptVisitor = CodeGen.TypescriptVisitor;
|
29 | const XmlSchemaVisitor = CodeGen.XmlSchemaVisitor;
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | class Commands {
|
37 |
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | static async generate(format, ctoFiles, outputDirectory) {
|
47 |
|
48 | const modelManager = new ModelManager();
|
49 |
|
50 | const modelFiles = ctoFiles.map((ctoFile) => {
|
51 | return fs.readFileSync(ctoFile, 'utf8');
|
52 | });
|
53 | modelManager.addModelFiles(modelFiles, ctoFiles, true);
|
54 | await modelManager.updateExternalModels();
|
55 |
|
56 | let visitor = null;
|
57 |
|
58 | switch(format) {
|
59 | case 'Go':
|
60 | visitor = new GoLangVisitor();
|
61 | break;
|
62 | case 'PlantUML':
|
63 | visitor = new PlantUMLVisitor();
|
64 | break;
|
65 | case 'Typescript':
|
66 | visitor = new TypescriptVisitor();
|
67 | break;
|
68 | case 'Java':
|
69 | visitor = new JavaVisitor();
|
70 | break;
|
71 | case 'Corda':
|
72 | visitor = new CordaVisitor();
|
73 | break;
|
74 | case 'JSONSchema':
|
75 | visitor = new JSONSchemaVisitor();
|
76 | break;
|
77 | case 'XMLSchema':
|
78 | visitor = new XmlSchemaVisitor();
|
79 | break;
|
80 | }
|
81 |
|
82 | if(visitor) {
|
83 | let parameters = {};
|
84 | parameters.fileWriter = new FileWriter(outputDirectory);
|
85 | modelManager.accept(visitor, parameters);
|
86 | return `Generated ${format} code.`;
|
87 | }
|
88 | else {
|
89 | return 'Unrecognized code generator: ' + format;
|
90 | }
|
91 | }
|
92 | }
|
93 |
|
94 | module.exports = Commands; |
\ | No newline at end of file |