UNPKG

maestro-cli-roku

Version:

command line tools for maestro-roku projects

200 lines (199 loc) 9.65 kB
"use strict"; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); var FileType_1 = require("../fileProcessing/FileType"); var XMLTag_1 = require("../fileProcessing/XMLTag"); var Feedback_1 = require("../utils/Feedback"); var Utils_1 = require("../utils/Utils"); var BindingType_1 = require("./BindingType"); // const xmldoc = require('../utils/xmldoc'); var BindingProcessor = /** @class */ (function () { function BindingProcessor() { } BindingProcessor.prototype.processFile = function (file) { var e_1, _a, e_2, _b; if (!file || file.fileType !== FileType_1.FileType.Xml && file.fileType !== FileType_1.FileType.ViewXml) { throw new Error('was given a non-xml file'); } if (file.hasProcessedBindings) { return; } if (file.parentFile && !file.parentFile.hasProcessedBindings) { //we must process and update the parent first, otherwise we can't filter the imports this.processFile(file.parentFile); } var tagsWithBindings = this.getTagsWithBindings(file); var fileContents = file.getFileContents(); try { for (var tagsWithBindings_1 = __values(tagsWithBindings), tagsWithBindings_1_1 = tagsWithBindings_1.next(); !tagsWithBindings_1_1.done; tagsWithBindings_1_1 = tagsWithBindings_1.next()) { var tag = tagsWithBindings_1_1.value; try { for (var _c = (e_2 = void 0, __values(tag.bindings)), _d = _c.next(); !_d.done; _d = _c.next()) { var binding = _d.value; file.componentIds.add(binding.nodeId); file.bindings.push(binding); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_d && !_d.done && (_b = _c.return)) _b.call(_c); } finally { if (e_2) throw e_2.error; } } fileContents = Utils_1.spliceString(fileContents, tag.startPosition, tag.text); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (tagsWithBindings_1_1 && !tagsWithBindings_1_1.done && (_a = tagsWithBindings_1.return)) _a.call(tagsWithBindings_1); } finally { if (e_1) throw e_1.error; } } if (file.associatedFile) { this.addFindNodeVarsMethodToFile(file.associatedFile); } if (tagsWithBindings.length > 0) { file.setFileContents(fileContents); if (file.associatedFile) { this.addBindingMethodsToFile(file.associatedFile); } else { Feedback_1.feedbackWarning(file, 'This XML file has bindings; but there is no code behind file!'); } } file.hasProcessedBindings = true; }; BindingProcessor.prototype.getTagsWithBindings = function (file) { var tagsWithBindings = []; try { var allParentIds_1 = file.associatedFile ? file.associatedFile.getAllParentTagIds() : null; var allParentFieldIds_1 = file.associatedFile ? file.associatedFile.getAllParentFieldIds() : null; var fileContents_1 = file.getFileContents(); var doc = file.xmlDoc; // const doc = new xmldoc.XmlDocument(fileContents); doc.allElements.filter(function (xmlElement) { return xmlElement.name.toLowerCase() !== 'interface' && xmlElement.name.toLowerCase() !== 'function' && xmlElement.name.toLowerCase() !== 'script' && xmlElement.name.toLowerCase() !== 'children'; }).forEach(function (xmlElement) { var tagText = fileContents_1.substring(xmlElement.startTagPosition, xmlElement.endTagPosition); xmlElement.children = []; var tag = new XMLTag_1.XMLTag(xmlElement, tagText, file); if (tag.isTopTag) { if (tag.id && file.associatedFile) { if (file.associatedFile.fieldIds.has(tag.id)) { Feedback_1.feedbackError(file, 'xml contains duplicate field id: ' + tag.id); } else if (allParentFieldIds_1.has(tag.id)) { Feedback_1.feedbackError(file, 'a parent of this xml file contains duplicate field id: ' + tag.id); } else { file.associatedFile.fieldIds.add(tag.id); } } } else { if (tag.id && file.associatedFile) { if (file.associatedFile.tagIds.has(tag.id)) { Feedback_1.feedbackError(file, 'xml contains duplicate tag id: ' + tag.id); } else if (allParentIds_1.has(tag.id)) { Feedback_1.feedbackError(file, 'a parent of this xml file contains duplicate tag id: ' + tag.id); } else { file.associatedFile.tagIds.add(tag.id); } } } if (tag.bindings.length > 0) { tagsWithBindings.push(tag); } }); } catch (e) { Feedback_1.feedbackError(file, 'Could not parse xml in file: ' + e.message); } return tagsWithBindings; }; BindingProcessor.prototype.addBindingMethodsToFile = function (file) { var bindings = file.associatedFile.bindings.concat(file.associatedFile.getAllParentBindings()); if (bindings.length > 0) { file.setFileContents(file.getFileContents() + '\n\n' + this.getBindingInitMethod(bindings.filter(function (b) { return (b.properties.type !== BindingType_1.BindingType.static && b.properties.type !== BindingType_1.BindingType.code); })) + '\n\n' + this.getStaticBindingsMethod(bindings.filter(function (b) { return b.properties.type === BindingType_1.BindingType.static || b.properties.type === BindingType_1.BindingType.code; }))); } }; BindingProcessor.prototype.getBindingInitMethod = function (bindings) { var funcText = 'function M_initBindings()'; var nodeIds = __spread(new Set(bindings.filter(function (b) { return !b.isTopBinding; }).map(function (b) { return b.nodeId; }))); funcText += '\n if m.vm <> invalid'; if (nodeIds.length > 0) { funcText += '\n M_createNodeVars()'; } funcText += '\n'; bindings.forEach(function (b) { return funcText += "\n " + b.getInitText(); }); funcText += '\n m.vm.onBindingsConfigured()'; funcText += '\n end if'; funcText += '\n'; funcText += '\nend function'; return funcText; }; BindingProcessor.prototype.getStaticBindingsMethod = function (bindings) { var funcText = 'function M_initStaticBindings()'; var nodeIds = __spread(new Set(bindings.filter(function (b) { return !b.isTopBinding; }).map(function (b) { return b.nodeId; }))); funcText += '\n if m.vm <> invalid'; if (nodeIds.length > 0) { funcText += '\n M_createNodeVars()'; } bindings.forEach(function (b) { return funcText += "\n " + b.getStaticText(); }); funcText += '\n end if'; funcText += '\n'; funcText += '\nend function'; return funcText; }; BindingProcessor.prototype.addFindNodeVarsMethodToFile = function (file) { var tagIds = Array.from(file.getAllParentTagIds().values()).concat(Array.from(file.tagIds.values())); if (tagIds.length > 0) { var funcText = 'function M_createNodeVars()'; funcText += '\n if m._isCreateNodeVarsCalled = true then return invalid else m._isCreateNodeVarsCalled = true'; funcText += '\n findNodes([' + tagIds.map(function (id) { return "\"" + id + "\""; }).join(','); funcText += '])\n'; funcText += '\nend function'; file.setFileContents(file.getFileContents() + '\n\n' + funcText); } }; return BindingProcessor; }()); exports.BindingProcessor = BindingProcessor;