From 6915d58db178133ae514fd4d9fbdb5dce8caed5d Mon Sep 17 00:00:00 2001 From: "Rodrigo Rodriguez (pragmatismo.io)" Date: Sat, 1 Dec 2018 20:48:08 -0200 Subject: [PATCH] fix(gbdialog): VBA hear must be a wrapper call. --- packages/core.gbapp/services/GBVMService.ts | 76 ++++++++++++++++++++- packages/default.gbdialog/bot.vbs | 25 +++++-- src/app.ts | 8 ++- 3 files changed, 100 insertions(+), 9 deletions(-) diff --git a/packages/core.gbapp/services/GBVMService.ts b/packages/core.gbapp/services/GBVMService.ts index 0e5e4b3a..e17f6dad 100644 --- a/packages/core.gbapp/services/GBVMService.ts +++ b/packages/core.gbapp/services/GBVMService.ts @@ -102,7 +102,7 @@ export class GBVMService implements IGBCoreService { // Convert TS into JS. const tsfile = `bot.ts`; const tsc = new TSCompiler(); - // TODO: tsc.compile([UrlJoin(path, tsfile)]); + tsc.compile([UrlJoin(path, tsfile)]); // Run JS into the GB context. const jsfile = `bot.js`; localPath = UrlJoin(path, jsfile); @@ -112,8 +112,24 @@ export class GBVMService implements IGBCoreService { code = code.replace(/^.*exports.*$/gm, ''); code = code.replace(/this\./gm, 'await this.'); code = code.replace(/function/gm, 'async function'); + var match; + let finalCode: string; + let pos = 0; + while ((match = /hear.*\(\)/.exec(code)) != null) { + pos = match.index; + console.log(pos); + finalCode += code.substring(0, pos); + let nextCode = code.substring(pos); + + // Find last } + + while ((match = /\{|\}/g.exec(nextCode)) != null) { + console.log(match.index); + } + } + //code = code.replace(/this\.hear\(\){/gm, 'this.hear(async () => { '); - + const sandbox: DialogClass = new DialogClass(min); const context = vm.createContext(sandbox); vm.runInContext(code, context); @@ -122,4 +138,60 @@ export class GBVMService implements IGBCoreService { logger.info(`[GBVMService] Finished loading of ${filename}`); } } + + public static async run2(source: any, path: string) { + // Converts VBS into TS. + + //vb2ts.convertFile(source); + + // Convert TS into JS. + const tsfile = `bot.ts`; + const tsc = new TSCompiler(); + //tsc.compile([UrlJoin(path, tsfile)]); + // Run JS into the GB context. + const jsfile = `bot.js`; + let localPath = UrlJoin(path, jsfile); + + if (fs.existsSync(localPath)) { + let code: string = fs.readFileSync(localPath, 'utf8'); + code = code.replace(/^.*exports.*$/gm, ''); + code = code.replace(/this\./gm, 'await this.'); + code = code.replace(/function/gm, 'async function'); + var match1; + var match2; + let finalCode: string; + let pos = 0; + while ((match1 = /hear.*\(\)/.exec(code))) { + pos = match1.index; + console.log(pos); + finalCode += code.substring(0, pos); + let nextCode = code.substring(pos); + + // Find last } + let right = 0; + let left = 1; + + while ((match2 = /\{|\}/.exec(nextCode))) { + let c = nextCode.substring(match2.index, match2.index+1); + if (c === '}') { + right++; + } else if (c === '{') { + left++; + } + + nextCode = nextCode.substring(match2.index + 1); + + if (left == right) + { + console.log('end '+match2.index); + } + console.log(match2.index); + } + } + + console.log(finalCode); + + //code = code.replace(/this\.hear\(\){/gm, 'this.hear(async () => { '); + } + } } diff --git a/packages/default.gbdialog/bot.vbs b/packages/default.gbdialog/bot.vbs index ffb1425a..a0fa9b17 100644 --- a/packages/default.gbdialog/bot.vbs +++ b/packages/default.gbdialog/bot.vbs @@ -31,8 +31,23 @@ ' '**************************************************************************** -this.talk ("Please, what's your e-mail address?") -let email = this.hear() -this.talk("Thanks, sending e-mail to: " + email); -this.sendEmail(email, "Message from VBA Bot", "Yes, I can send e-mails."); -%> \ No newline at end of file +talk ("Please, what's your e-mail address?") +email = hear () +talk("Thanks, sending e-mail to: " + email ) +sendEmail(email, "Message from VBA Bot", "Yes, I can send e-mails.") +if email = "" then + +end if + +select case email + case 1: + + case 2: + +end select + +if i > 10 then + +end if + +%> \ No newline at end of file diff --git a/src/app.ts b/src/app.ts index 01a438f7..1dece0c6 100644 --- a/src/app.ts +++ b/src/app.ts @@ -50,6 +50,7 @@ import { GBCoreService } from '../packages/core.gbapp/services/GBCoreService'; import { GBDeployer } from '../packages/core.gbapp/services/GBDeployer'; import { GBImporter } from '../packages/core.gbapp/services/GBImporterService'; import { GBMinService } from '../packages/core.gbapp/services/GBMinService'; +import { GBVMService } from '../packages/core.gbapp/services/GBVMService'; const appPackages = new Array(); @@ -150,5 +151,8 @@ export class GBServer { } // First line to run. - -GBServer.run(); +const path = 'packages/default.gbdialog'; +const file = 'bot.vbs'; +const source =(path + '/' + file); +GBVMService.run2(source, path) +//GBServer.run();