This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
commandHandlers.ts
executable file
·366 lines (311 loc) · 10.6 KB
/
commandHandlers.ts
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
/*
* 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';
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import MemoryWriter from './memorywriter';
import MermaidVisitor from './mermaidvisitor';
const Template = require('@accordproject/cicero-core').Template;
const CodeGen = require('@accordproject/concerto-tools').CodeGen;
const FileWriter = require('@accordproject/concerto-tools').FileWriter;
const Clause = require('@accordproject/cicero-core').Clause;
const Engine = require('@accordproject/cicero-engine').Engine;
var md = require('markdown-it')()
.use(require('@accordproject/markdown-it-template'))
.use(require('@accordproject/markdown-it-cicero'));
let outputChannel: vscode.OutputChannel
export function setOutputChannel(oc : vscode.OutputChannel) {
outputChannel = oc;
}
export async function exportArchive(file: vscode.Uri) {
try {
const template = await Template.fromDirectory(file.path, {skipUpdateExternalModels: true});
const archive = await template.toArchive('cicero');
const outputPath = path.join(file.path, `${template.getIdentifier()}.cta`);
fs.writeFileSync(outputPath, archive);
vscode.window.showInformationMessage(`Created archive ${outputPath}`);
return true;
}
catch(error) {
vscode.window.showErrorMessage(`Error ${error}`);
}
return false;
}
export async function downloadModels(file: vscode.Uri) {
try {
const template = await Template.fromDirectory(file.path);
const outputPath = path.join(file.path, 'model');
template.getModelManager().writeModelsToFileSystem(outputPath);
vscode.window.showInformationMessage(`Downloaded models to ${outputPath}`);
return true;
}
catch(error) {
vscode.window.showErrorMessage(`Error ${error}`);
}
return false;
}
async function getMermaid(ctoFile: vscode.Uri) {
try {
const templateRootPath = path.resolve(ctoFile.fsPath, '..', '..');
const modelFileName = ctoFile.fsPath;
const template = await Template.fromDirectory(templateRootPath, {skipUpdateExternalModels: true});
const modelManager = template.getModelManager();
outputChannel.appendLine(`Active file: ${modelFileName}`);
// const modelFile = modelManager.getModelFileByFileName(modelFileName);
// modelManager.getModelFiles().forEach(modelFile => {
// outputChannel.appendLine(`- ${modelFile.getName()}`);
// });
// if(!modelFile) {
// vscode.window.showErrorMessage(`Error failed to find model file ${modelFileName} in the model manager.`);
// return false;
// }
// outputChannel.appendLine(`Found: ${modelFile.getNamespace()}`);
const visitor = new MermaidVisitor();
let parameters = {} as any;
parameters.fileWriter = new MemoryWriter();
const virtualFileName = 'model.mmd';
// parameters.fileWriter.openFile('model.mmd');
// parameters.fileWriter.writeLine(0, 'classDiagram');
modelManager.accept(visitor, parameters);
// parameters.fileWriter.closeFile();
// outputChannel.appendLine(`Result: ${JSON.stringify(parameters.fileWriter.getFiles())}`);
return parameters.fileWriter.getFiles()['/model.mmd'];
}
catch(error) {
vscode.window.showErrorMessage(`Error ${error}`);
}
return false;
}
export async function exportClassDiagram(file: vscode.Uri) {
try {
const outputPath = path.join(file.path, 'model');
const template = await Template.fromDirectory(file.path, {skipUpdateExternalModels: true});
const modelManager = template.getModelManager();
const PlantUMLVisitor = CodeGen.PlantUMLVisitor;
const visitor = new PlantUMLVisitor();
let parameters = {} as any;
parameters.fileWriter = new FileWriter( outputPath );
modelManager.accept(visitor, parameters);
vscode.window.showInformationMessage(`Exported class diagram to ${outputPath}`);
return true;
}
catch(error) {
vscode.window.showErrorMessage(`Error ${error}`);
}
return false;
}
export async function triggerClause(file: vscode.Uri) {
try {
outputChannel.show();
const template = await Template.fromDirectory(file.path, {skipUpdateExternalModels: true});
const clause = new Clause(template);
const samplePath = path.join(file.path, 'text', 'sample.md');
if(!fs.existsSync(samplePath)) {
vscode.window.showErrorMessage('Cannot trigger: /text/sample.md file was not found.');
return false;
}
const requestPath = path.join(file.path, 'request.json');
if(!fs.existsSync(requestPath)) {
vscode.window.showErrorMessage('Cannot trigger:/request.json file was not found.');
return false;
}
const sampleText = fs.readFileSync( samplePath, 'utf8');
clause.parse(sampleText);
const parseResult = clause.getData();
outputChannel.appendLine( 'template' );
outputChannel.appendLine( '========' );
outputChannel.appendLine( template.getIdentifier() );
outputChannel.appendLine( '' );
outputChannel.appendLine( 'sample.md parse result' );
outputChannel.appendLine( '======================' );
outputChannel.appendLine( JSON.stringify(parseResult, null, 2) );
outputChannel.appendLine( '' );
const request = JSON.parse(fs.readFileSync( requestPath, 'utf8'));
outputChannel.appendLine( 'request.json' );
outputChannel.appendLine( '============' );
outputChannel.appendLine( JSON.stringify(request, null, 2) );
outputChannel.appendLine( '' );
const statePath = path.join(file.path, 'state.json');
const engine = new Engine();
let state = null;
if(!fs.existsSync(statePath)) {
const initResult = await engine.init(clause, null);
state = initResult.state;
} else {
state = JSON.parse(fs.readFileSync( statePath, 'utf8'));
}
outputChannel.appendLine( 'state.json' );
outputChannel.appendLine( '==========' );
outputChannel.appendLine( JSON.stringify(state, null, 2) );
outputChannel.appendLine( '' );
const result = await engine.trigger(clause, request, state, null );
outputChannel.appendLine( 'response' );
outputChannel.appendLine( '========' );
outputChannel.appendLine( JSON.stringify(result, null, 2) );
outputChannel.appendLine( '' );
return true;
}
catch(error) {
vscode.window.showErrorMessage(`Error ${error}`);
}
return false;
}
async function getHtml() {
let html = 'To display preview please open a grammar.tem.md or a *.cto file.';
if(vscode.window.activeTextEditor && vscode.window.activeTextEditor.document) {
if(vscode.window.activeTextEditor.document.languageId === 'ciceroMark') {
html = md.render(vscode.window.activeTextEditor.document.getText());
}
else if (vscode.window.activeTextEditor.document.languageId === 'concerto') {
const mermaid = await getMermaid(vscode.window.activeTextEditor.document.uri);
if(mermaid) {
outputChannel.appendLine(mermaid);
html = `<h1>Class Diagram</h2>
<div class="mermaid" id="mermaid-main">
${mermaid}
</div>
<div class="mermaid" id="mermaid-preview" style="position: fixed; top: 0; left: 0; width: 75px; height: 50px; z-index: 100; display: block">
${mermaid}
</div>
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
<script>mermaid.initialize({startOnLoad:true, theme: 'dark'});</script>
`;
}
else {
outputChannel.appendLine('Failed to created Mermaid diagram.');
}
}
}
// outputChannel.appendLine( html );
return html;
}
export async function getWebviewContent() {
const html = await getHtml();
const styles = `
.variable {
border: #A4BBE7 1px solid;
padding: 0px 3px;
border-radius: 2px;
background-color: var(--vscode-editor-background);
}
a {
color: #0f52ba;
}
.formula {
border: #AF54C4 1px solid;
border-radius: 2px;
background-color: var(--vscode-editor-background);
cursor: pointer;
}
.clause_block {
position: relative;
margin: 10px -10px;
padding: 0px 10px;
background-color: rgb(49, 48, 53);
border: 1px solid black;
border-radius: 3px;
}
.ulist_block {
border: #A4BBE7 1px solid;
padding: 0px 10px;
margin: 10 0;
border-radius: 2px;
background-color: rgb(65, 63, 70);
}
.olist_block {
border: #A4BBE7 1px solid;
padding: 0px 10px;
margin: 10px 0;
border-radius: 2px;
background-color: rgb(65, 63, 70);
}
.join_inline {
border: #A4BBE7 1px solid;
padding: 0px 3px;
border-radius: 2px;
background-color: var(--vscode-editor-background);
}
.if_inline {
border: #A4BBE7 1px solid;
padding: 0px 3px;
border-radius: 2px;
background-color: var(--vscode-editor-background);
}
.else_inline {
border: #A4BBE7 1px solid;
padding: 0px 3px;
border-radius: 2px;
background-color: var(--vscode-editor-background);
}
.optional_inline {
border: #A4BBE7 1px solid;
padding: 0px 3px;
border-radius: 2px;
background-color: var(--vscode-editor-background);
}
span {
position: relative;
display: inline;
}
span:after{
display: block;
visibility: hidden;
position: absolute;
bottom: 0;
left: 20%;
opacity: 1 !important;
content: attr(class); /* might also use attr(title) */
height: auto;
min-width: 100px;
padding: 1px 3px;
z-index: 999;
color: #FFFFFF;
text-decoration: none;
text-align: center;
border: 1px solid rgb(65, 63, 70);
background-color: rgb(65, 63, 70);
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 2px;
}
span:before {
position: absolute;
visibility: hidden;
width: 0;
height: 0;
left: 50%;
bottom: 0px;
opacity: 0;
content: "";
border-style: solid;
border-width: 6px 6px 0 6px;
border-color: rgb(65, 63, 70) transparent transparent transparent;
}
span:hover:after{ visibility: visible; opacity: 1; bottom: 20px; }
span:hover:before{ visibility: visible; opacity: 1; bottom: 14px; }
`
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Accord Project</title>
<style>${styles}</style>
</head>
<body>
${html}
</body>
</html>`;
}