UNPKG

2.92 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17const fs = require('fs');
18
19const ModelManager = require('@accordproject/concerto-core').ModelManager;
20const CodeGen = require('./codegen/codegen');
21
22const FileWriter = CodeGen.FileWriter;
23const GoLangVisitor = CodeGen.GoLangVisitor;
24const JavaVisitor = CodeGen.JavaVisitor;
25const CordaVisitor = CodeGen.CordaVisitor;
26const JSONSchemaVisitor = CodeGen.JSONSchemaVisitor;
27const PlantUMLVisitor = CodeGen.PlantUMLVisitor;
28const TypescriptVisitor = CodeGen.TypescriptVisitor;
29const XmlSchemaVisitor = CodeGen.XmlSchemaVisitor;
30
31/**
32 * Utility class that implements the commands exposed by the CLI.
33 * @class
34 * @memberof module:cicero-tools
35 */
36class Commands {
37
38 /**
39 * Converts the model for a template into code
40 *
41 * @param {string} format the format to generate
42 * @param {string[]} ctoFiles the CTO files to convert to code
43 * @param {string} outputDirectory the output directory
44 * @returns {string} Result of code generation
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
94module.exports = Commands;
\No newline at end of file