UNPKG

gulp-mu-build-filter

Version:

Filtering files by build commands.

814 lines (782 loc) 30.3 kB
// =========================================== // gulp-mu-build-filter // © 2016-2024 Meinolf Amekudzi // =========================================== // // This gulp plugin filters files by build and client properties. // Areas of code can be defined for special builds and clients by start and end comments. var through = require('through2'); var PluginError = require('gulp-util').PluginError; var PLUGIN_NAME = 'gulp-mu-build-filter'; var consoleFgRed = "\x1b[31m"; var consoleReset = "\x1b[0m"; var consoleFgBlue = "\x1b[34m"; var currentFilename = "[Value Text]"; Array.prototype.IsIntersectionOf = function (_a) { var i, l = this.length; if (typeof _a == "string") { return this.indexOf(_a) >= 0; } else if (_Array.isArray(_a)) { for (i = 0; i < l; i++)if (_a.indexOf(this[i]) >= 0) return true; } return false; } Array.prototype.ContainsAllOf = function (_a) { var i, l = this.length; if (typeof _a == "string") { for (i = 0; i < l; i++)if (_a.indexOf(this[i]) < 0) return false; } else if (_Array.isArray(_a)) { return a.sort().toString() == b.sort().toString() } else { return false; } return true; } Array.prototype.IsEqualTo = function (_a) { return this.join(",") == _a.join(","); } Array.prototype.IsVersionVisible = function (_version) { var vs = parseFloat(_version); if (isNaN(vs)) return true; var w = (_version+"").split("."); if (w.length > 1) { _version = parseInt(w[0], 10) + parseInt(w[1], 10) / 100000; } else { _version = parseInt(w[0], 10); } ret = false; for (var i = 0; i < this.length; i++) { var p = this[i]+""; var v = parseFloat(p); if (isNaN(v)) { ret = true; } else { w = p.split("."); if (w.length > 1) { v = parseInt(w[0], 10) + parseInt(w[1], 10) / 100000; } else { v = parseInt(w[0], 10); } switch (p[p.length - 1]) { case "+": if (_version >= v) ret = true; break; case "-": if (_version < v) ret = true; break; default: if (_version == v) ret = true; } } } return ret; } Array.prototype.GetVisibleLevel = function (_lev) { // returns // 0: level is full visible (inactive comment, comment will filter code) // 1: level is not visible (active comment, comment will not filter code) // 2: level is not visible (inneractive comment, comment is inside upper filter code) var i, noOfHiddens = 0; for (i = _lev; i > 0; i--) { if (!this[i]) { noOfHiddens++; if (noOfHiddens == 2) break; }; } if (this[_lev] && noOfHiddens == 1) { noOfHiddens = 2; } if (!this[_lev] && noOfHiddens == 1) { noOfHiddens = 1; } return noOfHiddens; } String.prototype.RestorePrefix = function (_orgLine) { var pref = ""; var i, l = _orgLine.length; for (i = 0; i < l; i++) { if (_orgLine.substr(i, 1) == " " || _orgLine.substr(i, 1) == "\t") { pref += _orgLine.substr(i, 1); } else { break; } } return pref + this.valueOf(); }; Number.prototype.GetActivityText = function () { switch (this.valueOf()) { case 0: return "inactive"; case 1: return "active"; case 2: return "inneractive"; } return ""; }; function MuBuildFilterWithCommentsSetup(_opts = null) { if (_opts == null) return ""; var text = "", mode = "remove", release = "*", client = "*", platform = "*", variants = "*", version = "*", unityvariants = null, isDebug = false, isProtocol = false; if (_opts.hasOwnProperty("release")) release = _opts.release; if (_opts.hasOwnProperty("client")) client = _opts.client; if (_opts.hasOwnProperty("platform")) platform = _opts.platform; if (_opts.hasOwnProperty("variants")) variants = _opts.variants.split(","); if (_opts.hasOwnProperty("unityvariants")) unityvariants = _opts.unityvariants; if (_opts.hasOwnProperty("version")) version = _opts.version; if (_opts.hasOwnProperty("text")) text = _opts.text; if (_opts.hasOwnProperty("debug")) isDebug = _opts.debug; if (_opts.hasOwnProperty("protocol")) isProtocol = _opts.protocol; if (Array.isArray(variants)) if (variants.length == 1) if (variants[0] == "*") variants = "*"; var lines = text.split(/\n/igm) var t, i, l = lines.length; var act = false; var ret = text; var activity; var c, b, visibles = [true], notations = [], cmds = [], levelParams = [], levelNo = 0, openclose, tokens, spacepos, colonSeparations, params, cmd, isCmd; //var cmdPrefixes = ["%%%% @", "<%-- @", "/*-- @", "**** @", "//-- @", "<!-- @", "*--- @", "**-- @", "***- @", "---- @"]; var cmdPrefixes = ["<!-- @", "-!-- @", "/*-- @", "//-- @", "<%-- @", "-%-- @", "---- @"]; var cmdInactiveBracketingBegins = { "<!-- @": ["<!--", "--->"], "-!-- @": ["<!--", "--->"], "/*-- @": ["/*--", "--*/"], "//-- @": ["/*--", "--*/"], "<%-- @": ["<%--", "--%>"], "-%-- @": ["<%--", "--%>"], "---- @": ["----", "----"] }; var cmdInactiveBracketingEnds = { "<!-- @": ["<!--", "--->"], "-!-- @": ["<!--", "--->"], "/*-- @": ["/*--", "--*/"], "//-- @": ["/*--", "--*/"], "<%-- @": ["<%--", "--%>"], "-%-- @": ["<%--", "--%>"], "---- @": ["----", "----"] }; var cmdActiveBracketingBegins = { "<!-- @": ["<!--", "----"], "-!-- @": ["<!--", "----"], "/*-- @": ["/*--", "----"], "//-- @": ["/*--", "----"], "<%-- @": ["<%--", "----"], "-%-- @": ["<%--", "----"], "---- @": ["----", "----"] }; var cmdActiveBracketingEnds = { "<!-- @": ["----", "--->"], "-!-- @": ["----", "--->"], "/*-- @": ["----", "--*/"], "//-- @": ["----", "--*/"], "<%-- @": ["----", "--%>"], "-%-- @": ["----", "--%>"], "---- @": ["----", "----"] }; var cmdInnerBracketingBegin = { "<!-- @": ["-!--", "----"], "-!-- @": ["-!--", "----"], "/*-- @": ["//--", "----"], "//-- @": ["----", "----"], "<%-- @": ["-%--", "----"], "-%-- @": ["-%--", "----"], "---- @": ["----", "----"] }; var cmdInnerBracketingEnds = { "<!-- @": ["----", "----"], "-!-- @": ["----", "----"], "/*-- @": ["----", "----"], "//-- @": ["----", "----"], "<%-- @": ["----", "----"], "-%-- @": ["----", "----"], "---- @": ["----", "----"] }; if (unityvariants != null) { for (i = 0; i < l; i++) { t = lines[i].trim(); if (t.substr(0, 15) == "//@ONLY_ANY_OF:") { act = t.substr(15).split(",").IsIntersectionOf(unityvariants); if (!act) t = "/*" + t.substr(2); lines[i] = t.RestorePrefix(lines[i]); } else if (t.substr(0, 15) == "/*@ONLY_ANY_OF:") { act = t.substr(15).split(",").IsIntersectionOf(unityvariants); if (act) t = "//" + t.substr(2); lines[i] = t.RestorePrefix(lines[i]); } else if (t.substr(0, 15) == "//@ONLY_ALL_OF:") { act = t.substr(15).split(",").ContainsAllOf(unityvariants); if (act) t = "//" + t.substr(2); lines[i] = t.RestorePrefix(lines[i]); } else if (t.substr(0, 15) == "/*@ONLY_ALL_OF:") { act = t.substr(15).split(",").ContainsAllOf(unityvariants); if (act) t = "//" + t.substr(2); lines[i] = t.RestorePrefix(lines[i]); } else if (t.substr(0, 16) == "//@NEVER_ANY_OF:") { act = !t.substr(16).split(",").IsIntersectionOf(unityvariants); if (!act) t = "/*" + t.substr(2); lines[i] = t.RestorePrefix(lines[i]); } else if (t.substr(0, 16) == "/*@NEVER_ANY_OF:") { act = !t.substr(16).split(",").IsIntersectionOf(unityvariants); if (act) t = "//" + t.substr(2); lines[i] = t.RestorePrefix(lines[i]); } else if (t.substr(0, 16) == "//@NEVER_ALL_OF:") { act = !t.substr(16).split(",").ContainsAllOf(unityvariants); if (!act) t = "/*" + t.substr(2); lines[i] = t.RestorePrefix(lines[i]); } else if (t.substr(0, 16) == "/*@NEVER_ALL_OF:") { act = !t.substr(16).split(",").ContainsAllOf(unityvariants); if (act) t = "//" + t.substr(2); lines[i] = t.RestorePrefix(lines[i]); } else if (t.substr(0, 8) == "//@END*/") { if (!act) { t = "//@END*/"; } else { t = "//@END"; act = false; } lines[i] = t.RestorePrefix(lines[i]); } else if (t.substr(0, 6) == "//@END") { if (!act) { t = "//@END*/"; } else { t = "//@END"; act = false; } lines[i] = t.RestorePrefix(lines[i]); } } ret = lines.join("\n"); } if (!(release == null || client == null || version == null)) { l = lines.length; for (i = 0; i < l; i++) { b = lines[i].trim(); c = b.substr(0, 6); if (cmdPrefixes.indexOf(c) >= 0) { openclose = b.substr(6, 1); if (openclose == "<" || openclose == ">") { tokens = b.substr(7); spacepos = tokens.indexOf(' '); if (spacepos > 0) { colonSeparations = tokens.substr(0, spacepos).split(':'); if (colonSeparations.length > 1 || openclose == ">") { if (colonSeparations.length > 1) { params = colonSeparations[1].trim().split(','); } else { params = []; }; cmd = colonSeparations[0].trim(); if (openclose == "<") { isCmd = false; switch (cmd) { case "BUILD_NEVER_AT_RELEASES": isCmd = true; levelNo++; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = (params.indexOf(release) < 0 || release == "*"); levelParams[levelNo] = params; notations[levelNo] = c; break; case "BUILD_ONLY_AT_RELEASES": isCmd = true; levelNo++; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = (params.indexOf(release) >= 0 || release == "*"); levelParams[levelNo] = params; notations[levelNo] = c; break; case "BUILD_NEVER_AT_CLIENTS": isCmd = true; levelNo++; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = (params.indexOf(client) < 0 || client == "*"); levelParams[levelNo] = params; notations[levelNo] = c; break; case "BUILD_ONLY_AT_CLIENTS": isCmd = true; levelNo++; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = (params.indexOf(client) >= 0 || client == "*"); levelParams[levelNo] = params; notations[levelNo] = c; break; case "BUILD_NEVER_AT_VERSIONS": isCmd = true; levelNo++; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = (!params.IsVersionVisible(version) || version == "*"); levelParams[levelNo] = params; notations[levelNo] = c; break; case "BUILD_ONLY_AT_VERSIONS": isCmd = true; levelNo++; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = (params.IsVersionVisible(version) || version == "*"); levelParams[levelNo] = params; notations[levelNo] = c; break; case "BUILD_NEVER_AT_PLATFORMS": isCmd = true; levelNo++; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = (params.indexOf(platform) < 0 || platform == "*"); levelParams[levelNo] = params; notations[levelNo] = c; break; case "BUILD_ONLY_AT_PLATFORMS": isCmd = true; levelNo++; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = (params.indexOf(platform) >= 0 || platform == "*"); levelParams[levelNo] = params; notations[levelNo] = c; break; case "BUILD_ONLY_AT_ANY_VARIANTS": isCmd = true; levelNo++; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = (params.IsIntersectionOf(variants) >= 0 || variants == "*"); levelParams[levelNo] = params; notations[levelNo] = c; break; case "BUILD_ONLY_AT_ALL_VARIANTS": isCmd = true; levelNo++; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = (params.ContainsAllOf(variants) >= 0 || variants == "*"); levelParams[levelNo] = params; notations[levelNo] = c; break; case "BUILD_NEVER_AT_ANY_VARIANTS": isCmd = true; levelNo++; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = (!params.IsIntersectionOf(variants) >= 0 || variants == "*"); levelParams[levelNo] = params; notations[levelNo] = c; break; case "BUILD_NEVER_AT_ALL_VARIANTS": isCmd = true; levelNo++; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = (!params.ContainsAllOf(variants) >= 0 || variants == "*"); levelParams[levelNo] = params; notations[levelNo] = c; break; case "BUILD_NEVER_AT_GULP": isCmd = true; levelNo++; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = true; levelParams[levelNo] = params; notations[levelNo] = c; break; case "BUILD_ONLY_AT_GULP": isCmd = true; levelNo++; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = true; levelParams[levelNo] = params; notations[levelNo] = c; break; case "DO_NOT_AT_COMPACT": isCmd = true; levelNo++; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = true; levelParams[levelNo] = params; notations[levelNo] = c; break; } if (isCmd) { activity = visibles.GetVisibleLevel(levelNo); if (isDebug) { console.log('Comment start ' + 'Line: ' + (i + 1) + " " + cmd + ":" + params.join(",") + ' level:' + levelNo + " " + activity.GetActivityText()); //console.log(release, levelNo, params, cmd, i + 1, visibles[levelNo], visibles, visibles.GetVisibleLevel(levelNo), c, cmdInactiveBracketingBegins, cmdInactiveBracketingBegins[c][0], cmdInactiveBracketingBegins[c][1]); } switch (activity) { case 0: lines[i] = (cmdInactiveBracketingBegins[c][0] + " @<" + cmd + ":" + params.join(",") + " " + cmdInactiveBracketingBegins[c][1]).RestorePrefix(lines[i]); break; case 1: lines[i] = (cmdActiveBracketingBegins[c][0] + " @<" + cmd + ":" + params.join(",") + " " + cmdActiveBracketingBegins[c][1]).RestorePrefix(lines[i]); break; case 2: lines[i] = (cmdInnerBracketingBegin[c][0] + " @<" + cmd + ":" + params.join(",") + " " + cmdInnerBracketingBegin[c][1]).RestorePrefix(lines[i]); break; } } } else { switch (cmd) { case "BUILD_ONLY_AT_RELEASES": case "BUILD_NEVER_AT_RELEASES": case "BUILD_ONLY_AT_CLIENTS": case "BUILD_NEVER_AT_CLIENTS": case "BUILD_ONLY_AT_VERSIONS": case "BUILD_NEVER_AT_VERSIONS": case "BUILD_ONLY_AT_PLATFORMS": case "BUILD_NEVER_AT_PLATFORMS": case "BUILD_ONLY_AT_ANY_VARIANTS": case "BUILD_ONLY_AT_ALL_VARIANTS": case "BUILD_NEVER_AT_ANY_VARIANTS": case "BUILD_NEVER_AT_ALL_VARIANTS": case "BUILD_NEVER_AT_GULP": case "BUILD_ONLY_AT_GULP": case "DO_NOT_AT_COMPACT": if (c == "---- @") c = notations[levelNo]; switch (visibles.GetVisibleLevel(levelNo)) { case 0: lines[i] = (cmdInactiveBracketingEnds[c][0] + " @>" + cmd + (params.length > 0 ? ":" : "") + params.join(",") + " " + cmdInactiveBracketingEnds[c][1]).RestorePrefix(lines[i]); break; case 1: lines[i] = (cmdActiveBracketingEnds[c][0] + " @>" + cmd + (params.length > 0 ? ":" : "") + params.join(",") + " " + cmdActiveBracketingEnds[c][1]).RestorePrefix(lines[i]); break; case 2: lines[i] = (cmdInnerBracketingEnds[c][0] + " @>" + cmd + (params.length > 0 ? ":" : "") + params.join(",") + " " + cmdInnerBracketingEnds[c][1]).RestorePrefix(lines[i]); break; } if (levelNo > 0) { if (cmds[levelNo].split(":")[0] != cmd) { if (params.length > 0) { if (!levelParams[levelNo].IsEqualTo(params)) { console.log(consoleFgRed + PLUGIN_NAME + ': Wrong command and wrong parameter nesting! "' + cmds[levelNo] + '" expected "' + levelParams[levelNo].join(",") + '" Line ' + (i + 1) + ' in file "' + currentFilename + '".' + consoleReset); } else { console.log(consoleFgRed + PLUGIN_NAME + ': Wrong command nesting! "' + cmd + '" expected "' + cmds[levelNo].split(":")[0] + '" Line ' + (i + 1) + ' in file "' + currentFilename + '".' + consoleReset); } } } else if (params.length > 0) { if (!levelParams[levelNo].IsEqualTo(params)) { console.log(consoleFgRed + PLUGIN_NAME + ': Wrong parameter nesting! "' + cmd + ":" + params.join(",") + '" expected "' + cmds[levelNo] + '" Line ' + (i + 1) + ' in file "' + currentFilename + '".' + consoleReset); } } } if (levelNo > 0) levelNo--; if (isDebug) { console.log('Comment end ' + 'Line: ' + (i + 1) + " " + cmd + ":" + params.join(",") + ' level:' + levelNo + " " + activity.GetActivityText()); //console.log(release, levelNo, params, cmd, i + 1, visibles[levelNo], visibles, visibles.GetVisibleLevel(levelNo), cmdActiveBracketingBegins[c][0]); } break; } } } } } } } if (levelNo != 0) console.log(consoleFgRed + PLUGIN_NAME + ': Wrong command nesting at end of file! " Line ' + (i + 1) + ' in file "' + currentFilename + '".' + consoleReset); ret = lines.join("\n"); } return ret; }; function MuBuildFilterWithRemove(_opts = null) { if (_opts == null) return ""; var text = "", mode = "remove", release = "*", client = "*", platform = "*", version = "*", unityvariants = null, isDebug = false, isProtocol = false; if (_opts.hasOwnProperty("release")) release = _opts.release; if (_opts.hasOwnProperty("client")) client = _opts.client; if (_opts.hasOwnProperty("platform")) platform = _opts.platform; if (_opts.hasOwnProperty("unityvariants")) unityvariants = _opts.unityvariants; if (_opts.hasOwnProperty("version")) version = _opts.version; if (_opts.hasOwnProperty("text")) text = _opts.text; if (_opts.hasOwnProperty("mode")) mode = _opts.mode; if (_opts.hasOwnProperty("debug")) isDebug = _opts.debug; if (_opts.hasOwnProperty("protocol")) isProtocol = _opts.protocol; var lines = text.split(/\n/igm) var ret = text, i, l = lines.length; var c, b, visibles = [], levels = [], cmds = [], levelParams = [], levelNo = 0, openclose, tokens, spacepos, colonSeparations, params, cmd; visibles[levelNo] = true; levels[levelNo] = []; var cmdPrefixes = ["%%%% @", "<%-- @", "/*-- @", "**** @", "//-- @", "<!-- @", "*--- @", "**-- @", "***- @", "---- @"]; // remove lines for variant (U3D,W2D etc.) // use e.g.: //@ONLY:U3D //@NEVER:U3D,W2D //@END var t; var act = true; var newlines = []; var isUCS = false, splits; if (unityvariants != null) { for (i = 0; i < l; i++) { t = lines[i].trim(); if (t.substr(0, 15) == "//@ONLY_ANY_OF:") { splits = t.substr(15).split(","); act = splits.IsIntersectionOf(unityvariants); isUCS = splits.indexOf("UCS") >= 0; if (isUCS && splits.indexOf("WJS") >= 0) isUCS = false; } else if (t.substr(0, 15) == "/*@ONLY_ANY_OF:") { splits = t.substr(15).split(","); act = splits.IsIntersectionOf(unityvariants); isUCS = splits.indexOf("UCS") >= 0; if (isUCS && splits.indexOf("WJS") >= 0) isUCS = false; } else if (t.substr(0, 15) == "//@ONLY_ALL_OF:") { splits = t.substr(15).split(","); act = splits.IsIntersectionOf(unityvariants); isUCS = splits.indexOf("UCS") >= 0; if (isUCS && splits.indexOf("WJS") >= 0) isUCS = false; } else if (t.substr(0, 15) == "/*@ONLY_ALL_OF:") { splits = t.substr(15).split(","); act = splits.IsIntersectionOf(unityvariants); isUCS = splits.indexOf("UCS") >= 0; if (isUCS && splits.indexOf("WJS") >= 0) isUCS = false; } else if (t.substr(0, 16) == "//@NEVER_ANY_OF:") { splits = t.substr(16).split(","); act = !t.substr(16).split(",").IsIntersectionOf(unityvariants); if (isUCS && splits.indexOf("UCS") >= 0) isUCS = false; if (splits.indexOf("WJS") >= 0) isUCS = true; } else if (t.substr(0, 16) == "/*@NEVER_ANY_OF:") { splits = t.substr(16).split(","); act = !t.substr(16).split(",").IsIntersectionOf(unityvariants); if (isUCS && splits.indexOf("UCS") >= 0) isUCS = false; if (splits.indexOf("WJS") >= 0) isUCS = true; } else if (t.substr(0, 16) == "//@NEVER_ALL_OF:") { splits = t.substr(16).split(","); act = !t.substr(16).split(",").ContainsAllOf(unityvariants); if (isUCS && splits.indexOf("UCS") >= 0) isUCS = false; if (splits.indexOf("WJS") >= 0) isUCS = true; } else if (t.substr(0, 16) == "/*@NEVER_ALL_OF:") { splits = t.substr(16).split(","); act = !t.substr(16).split(",").ContainsAllOf(unityvariants); if (isUCS && splits.indexOf("UCS") >= 0) isUCS = false; if (splits.indexOf("WJS") >= 0) isUCS = true; } else if (t.substr(0, 8) == "//@END*/") { act = true; isUCS = false; } else if (t.substr(0, 6) == "//@END") { act = true; isUCS = false; } else { if (act) { if (isUCS) { newlines.push("####C####" + lines[i]); } else { newlines.push(lines[i]); } } } } lines = newlines; ret = lines.join("\n"); } if (!(release == null || client == null || version == null)) { l = lines.length; for (i = 0; i < l; i++) { b = lines[i].trim(); c = b.substr(0, 6); if (cmdPrefixes.indexOf(c) >= 0) { openclose = b.substr(6, 1); if (openclose == "<" || openclose == ">") { tokens = b.substr(7); spacepos = tokens.indexOf(' '); if (spacepos > 0) { colonSeparations = tokens.substr(0, spacepos).split(':'); if (colonSeparations.length > 1 || openclose == ">") { if (colonSeparations.length > 1) { params = colonSeparations[1].trim().split(','); } else { params = []; }; cmd = colonSeparations[0].trim(); if (openclose == "<") { switch (cmd) { case "BUILD_ONLY_AT_RELEASES": levelNo++; levels[levelNo] = []; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = params.indexOf(release) >= 0 || release == "*"; levelParams[levelNo] = params; break; case "BUILD_NEVER_AT_RELEASES": levelNo++; levels[levelNo] = []; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = params.indexOf(release) < 0 || release == "*"; levelParams[levelNo] = params; break; case "BUILD_ONLY_AT_CLIENTS": levelNo++; levels[levelNo] = []; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = params.indexOf(client) >= 0 || client == "*"; levelParams[levelNo] = params; break; case "BUILD_NEVER_AT_CLIENTS": levelNo++; levels[levelNo] = []; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = params.indexOf(client) < 0 || client == "*"; levelParams[levelNo] = params; break; case "BUILD_ONLY_AT_VERSIONS": levelNo++; levels[levelNo] = []; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = params.IsVersionVisible(version) || version == "*"; levelParams[levelNo] = params; break; case "BUILD_NEVER_AT_VERSIONS": levelNo++; levels[levelNo] = []; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = !params.IsVersionVisible(version) || version == "*"; levelParams[levelNo] = params; break; case "BUILD_ONLY_AT_PLATFORMS": levelNo++; levels[levelNo] = []; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = params.indexOf(platform) >= 0 || platform == "*"; levelParams[levelNo] = params; break; case "BUILD_NEVER_AT_PLATFORMS": levelNo++; levels[levelNo] = []; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = params.indexOf(platform) < 0 || platform == "*"; levelParams[levelNo] = params; break; case "BUILD_ONLY_AT_ANY_VARIANTS": levelNo++; levels[levelNo] = []; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = params.IsIntersectionOf(variants) < 0 || variants == "*"; levelParams[levelNo] = params; break; case "BUILD_ONLY_AT_ALL_VARIANTS": levelNo++; levels[levelNo] = []; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = params.ContainsAllOf(variants) < 0 || variants == "*"; levelParams[levelNo] = params; break; case "BUILD_NEVER_AT_ANY_VARIANTS": levelNo++; levels[levelNo] = []; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = !params.IsIntersectionOf(variants) < 0 || variants == "*"; levelParams[levelNo] = params; break; case "BUILD_NEVER_AT_ALL_VARIANTS": levelNo++; levels[levelNo] = []; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = !params.ContainsAllOf(variants) < 0 || variants == "*"; levelParams[levelNo] = params; break; case "BUILD_NEVER_AT_GULP": levelNo++; levels[levelNo] = []; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = false; levelParams[levelNo] = params; break; case "BUILD_ONLY_AT_GULP": levelNo++; levels[levelNo] = []; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = true; levelParams[levelNo] = params; break; case "DO_NOT_AT_COMPACT": levelNo++; levels[levelNo] = []; cmds[levelNo] = cmd + ":" + params.join(","); visibles[levelNo] = true; levelParams[levelNo] = params; break; default: levels[levelNo].push(lines[i]); }; } else { switch (cmd) { case "BUILD_ONLY_AT_RELEASES": case "BUILD_NEVER_AT_RELEASES": case "BUILD_ONLY_AT_CLIENTS": case "BUILD_NEVER_AT_CLIENTS": case "BUILD_ONLY_AT_VERSIONS": case "BUILD_NEVER_AT_VERSIONS": case "BUILD_ONLY_AT_PLATFORMS": case "BUILD_NEVER_AT_PLATFORMS": case "BUILD_ONLY_AT_ANY_VARIANTS": case "BUILD_ONLY_AT_ALL_VARIANTS": case "BUILD_NEVER_AT_ANY_VARIANTS": case "BUILD_NEVER_AT_ALL_VARIANTS": case "BUILD_NEVER_AT_GULP": case "BUILD_ONLY_AT_GULP": case "DO_NOT_AT_COMPACT": if (levelNo > 0) { if (cmds[levelNo].split(":")[0] != cmd) { console.log(consoleFgRed + PLUGIN_NAME + ': Wrong command nesting! "' + cmds[levelNo] + '" Line ' + (i + 1) + ' in file "' + currentFilename + '".' + consoleReset); } } if (levelNo > 0) { if (visibles[levelNo]) levels[levelNo - 1] = levels[levelNo - 1].concat(levels[levelNo]); levelNo--; }; break; default: levels[levelNo].push(lines[i]); }; }; } else { levels[levelNo].push(lines[i]); }; } else { levels[levelNo].push(lines[i]); }; } else { levels[levelNo].push(lines[i]); }; } else { levels[levelNo].push(lines[i]); }; }; lines = levels[0]; l = lines.length; //for (i = 0; i < l; i++)c[i] = c[i].trim().replace(/(\/\*\#[a-zA-Z0-9\(\)\s\.\,\-_\[\]\#\*\+\}\{\&\%\$\!]*\*\/)/gm, ""); for (i = 0; i < l; i++)lines[i] = lines[i].replace(/(\/\*#[a-zA-Z0-9\(\)\s\.\,\-_\[\]#\*\+\}\{\&\%\$\!]*\*\/)/gm, ""); if (levelNo != 0) console.log(consoleFgRed + PLUGIN_NAME + ': Wrong command nesting at end of file! " Line ' + (i + 1) + ' in file "' + currentFilename + '".' + consoleReset); ret = lines.join("\n"); } return ret; } module.exports = function (_opts = null) { if (_opts == null) return ""; var text = null, mode = "remove", isProtocol = false; if (_opts.hasOwnProperty("text")) text = _opts.text; if (_opts.hasOwnProperty("mode")) mode = _opts.mode; if (_opts.hasOwnProperty("protocol")) isProtocol = _opts.protocol; if (text != null) { if (mode == "remove") { return MuBuildFilterWithRemove(_opts); } else { return MuBuildFilterWithCommentsSetup(_opts); } } return through.obj(function (file, encoding, callback) { if (file.isNull()) { // nothing to do return callback(null, file); } if (file.isStream()) { // file.contents is a Stream - https://nodejs.org/api/stream.html this.emit('error', new PluginError(PLUGIN_NAME, 'Streams not supported!')); // or, if you can handle Streams: //file.contents = file.contents.pipe(... //return callback(null, file); } else if (file.isBuffer()) { // file.contents is a Buffer - https://nodejs.org/api/buffer.html //this.emit('error', new PluginError(PLUGIN_NAME, 'Buffers not supported!')); // remove binary contant statements (uglify bug)... //var lines=file.contents.toString().replace(/\W*(0b\d+)/g,function(match,capture,offset){return match.split(capture).join(eval(capture));}).split(/\n/ig); //console.log(file.path); var o; currentFilename = file.path; if (isProtocol) { console.log(consoleFgBlue + 'µBuildFiler file "' + currentFilename + '"...' + consoleReset); } _opts.text = file.contents.toString(); if (mode == "remove") { o = MuBuildFilterWithRemove(_opts); } else { o = MuBuildFilterWithCommentsSetup(_opts); } //console.log("???"+o); file.contents = new Buffer(o); this.push(file); return callback(null, file); } }) }