fix(gbdialog): Support for multiples hear blocks.
This commit is contained in:
parent
6915d58db1
commit
3bb9d652fd
3 changed files with 136 additions and 126 deletions
|
@ -64,12 +64,111 @@ export class GBVMService implements IGBCoreService {
|
||||||
|
|
||||||
// Example when handled through fs.watch() listener
|
// Example when handled through fs.watch() listener
|
||||||
fs.watchFile(source, async (curr, prev) => {
|
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);
|
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) {
|
private addHearDialog(min) {
|
||||||
min.dialogs.add(
|
min.dialogs.add(
|
||||||
new WaterfallDialog('/hear', [
|
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 () => { ');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,23 +31,34 @@
|
||||||
'
|
'
|
||||||
'****************************************************************************
|
'****************************************************************************
|
||||||
|
|
||||||
talk ("Please, what's your e-mail address?")
|
talk ("Please, what's your e-mail address?")
|
||||||
email = hear ()
|
talk ("Please, what's your e-mail address?")
|
||||||
talk("Thanks, sending e-mail to: " + email )
|
talk ("Please, what's your e-mail address?")
|
||||||
sendEmail(email, "Message from VBA Bot", "Yes, I can send e-mails.")
|
talk ("Please, what's your e-mail address?")
|
||||||
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?")
|
||||||
|
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
|
|
||||||
|
|
||||||
%>
|
%>
|
11
src/app.ts
11
src/app.ts
|
@ -150,9 +150,10 @@ export class GBServer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// First line to run.
|
// // First line to run.
|
||||||
const path = 'packages/default.gbdialog';
|
const path = 'packages/default.gbdialog';
|
||||||
const file = 'bot.vbs';
|
const file = 'bot.vbs';
|
||||||
const source =(path + '/' + file);
|
const source =(path + '/' + file);
|
||||||
GBVMService.run2(source, path)
|
let s = new GBVMService();
|
||||||
|
s.run(source, path, null, null, null)
|
||||||
//GBServer.run();
|
//GBServer.run();
|
||||||
|
|
Loading…
Add table
Reference in a new issue