1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | 'use strict';
|
16 |
|
17 | const slash = require('slash');
|
18 | const fsPath = require('path');
|
19 |
|
20 | const Parser = require('@accordproject/concerto-cto').Parser;
|
21 | const ModelManager = require('@accordproject/concerto-core').ModelManager;
|
22 | const ModelFile = require('@accordproject/concerto-core').ModelFile;
|
23 | const Builtin = require('./builtin');
|
24 |
|
25 | const processModelFiles = (modelFiles) => {
|
26 | let models = [];
|
27 | modelFiles.forEach(function (file) {
|
28 |
|
29 | if (file.isSystemModelFile()) {
|
30 | return;
|
31 | }
|
32 | let fileName = file.getName();
|
33 | if (fileName === 'UNKNOWN' || !fileName) {
|
34 | fileName = file.getNamespace() + '.cto';
|
35 | } else {
|
36 | fileName = fsPath.basename(fileName);
|
37 | }
|
38 | models.push({ 'name' : fileName, 'namespace': file.getNamespace(), 'content' : file.definitions });
|
39 | });
|
40 | return models;
|
41 | };
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | class APModelManager extends ModelManager {
|
51 |
|
52 | |
53 |
|
54 | constructor() {
|
55 | super();
|
56 | this.addCTOModel(Builtin.TimeModel, '@models.accordproject.org.time@0.2.0.cto');
|
57 | this.addCTOModel(Builtin.MoneyModel, '@models.accordproject.org.accordproject.money@0.2.0.cto');
|
58 | this.addCTOModel(Builtin.ContractModel, '@models.accordproject.org.accordproject.contract.cto');
|
59 | this.addCTOModel(Builtin.RuntimeModel, '@models.accordproject.org.accordproject.runtime.cto');
|
60 | this.addCTOModel(Builtin.OptionsModel, '@org.accordproject.ergo.options.cto');
|
61 | this.validateModelFiles();
|
62 | this.builtInNamespaces = this.getNamespaces();
|
63 | }
|
64 |
|
65 | |
66 |
|
67 |
|
68 |
|
69 | getModels() {
|
70 | return processModelFiles(this.getModelFiles());
|
71 | }
|
72 |
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 | addAPModelFile(modelFileContent, fileName) {
|
79 | const name = slash(fileName);
|
80 | const ast = Parser.parse(modelFileContent, fileName);
|
81 | const modelFile = new ModelFile(this, ast, modelFileContent, name);
|
82 | if (!this.builtInNamespaces.includes(modelFile.getNamespace())) {
|
83 | this.addModelFile(modelFile,modelFileContent,name,true);
|
84 | }
|
85 | }
|
86 |
|
87 | |
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | async addAPModelFiles(modelFiles, modelFileNames, offline) {
|
96 | modelFiles.map((modelFileContent, index) => {
|
97 | const modelFileName = modelFileNames[index];
|
98 | this.addAPModelFile(modelFileContent, modelFileName);
|
99 | });
|
100 | if (offline) {
|
101 | this.validateModelFiles();
|
102 | return [];
|
103 | } else {
|
104 | const externalModelFiles = await this.updateExternalModels();
|
105 | return processModelFiles(externalModelFiles);
|
106 | }
|
107 | }
|
108 |
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 | updateModel(content, name) {
|
115 | const currentModels = this.getModelFiles();
|
116 |
|
117 | if (!currentModels.some(x => x.getName() === name)) {
|
118 | this.addCTOModel(content, name);
|
119 | } else {
|
120 | const previousModelFile =
|
121 | (currentModels.filter(x => x.getName() === name))[0];
|
122 | const previousContent = previousModelFile.getDefinitions();
|
123 | if (content !== previousContent) {
|
124 | const previousNamespace = previousModelFile.getNamespace();
|
125 | const ast = Parser.parse(content, name);
|
126 | const newNamespace = new ModelFile(this, ast, content, name).getNamespace();
|
127 | if (previousNamespace === newNamespace) {
|
128 | this.updateModelFile(content, name, true);
|
129 | } else {
|
130 | this.deleteModelFile(previousNamespace);
|
131 | this.addCTOModel(content, name, true);
|
132 | }
|
133 | }
|
134 | }
|
135 | }
|
136 | }
|
137 |
|
138 | module.exports = APModelManager; |
\ | No newline at end of file |