From 16a717cdee20d0d29c3ea5e59099b5e927c736df Mon Sep 17 00:00:00 2001 From: Dan Selman Date: Wed, 5 Aug 2020 12:22:20 +0200 Subject: [PATCH] feat - add trigger command Signed-off-by: Dan Selman --- .vscode/extensions.json | 3 +- CHANGELOG.md | 5 ++ README.md | 2 + client/package-lock.json | 12 +++- client/package.json | 5 +- client/src/commandHandlers.ts | 85 +++++++++++++++++++++++++ client/src/extension.ts | 26 ++++++-- client/src/test/suite/extension.test.ts | 9 +++ package.json | 22 ++++++- server/package.json | 2 +- 10 files changed, 158 insertions(+), 13 deletions(-) diff --git a/.vscode/extensions.json b/.vscode/extensions.json index af51550..24c05f2 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -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" ] } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 29c2711..695dc16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 1b7b677..02ad9d8 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/client/package-lock.json b/client/package-lock.json index 9abb74a..de56368 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -1,6 +1,6 @@ { "name": "cicero-vscode-extension-client", - "version": "0.21.5", + "version": "0.21.6", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -67,6 +67,16 @@ } } }, + "@accordproject/cicero-engine": { + "version": "0.21.0", + "resolved": "https://registry.npmjs.org/@accordproject/cicero-engine/-/cicero-engine-0.21.0.tgz", + "integrity": "sha512-MylhOuLB7N9HbIuOuP4Wf0YIIvuxZti69lqZhudLr1JoZ5o0C9evFYJYYnlOLq4UFC9A4LFvlWKElkcxnfvmBA==", + "requires": { + "@accordproject/cicero-core": "0.21.0", + "@accordproject/ergo-engine": "^0.21.0", + "moment-mini": "2.22.1" + } + }, "@accordproject/concerto-core": { "version": "0.82.8", "resolved": "https://registry.npmjs.org/@accordproject/concerto-core/-/concerto-core-0.82.8.tgz", diff --git a/client/package.json b/client/package.json index 5ea0820..7b4607a 100755 --- a/client/package.json +++ b/client/package.json @@ -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", @@ -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" } } diff --git a/client/src/commandHandlers.ts b/client/src/commandHandlers.ts index fd1f02f..7463a9d 100755 --- a/client/src/commandHandlers.ts +++ b/client/src/commandHandlers.ts @@ -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 @@ -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; } \ No newline at end of file diff --git a/client/src/extension.ts b/client/src/extension.ts index 502334a..bd36b6a 100755 --- a/client/src/extension.ts +++ b/client/src/extension.ts @@ -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; @@ -54,13 +54,17 @@ 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)); @@ -68,17 +72,27 @@ export function activate(context: vscode.ExtensionContext) { .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 | undefined { diff --git a/client/src/test/suite/extension.test.ts b/client/src/test/suite/extension.test.ts index c74fbee..1bd733a 100755 --- a/client/src/test/suite/extension.test.ts +++ b/client/src/test/suite/extension.test.ts @@ -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); + }); + }); + }); diff --git a/package.json b/package.json index b7ecd14..41828ea 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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": { @@ -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": { @@ -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": [ @@ -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", diff --git a/server/package.json b/server/package.json index 37b147b..63b83ff 100755 --- a/server/package.json +++ b/server/package.json @@ -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": "*"