decova-gotcha
Version:
It's my personal trial for automating daunting tasks
345 lines • 16 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExecFromSheet = void 0;
const WalkthroughsSheet_1 = require("./WalkthroughsSheet");
const Intellisense_1 = require("./Intellisense");
const TerminalAgent_1 = require("./TerminalAgent");
const LTool_CheckGotchaLocalRepo_1 = require("../local-tools-impl/LTool_CheckGotchaLocalRepo");
const CommonMenu_1 = require("../local-tools-impl/Techies/CommonMenu");
const PathMan_1 = require("../local-tools-impl/Techies/PathMan");
const Hub_1 = require("../Hub");
const tsyringe_1 = require("tsyringe");
const LocalToolsDispatcher_1 = require("../local-tools-impl/LocalToolsDispatcher");
const decova_dotnet_1 = require("decova-dotnet");
class ExecFromSheet {
constructor() {
this.srv_PathMan = tsyringe_1.container.resolve(PathMan_1.PathMan);
this.srv_WalkthroughsSheet = tsyringe_1.container.resolve(WalkthroughsSheet_1.WalkthroughsSheet);
this.srv_LTool_CheckGotchaLocalRepo = tsyringe_1.container.resolve(LTool_CheckGotchaLocalRepo_1.LTool_CheckOutGotchaLocalRepo);
this.srv_LocalToolsDispatcher = tsyringe_1.container.resolve(LocalToolsDispatcher_1.LocalToolsDispatcher);
this._currentAggregation = null;
}
HandlePromptTextAsync(prompt, vars) {
return __awaiter(this, void 0, void 0, function* () {
if (!prompt)
TerminalAgent_1.TerminalAgent.ShowError(`prompt argument cannot be null 8912`);
if (!vars)
TerminalAgent_1.TerminalAgent.ShowError(`vars argument cannot be null`);
if (!prompt.VarNameComposer)
TerminalAgent_1.TerminalAgent.ShowError(`prompt.VarNameComposer cannot be null 216654`);
const varName = this.CompileScript(prompt.VarNameComposer, vars);
if (!varName) {
}
do {
const quest = this.CompileScript(prompt.QuestionComposer, vars);
const ans = yield TerminalAgent_1.TerminalAgent.AskForTextAsync(quest);
const hasPattern = String.xIsNullOrWhiteSpace(prompt.Regex) == false;
if (hasPattern) {
const regex = new RegExp(prompt.Regex);
if (regex.test(ans.trim()) == false) {
TerminalAgent_1.TerminalAgent.ShowError(`[${varName}] doesn't match the pattern ${prompt.Regex}`);
continue;
}
}
vars.xEnsure(varName, ans, true);
} while (vars.xContains(varName) == false);
});
}
HandleMcqAsync(mcq, vars) {
return __awaiter(this, void 0, void 0, function* () {
const plainOptions = this.CompileScript(mcq.OptionsComposer, vars);
const options = plainOptions.xSelect(op => ({ label: op }));
const intelli = new Intellisense_1.Intellisense(options, (op) => op.label);
const question = this.CompileScript(mcq.QuestionComposer, vars);
const varName = this.CompileScript(mcq.VarNameComposer, vars);
TerminalAgent_1.TerminalAgent.ShowQuestion(question);
const ans = yield intelli.PromptAsync('>>>');
vars.xEnsure(varName, ans.label, true);
});
}
CompileScript(composer, vars) {
let output = composer;
for (let key of vars.xKeys()) {
output = output.xReplaceAll(`<${key}>`, `${vars.xGet(key)}`);
}
try {
if (output.xStartsWith("(")) {
const func = eval(output);
if (func.constructor != Function) {
TerminalAgent_1.TerminalAgent.ShowError(`Error evaluating expression: ${output} 98513`);
throw new decova_dotnet_1.Exception(`Error evaluating expression: ${output} 98513`);
}
return func.call([]);
}
else {
if (output == null || output == '') {
TerminalAgent_1.TerminalAgent.ShowError(`Expression (${composer}) was evaluated to null or empty 24165489`);
throw `Error evaluating expression: ${output}`;
}
return eval(output);
}
}
catch (err) {
TerminalAgent_1.TerminalAgent.ShowError(`Error evaluating expression: ${output} 12468;\r\n ${err}`);
throw `Error evaluating expression: ${output}`;
}
}
HandleAggregatorAsync(aggregator, vars) {
return __awaiter(this, void 0, void 0, function* () {
this._currentAggregation = [];
for (let step of aggregator.Steps) {
yield this.HandleStep(step, vars);
}
const finalCommand = this._currentAggregation.join('');
vars.xAdd(aggregator.OutputVarName, finalCommand);
this._currentAggregation = null;
});
}
HandleIfStatementAsync(ifStatement, vars) {
return __awaiter(this, void 0, void 0, function* () {
let expValue = undefined;
try {
expValue = this.CompileScript(ifStatement.ExpressionComposer, vars);
}
catch (err) {
TerminalAgent_1.TerminalAgent.ShowError(`Cannot evaluate expression [${ifStatement.ExpressionComposer}]`);
return;
}
if (expValue) {
for (let step of ifStatement.Steps) {
yield this.HandleStep(step, vars);
}
}
});
}
HandleWhileStatementAsync(whileStatement, vars) {
return __awaiter(this, void 0, void 0, function* () {
let expValue = undefined;
try {
expValue = this.CompileScript(whileStatement.ExpressionComposer, vars);
}
catch (err) {
TerminalAgent_1.TerminalAgent.ShowError(`Cannot evaluate expression [${whileStatement.ExpressionComposer}]`);
return;
}
while (expValue) {
for (let step of whileStatement.Steps) {
yield this.HandleStep(step, vars);
}
expValue = this.CompileScript(whileStatement.ExpressionComposer, vars);
}
});
}
HandleSetVar(setVarStatement, vars) {
return __awaiter(this, void 0, void 0, function* () {
let varName = undefined;
let expressionValue = undefined;
try {
varName = this.CompileScript(setVarStatement.VarNameComposer, vars);
}
catch (err) {
TerminalAgent_1.TerminalAgent.ShowError(`Cannot evaluate expression [${setVarStatement.ExpressionComposer}]`);
return;
}
try {
const expression = this.CompileScript(setVarStatement.ExpressionComposer, vars);
try {
expressionValue = eval(expression);
}
catch (err) {
TerminalAgent_1.TerminalAgent.ShowError(`Cannot evaluate expression [${expression}]`);
return;
}
}
catch (err) {
TerminalAgent_1.TerminalAgent.ShowError(`Cannot evaluate expression [${setVarStatement.ExpressionComposer}]`);
return;
}
vars.xSet(varName, expressionValue);
});
}
HandleRepeaterAsync(repeater, vars) {
return __awaiter(this, void 0, void 0, function* () {
while (true) {
const doRepeat = yield TerminalAgent_1.TerminalAgent.YesNoQuestionAsync(repeater.DoRepeatQuestionComposer);
if (!doRepeat) {
break;
}
for (let step of repeater.IterationSteps) {
yield this.HandleStep(step, vars);
}
}
});
}
HandleCommandAsync(command, vars) {
return __awaiter(this, void 0, void 0, function* () {
const hintIsApplicable = this._currentAggregation == null && command.WillDoHintComposer;
if (hintIsApplicable) {
const hint = this.CompileScript(command.WillDoHintComposer, vars);
TerminalAgent_1.TerminalAgent.Hint(hint);
}
const output = this.CompileScript(command.CommandComposer, vars);
TerminalAgent_1.TerminalAgent.ShowSuccess(output);
if (this._currentAggregation != null) {
this._currentAggregation.push(output);
}
else if (command.SkipPrompt) {
TerminalAgent_1.TerminalAgent.Exec(output);
}
else {
const ans = yield CommonMenu_1.CommonMenu.ShowContinueSkipAsync('>>>');
if (ans) {
const commandParts = output.split(/\s+/g);
const args = commandParts.length < 3 ? '' : commandParts.xSkip(2).join(' ');
const itWasLocalTool = yield this.srv_LocalToolsDispatcher.TryAimToolAsync(commandParts[1], args);
if (!itWasLocalTool) {
TerminalAgent_1.TerminalAgent.Exec(output);
}
}
else {
TerminalAgent_1.TerminalAgent.ShowError("Command skipped!");
}
}
});
}
HandleInstructionAsync(instruction, vars) {
return __awaiter(this, void 0, void 0, function* () {
let output = yield this.CompileScript(instruction.InstructionComposer, vars);
TerminalAgent_1.TerminalAgent.Instruct(output);
const ans = yield CommonMenu_1.CommonMenu.ShowContinueSkipAsync('>>>');
if (ans) {
TerminalAgent_1.TerminalAgent.ShowSuccess("Done!");
}
else {
TerminalAgent_1.TerminalAgent.ShowError("Instruction skipped!");
}
});
}
HandleStep(step, vars) {
return __awaiter(this, void 0, void 0, function* () {
if (!step)
throw 'step argument cannot be null';
if (!vars)
throw 'vars argument cannot be null';
if (step.RunOnlyIfComposer) {
const condition = this.CompileScript(step.RunOnlyIfComposer, vars);
if (!condition) {
return;
}
}
if (step.Command) {
yield this.HandleCommandAsync(step.Command, vars);
}
else if (step.Instruction) {
yield this.HandleInstructionAsync(step.Instruction, vars);
}
else if (step.Mcq) {
yield this.HandleMcqAsync(step.Mcq, vars);
}
else if (step.Prompt) {
yield this.HandlePromptTextAsync(step.Prompt, vars);
}
else if (step.Repeater) {
yield this.HandleRepeaterAsync(step.Repeater, vars);
}
else if (step.Aggregator) {
yield this.HandleAggregatorAsync(step.Aggregator, vars);
}
else if (step.If) {
yield this.HandleIfStatementAsync(step.If, vars);
}
else if (step.While) {
yield this.HandleWhileStatementAsync(step.While, vars);
}
else if (step.SetVar) {
yield this.HandleSetVar(step.SetVar, vars);
}
});
}
HanldeWalkthrough(wk) {
return __awaiter(this, void 0, void 0, function* () {
if (!wk)
TerminalAgent_1.TerminalAgent.ShowError(`wk must be a non-null IWalkthrough`);
if (wk.IsCommentedOut)
return;
const vars = new Map();
for (let step of wk.Steps) {
yield this.HandleStep(step, vars);
}
;
});
}
WalkthroughFromLocalTool(localTool) {
return {
Title: `Ops >> Gotcha >> Local Tool >> {g ${localTool.GetShortcut()}} ${localTool.GetHint()}`,
IsCommentedOut: false,
Steps: [
{
Command: {
CommandComposer: `'g ${localTool.GetShortcut()}'`,
WillDoHintComposer: `'>>>'`,
SkipPrompt: false
},
Instruction: undefined,
Mcq: undefined,
Prompt: undefined,
Repeater: undefined,
RunOnlyIfComposer: undefined,
Aggregator: undefined,
If: undefined,
While: undefined,
SetVar: undefined
}
],
Shortcut: undefined
};
}
TakeControlAsync(shortcut) {
return __awaiter(this, void 0, void 0, function* () {
while (this.srv_WalkthroughsSheet.FileExists() == false) {
TerminalAgent_1.TerminalAgent.ShowError(`Walkthroughs sheet doesn't exist in the local repo!`);
yield LocalToolsDispatcher_1.LocalToolsDispatcher.RunAsync(this.srv_LTool_CheckGotchaLocalRepo);
TerminalAgent_1.TerminalAgent.Hint(this.srv_PathMan.GotchaLocalRepo_WalkthroughsSheet.FullName);
}
let allWks = WalkthroughsSheet_1.WalkthroughsSheet.Singleton.WalkthroughList;
const localToolsAsWalkthroughs = this.srv_LocalToolsDispatcher.RegisteredTools.xSelect(this.WalkthroughFromLocalTool);
allWks.xAddRange(localToolsAsWalkthroughs);
allWks = allWks.xSort((w1, w2) => w1.Title.localeCompare(w2.Title));
if (shortcut) {
yield this.HandleWalkthroughOfShortcut(allWks, shortcut);
}
else {
yield this.PromptUserToPickAndRunWalkthrough(allWks);
}
});
}
HandleWalkthroughOfShortcut(allWks, shortcut) {
return __awaiter(this, void 0, void 0, function* () {
const wk = allWks.xFirstOrNull(wk => { var _a; return ((_a = wk.Shortcut) === null || _a === void 0 ? void 0 : _a.toLowerCase()) == shortcut.toLowerCase(); });
if (!wk) {
Hub_1.Terminal.DisplayErrorAsync(`The shortcut [${shortcut}] wasn't recognized by Gotcha!`);
}
else {
yield this.HanldeWalkthrough(wk);
}
});
}
PromptUserToPickAndRunWalkthrough(allWks) {
return __awaiter(this, void 0, void 0, function* () {
const intelli = new Intellisense_1.Intellisense(allWks, (op) => op.Title);
let ans = yield intelli.PromptAsync('>>>');
yield this.HanldeWalkthrough(ans);
});
}
}
exports.ExecFromSheet = ExecFromSheet;
//# sourceMappingURL=ExecFromSheet.js.map