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 | ;
|
16 |
|
17 | const slash = require('slash');
|
18 |
|
19 | const Introspector = require('@accordproject/concerto-core').Introspector;
|
20 | const APModelManager = require('../lib/apmodelmanager');
|
21 | const Script = require('./script');
|
22 | const ScriptManager = require('../lib/scriptmanager');
|
23 | const ErgoCompiler = require('./compiler');
|
24 |
|
25 | /**
|
26 | * Packages the logic for a legal clause or contract template and a given target platform. This includes the model, Ergo logic and compiled version of that logic when required.
|
27 | * @class
|
28 | * @public
|
29 | * @abstract
|
30 | * @memberof module:ergo-compiler
|
31 | */
|
32 | class LogicManager {
|
33 |
|
34 | /**
|
35 | * Create the LogicManager.
|
36 | * @param {String} target - compiler target (either: 'es6', or 'java')
|
37 | * @param {Object} options - e.g., { warnings: true }
|
38 | */
|
39 | constructor(target, options) {
|
40 | ErgoCompiler.isValidTarget(target);
|
41 | this.target = target;
|
42 | this.contractName = null;
|
43 | this.modelManager = new APModelManager();
|
44 | this.scriptManager = new ScriptManager(this.target, this.modelManager, options);
|
45 | this.introspector = new Introspector(this.modelManager);
|
46 | }
|
47 |
|
48 | /**
|
49 | * Get the compilation target.
|
50 | * @return {String} the compiler target (either: 'es6', or 'java')
|
51 | */
|
52 | getTarget() {
|
53 | return this.target;
|
54 | }
|
55 |
|
56 | /**
|
57 | * Set the compilation target. Note: This might force recompilation if logic has already been compiled.
|
58 | * @param {String} target - compiler target (either: 'es6', or 'java')
|
59 | * @param {boolean} recompile - whether to force recompilation of the logic
|
60 | */
|
61 | setTarget(target, recompile) {
|
62 | this.target = target;
|
63 | this.getScriptManager().changeTarget(target, recompile);
|
64 | }
|
65 |
|
66 | /**
|
67 | * Set the contract name
|
68 | * @param {String} contractName - the contract name
|
69 | */
|
70 | setContractName(contractName) {
|
71 | this.contractName = ErgoCompiler.contractCallName(contractName);
|
72 | }
|
73 |
|
74 | /**
|
75 | * Get the contract name
|
76 | * @return {String} the contract name
|
77 | */
|
78 | getContractName() {
|
79 | return this.contractName;
|
80 | }
|
81 |
|
82 | /**
|
83 | * Provides access to the Introspector for this TemplateLogic. The Introspector
|
84 | * is used to reflect on the types defined within this TemplateLogic.
|
85 | * @return {Introspector} the Introspector for this TemplateLogic
|
86 | */
|
87 | getIntrospector() {
|
88 | return this.introspector;
|
89 | }
|
90 |
|
91 | /**
|
92 | * Provides access to the ScriptManager for this TemplateLogic. The ScriptManager
|
93 | * manage access to the scripts that have been defined within this TemplateLogic.
|
94 | * @return {ScriptManager} the ScriptManager for this TemplateLogic
|
95 | */
|
96 | getScriptManager() {
|
97 | return this.scriptManager;
|
98 | }
|
99 |
|
100 | /**
|
101 | * Provides access to the ModelManager for this TemplateLogic. The ModelManager
|
102 | * manage access to the models that have been defined within this TemplateLogic.
|
103 | * @return {ModelManager} the ModelManager for this TemplateLogic
|
104 | */
|
105 | getModelManager() {
|
106 | return this.modelManager;
|
107 | }
|
108 |
|
109 | /**
|
110 | * Adds a logic file (as a string) to the TemplateLogic.
|
111 | * @param {string} logicFile - The logic file as a string
|
112 | * @param {string} fileName - an optional file name to associate with the logic file
|
113 | */
|
114 | addLogicFile(logicFile,fileName) {
|
115 | const logicFileName = slash(fileName);
|
116 | let logicExt;
|
117 | if (fileName.indexOf('.') === -1) {
|
118 | logicExt = '.ergo';
|
119 | } else {
|
120 | logicExt = '.' + fileName.split('.').pop();
|
121 | }
|
122 | let scriptObject = this.getScriptManager().createScript(logicFileName, logicExt, logicFile);
|
123 | this.getScriptManager().addScript(scriptObject);
|
124 | }
|
125 |
|
126 | /**
|
127 | * Adds a template file (as a string) to the TemplateLogic.
|
128 | * @param {string} templateFile - The template file as a string
|
129 | * @param {string} fileName - an optional file name to associate with the template file
|
130 | */
|
131 | addTemplateFile(templateFile,fileName) {
|
132 | this.getScriptManager().addTemplateFile(templateFile,slash(fileName));
|
133 | }
|
134 |
|
135 | /**
|
136 | * Register compiled logic
|
137 | */
|
138 | registerCompiledLogicSync() {
|
139 | const scriptManager = this.getScriptManager();
|
140 | const mainScript = scriptManager.getCombinedScripts();
|
141 | if (mainScript) {
|
142 | const script = new Script(this, 'main.js', '.js', mainScript, null);
|
143 | const contractName = script.getContractName();
|
144 | if (contractName) { this.setContractName(contractName); }
|
145 | scriptManager.compiledScript = script;
|
146 | }
|
147 | }
|
148 |
|
149 | /**
|
150 | * Compiles the logic to the target.
|
151 | * @param {boolean} force - whether to force recompilation of the logic
|
152 | * @return {object} The script compiled to JavaScript
|
153 | */
|
154 | compileLogicSync(force) {
|
155 | this.getModelManager().validateModelFiles();
|
156 | const script = this.getScriptManager().compileLogic(force);
|
157 | if (script && script.getContractName()) {
|
158 | this.setContractName(script.getContractName());
|
159 | }
|
160 | return script;
|
161 | }
|
162 |
|
163 | /**
|
164 | * Compiles the logic to the target.
|
165 | * @param {boolean} force - whether to force recompilation of the logic
|
166 | * @return {object} A promise to the script compiled to JavaScript
|
167 | */
|
168 | compileLogic(force) {
|
169 | try {
|
170 | this.compileLogicSync(force);
|
171 | return Promise.resolve(undefined);
|
172 | } catch (error) {
|
173 | return Promise.reject(error);
|
174 | }
|
175 | }
|
176 |
|
177 | /**
|
178 | * Update of a given logic file
|
179 | * @param {string} content - the logic content
|
180 | * @param {string} name - the logic name
|
181 | */
|
182 | updateLogic(content, name) {
|
183 | const scriptManager = this.getScriptManager();
|
184 | if (!scriptManager.getScript(name)) {
|
185 | this.addLogicFile(content,name);
|
186 | } else {
|
187 | if (scriptManager.getScript(name).getContents() !== content) {
|
188 | scriptManager.modifyScript(name, '.ergo', content);
|
189 | }
|
190 | }
|
191 | }
|
192 |
|
193 | }
|
194 |
|
195 | module.exports = LogicManager; |
\ | No newline at end of file |