Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
feat - add trigger command
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Selman <[email protected]>
  • Loading branch information
dselman committed Aug 5, 2020
1 parent 0980607 commit 16a717c
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// List of extensions which should be recommended for users of this workspace.
"recommendations": [
"dbaeumer.vscode-eslint"
"dbaeumer.vscode-eslint",
"camel-tooling.yo"
]
}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Change Log

### 0.21.7

- Added trigger command
- Registered markdown-it extensions with markdown preview (wip)

### 0.21.6

- Added a command to export the PlantUML class diagram
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Please visit the [Visual Studio Code Marketplace](https://marketplace.visualstud
- Develop the logic for templates using the Ergo domain specific language
- Write the natural language text for templates using the CiceroMark extended markdown syntax
- Run unit tests for templates using the Cucumber BDD testing framework
- Trigger templates
- Syntax highlighting for all files
- Compilation and problem markers

Expand All @@ -34,6 +35,7 @@ Please visit the [Visual Studio Code Marketplace](https://marketplace.visualstud
- Work offline by downloading data model dependencies (context-click on root folder)
- Package templates into Cicero Template Archive (cta) files (context-click on root folder)
- Generate PlantUML class diagram (context-click on root folder)
- Trigger a template, parsing data from sample.md and passing in `request.json` and `state.json` (context-click on root folder)

### Quick Fixes

Expand Down
12 changes: 11 additions & 1 deletion client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Accord Project extension for Visual Studio Code, providing tools for template development.",
"author": "Accord Project",
"license": "Apache-2.0",
"version": "0.21.6",
"version": "0.21.7",
"publisher": "accordproject",
"repository": {
"type": "git",
Expand All @@ -19,8 +19,11 @@
},
"dependencies": {
"@accordproject/cicero-core": "^0.21.0",
"@accordproject/cicero-engine": "^0.21.0",
"@accordproject/concerto-core": "^0.82.8",
"@accordproject/concerto-tools": "^0.82.8",
"@accordproject/markdown-it-cicero": "^0.12.0",
"@accordproject/markdown-it-template": "^0.12.0",
"vscode-languageclient": "^6.1.3"
}
}
85 changes: 85 additions & 0 deletions client/src/commandHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import * as fs from 'fs';
import * as path from 'path';
import { TypeDefinitionRequest } from 'vscode-languageclient';

let outputChannel: vscode.OutputChannel

export function setOutputChannel(oc : vscode.OutputChannel) {
outputChannel = oc;
}

export async function exportArchive(file: vscode.Uri) {
try {
// HACK - we load the module lazily so that process.browser is set
Expand Down Expand Up @@ -80,5 +86,84 @@ export async function exportClassDiagram(file: vscode.Uri) {
vscode.window.showErrorMessage(`Error ${error}`);
}

return false;
}

export async function triggerClause(file: vscode.Uri) {
try {
// HACK - we load the module lazily so that process.browser is set
// (see extension.ts)
outputChannel.show();

const Template = require('@accordproject/cicero-core').Template;
const Clause = require('@accordproject/cicero-core').Clause;
const Engine = require('@accordproject/cicero-engine').Engine;

const template = await Template.fromDirectory(file.path);
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 statePath = path.join(file.path, 'state.json');

if(!fs.existsSync(statePath)) {
vscode.window.showErrorMessage('Cannot trigger: /state.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 state = JSON.parse(fs.readFileSync( requestPath, 'utf8'));

outputChannel.appendLine( 'state.json' );
outputChannel.appendLine( '==========' );
outputChannel.appendLine( JSON.stringify(state, null, 2) );
outputChannel.appendLine( '' );

const engine = new Engine();
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;
}
26 changes: 20 additions & 6 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as path from 'path';
import * as vscode from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient';

import { exportArchive, downloadModels, exportClassDiagram } from './commandHandlers';
import { exportArchive, downloadModels, exportClassDiagram, triggerClause, setOutputChannel } from './commandHandlers';

let client: LanguageClient;

Expand Down Expand Up @@ -54,31 +54,45 @@ export function activate(context: vscode.ExtensionContext) {
{scheme: 'file', language: 'markdown', pattern: '**/sample*.md'}
],
synchronize: {
// Synchronize the setting section 'languageServerExample' to the server
// Synchronize the setting section 'Cicero' to the server
configurationSection: 'Cicero',
// Notify the server about file changes to '.clientrc files contain in the workspace
fileEvents: vscode.workspace.createFileSystemWatcher('**/.clientrc')
}
};


// create the output channel
const outputChannel = vscode.window.createOutputChannel('Cicero');
setOutputChannel(outputChannel);

// Register commands
context.subscriptions.push(vscode.commands
.registerCommand('cicero-vscode-extension.exportArchive', exportArchive));
context.subscriptions.push(vscode.commands
.registerCommand('cicero-vscode-extension.downloadModels', downloadModels));
context.subscriptions.push(vscode.commands
.registerCommand('cicero-vscode-extension.exportClassDiagram', exportClassDiagram));

context.subscriptions.push(vscode.commands
.registerCommand('cicero-vscode-extension.triggerClause', triggerClause));

// Create the language client and start the client.
client = new LanguageClient(
'Cicero',
'Cicero',
'cicero',
'Cicero Language Server',
serverOptions,
clientOptions
);

// Start the client. This will also launch the server
client.start();

// extend markdown preview
return {
extendMarkdownIt(md) {
return md.use(require('@accordproject/markdown-it-template'))
.use(require('@accordproject/markdown-it-cicero'))
}
}
}

export function deactivate(): Thenable<void> | undefined {
Expand Down
9 changes: 9 additions & 0 deletions client/src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,13 @@ suite('Extension Tests', () => {
assert.ok(result);
});
});

test('should execute triggerClause command', () => {
const uri = vscode.Uri.file(path.join(rootPath, '../test/data/valid/template/acceptance-of-delivery'));

vscode.commands.executeCommand('cicero-vscode-extension.triggerClause', uri).then((result) => {
assert.ok(result);
});
});

});
22 changes: 19 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Accord Project extension for Visual Studio Code, providing tools for template development.",
"author": "Accord Project",
"license": "Apache-2.0",
"version": "0.21.6",
"version": "0.21.7",
"icon": "icon.png",
"repository": {
"type": "git",
Expand Down Expand Up @@ -32,7 +32,8 @@
"onLanguage:markdown",
"onCommand:cicero-vscode-extension.exportArchive",
"onCommand:cicero-vscode-extension.downloadModels",
"onCommand:cicero-vscode-extension.viewClassDiagram"
"onCommand:cicero-vscode-extension.viewClassDiagram",
"onCommand:cicero-vscode-extension.triggerClause"
],
"main": "./client/out/extension",
"contributes": {
Expand Down Expand Up @@ -112,6 +113,11 @@
"command": "cicero-vscode-extension.exportClassDiagram",
"title": "Export Class Diagram",
"category": "Accord Project"
},
{
"command": "cicero-vscode-extension.triggerClause",
"title": "Trigger",
"category": "Accord Project"
}
],
"menus": {
Expand All @@ -130,6 +136,11 @@
"when": "explorerResourceIsFolder",
"command": "cicero-vscode-extension.exportClassDiagram",
"group": "AccordProject@3"
},
{
"when": "explorerResourceIsFolder",
"command": "cicero-vscode-extension.triggerClause",
"group": "AccordProject@3"
}
],
"commandPalette": [
Expand All @@ -144,9 +155,14 @@
{
"command": "cicero-vscode-extension.exportClassDiagram",
"when": "false"
},
{
"command": "cicero-vscode-extension.triggerClause",
"when": "false"
}
]
}
},
"markdown.markdownItPlugins": true
},
"scripts": {
"vscode:prepublish": "npm run compile",
Expand Down
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cicero-vscode-extension-server",
"description": "A language server for Accord Project templates",
"version": "0.21.6",
"version": "0.21.7",
"license": "Apache-2.0",
"engines": {
"node": "*"
Expand Down

0 comments on commit 16a717c

Please sign in to comment.