From 3bb9d652fd732f2f57314901c4e7c491bd9e7062 Mon Sep 17 00:00:00 2001 From: "Rodrigo Rodriguez (pragmatismo.io)" Date: Sat, 1 Dec 2018 23:01:42 -0200 Subject: [PATCH] fix(gbdialog): Support for multiples hear blocks. --- packages/core.gbapp/services/GBVMService.ts | 204 ++++++++++---------- packages/default.gbdialog/bot.vbs | 47 +++-- src/app.ts | 11 +- 3 files changed, 136 insertions(+), 126 deletions(-) diff --git a/packages/core.gbapp/services/GBVMService.ts b/packages/core.gbapp/services/GBVMService.ts index e17f6dad..a281aeb0 100644 --- a/packages/core.gbapp/services/GBVMService.ts +++ b/packages/core.gbapp/services/GBVMService.ts @@ -64,12 +64,111 @@ export class GBVMService implements IGBCoreService { // Example when handled through fs.watch() listener fs.watchFile(source, async (curr, prev) => { - await this.run(source, path, localPath, min, deployer, filename); + await this.run(source, path, min, deployer, filename); }); - await this.run(source, path, localPath, min, deployer, filename); + await this.run(source, path, min, deployer, filename); this.addHearDialog(min); } + public async run(source: any, path: string, min: any, deployer: GBDeployer, filename: 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, ''); + let match1; + let match2; + let finalCode = ''; + let pos = 0; + let nextCode = code; + let hearExp = /(\w+).*hear.*\(\)/; + + while (match1 = hearExp.exec(nextCode)) { + + // Write async body. + + const variable = match1[1]; // variable = hear(); + + finalCode += code.substring(pos, pos + match1.index); + finalCode += `hear (async (${variable}) => {\n`; + + // Skip old construction and point to the async block. + + pos = pos + match1.index; + nextCode = code.substring(pos + match1[0].length + 1); + let start = pos; + + // Find last } + + let right = 0; + let left = 1; + while ((match2 = /\{|\}/.exec(nextCode))) { + const c = nextCode.substring(match2.index, match2.index + 1); + + if (c === '}') { + right++; + } else if (c === '{') { + left++; + } + + let match3 + if (match3 = hearExp.exec(nextCode)) + { + nextCode = nextCode.substring(match3.index + 1); + pos += match3.index; + break; + } + + nextCode = nextCode.substring(match2.index + 1); + pos += match2.index + 1; + + if (left === right) { + break; + } + + } + + finalCode += code.substring(start + match1[0].length + 1, pos + match1[0].length); + finalCode += '});\n'; + + nextCode = code.substring(pos + match1[0].length); + } + + finalCode = finalCode.replace(/("[^"]*"|'[^']*')|\btalk\b/g, function($0, $1) { + return $1 == undefined ? 'this.talk' : $1; + }); + + finalCode = finalCode.replace(/("[^"]*"|'[^']*')|\bhear\b/g, function($0, $1) { + return $1 == undefined ? 'this.hear' : $1; + }); + + finalCode = finalCode.replace(/("[^"]*"|'[^']*')|\bsendEmail\b/g, function($0, $1) { + return $1 == undefined ? 'this.sendEmail' : $1; + }); + + finalCode = finalCode.replace(/this\./gm, 'await this.'); + finalCode = finalCode.replace(/function/gm, 'async function'); + console.log(finalCode); + + const sandbox: DialogClass = new DialogClass(min); + const context = vm.createContext(sandbox); + vm.runInContext(finalCode, context); + min.sandbox = sandbox; + await deployer.deployScriptToStorage(min.instanceId, filename); + logger.info(`[GBVMService] Finished loading of ${filename}`); + } + } + private addHearDialog(min) { min.dialogs.add( new WaterfallDialog('/hear', [ @@ -93,105 +192,4 @@ export class GBVMService implements IGBCoreService { ]) ); } - - private async run(source: any, path: string, localPath: string, min: any, deployer: GBDeployer, filename: 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`; - 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 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); - min.sandbox = sandbox; - await deployer.deployScriptToStorage(min.instanceId, filename); - 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 a0fa9b17..e065b770 100644 --- a/packages/default.gbdialog/bot.vbs +++ b/packages/default.gbdialog/bot.vbs @@ -31,23 +31,34 @@ ' '**************************************************************************** -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 + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + talk ("Please, what's your e-mail address?") + 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.") -end if + name = hear () + talk("Hey " + name + "!") + -select case email - case 1: - - case 2: - -end select - -if i > 10 then - -end if - -%> \ No newline at end of file +%> \ No newline at end of file diff --git a/src/app.ts b/src/app.ts index 1dece0c6..86da8a7f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -150,9 +150,10 @@ export class GBServer { } } -// First line to run. -const path = 'packages/default.gbdialog'; -const file = 'bot.vbs'; -const source =(path + '/' + file); -GBVMService.run2(source, path) +// // First line to run. + const path = 'packages/default.gbdialog'; + const file = 'bot.vbs'; + const source =(path + '/' + file); + let s = new GBVMService(); + s.run(source, path, null, null, null) //GBServer.run();